From f3fdc7b38739135b61f8c8277a773fc0c4138d76 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 19 Nov 2022 16:14:37 -0600 Subject: [PATCH] Translations: strip compatibility features from HTML --- app/soapbox/reducers/statuses.ts | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/app/soapbox/reducers/statuses.ts b/app/soapbox/reducers/statuses.ts index 2c19b2511..513a34b94 100644 --- a/app/soapbox/reducers/statuses.ts +++ b/app/soapbox/reducers/statuses.ts @@ -1,5 +1,5 @@ import escapeTextContentForBrowser from 'escape-html'; -import { Map as ImmutableMap, List as ImmutableList, fromJS } from 'immutable'; +import { Map as ImmutableMap, List as ImmutableList } from 'immutable'; import emojify from 'soapbox/features/emoji/emoji'; import { normalizeStatus } from 'soapbox/normalizers'; @@ -195,6 +195,24 @@ const simulateFavourite = ( return state.set(statusId, updatedStatus); }; +interface Translation { + content: string, + detected_source_language: string, + provider: string, +} + +/** Import translation from translation service into the store. */ +const importTranslation = (state: State, statusId: string, translation: Translation) => { + const map = ImmutableMap(translation); + const result = map.set('content', stripCompatibilityFeatures(map.get('content', ''))); + return state.setIn([statusId, 'translation'], result); +}; + +/** Delete translation from the store. */ +const deleteTranslation = (state: State, statusId: string) => { + return state.deleteIn([statusId, 'translation']); +}; + const initialState: State = ImmutableMap(); export default function statuses(state = initialState, action: AnyAction): State { @@ -258,9 +276,9 @@ export default function statuses(state = initialState, action: AnyAction): State case STATUS_DELETE_FAIL: return incrementReplyCount(state, action.params); case STATUS_TRANSLATE_SUCCESS: - return state.setIn([action.id, 'translation'], fromJS(action.translation)); + return importTranslation(state, action.id, action.translation); case STATUS_TRANSLATE_UNDO: - return state.deleteIn([action.id, 'translation']); + return deleteTranslation(state, action.id); case TIMELINE_DELETE: return deleteStatus(state, action.id, action.references); default: