Skip to content

Commit

Permalink
chore: move to non deprecated task api
Browse files Browse the repository at this point in the history
Signed-off-by: Grigory Vodyanov <[email protected]>
  • Loading branch information
GVodyanov committed Dec 19, 2024
1 parent ad1bc6f commit 93d9d81
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
7 changes: 6 additions & 1 deletion src/components/MenuEnvelope.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
</template>
{{ t('mail', 'Unsnooze') }}
</ActionButton>
<ActionButton
<ActionButton v-if="isTranslationAvailable"
:close-after-click="true"
@click.prevent="$emit('open-translation-modal')">
<template #icon>
Expand Down Expand Up @@ -310,6 +310,11 @@ export default {
type: Boolean,
default: true,
},
isTranslationAvailable: {
type: Boolean,
required: false,
default: false,
},
},
data() {
return {
Expand Down
8 changes: 8 additions & 0 deletions src/components/ThreadEnvelope.vue
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
:with-select="false"
:with-show-source="true"
:more-actions-open.sync="moreActionsOpen"
:is-translation-available="!!availableTranslationLanguages.length"
@reply="onReply"
@delete="$emit('delete',envelope.databaseId)"
@show-source-modal="onShowSourceModal"
Expand Down Expand Up @@ -221,6 +222,7 @@
<TranslationModal v-if="showTranslationModal"
:rich-parameters="{}"
:message="plainTextBody"
:available-languages="availableTranslationLanguages"
@close="onCloseTranslationModal" />
</template>
</div>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -614,6 +618,10 @@ export default {
clearInterval(this.$checkInterval)
}
}, 100)

const response = await getTranslationLanguages()
this.availableTranslationLanguages = response.data.ocs.data.languages

Check failure on line 623 in src/components/ThreadEnvelope.vue

View workflow job for this annotation

GitHub Actions / NPM lint

Unexpected console statement
console.log('availableTranslationLanguages', this.availableTranslationLanguages)
},
beforeDestroy() {
if (this.seenTimer !== undefined) {
Expand Down
12 changes: 5 additions & 7 deletions src/components/TranslationModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -99,14 +99,17 @@ export default {
type: Object,
required: true,
},
availableLanguages: {
type: Array,
required: true,
},
},

emits: ['close'],

data() {
return {
isMounted: false,
availableLanguages: null,
selectedFrom: null,
selectedTo: null,
isLoading: false,
Expand Down Expand Up @@ -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

Expand Down
30 changes: 24 additions & 6 deletions src/service/translationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

0 comments on commit 93d9d81

Please sign in to comment.