From 65902f6d5836b8e08831d64c685a7ff756f79c37 Mon Sep 17 00:00:00 2001 From: infinite-persistence Date: Wed, 15 Sep 2021 15:41:57 +0800 Subject: [PATCH] Commentron err replacement: fix readability + added link support. - Hopefully this is easier to understand, and easier to add/remove entries. - The link support will be needed by the upcoming Appeal process. --- ui/redux/actions/comments.js | 121 ++++++++++++++++++++++------------- 1 file changed, 77 insertions(+), 44 deletions(-) diff --git a/ui/redux/actions/comments.js b/ui/redux/actions/comments.js index 5eb9c02d6..0e5425ea0 100644 --- a/ui/redux/actions/comments.js +++ b/ui/redux/actions/comments.js @@ -22,19 +22,45 @@ import { doAlertWaitingForSync } from 'redux/actions/app'; const isDev = process.env.NODE_ENV !== 'production'; const FETCH_API_FAILED_TO_FETCH = 'Failed to fetch'; -const COMMENTRON_MSG_REMAP = { - // <-- Commentron msg --> : <-- App msg --> - 'channel is blocked by publisher': 'Unable to comment. This channel has blocked you.', - 'channel is not allowed to post comments': 'Unable to comment. Your channel has been blocked by an admin.', - 'comments are disabled by the creator': 'Unable to comment. The content owner has disabled comments.', - 'duplicate comment!': 'Please do not spam.', +declare type CommentronErrorMap = { + [string]: { + commentron: string | RegExp, + replacement: string, + linkText?: string, + linkTarget?: string, + }, }; -const COMMENTRON_REGEX_MAP = { - // <-- App msg --> : <-- Regex of Commentron msg --> - 'Your user name "%1%" is too close to the creator\'s user name "%2%" and may cause confusion. Please use another identity.': /^your user name (.*) is too close to the creator's user name (.*) and may cause confusion. Please use another identity.$/, - 'Slow mode is on. Please wait up to %1% seconds before commenting again.': /^Slow mode is on. Please wait at most (.*) seconds before commenting again.$/, - 'The comment contains contents that are blocked by %1%.': /^the comment contents are blocked by (.*)$/, +// prettier-ignore +const ERR_MAP: CommentronErrorMap = { + SIMILAR_NAME: { + commentron: /^your user name (.*) is too close to the creator's user name (.*) and may cause confusion. Please use another identity.$/, + replacement: 'Your user name "%1%" is too close to the creator\'s user name "%2%" and may cause confusion. Please use another identity.', + }, + SLOW_MODE_IS_ON: { + commentron: /^Slow mode is on. Please wait at most (.*) seconds before commenting again.$/, + replacement: 'Slow mode is on. Please wait up to %1% seconds before commenting again.', + }, + HAS_MUTED_WORDS: { + commentron: /^the comment contents are blocked by (.*)$/, + replacement: 'The comment contains contents that are blocked by %1%.', + }, + BLOCKED_BY_CREATOR: { + commentron: 'channel is blocked by publisher', + replacement: 'Unable to comment. This channel has blocked you.', + }, + BLOCKED_BY_ADMIN: { + commentron: 'channel is not allowed to post comments', + replacement: 'Unable to comment. Your channel has been blocked by an admin.', + }, + CREATOR_DISABLED: { + commentron: 'comments are disabled by the creator', + replacement: 'Unable to comment. The content owner has disabled comments.', + }, + STOP_SPAMMING: { + commentron: 'duplicate comment!', + replacement: 'Please do not spam.', + }, }; function devToast(dispatch, msg) { @@ -44,6 +70,45 @@ function devToast(dispatch, msg) { } } +function resolveCommentronError(commentronMsg: string) { + for (const key in ERR_MAP) { + // noinspection JSUnfilteredForInLoop + const data = ERR_MAP[key]; + if (typeof data.commentron === 'string') { + if (data.commentron === commentronMsg) { + return { + message: __(data.replacement), + linkText: data.linkText ? __(data.linkText) : undefined, + linkTarget: data.linkTarget, + isError: true, + }; + } + } else { + const match = commentronMsg.match(data.commentron); + if (match) { + const subs = {}; + for (let i = 1; i < match.length; ++i) { + subs[`${i}`] = match[i]; + } + + return { + message: __(data.replacement, subs), + linkText: data.linkText ? __(data.linkText) : undefined, + linkTarget: data.linkTarget, + isError: true, + }; + } + } + } + + return { + // Fallback to commentron original message. It will be in English + // only and most likely not capitalized correctly. + message: commentronMsg, + isError: true, + }; +} + export function doCommentList( uri: string, parentId: string, @@ -471,39 +536,7 @@ export function doCommentCreate( }) .catch((error) => { dispatch({ type: ACTIONS.COMMENT_CREATE_FAILED, data: error }); - - let toastMessage; - - for (const commentronMsg in COMMENTRON_MSG_REMAP) { - if (error.message === commentronMsg) { - toastMessage = __(COMMENTRON_MSG_REMAP[commentronMsg]); - break; - } - } - - if (!toastMessage) { - for (const i18nStr in COMMENTRON_REGEX_MAP) { - const regex = COMMENTRON_REGEX_MAP[i18nStr]; - const match = error.message.match(regex); - if (match) { - const subs = {}; - for (let i = 1; i < match.length; ++i) { - subs[`${i}`] = match[i]; - } - - toastMessage = __(i18nStr, subs); - break; - } - } - } - - if (!toastMessage) { - // Fallback to commentron original message. It will be in English - // only and most likely not capitalized correctly. - toastMessage = error.message; - } - - dispatch(doToast({ message: toastMessage, isError: true })); + dispatch(doToast(resolveCommentronError(error.message))); return Promise.reject(error); }); };