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.
This commit is contained in:
infinite-persistence 2021-07-17 21:37:20 +08:00
parent 47d39b20d3
commit 84a2a74c8c
No known key found for this signature in database
GPG key ID: B9C3252EDC3D0AA0
2 changed files with 45 additions and 24 deletions

View file

@ -221,6 +221,13 @@ export function doCommentReactList(commentIds: Array<string>) {
if (activeChannelClaim) { if (activeChannelClaim) {
const signatureData = await channelSignName(activeChannelClaim.claim_id, activeChannelClaim.name); const signatureData = await channelSignName(activeChannelClaim.claim_id, activeChannelClaim.name);
if (!signatureData) { 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.') })); return dispatch(doToast({ isError: true, message: __('Unable to verify your channel. Please try again.') }));
} }
@ -236,9 +243,10 @@ export function doCommentReactList(commentIds: Array<string>) {
dispatch({ dispatch({
type: ACTIONS.COMMENT_REACTION_LIST_COMPLETED, type: ACTIONS.COMMENT_REACTION_LIST_COMPLETED,
data: { data: {
myReactions: myReactions || {}, myReactions,
othersReactions, othersReactions,
channelId: activeChannelClaim ? activeChannelClaim.claim_id : undefined, channelId: activeChannelClaim ? activeChannelClaim.claim_id : undefined,
commentIds,
}, },
}); });
}) })
@ -246,7 +254,10 @@ export function doCommentReactList(commentIds: Array<string>) {
devToast(dispatch, `doCommentReactList: ${error.message}`); devToast(dispatch, `doCommentReactList: ${error.message}`);
dispatch({ dispatch({
type: ACTIONS.COMMENT_REACTION_LIST_FAILED, type: ACTIONS.COMMENT_REACTION_LIST_FAILED,
data: error, data: {
channelId: activeChannelClaim ? activeChannelClaim.claim_id : undefined,
commentIds,
},
}); });
}); });
}; };

View file

@ -143,10 +143,24 @@ export default handleActions(
isFetchingReacts: true, isFetchingReacts: true,
}), }),
[ACTIONS.COMMENT_REACTION_LIST_FAILED]: (state: CommentsState, action: any) => ({ [ACTIONS.COMMENT_REACTION_LIST_FAILED]: (state: CommentsState, action: any) => {
...state, const { channelId, commentIds } = action.data;
isFetchingReacts: false, 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 => { [ACTIONS.COMMENT_REACT_FAILED]: (state: CommentsState, action: any): CommentsState => {
const commentReaction = action.data; // String: reactionHash + type const commentReaction = action.data; // String: reactionHash + type
@ -182,28 +196,24 @@ export default handleActions(
}, },
[ACTIONS.COMMENT_REACTION_LIST_COMPLETED]: (state: CommentsState, action: any): CommentsState => { [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 myReacts = Object.assign({}, state.myReactsByCommentId);
const othersReacts = Object.assign({}, state.othersReactsByCommentId); const othersReacts = Object.assign({}, state.othersReactsByCommentId);
if (myReactions) { commentIds.forEach((commentId) => {
Object.entries(myReactions).forEach(([commentId, reactions]) => { const key = channelId ? `${commentId}:${channelId}` : commentId;
const key = channelId ? `${commentId}:${channelId}` : commentId; const mine = myReactions ? myReactions[commentId] : {};
myReacts[key] = Object.entries(reactions).reduce((acc, [name, count]) => { const others = othersReactions ? othersReactions[commentId] : {};
if (count === 1) {
acc.push(name);
}
return acc;
}, []);
});
}
if (othersReactions) { myReacts[key] = Object.entries(mine).reduce((acc, [name, count]) => {
Object.entries(othersReactions).forEach(([commentId, reactions]) => { if (count === 1) {
const key = channelId ? `${commentId}:${channelId}` : commentId; acc.push(name);
othersReacts[key] = reactions; }
}); return acc;
} }, []);
othersReacts[key] = others;
});
return { return {
...state, ...state,