From 9743f4b49ea5cc3e1670c35716aeaaf15e608ba1 Mon Sep 17 00:00:00 2001 From: infinite-persistence Date: Mon, 17 Jan 2022 10:23:49 +0800 Subject: [PATCH] Move commentron error mapping to separate file --- ui/redux/actions/comments.js | 81 +----------------------------------- ui/util/commentron-error.js | 81 ++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 80 deletions(-) create mode 100644 ui/util/commentron-error.js diff --git a/ui/redux/actions/comments.js b/ui/redux/actions/comments.js index 0e4798b8c..573919ac3 100644 --- a/ui/redux/actions/comments.js +++ b/ui/redux/actions/comments.js @@ -5,6 +5,7 @@ import * as PAGES from 'constants/pages'; import { SORT_BY, BLOCK_LEVEL } from 'constants/comment'; import Lbry from 'lbry'; import { parseURI, buildURI, isURIEqual } from 'util/lbryURI'; +import { resolveCommentronError } from 'util/commentron-error'; import { selectClaimForUri, selectClaimsByUri, selectMyChannelClaims } from 'redux/selectors/claims'; import { doResolveUri, doClaimSearch } from 'redux/actions/claims'; import { doToast, doSeeNotifications } from 'redux/actions/notifications'; @@ -33,47 +34,6 @@ declare type MentionedChannel = { channelID: string, }; -declare type CommentronErrorMap = { - [string]: { - commentron: string | RegExp, - replacement: string, - linkText?: string, - linkTarget?: string, - }, -}; - -// 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) { if (isDev) { console.error(msg); // eslint-disable-line @@ -81,45 +41,6 @@ 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, diff --git a/ui/util/commentron-error.js b/ui/util/commentron-error.js new file mode 100644 index 000000000..f44d62ae8 --- /dev/null +++ b/ui/util/commentron-error.js @@ -0,0 +1,81 @@ +// @flow + +declare type CommentronErrorMap = { + [string]: { + commentron: string | RegExp, + replacement: string, + linkText?: string, + linkTarget?: string, + }, +}; + +// 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.', + }, +}; + +export 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, + }; +}