summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/notes/translate.ts
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2021-11-12 02:02:25 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2021-11-12 02:02:25 +0900
commit0e4a111f81cceed275d9bec2695f6e401fb654d8 (patch)
tree40874799472fa07416f17b50a398ac33b7771905 /src/server/api/endpoints/notes/translate.ts
parentupdate deps (diff)
downloadsharkey-0e4a111f81cceed275d9bec2695f6e401fb654d8.tar.gz
sharkey-0e4a111f81cceed275d9bec2695f6e401fb654d8.tar.bz2
sharkey-0e4a111f81cceed275d9bec2695f6e401fb654d8.zip
refactoring
Resolve #7779
Diffstat (limited to 'src/server/api/endpoints/notes/translate.ts')
-rw-r--r--src/server/api/endpoints/notes/translate.ts89
1 files changed, 0 insertions, 89 deletions
diff --git a/src/server/api/endpoints/notes/translate.ts b/src/server/api/endpoints/notes/translate.ts
deleted file mode 100644
index b56b1debdd..0000000000
--- a/src/server/api/endpoints/notes/translate.ts
+++ /dev/null
@@ -1,89 +0,0 @@
-import $ from 'cafy';
-import { ID } from '@/misc/cafy-id';
-import define from '../../define';
-import { getNote } from '../../common/getters';
-import { ApiError } from '../../error';
-import fetch from 'node-fetch';
-import config from '@/config/index';
-import { getAgentByUrl } from '@/misc/fetch';
-import { URLSearchParams } from 'url';
-import { fetchMeta } from '@/misc/fetch-meta';
-import { Notes } from '@/models';
-
-export const meta = {
- tags: ['notes'],
-
- requireCredential: false as const,
-
- params: {
- noteId: {
- validator: $.type(ID),
- },
- targetLang: {
- validator: $.str,
- },
- },
-
- res: {
- type: 'object' as const,
- optional: false as const, nullable: false as const,
- },
-
- errors: {
- noSuchNote: {
- message: 'No such note.',
- code: 'NO_SUCH_NOTE',
- id: 'bea9b03f-36e0-49c5-a4db-627a029f8971'
- }
- }
-};
-
-export default define(meta, async (ps, user) => {
- const note = await getNote(ps.noteId).catch(e => {
- if (e.id === '9725d0ce-ba28-4dde-95a7-2cbb2c15de24') throw new ApiError(meta.errors.noSuchNote);
- throw e;
- });
-
- if (!(await Notes.isVisibleForMe(note, user ? user.id : null))) {
- return 204; // TODO: 良い感じのエラー返す
- }
-
- if (note.text == null) {
- return 204;
- }
-
- const instance = await fetchMeta();
-
- if (instance.deeplAuthKey == null) {
- return 204; // TODO: 良い感じのエラー返す
- }
-
- let targetLang = ps.targetLang;
- if (targetLang.includes('-')) targetLang = targetLang.split('-')[0];
-
- const params = new URLSearchParams();
- params.append('auth_key', instance.deeplAuthKey);
- params.append('text', note.text);
- params.append('target_lang', targetLang);
-
- const endpoint = instance.deeplIsPro ? 'https://api.deepl.com/v2/translate' : 'https://api-free.deepl.com/v2/translate';
-
- const res = await fetch(endpoint, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'User-Agent': config.userAgent,
- Accept: 'application/json, */*'
- },
- body: params,
- timeout: 10000,
- agent: getAgentByUrl,
- });
-
- const json = await res.json();
-
- return {
- sourceLang: json.translations[0].detected_source_language,
- text: json.translations[0].text
- };
-});