From fd66e6b9b18ce8c78c41ac06ef68df22a91b8a8d Mon Sep 17 00:00:00 2001 From: infinite-persistence Date: Tue, 10 Aug 2021 10:20:55 +0800 Subject: [PATCH] Refactor Commentron error msg handling ## Issue 6832: Pass through commenting error if it fails current error list ## Changes - Refactor the error-msg-replacing code to make it easier to add new ones in the future. - Made the new error message localizable (i.e. extract the variable value, pass to `__()`) - Fallback to Commentron original message for those unhandled ones (usually fresh messages from Commentron). --- static/app-strings.json | 5 +- ui/redux/actions/comments.js | 88 ++++++++++++++++++------------------ 2 files changed, 46 insertions(+), 47 deletions(-) diff --git a/static/app-strings.json b/static/app-strings.json index b3ca767d7..a4476a78e 100644 --- a/static/app-strings.json +++ b/static/app-strings.json @@ -1455,8 +1455,9 @@ "Unable to comment. Your channel has been blocked by an admin.": "Unable to comment. Your channel has been blocked by an admin.", "Unable to comment. The content owner has disabled comments.": "Unable to comment. The content owner has disabled comments.", "Please do not spam.": "Please do not spam.", - "Slow mode is on. Please wait up to %value% seconds before commenting again.": "Slow mode is on. Please wait up to %value% seconds before commenting again.", - "The comment contains contents that are blocked by %author%": "The comment contains contents that are blocked by %author%", + "Slow mode is on. Please wait up to %1% seconds before commenting again.": "Slow mode is on. Please wait up to %1% seconds before commenting again.", + "The comment contains contents that are blocked by %1%.": "The comment contains contents that are blocked by %1%.", + "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 \"%1%\" is too close to the creator's user name \"%2%\" and may cause confusion. Please use another identity.", "Your channel is still being setup, try again in a few moments.": "Your channel is still being setup, try again in a few moments.", "Unable to delete this comment, please try again later.": "Unable to delete this comment, please try again later.", "Unable to edit this comment, please try again later.": "Unable to edit this comment, please try again later.", diff --git a/ui/redux/actions/comments.js b/ui/redux/actions/comments.js index 77cfe58df..36bb55353 100644 --- a/ui/redux/actions/comments.js +++ b/ui/redux/actions/comments.js @@ -29,6 +29,21 @@ import { doAlertWaitingForSync } from 'redux/actions/app'; const isDev = process.env.NODE_ENV !== 'production'; +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.', +}; + +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 (.*)$/, +}; + function devToast(dispatch, msg) { if (isDev) { console.error(msg); // eslint-disable-line @@ -448,57 +463,40 @@ export function doCommentCreate( return result; }) .catch((error) => { - dispatch({ - type: ACTIONS.COMMENT_CREATE_FAILED, - data: error, - }); + dispatch({ type: ACTIONS.COMMENT_CREATE_FAILED, data: error }); - let toastMessage = __('Unable to create comment, please try again later.'); - if (error && error.message === 'channel is blocked by publisher') { - toastMessage = __('Unable to comment. This channel has blocked you.'); - } + let toastMessage; - if (error) { - // TODO: Use error codes when commentron implements it. - switch (error.message) { - case 'channel is blocked by publisher': - toastMessage = __('Unable to comment. This channel has blocked you.'); - break; - case 'channel is not allowed to post comments': - toastMessage = __('Unable to comment. Your channel has been blocked by an admin.'); - break; - case 'comments are disabled by the creator': - toastMessage = __('Unable to comment. The content owner has disabled comments.'); - break; - case 'duplicate comment!': - toastMessage = __('Please do not spam.'); - break; - default: - const BLOCKED_WORDS_ERR_MSG = 'the comment contents are blocked by'; - const SLOW_MODE_PARTIAL_ERR_MSG = 'Slow mode is on. Please wait at most'; - - if (error.message.startsWith(BLOCKED_WORDS_ERR_MSG)) { - const channelName = error.message.substring(BLOCKED_WORDS_ERR_MSG.length); - toastMessage = __('The comment contains contents that are blocked by %author%', { - author: channelName, - }); - } else if (error.message.startsWith(SLOW_MODE_PARTIAL_ERR_MSG)) { - const value = error.message.replace(/\D/g, ''); - toastMessage = __('Slow mode is on. Please wait up to %value% seconds before commenting again.', { - value, - }); - } - break; + for (const commentronMsg in COMMENTRON_MSG_REMAP) { + if (error.message === commentronMsg) { + toastMessage = __(COMMENTRON_MSG_REMAP[commentronMsg]); + break; } } - dispatch( - doToast({ - message: toastMessage, - isError: true, - }) - ); + 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 })); return Promise.reject(error); }); };