From 84a2a74c8ca39087df681be78a0066bb7c65f8b0 Mon Sep 17 00:00:00 2001 From: infinite-persistence Date: Sat, 17 Jul 2021 21:37:20 +0800 Subject: [PATCH] Reactions: consider failures as "fetched" In the event that Commentron returned an empty object, we'll end up re-requesting the same IDs again. Haven't seens this happen before, but since we don't know what's causing the spike, we'll just consider failures and "fetched" to stop the loop. User can always click Refresh to repopulate the values. --- ui/redux/actions/comments.js | 15 ++++++++-- ui/redux/reducers/comments.js | 54 +++++++++++++++++++++-------------- 2 files changed, 45 insertions(+), 24 deletions(-) diff --git a/ui/redux/actions/comments.js b/ui/redux/actions/comments.js index 75fb0aa27..f3052ea5d 100644 --- a/ui/redux/actions/comments.js +++ b/ui/redux/actions/comments.js @@ -221,6 +221,13 @@ export function doCommentReactList(commentIds: Array) { if (activeChannelClaim) { const signatureData = await channelSignName(activeChannelClaim.claim_id, activeChannelClaim.name); if (!signatureData) { + dispatch({ + type: ACTIONS.COMMENT_REACTION_LIST_FAILED, + data: { + channelId: activeChannelClaim ? activeChannelClaim.claim_id : undefined, + commentIds, + }, + }); return dispatch(doToast({ isError: true, message: __('Unable to verify your channel. Please try again.') })); } @@ -236,9 +243,10 @@ export function doCommentReactList(commentIds: Array) { dispatch({ type: ACTIONS.COMMENT_REACTION_LIST_COMPLETED, data: { - myReactions: myReactions || {}, + myReactions, othersReactions, channelId: activeChannelClaim ? activeChannelClaim.claim_id : undefined, + commentIds, }, }); }) @@ -246,7 +254,10 @@ export function doCommentReactList(commentIds: Array) { devToast(dispatch, `doCommentReactList: ${error.message}`); dispatch({ type: ACTIONS.COMMENT_REACTION_LIST_FAILED, - data: error, + data: { + channelId: activeChannelClaim ? activeChannelClaim.claim_id : undefined, + commentIds, + }, }); }); }; diff --git a/ui/redux/reducers/comments.js b/ui/redux/reducers/comments.js index 5065fa9bd..d7557d227 100644 --- a/ui/redux/reducers/comments.js +++ b/ui/redux/reducers/comments.js @@ -143,10 +143,24 @@ export default handleActions( isFetchingReacts: true, }), - [ACTIONS.COMMENT_REACTION_LIST_FAILED]: (state: CommentsState, action: any) => ({ - ...state, - isFetchingReacts: false, - }), + [ACTIONS.COMMENT_REACTION_LIST_FAILED]: (state: CommentsState, action: any) => { + const { channelId, commentIds } = action.data; + const myReactsByCommentId = Object.assign({}, state.myReactsByCommentId); + const othersReactsByCommentId = Object.assign({}, state.othersReactsByCommentId); + + commentIds.forEach((commentId) => { + const key = channelId ? `${commentId}:${channelId}` : commentId; + myReactsByCommentId[key] = []; + othersReactsByCommentId[key] = {}; + }); + + return { + ...state, + isFetchingReacts: false, + myReactsByCommentId, + othersReactsByCommentId, + }; + }, [ACTIONS.COMMENT_REACT_FAILED]: (state: CommentsState, action: any): CommentsState => { const commentReaction = action.data; // String: reactionHash + type @@ -182,28 +196,24 @@ export default handleActions( }, [ACTIONS.COMMENT_REACTION_LIST_COMPLETED]: (state: CommentsState, action: any): CommentsState => { - const { myReactions, othersReactions, channelId } = action.data; + const { myReactions, othersReactions, channelId, commentIds } = action.data; const myReacts = Object.assign({}, state.myReactsByCommentId); const othersReacts = Object.assign({}, state.othersReactsByCommentId); - if (myReactions) { - Object.entries(myReactions).forEach(([commentId, reactions]) => { - const key = channelId ? `${commentId}:${channelId}` : commentId; - myReacts[key] = Object.entries(reactions).reduce((acc, [name, count]) => { - if (count === 1) { - acc.push(name); - } - return acc; - }, []); - }); - } + commentIds.forEach((commentId) => { + const key = channelId ? `${commentId}:${channelId}` : commentId; + const mine = myReactions ? myReactions[commentId] : {}; + const others = othersReactions ? othersReactions[commentId] : {}; - if (othersReactions) { - Object.entries(othersReactions).forEach(([commentId, reactions]) => { - const key = channelId ? `${commentId}:${channelId}` : commentId; - othersReacts[key] = reactions; - }); - } + myReacts[key] = Object.entries(mine).reduce((acc, [name, count]) => { + if (count === 1) { + acc.push(name); + } + return acc; + }, []); + + othersReacts[key] = others; + }); return { ...state,