From 2d06e269e426349a3c009e074aee1b37e17eaad9 Mon Sep 17 00:00:00 2001 From: Konstantin Markov Date: Fri, 4 Oct 2024 12:35:05 +0300 Subject: [PATCH 1/2] Pass article through translate endpoint on create article from translation --- scripts/api/article.ts | 34 +++++++++++++++++++ .../services/TranslationService.ts | 30 +++------------- .../core/get-superdesk-api-implementation.tsx | 1 + scripts/core/superdesk-api.d.ts | 1 + .../src/translations/translations-body.tsx | 24 +++---------- 5 files changed, 45 insertions(+), 45 deletions(-) diff --git a/scripts/api/article.ts b/scripts/api/article.ts index 3eba4dd92f..837dea7d1a 100644 --- a/scripts/api/article.ts +++ b/scripts/api/article.ts @@ -21,6 +21,7 @@ import { IAuthoringActionType, IDangerousArticlePatchingOptions, IDesk, + IExtensionActivationResult, IStage, onPublishMiddlewareResult, } from 'superdesk-api'; @@ -231,6 +232,37 @@ function createNewWithData(data: Partial, contentProfileId: string): v }); } +function translate(item: IArticle, language: string): Promise { + return httpRequestJsonLocal({ + method: 'POST', + path: '/archive/translate', + payload: { + guid: item.guid, + language, + desk: sdApi.desks.getCurrentDeskId(), + }, + }).then((_item) => { + const onTranslateAfterMiddlewares + : Array + = flatMap( + Object.values(extensions).map(({activationResult}) => activationResult), + (activationResult) => activationResult?.contributions?.entities?.article?.onTranslateAfter ?? [], + ); + + if (onTranslateAfterMiddlewares.length > 0) { + onTranslateAfterMiddlewares.forEach((fn) => { + fn(item, _item); + }); + } else { + openArticle(_item._id, 'edit'); + notify.success(gettext('Item Translated')); + } + + return _item; + }) +} + + /** * Checks if associations is with rewrite_of item then open then modal to add associations. * The user has options to add associated media to the current item and review the media change @@ -514,6 +546,7 @@ function rewrite(item: IArticle): void { } interface IArticleApi { + translate(item: IArticle, language: string): Promise; get(id: IArticle['_id']): Promise; isLocked(article: IArticle): boolean; isEditable(article: IArticle): boolean; @@ -608,6 +641,7 @@ interface IArticleApi { } export const article: IArticleApi = { + translate, rewrite, isLocked, isEditable, diff --git a/scripts/apps/translations/services/TranslationService.ts b/scripts/apps/translations/services/TranslationService.ts index 282f294049..502e7603d5 100644 --- a/scripts/apps/translations/services/TranslationService.ts +++ b/scripts/apps/translations/services/TranslationService.ts @@ -2,8 +2,10 @@ import _, {flatMap} from 'lodash'; import {AuthoringWorkspaceService} from 'apps/authoring/authoring/services/AuthoringWorkspaceService'; import {gettext} from 'core/utils'; import {extensions} from 'appConfig'; -import {IExtensionActivationResult} from 'superdesk-api'; +import {IArticle, IExtensionActivationResult} from 'superdesk-api'; import ng from 'core/services/ng'; +import {sdApi} from 'api'; +import {ILanguage} from 'superdesk-interfaces/Language'; /** * @ngdoc service @@ -59,30 +61,8 @@ export function TranslationService( * @param {Object} item item to be translated * @param {Object} language translate language */ - service.set = function(item, language) { - var params = { - guid: item.guid, - language: language.language, - desk: desks.getCurrentDeskId(), - }; - - api.save('translate', params).then((_item) => { - const onTranslateAfterMiddlewares - : Array - = flatMap( - Object.values(extensions).map(({activationResult}) => activationResult), - (activationResult) => activationResult?.contributions?.entities?.article?.onTranslateAfter ?? [], - ); - - if (onTranslateAfterMiddlewares.length > 0) { - onTranslateAfterMiddlewares.forEach((fn) => { - fn(item, _item); - }); - } else { - ng.get('authoringWorkspace').open(_item); - notify.success(gettext('Item Translated')); - } - + service.set = function(item: IArticle, language: ILanguage) { + sdApi.article.translate(item, language.language).then(() => { $rootScope.$broadcast('item:translate'); }); }; diff --git a/scripts/core/get-superdesk-api-implementation.tsx b/scripts/core/get-superdesk-api-implementation.tsx index 82c629a8a7..786d2d9642 100644 --- a/scripts/core/get-superdesk-api-implementation.tsx +++ b/scripts/core/get-superdesk-api-implementation.tsx @@ -325,6 +325,7 @@ export function getSuperdeskApiImplementation( getExtensionConfig: () => extensions[requestingExtensionId]?.configuration ?? {}, entities: { article: { + translate: sdApi.article.translate, get: sdApi.article.get, isPersonal: sdApi.article.isPersonal, isLocked: sdApi.article.isLocked, diff --git a/scripts/core/superdesk-api.d.ts b/scripts/core/superdesk-api.d.ts index 5395f3879d..6ba40ed9c8 100644 --- a/scripts/core/superdesk-api.d.ts +++ b/scripts/core/superdesk-api.d.ts @@ -2891,6 +2891,7 @@ declare module 'superdesk-api' { }; entities: { article: { + translate(article: IArticle, language: string): Promise; get(articleId: IArticle['_id']): Promise; isLocked(article: IArticle): boolean; // returns true if locked by anyone, including the current user isLockedInCurrentSession(article: IArticle): boolean; diff --git a/scripts/extensions/ai-widget/src/translations/translations-body.tsx b/scripts/extensions/ai-widget/src/translations/translations-body.tsx index d0c073ae7c..d72acb57a6 100644 --- a/scripts/extensions/ai-widget/src/translations/translations-body.tsx +++ b/scripts/extensions/ai-widget/src/translations/translations-body.tsx @@ -124,26 +124,10 @@ export default class TranslationsBody extends React.Component { {this.props.mode === 'current' && (