summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api
diff options
context:
space:
mode:
authorHazelnoot <acomputerdog@gmail.com>2025-05-12 13:20:17 -0400
committerHazelnoot <acomputerdog@gmail.com>2025-05-12 21:35:06 -0400
commit871a4d3fb1a2dc3e657a601837275709958f4f95 (patch)
treebee4f8cbc778e6449c351dcee10c390a3896ec2b /packages/backend/src/server/api
parentadd redis cache for note translations (diff)
downloadsharkey-871a4d3fb1a2dc3e657a601837275709958f4f95.tar.gz
sharkey-871a4d3fb1a2dc3e657a601837275709958f4f95.tar.bz2
sharkey-871a4d3fb1a2dc3e657a601837275709958f4f95.zip
cache and re-use note translations
Diffstat (limited to 'packages/backend/src/server/api')
-rw-r--r--packages/backend/src/server/api/endpoints/notes/translate.ts60
1 files changed, 47 insertions, 13 deletions
diff --git a/packages/backend/src/server/api/endpoints/notes/translate.ts b/packages/backend/src/server/api/endpoints/notes/translate.ts
index 39119bc206..1566a8404b 100644
--- a/packages/backend/src/server/api/endpoints/notes/translate.ts
+++ b/packages/backend/src/server/api/endpoints/notes/translate.ts
@@ -10,22 +10,28 @@ import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
import { HttpRequestService } from '@/core/HttpRequestService.js';
import { GetterService } from '@/server/api/GetterService.js';
import { RoleService } from '@/core/RoleService.js';
-import { ApiError } from '../../error.js';
-import { MiMeta } from '@/models/_.js';
+import type { MiMeta, MiNote } from '@/models/_.js';
import { DI } from '@/di-symbols.js';
+import { CacheService } from '@/core/CacheService.js';
+import { hasText } from '@/models/Note.js';
+import { ApiLoggerService } from '@/server/api/ApiLoggerService.js';
+import { ApiError } from '../../error.js';
export const meta = {
tags: ['notes'],
+ // TODO allow unauthenticated if default template allows?
+ // Maybe a value 'optional' that allows unauthenticated OR a token w/ appropriate role.
+ // This will allow unauthenticated requests without leaking post data to restricted clients.
requireCredential: true,
kind: 'read:account',
res: {
type: 'object',
- optional: true, nullable: false,
+ optional: false, nullable: false,
properties: {
- sourceLang: { type: 'string' },
- text: { type: 'string' },
+ sourceLang: { type: 'string', optional: true, nullable: false },
+ text: { type: 'string', optional: true, nullable: false },
},
},
@@ -45,6 +51,11 @@ export const meta = {
code: 'CANNOT_TRANSLATE_INVISIBLE_NOTE',
id: 'ea29f2ca-c368-43b3-aaf1-5ac3e74bbe5d',
},
+ translationFailed: {
+ message: 'Failed to translate note. Please try again later or contact an administrator for assistance.',
+ code: 'TRANSLATION_FAILED',
+ id: '4e7a1a4f-521c-4ba2-b10a-69e5e2987b2f',
+ },
},
// 10 calls per 5 seconds
@@ -73,6 +84,8 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
private getterService: GetterService,
private httpRequestService: HttpRequestService,
private roleService: RoleService,
+ private readonly cacheService: CacheService,
+ private readonly loggerService: ApiLoggerService,
) {
super(meta, paramDef, async (ps, me) => {
const policies = await this.roleService.getUserPolicies(me.id);
@@ -89,8 +102,8 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
throw new ApiError(meta.errors.cannotTranslateInvisibleNote);
}
- if (note.text == null) {
- return;
+ if (!hasText(note)) {
+ return {};
}
const canDeeplFree = this.serverSettings.deeplFreeMode && !!this.serverSettings.deeplFreeInstance;
@@ -101,13 +114,32 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
let targetLang = ps.targetLang;
if (targetLang.includes('-')) targetLang = targetLang.split('-')[0];
+ let response = await this.cacheService.getCachedTranslation(note, targetLang);
+ if (!response) {
+ response = await this.fetchTranslation(note, targetLang);
+ if (!response) {
+ throw new ApiError(meta.errors.translationFailed);
+ }
+
+ await this.cacheService.setCachedTranslation(note, targetLang, response);
+ }
+ return response;
+ });
+ }
+
+ private async fetchTranslation(note: MiNote & { text: string }, targetLang: string) {
+ // Load-bearing try/catch - removing this will shift indentation and cause ~80 lines of upstream merge conflicts
+ try {
+ // Ignore deeplFreeInstance unless deeplFreeMode is set
+ const deeplFreeInstance = this.serverSettings.deeplFreeMode ? this.serverSettings.deeplFreeInstance : null;
+
// DeepL/DeepLX handling
- if (canDeepl) {
+ if (this.serverSettings.deeplAuthKey || deeplFreeInstance) {
const params = new URLSearchParams();
if (this.serverSettings.deeplAuthKey) params.append('auth_key', this.serverSettings.deeplAuthKey);
params.append('text', note.text);
params.append('target_lang', targetLang);
- const endpoint = canDeeplFree ? this.serverSettings.deeplFreeInstance as string : this.serverSettings.deeplIsPro ? 'https://api.deepl.com/v2/translate' : 'https://api-free.deepl.com/v2/translate';
+ const endpoint = deeplFreeInstance ?? this.serverSettings.deeplIsPro ? 'https://api.deepl.com/v2/translate' : 'https://api-free.deepl.com/v2/translate';
const res = await this.httpRequestService.send(endpoint, {
method: 'POST',
@@ -151,8 +183,8 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
}
// LibreTranslate handling
- if (canLibre) {
- const res = await this.httpRequestService.send(this.serverSettings.libreTranslateURL as string, {
+ if (this.serverSettings.libreTranslateURL) {
+ const res = await this.httpRequestService.send(this.serverSettings.libreTranslateURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
@@ -182,8 +214,10 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
text: json.translatedText,
};
}
+ } catch (e) {
+ this.loggerService.logger.error('Unhandled error from translation API: ', { e });
+ }
- return;
- });
+ return null;
}
}