From 6a8d6b07d67c5f8f8dc9d72beb911f591dccc8b4 Mon Sep 17 00:00:00 2001 From: Vinh Date: Wed, 5 Aug 2020 22:56:11 +0200 Subject: [PATCH] [CORL-1240] Check existing data before handling subscriptions (#3077) * fix: check existing data before handling subscriptions * fix: typo Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- .../Queue/QueueCommentEnteredSubscription.tsx | 13 +++++++++---- .../Queue/QueueCommentLeftSubscription.tsx | 8 +++----- .../SingleModerateSubscription.tsx | 10 ++++------ .../CommentReplyCreatedSubscription.tsx | 11 +++++++++++ .../CommentCreatedSubscription.tsx | 18 ++++++++++++++++++ .../CommentReleasedSubscription.tsx | 17 +++++++++++++++++ .../UnansweredCommentCreatedSubscription.tsx | 17 +++++++++++++++++ .../UnansweredCommentReleasedSubscription.tsx | 17 +++++++++++++++++ 8 files changed, 96 insertions(+), 15 deletions(-) diff --git a/src/core/client/admin/routes/Moderate/Queue/QueueCommentEnteredSubscription.tsx b/src/core/client/admin/routes/Moderate/Queue/QueueCommentEnteredSubscription.tsx index 0d2a22905..04188826d 100644 --- a/src/core/client/admin/routes/Moderate/Queue/QueueCommentEnteredSubscription.tsx +++ b/src/core/client/admin/routes/Moderate/Queue/QueueCommentEnteredSubscription.tsx @@ -24,11 +24,16 @@ function handleCommentEnteredModerationQueue( return; } const comment = rootField.getLinkedRecord("comment")!; + const commentID = comment.getValue("id")!; + const edgeID = `edge-${commentID}`; + const edgeInQueue = Boolean(store.get(edgeID)); + if (edgeInQueue) { + // Comment edge already in the queue, ignore it as it might be just expected race condition, + // unless the server is sending the same response multiple times. + return; + } comment.setValue(true, "enteredLive"); - const commentsEdge = store.create( - `edge-${comment.getValue("id")!}`, - "CommentsEdge" - ); + const commentsEdge = store.create(edgeID, "CommentsEdge"); commentsEdge.setValue(comment.getValue("createdAt"), "cursor"); commentsEdge.setLinkedRecord(comment, "node"); const connection = getQueueConnection(store, queue, storyID, siteID, section); diff --git a/src/core/client/admin/routes/Moderate/Queue/QueueCommentLeftSubscription.tsx b/src/core/client/admin/routes/Moderate/Queue/QueueCommentLeftSubscription.tsx index 8b9f83958..b3f20d1ee 100644 --- a/src/core/client/admin/routes/Moderate/Queue/QueueCommentLeftSubscription.tsx +++ b/src/core/client/admin/routes/Moderate/Queue/QueueCommentLeftSubscription.tsx @@ -23,14 +23,12 @@ function handleCommentLeftModerationQueue( if (!rootField) { return; } + const comment = rootField.getLinkedRecord("comment")!; const commentID = rootField .getLinkedRecord("comment")! .getValue("id")! as string; - const commentInStore = store.get(commentID); - if (commentInStore) { - // Mark that the status of the comment was live updated. - commentInStore.setValue(true, "statusLiveUpdated"); - } + // Mark that the status of the comment was live updated. + comment.setValue(true, "statusLiveUpdated"); const connection = getQueueConnection(store, queue, storyID, siteID, section); if (connection) { const linked = connection.getLinkedRecords("viewNewEdges") || []; diff --git a/src/core/client/admin/routes/Moderate/SingleModerate/SingleModerateSubscription.tsx b/src/core/client/admin/routes/Moderate/SingleModerate/SingleModerateSubscription.tsx index 01fc3a3a1..c12baa1a3 100644 --- a/src/core/client/admin/routes/Moderate/SingleModerate/SingleModerateSubscription.tsx +++ b/src/core/client/admin/routes/Moderate/SingleModerate/SingleModerateSubscription.tsx @@ -30,14 +30,12 @@ const SingleModerateSubscription = createSubscription( `, variables, updater: (store) => { - const commentID = store + const comment = store .getRootField("commentStatusUpdated")! - .getLinkedRecord("comment")! - .getValue("id")! as string; - const commentInStore = store.get(commentID); - if (commentInStore) { + .getLinkedRecord("comment")!; + if (comment) { // Mark that the status of the comment was live updated. - commentInStore.setValue(true, "statusLiveUpdated"); + comment.setValue(true, "statusLiveUpdated"); } }, }) diff --git a/src/core/client/stream/tabs/Comments/ReplyList/CommentReplyCreatedSubscription.tsx b/src/core/client/stream/tabs/Comments/ReplyList/CommentReplyCreatedSubscription.tsx index e5a74cab8..40ace7cc3 100644 --- a/src/core/client/stream/tabs/Comments/ReplyList/CommentReplyCreatedSubscription.tsx +++ b/src/core/client/stream/tabs/Comments/ReplyList/CommentReplyCreatedSubscription.tsx @@ -58,6 +58,17 @@ const CommentReplyCreatedSubscription = createSubscription( return; } const comment = rootField.getLinkedRecord("comment")!; + const commentInStore = Boolean( + // We use store from environment here, because it does not contain the response data yet! + environment + .getStore() + .getSource() + .get(comment.getValue("id") as string) + ); + if (commentInStore) { + return; + } + comment.setValue(true, "enteredLive"); const parentID = comment diff --git a/src/core/client/stream/tabs/Comments/Stream/AllCommentsTab/CommentCreatedSubscription.tsx b/src/core/client/stream/tabs/Comments/Stream/AllCommentsTab/CommentCreatedSubscription.tsx index 0a2c48ea2..5e323997e 100644 --- a/src/core/client/stream/tabs/Comments/Stream/AllCommentsTab/CommentCreatedSubscription.tsx +++ b/src/core/client/stream/tabs/Comments/Stream/AllCommentsTab/CommentCreatedSubscription.tsx @@ -72,6 +72,24 @@ const CommentCreatedSubscription = createSubscription( `, variables, updater: (store) => { + const rootField = store.getRootField("commentCreated"); + if (!rootField) { + return; + } + const commentID = rootField + .getLinkedRecord("comment")! + .getValue("id")! as string; + + const commentInStore = Boolean( + // We use store from environment here, because it does not contain the response data yet! + environment.getStore().getSource().get(commentID) + ); + if (commentInStore) { + // Comment already in the queue, ignore it as it might be just expected race condition, + // unless the server is sending the same response multiple times. + return; + } + if (variables.orderBy === GQLCOMMENT_SORT.CREATED_AT_DESC) { updateForNewestFirst(store, variables.storyID); return; diff --git a/src/core/client/stream/tabs/Comments/Stream/AllCommentsTab/CommentReleasedSubscription.tsx b/src/core/client/stream/tabs/Comments/Stream/AllCommentsTab/CommentReleasedSubscription.tsx index 1a5bf6681..42585e5d4 100644 --- a/src/core/client/stream/tabs/Comments/Stream/AllCommentsTab/CommentReleasedSubscription.tsx +++ b/src/core/client/stream/tabs/Comments/Stream/AllCommentsTab/CommentReleasedSubscription.tsx @@ -72,6 +72,23 @@ const CommentReleasedSubscription = createSubscription( `, variables, updater: (store) => { + const rootField = store.getRootField("commentReleased"); + if (!rootField) { + return; + } + const commentID = rootField + .getLinkedRecord("comment")! + .getValue("id")! as string; + const commentInStore = Boolean( + // We use store from environment here, because it does not contain the response data yet! + environment.getStore().getSource().get(commentID) + ); + if (commentInStore) { + // Comment already in the queue, ignore it as it might be just expected race condition, + // unless the server is sending the same response multiple times. + return; + } + if (variables.orderBy === GQLCOMMENT_SORT.CREATED_AT_DESC) { updateForNewestFirst(store, variables.storyID); return; diff --git a/src/core/client/stream/tabs/Comments/Stream/UnansweredCommentsTab/UnansweredCommentCreatedSubscription.tsx b/src/core/client/stream/tabs/Comments/Stream/UnansweredCommentsTab/UnansweredCommentCreatedSubscription.tsx index cf33b1bff..f1b4d4719 100644 --- a/src/core/client/stream/tabs/Comments/Stream/UnansweredCommentsTab/UnansweredCommentCreatedSubscription.tsx +++ b/src/core/client/stream/tabs/Comments/Stream/UnansweredCommentsTab/UnansweredCommentCreatedSubscription.tsx @@ -75,6 +75,23 @@ const UnansweredCommentCreatedSubscription = createSubscription( `, variables, updater: (store) => { + const rootField = store.getRootField("commentCreated"); + if (!rootField) { + return; + } + const commentID = rootField + .getLinkedRecord("comment")! + .getValue("id")! as string; + const commentInStore = Boolean( + // We use store from environment here, because it does not contain the response data yet! + environment.getStore().getSource().get(commentID) + ); + if (commentInStore) { + // Comment already in the queue, ignore it as it might be just expected race condition, + // unless the server is sending the same response multiple times. + return; + } + if (variables.orderBy === GQLCOMMENT_SORT.CREATED_AT_DESC) { updateForNewestFirst(store, variables.storyID); return; diff --git a/src/core/client/stream/tabs/Comments/Stream/UnansweredCommentsTab/UnansweredCommentReleasedSubscription.tsx b/src/core/client/stream/tabs/Comments/Stream/UnansweredCommentsTab/UnansweredCommentReleasedSubscription.tsx index 26dd72831..1067dd3eb 100644 --- a/src/core/client/stream/tabs/Comments/Stream/UnansweredCommentsTab/UnansweredCommentReleasedSubscription.tsx +++ b/src/core/client/stream/tabs/Comments/Stream/UnansweredCommentsTab/UnansweredCommentReleasedSubscription.tsx @@ -75,6 +75,23 @@ const UnansweredCommentReleasedSubscription = createSubscription( `, variables, updater: (store) => { + const rootField = store.getRootField("commentReleased"); + if (!rootField) { + return; + } + const commentID = rootField + .getLinkedRecord("comment")! + .getValue("id")! as string; + const commentInStore = Boolean( + // We use store from environment here, because it does not contain the response data yet! + environment.getStore().getSource().get(commentID) + ); + if (commentInStore) { + // Comment already in the queue, ignore it as it might be just expected race condition, + // unless the server is sending the same response multiple times. + return; + } + if (variables.orderBy === GQLCOMMENT_SORT.CREATED_AT_DESC) { updateForNewestFirst(store, variables.storyID); return;