2021-08-19 14:55:45 +02:00
|
|
|
import define from '../../define';
|
|
|
|
import { getNote } from '../../common/getters';
|
|
|
|
import { ApiError } from '../../error';
|
2021-08-15 13:26:44 +02:00
|
|
|
import fetch from 'node-fetch';
|
2021-08-19 14:55:45 +02:00
|
|
|
import config from '@/config/index';
|
|
|
|
import { getAgentByUrl } from '@/misc/fetch';
|
2021-08-15 13:26:44 +02:00
|
|
|
import { URLSearchParams } from 'url';
|
2021-08-19 14:55:45 +02:00
|
|
|
import { fetchMeta } from '@/misc/fetch-meta';
|
2021-09-05 09:25:30 +02:00
|
|
|
import { Notes } from '@/models';
|
2021-08-15 13:26:44 +02:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['notes'],
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: false,
|
2021-08-15 13:26:44 +02:00
|
|
|
|
|
|
|
res: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2021-08-15 13:26:44 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchNote: {
|
|
|
|
message: 'No such note.',
|
|
|
|
code: 'NO_SUCH_NOTE',
|
2021-12-09 15:58:30 +01:00
|
|
|
id: 'bea9b03f-36e0-49c5-a4db-627a029f8971',
|
|
|
|
},
|
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2021-08-15 13:26:44 +02:00
|
|
|
|
2022-02-20 05:15:40 +01:00
|
|
|
export const paramDef = {
|
2022-02-19 06:05:32 +01:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
noteId: { type: 'string', format: 'misskey:id' },
|
|
|
|
targetLang: { type: 'string' },
|
|
|
|
},
|
|
|
|
required: ['noteId', 'targetLang'],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-02 18:12:50 +01:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-19 06:05:32 +01:00
|
|
|
export default define(meta, paramDef, async (ps, user) => {
|
2021-08-15 13:26:44 +02:00
|
|
|
const note = await getNote(ps.noteId).catch(e => {
|
|
|
|
if (e.id === '9725d0ce-ba28-4dde-95a7-2cbb2c15de24') throw new ApiError(meta.errors.noSuchNote);
|
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
|
2021-09-05 09:25:30 +02:00
|
|
|
if (!(await Notes.isVisibleForMe(note, user ? user.id : null))) {
|
|
|
|
return 204; // TODO: 良い感じのエラー返す
|
|
|
|
}
|
|
|
|
|
2021-08-15 13:26:44 +02:00
|
|
|
if (note.text == null) {
|
|
|
|
return 204;
|
|
|
|
}
|
|
|
|
|
|
|
|
const instance = await fetchMeta();
|
|
|
|
|
|
|
|
if (instance.deeplAuthKey == null) {
|
|
|
|
return 204; // TODO: 良い感じのエラー返す
|
|
|
|
}
|
|
|
|
|
2021-08-15 14:52:58 +02:00
|
|
|
let targetLang = ps.targetLang;
|
|
|
|
if (targetLang.includes('-')) targetLang = targetLang.split('-')[0];
|
|
|
|
|
2021-08-15 13:26:44 +02:00
|
|
|
const params = new URLSearchParams();
|
|
|
|
params.append('auth_key', instance.deeplAuthKey);
|
|
|
|
params.append('text', note.text);
|
2021-08-15 14:52:58 +02:00
|
|
|
params.append('target_lang', targetLang);
|
2021-08-15 13:26:44 +02:00
|
|
|
|
2021-08-24 06:19:21 +02:00
|
|
|
const endpoint = instance.deeplIsPro ? 'https://api.deepl.com/v2/translate' : 'https://api-free.deepl.com/v2/translate';
|
|
|
|
|
|
|
|
const res = await fetch(endpoint, {
|
2021-08-15 13:26:44 +02:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'User-Agent': config.userAgent,
|
2021-12-09 15:58:30 +01:00
|
|
|
Accept: 'application/json, */*',
|
2021-08-15 13:26:44 +02:00
|
|
|
},
|
|
|
|
body: params,
|
|
|
|
timeout: 10000,
|
|
|
|
agent: getAgentByUrl,
|
|
|
|
});
|
|
|
|
|
|
|
|
const json = await res.json();
|
|
|
|
|
|
|
|
return {
|
|
|
|
sourceLang: json.translations[0].detected_source_language,
|
2021-12-09 15:58:30 +01:00
|
|
|
text: json.translations[0].text,
|
2021-08-15 13:26:44 +02:00
|
|
|
};
|
|
|
|
});
|