Skip to content

Commit

Permalink
Pass article through translate endpoint on create article (#4654)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecalcc committed Oct 10, 2024
1 parent a19b3e1 commit 9fa505f
Showing 5 changed files with 45 additions and 38 deletions.
18 changes: 18 additions & 0 deletions scripts/api/article.ts
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ import {
IAuthoringActionType,
IDangerousArticlePatchingOptions,
IDesk,
IExtensionActivationResult,
IStage,
onPublishMiddlewareResult,
} from 'superdesk-api';
@@ -231,6 +232,21 @@ function createNewWithData(data: Partial<IArticle>, contentProfileId: string): v
});
}

function translate(
item: IArticle,
language: string,
): Promise<IArticle> {
return httpRequestJsonLocal<IArticle>({
method: 'POST',
path: '/archive/translate',
payload: {
guid: item.guid,
language: language,
desk: sdApi.desks.getCurrentDeskId(),
},
});
}

/**
* 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 +530,7 @@ function rewrite(item: IArticle): void {
}

interface IArticleApi {
translate(item: IArticle, language: string): Promise<IArticle>;
get(id: IArticle['_id']): Promise<IArticle>;
isLocked(article: IArticle): boolean;
isEditable(article: IArticle): boolean;
@@ -608,6 +625,7 @@ interface IArticleApi {
}

export const article: IArticleApi = {
translate,
rewrite,
isLocked,
isEditable,
26 changes: 8 additions & 18 deletions scripts/apps/translations/services/TranslationService.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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 ng from 'core/services/ng';
import {IArticle, IExtensionActivationResult} from 'superdesk-api';
import {sdApi} from 'api';
import {ILanguage} from 'superdesk-interfaces/Language';
import {openArticle} from 'core/get-superdesk-api-implementation';

/**
* @ngdoc service
@@ -18,13 +18,10 @@ import ng from 'core/services/ng';
* @description Provides set of methods to translate items to different languages
*/

TranslationService.$inject = ['api', '$rootScope', 'notify', 'authoringWorkspace', 'desks', 'search'];
TranslationService.$inject = ['api', '$rootScope', 'search'];
export function TranslationService(
api,
$rootScope,
notify,
authoringWorkspace: AuthoringWorkspaceService,
desks,
search,
) {
var service: any = {};
@@ -59,14 +56,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) => {
service.set = function(item: IArticle, language: ILanguage) {
sdApi.article.translate(item, language.language).then((_item) => {
const onTranslateAfterMiddlewares
: Array<IExtensionActivationResult['contributions']['entities']['article']['onTranslateAfter']>
= flatMap(
@@ -79,8 +70,7 @@ export function TranslationService(
fn(item, _item);
});
} else {
ng.get('authoringWorkspace').open(_item);
notify.success(gettext('Item Translated'));
return openArticle(_item._id, 'edit');
}

$rootScope.$broadcast('item:translate');
2 changes: 2 additions & 0 deletions scripts/core/get-superdesk-api-implementation.tsx
Original file line number Diff line number Diff line change
@@ -325,6 +325,8 @@ 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,
isLockedInCurrentSession: sdApi.article.isLockedInCurrentSession,
2 changes: 2 additions & 0 deletions scripts/core/superdesk-api.d.ts
Original file line number Diff line number Diff line change
@@ -2883,6 +2883,8 @@ declare module 'superdesk-api' {
};
entities: {
article: {
translate(article: IArticle, language: string): Promise<IArticle>;
get(articleId: IArticle['_id']): Promise<IArticle>;
isLocked(article: IArticle): boolean; // returns true if locked by anyone, including the current user
isLockedInCurrentSession(article: IArticle): boolean;
isLockedInOtherSession(article: IArticle): boolean;
35 changes: 15 additions & 20 deletions scripts/extensions/ai-widget/src/translations/translations-body.tsx
Original file line number Diff line number Diff line change
@@ -124,26 +124,21 @@ export default class TranslationsBody extends React.Component<IProps, IState> {
{this.props.mode === 'current' && (
<Button
onClick={() => {
const currentDeskId = superdesk.entities.desk.getActiveDeskId();
const taskData = (() => {
if (currentDeskId != null) {
const currentDesk = superdesk.entities.desk.getDeskById(currentDeskId);
const {article: articleApi} = superdesk.entities;

return {
user: article.task.user,
desk: currentDesk._id,
stage: currentDesk.working_stage,
};
}

return {user: article.task.user};
})();

superdesk.entities.article.createNewWithData({
body_html: translation,
task: taskData,
language: this.props.activeLanguageId,
}, article.profile);
articleApi.translate(
article,
this.props.activeLanguageId,
).then((translatedItem) =>
articleApi.get(translatedItem._id).then((fullTranslatedItem) => {
return articleApi.patch(
fullTranslatedItem,
{body_html: translation, fields_meta: {}},
).then(() => {
superdesk.ui.article.edit(fullTranslatedItem._id);
});
}),
);
}}
size="small"
text={gettext('Create article')}
@@ -160,7 +155,7 @@ export default class TranslationsBody extends React.Component<IProps, IState> {
'body_html',
superdesk.helpers.editor3ToOperationalFormat(
{rawContentState: rawState},
this.props.article.language,
article.language,
),
));
} else {

0 comments on commit 9fa505f

Please sign in to comment.