[Glitch] Preserve translation on status re-import

Port 6781dc6462 to glitch-soc

Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
Christian Schmidt 2023-07-25 20:29:31 +02:00 committed by Claire
parent 454d8567d4
commit 88ba538aad
2 changed files with 30 additions and 9 deletions

View File

@ -81,7 +81,7 @@ export function importFetchedStatuses(statuses) {
} }
if (status.poll && status.poll.id) { if (status.poll && status.poll.id) {
pushUnique(polls, normalizePoll(status.poll)); pushUnique(polls, normalizePoll(status.poll, getState().getIn(['polls', status.poll.id])));
} }
} }
@ -95,7 +95,7 @@ export function importFetchedStatuses(statuses) {
} }
export function importFetchedPoll(poll) { export function importFetchedPoll(poll) {
return dispatch => { return (dispatch, getState) => {
dispatch(importPolls([normalizePoll(poll)])); dispatch(importPolls([normalizePoll(poll, getState().getIn(['polls', poll.id]))]));
}; };
} }

View File

@ -75,6 +75,7 @@ export function normalizeStatus(status, normalOldStatus, settings) {
normalStatus.contentHtml = normalOldStatus.get('contentHtml'); normalStatus.contentHtml = normalOldStatus.get('contentHtml');
normalStatus.spoilerHtml = normalOldStatus.get('spoilerHtml'); normalStatus.spoilerHtml = normalOldStatus.get('spoilerHtml');
normalStatus.hidden = normalOldStatus.get('hidden'); normalStatus.hidden = normalOldStatus.get('hidden');
normalStatus.translation = normalOldStatus.get('translation');
} else { } else {
const spoilerText = normalStatus.spoiler_text || ''; const spoilerText = normalStatus.spoiler_text || '';
const searchContent = ([spoilerText, status.content].concat((status.poll && status.poll.options) ? status.poll.options.map(option => option.title) : [])).concat(status.media_attachments.map(att => att.description)).join('\n\n').replace(/<br\s*\/?>/g, '\n').replace(/<\/p><p>/g, '\n\n'); const searchContent = ([spoilerText, status.content].concat((status.poll && status.poll.options) ? status.poll.options.map(option => option.title) : [])).concat(status.media_attachments.map(att => att.description)).join('\n\n').replace(/<br\s*\/?>/g, '\n').replace(/<\/p><p>/g, '\n\n');
@ -86,6 +87,18 @@ export function normalizeStatus(status, normalOldStatus, settings) {
normalStatus.hidden = (spoilerText.length > 0 || normalStatus.sensitive) && autoHideCW(settings, spoilerText); normalStatus.hidden = (spoilerText.length > 0 || normalStatus.sensitive) && autoHideCW(settings, spoilerText);
} }
if (normalOldStatus) {
const list = normalOldStatus.get('media_attachments');
if (normalStatus.media_attachments && list) {
normalStatus.media_attachments.forEach(item => {
const oldItem = list.find(i => i.get('id') === item.id);
if (oldItem && oldItem.get('description') === item.description) {
item.translation = oldItem.get('translation')
}
});
}
}
return normalStatus; return normalStatus;
} }
@ -104,15 +117,23 @@ export function normalizeStatusTranslation(translation, status) {
return normalTranslation; return normalTranslation;
} }
export function normalizePoll(poll) { export function normalizePoll(poll, normalOldPoll) {
const normalPoll = { ...poll }; const normalPoll = { ...poll };
const emojiMap = makeEmojiMap(poll.emojis); const emojiMap = makeEmojiMap(poll.emojis);
normalPoll.options = poll.options.map((option, index) => ({ normalPoll.options = poll.options.map((option, index) => {
...option, const normalOption = {
voted: poll.own_votes && poll.own_votes.includes(index), ...option,
titleHtml: emojify(escapeTextContentForBrowser(option.title), emojiMap), voted: poll.own_votes && poll.own_votes.includes(index),
})); titleHtml: emojify(escapeTextContentForBrowser(option.title), emojiMap),
}
if (normalOldPoll && normalOldPoll.getIn(['options', index, 'title']) === option.title) {
normalOption.translation = normalOldPoll.getIn(['options', index, 'translation']);
}
return normalOption
});
return normalPoll; return normalPoll;
} }