Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass article through translate endpoint on create article #4654

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions scripts/api/article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
IAuthoringActionType,
IDangerousArticlePatchingOptions,
IDesk,
IExtensionActivationResult,
IStage,
onPublishMiddlewareResult,
} from 'superdesk-api';
Expand Down Expand Up @@ -231,6 +232,37 @@ 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,
desk: sdApi.desks.getCurrentDeskId(),
},
}).then((_item) => {
const onTranslateAfterMiddlewares
: Array<IExtensionActivationResult['contributions']['entities']['article']['onTranslateAfter']>
= 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
Expand Down Expand Up @@ -514,6 +546,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;
Expand Down Expand Up @@ -608,6 +641,7 @@ interface IArticleApi {
}

export const article: IArticleApi = {
translate,
rewrite,
isLocked,
isEditable,
Expand Down
30 changes: 5 additions & 25 deletions scripts/apps/translations/services/TranslationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<IExtensionActivationResult['contributions']['entities']['article']['onTranslateAfter']>
= 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');
});
};
Expand Down
1 change: 1 addition & 0 deletions scripts/core/get-superdesk-api-implementation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions scripts/core/superdesk-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2891,6 +2891,7 @@ 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,26 +124,10 @@ 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);

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);
superdesk.entities.article.translate(article, this.props.activeLanguageId)
.then((item) => {
superdesk.entities.article.patch(item, {body_html: translation});
});
}}
size="small"
text={gettext('Create article')}
Expand Down
Loading