From f9bc5f79ec4feabd2e700b66776d64fd8e67698e Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Mon, 28 Aug 2017 18:08:33 +0700 Subject: [PATCH] Don't sort and ignore duplicate comments (embed-stream) --- .../coral-embed-stream/src/graphql/utils.js | 13 +++-------- client/coral-framework/utils/index.js | 23 ++++--------------- .../containers/ProfileContainer.js | 4 ++-- plugin-api/beta/client/utils/index.js | 3 ++- .../client/containers/TabPane.js | 4 ++-- .../client/index.js | 4 ++-- 6 files changed, 16 insertions(+), 35 deletions(-) diff --git a/client/coral-embed-stream/src/graphql/utils.js b/client/coral-embed-stream/src/graphql/utils.js index 11d3d411b..a63b6f8a6 100644 --- a/client/coral-embed-stream/src/graphql/utils.js +++ b/client/coral-embed-stream/src/graphql/utils.js @@ -1,5 +1,5 @@ import update from 'immutability-helper'; -import {insertCommentsSorted} from 'coral-framework/utils'; +import {appendNewNodes} from 'coral-framework/utils'; function determineCommentDepth(comment) { let depth = 0; @@ -159,14 +159,7 @@ function findAndInsertFetchedComments(parent, comments, parent_id) { [connectionField]: { hasNextPage: {$set: comments.hasNextPage}, endCursor: {$set: comments.endCursor}, - nodes: {$apply: (nodes) => { - if (isAsset) { - return nodes.concat(comments.nodes); - } - return insertCommentsSorted(nodes, comments.nodes.filter( - (comment) => !nodes.some((node) => node.id === comment.id) - )); - }}, + nodes: {$apply: (nodes) => appendNewNodes(nodes, comments.nodes)}, }, }); } @@ -198,7 +191,7 @@ export function attachCommentToParent(topLevelComment, comment) { return update(topLevelComment, { replies: { nodes: { - $apply: (nodes) => insertCommentsSorted(nodes, comment), + $apply: (nodes) => appendNewNodes(nodes, [comment]), }, }, replyCount: { diff --git a/client/coral-framework/utils/index.js b/client/coral-framework/utils/index.js index 4b771cef1..533ed85db 100644 --- a/client/coral-framework/utils/index.js +++ b/client/coral-framework/utils/index.js @@ -154,25 +154,12 @@ export function getErrorMessages(error) { return result; } -const ascending = (a, b) => { - const dateA = new Date(a.created_at); - const dateB = new Date(b.created_at); - if (dateA < dateB) { return -1; } - if (dateA > dateB) { return 1; } - return 0; -}; +export function appendNewNodes(nodesA, nodesB) { + return nodesA.concat(nodesB.filter((nodeB) => !nodesA.some((nodeA) => nodeA.id === nodeB.id))); +} -const descending = (a, b) => ascending(a, b) * -1; - -export function insertCommentsSorted(nodes, comments, sortOrder = 'ASC') { - const added = nodes.concat(comments); - if (sortOrder === 'ASC') { - return added.sort(ascending); - } - if (sortOrder === 'DESC') { - return added.sort(descending); - } - throw new Error(`Unknown sort order ${sortOrder}`); +export function prependNewNodes(nodesA, nodesB) { + return nodesB.filter((nodeB) => !nodesA.some((nodeA) => nodeA.id === nodeB.id)).concat(nodesA); } export const isTagged = (tags, which) => tags.some((t) => t.tag.name === which); diff --git a/client/coral-settings/containers/ProfileContainer.js b/client/coral-settings/containers/ProfileContainer.js index c59327338..f19aabff8 100644 --- a/client/coral-settings/containers/ProfileContainer.js +++ b/client/coral-settings/containers/ProfileContainer.js @@ -15,7 +15,7 @@ import CommentHistory from 'talk-plugin-history/CommentHistory'; // TODO: Auth logic needs refactoring. import {showSignInDialog, checkLogin} from 'coral-embed-stream/src/actions/auth'; -import {insertCommentsSorted} from 'plugin-api/beta/client/utils'; +import {appendNewNodes} from 'plugin-api/beta/client/utils'; import update from 'immutability-helper'; import {getSlotFragmentSpreads} from 'coral-framework/utils'; @@ -42,7 +42,7 @@ class ProfileContainer extends Component { me: { comments: { nodes: { - $apply: (nodes) => insertCommentsSorted(nodes, comments.nodes, 'DESC'), + $apply: (nodes) => appendNewNodes(nodes, comments.nodes), }, hasNextPage: {$set: comments.hasNextPage}, endCursor: {$set: comments.endCursor}, diff --git a/plugin-api/beta/client/utils/index.js b/plugin-api/beta/client/utils/index.js index fe1ba8bad..6dd18f3a1 100644 --- a/plugin-api/beta/client/utils/index.js +++ b/plugin-api/beta/client/utils/index.js @@ -1,6 +1,7 @@ export { isTagged, - insertCommentsSorted, + prependNewNodes, + appendNewNodes, getSlotFragmentSpreads, forEachError, capitalize, diff --git a/plugins/talk-plugin-featured-comments/client/containers/TabPane.js b/plugins/talk-plugin-featured-comments/client/containers/TabPane.js index 5b1686353..d66a0fd49 100644 --- a/plugins/talk-plugin-featured-comments/client/containers/TabPane.js +++ b/plugins/talk-plugin-featured-comments/client/containers/TabPane.js @@ -6,7 +6,7 @@ import {withFragments, connect} from 'plugin-api/beta/client/hocs'; import Comment from '../containers/Comment'; import {addNotification} from 'plugin-api/beta/client/actions/notification'; import {viewComment} from 'coral-embed-stream/src/actions/stream'; -import {getDefinitionName} from 'plugin-api/beta/client/utils'; +import {appendNewNodes, getDefinitionName} from 'plugin-api/beta/client/utils'; import update from 'immutability-helper'; class TabPaneContainer extends React.Component { @@ -27,7 +27,7 @@ class TabPaneContainer extends React.Component { asset: { featuredComments: { nodes: { - $push: comments.nodes, + $apply: (nodes) => appendNewNodes(nodes, comments.nodes), }, hasNextPage: {$set: comments.hasNextPage}, endCursor: {$set: comments.endCursor}, diff --git a/plugins/talk-plugin-featured-comments/client/index.js b/plugins/talk-plugin-featured-comments/client/index.js index 30d59c546..c9598b1dd 100644 --- a/plugins/talk-plugin-featured-comments/client/index.js +++ b/plugins/talk-plugin-featured-comments/client/index.js @@ -9,7 +9,7 @@ import ModTag from './containers/ModTag'; import ModSubscription from './containers/ModSubscription'; import {findCommentInEmbedQuery} from 'coral-embed-stream/src/graphql/utils'; -import {insertCommentsSorted} from 'plugin-api/beta/client/utils'; +import {prependNewNodes} from 'plugin-api/beta/client/utils'; export default { reducer, @@ -60,7 +60,7 @@ export default { asset: { featuredComments: { nodes: { - $apply: (nodes) => insertCommentsSorted(nodes, comment, 'DESC') + $apply: (nodes) => prependNewNodes(nodes, [comment]), } }, featuredCommentsCount: {