From 93d9d815a5a9f837572bf789be8757b2efcdadfd Mon Sep 17 00:00:00 2001 From: Grigory Vodyanov Date: Thu, 19 Dec 2024 20:12:33 +0100 Subject: [PATCH] chore: move to non deprecated task api Signed-off-by: Grigory Vodyanov --- src/components/MenuEnvelope.vue | 7 ++++++- src/components/ThreadEnvelope.vue | 8 ++++++++ src/components/TranslationModal.vue | 12 +++++------- src/service/translationService.js | 30 +++++++++++++++++++++++------ 4 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/components/MenuEnvelope.vue b/src/components/MenuEnvelope.vue index 90474acb72..09745993cb 100644 --- a/src/components/MenuEnvelope.vue +++ b/src/components/MenuEnvelope.vue @@ -83,7 +83,7 @@ {{ t('mail', 'Unsnooze') }} - @@ -315,6 +317,7 @@ import moment from '@nextcloud/moment' import { translateTagDisplayName } from '../util/tag.js' import { FOLLOW_UP_TAG_LABEL } from '../store/constants.js' import { Text, toPlain } from '../util/text.js' +import { getTranslationLanguages } from '../service/translationService.js' // Ternary loading state const LOADING_DONE = 0 @@ -412,6 +415,7 @@ export default { rawMessage: '', // Will hold the raw source of the message when requested isInternal: true, enabledSmartReply: loadState('mail', 'llm_freeprompt_available', false), + availableTranslationLanguages: [], } }, computed: { @@ -614,6 +618,10 @@ export default { clearInterval(this.$checkInterval) } }, 100) + + const response = await getTranslationLanguages() + this.availableTranslationLanguages = response.data.ocs.data.languages + console.log('availableTranslationLanguages', this.availableTranslationLanguages) }, beforeDestroy() { if (this.seenTimer !== undefined) { diff --git a/src/components/TranslationModal.vue b/src/components/TranslationModal.vue index 8223f1bce8..d31603d82c 100644 --- a/src/components/TranslationModal.vue +++ b/src/components/TranslationModal.vue @@ -74,7 +74,7 @@ import { showError, showSuccess } from '@nextcloud/dialogs' import { NcButton, NcDialog, NcLoadingIcon, NcRichText, NcSelect } from '@nextcloud/vue' -import { getTranslationLanguages, translateText } from '../service/translationService.js' +import { translateText } from '../service/translationService.js' export default { name: 'TranslationModal', @@ -99,6 +99,10 @@ export default { type: Object, required: true, }, + availableLanguages: { + type: Array, + required: true, + }, }, emits: ['close'], @@ -106,7 +110,6 @@ export default { data() { return { isMounted: false, - availableLanguages: null, selectedFrom: null, selectedTo: null, isLoading: false, @@ -183,11 +186,6 @@ export default { }, }, - async created() { - const response = await getTranslationLanguages() - this.availableLanguages = response.data.ocs.data.languages - }, - async mounted() { this.selectedTo = this.optionsTo.find(language => language.id === this.userLanguage) || null diff --git a/src/service/translationService.js b/src/service/translationService.js index be84261421..89fa15531a 100644 --- a/src/service/translationService.js +++ b/src/service/translationService.js @@ -10,12 +10,30 @@ const getTranslationLanguages = async function(options) { return axios.get(generateOcsUrl('/translation/languages', undefined, options), options) } -const translateText = async function(text, fromLanguage, toLanguage, options) { - return axios.post(generateOcsUrl('/translation/translate', undefined, options), { - text, - fromLanguage, - toLanguage, - }, options) +const translateText = async function(text, fromLanguage, toLanguage) { + try { + const scheduleResponse = await axios.post(generateOcsUrl('taskprocessing/schedule'), { + input: { + origin_language: fromLanguage ?? null, + input: text, + target_language: toLanguage, + }, + type: 'core:text2text:translate', + appId: 'mail', + }) + const task = scheduleResponse.data.ocs.data.task + const getTaskOutput = async (task) => { + if (task.output) { + return task.output.output + } + await new Promise(resolve => setTimeout(resolve, 2000)) + const taskResponse = await axios.get(generateOcsUrl(`taskprocessing/task/${task.id}`)) + return getTaskOutput(taskResponse.data.ocs.data.task) + } + return await getTaskOutput(task) + } catch (e) { + console.error('Failed to translate', e) + } } export { getTranslationLanguages, translateText }