diff --git a/src/pages/thread/CommentSection.js b/src/pages/thread/CommentSection.js index a55cbf5..435d8a8 100644 --- a/src/pages/thread/CommentSection.js +++ b/src/pages/thread/CommentSection.js @@ -6,35 +6,20 @@ import { showRemovedAndDeleted, showRemoved, showDeleted } from '../../utils' -const arrayToLookup = (commentList, removed, deleted) => { - const lookup = new Map() - - commentList.forEach(comment => { - comment.replies = [] - - if (removed.has(comment.id)) { - comment.removed = true - } else if (deleted.has(comment.id)) { - comment.deleted = true - } - - lookup.set(comment.id, comment) - }) - - return lookup -} - -const unflatten = (comments, root, removed, deleted) => { - const lookup = arrayToLookup(comments, removed, deleted) +const unflatten = (commentMap, root, removed, deleted) => { const commentTree = [] - lookup.forEach(comment => { + commentMap.forEach(comment => { + comment.replies = [] + }) + + commentMap.forEach(comment => { const parentID = comment.parent_id let parentComment if (parentID === root) { commentTree.push(comment) - } else if ((parentComment = lookup.get(parentID)) !== undefined) { + } else if ((parentComment = commentMap.get(parentID)) !== undefined) { parentComment.replies.push(comment) } else { console.error('MISSING PARENT ID:', parentID, 'for comment', comment) @@ -42,7 +27,7 @@ const unflatten = (comments, root, removed, deleted) => { }) let rootComment - if ((rootComment = lookup.get(root)) !== undefined) { + if ((rootComment = commentMap.get(root)) !== undefined) { rootComment.replies = commentTree return [rootComment] } diff --git a/src/pages/thread/index.js b/src/pages/thread/index.js index 7556e3d..a2e34b7 100644 --- a/src/pages/thread/index.js +++ b/src/pages/thread/index.js @@ -18,9 +18,9 @@ import CommentInfo from './CommentInfo' class Thread extends React.Component { state = { post: {}, - pushshiftComments: [], - removed: new Set(), - deleted: new Set(), + pushshiftCommentLookup: new Map(), + removed: [], + deleted: [], loadingComments: true } @@ -74,16 +74,17 @@ class Thread extends React.Component { return getRedditComments(ids) .then(redditComments => { console.log(`Reddit: ${redditComments.length} comments`) - const removed = new Set() - const deleted = new Set() + const removed = [] + const deleted = [] redditComments.forEach(comment => { - const pushshiftComment = pushshiftCommentLookup.get(comment.id) + let pushshiftComment = pushshiftCommentLookup.get(comment.id) if (pushshiftComment === undefined) { // When a parent comment is missing from pushshift, use the reddit comment instead comment.parent_id = comment.parent_id.substring(3) comment.link_id = comment.link_id.substring(3) - pushshiftCommentLookup.set(comment.id, comment) + pushshiftComment = comment + pushshiftCommentLookup.set(comment.id, pushshiftComment) } else { // Replace pushshift score with reddit (it's usually more accurate) pushshiftComment.score = comment.score @@ -91,10 +92,12 @@ class Thread extends React.Component { // Check what is removed / deleted according to reddit if (isRemoved(comment.body)) { - removed.add(comment.id) + removed.push(comment.id) + pushshiftComment.removed = true } else if (isDeleted(comment.body)) { - deleted.add(comment.id) - } else if (pushshiftComment !== undefined && isRemoved(pushshiftComment.body)) { + deleted.push(comment.id) + pushshiftComment.deleted = true + } else if (pushshiftComment !== comment && isRemoved(pushshiftComment.body)) { // If it's deleted in pushshift, but later restored by a mod, use the restored comment.parent_id = comment.parent_id.substring(3) comment.link_id = comment.link_id.substring(3) @@ -104,7 +107,7 @@ class Thread extends React.Component { this.props.global.setSuccess() this.setState({ - pushshiftComments: Array.from(pushshiftCommentLookup.values()), + pushshiftCommentLookup, removed, deleted, loadingComments: false @@ -129,9 +132,9 @@ class Thread extends React.Component { (!this.state.loadingComments && root) && {isSingleComment && @@ -142,7 +145,7 @@ class Thread extends React.Component { }