-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #204 from SquareTable/203-make-comment-collection-…
…and-schema Start to use Comment collection schema
- Loading branch information
Showing
11 changed files
with
3,023 additions
and
4,073 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
//This is the OneTimeExecuteCode for moving comments from being an array inside of a post document to being its own collection | ||
|
||
const ImagePost = require('../models/ImagePost'); | ||
const Poll = require('../models/Poll'); | ||
const Thread = require('../models/Thread'); | ||
const Comment = require('../models/Comment'); | ||
const mongoose = require('mongoose'); | ||
|
||
//Usually we would have to implement functionality for comment replies, but as of writing this code, no comment replies have been made. | ||
//For the sake of simplicity, comment reply transfers will be ignored (since there are none) | ||
|
||
Promise.all([ | ||
ImagePost.find({"comments.0": {$exists: true}}).lean(), | ||
Poll.find({"comments.0": {$exists: true}}).lean(), | ||
Thread.find({"comments.0": {$exists: true}}).lean() | ||
]).then(([images, polls, threads]) => { | ||
const updates = []; | ||
|
||
for (const image of images) { | ||
for (const comment of image.comments) { | ||
updates.push({ | ||
commenterId: comment.commenterId, | ||
text: comment.commentsText, | ||
datePosted: comment.datePosted, | ||
postId: image._id, | ||
postFormat: "Image", | ||
replies: 0 | ||
}) | ||
} | ||
} | ||
|
||
for (const poll of polls) { | ||
for (const comment of poll.comments) { | ||
updates.push({ | ||
commenterId: comment.commenterId, | ||
text: comment.commentsText, | ||
datePosted: comment.datePosted, | ||
postId: poll._id, | ||
postFormat: "Poll", | ||
replies: 0 | ||
}) | ||
} | ||
} | ||
|
||
for (const thread of threads) { | ||
for (const comment of thread.comments) { | ||
updates.push({ | ||
commenterId: comment.commenterId, | ||
text: comment.commentsText, | ||
datePosted: comment.datePosted, | ||
postId: thread._id, | ||
postFormat: "Thread", | ||
replies: 0 | ||
}) | ||
} | ||
} | ||
|
||
mongoose.startSession().then(session => { | ||
session.startTransaction(); | ||
|
||
Promise.all([ | ||
Comment.insertMany(updates, {session}), | ||
ImagePost.updateMany({}, {$unset: {comments: ""}}, {session}), | ||
Poll.updateMany({}, {$unset: {comments: ""}}, {session}), | ||
Thread.updateMany({}, {$unset: {comments: ""}}, {session}) | ||
]).then(() => { | ||
session.commitTransaction().then(() => { | ||
session.endSession().then(() => { | ||
console.log('Successfully transferred', updates.length, 'comments into the Comment collection!') | ||
}).catch(error => { | ||
console.error('An erorr occurred while ending Mongoose session:', error) | ||
}) | ||
}).catch(error => { | ||
console.error('An error occurred while committing comment transfer transaction:', error) | ||
session.endSession().catch(error => { | ||
console.error('An error occurred while ending Mongoose session:', error) | ||
}) | ||
}) | ||
}).catch(error => { | ||
console.error('An error occurred while making comment transfer database operations:', error) | ||
session.endSession().catch(error => { | ||
console.error('An error occurred while ending Mongoose session:', error) | ||
}) | ||
}) | ||
}).catch(error => { | ||
console.error('An error occurred while starting Mongoose session:', error) | ||
}) | ||
}).catch(error => { | ||
console.error('An error occurred while getting images, polls, and threads:', error) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,37 @@ | ||
class Array { | ||
class ArrayClass { | ||
returnSomeItems(array, skip, limit) { | ||
return {items: array.splice(skip, limit), noMoreItems: skip >= array.length} | ||
} | ||
|
||
returnOwnerPostPairs(posts, owners, ownerIdKey) { | ||
const users = {}; | ||
for (const user of owners) { | ||
users[String(user._id)] = user; | ||
} | ||
|
||
const postOwnerPairMap = new Map(); | ||
const postsWithNoOwners = []; | ||
|
||
for (const post of posts) { | ||
const ownerId = String(post[ownerIdKey]) | ||
const owner = users[ownerId]; | ||
|
||
if (owner) { | ||
if (postOwnerPairMap.has(owner)) { | ||
postOwnerPairMap.get(owner).push(post) | ||
} else { | ||
postOwnerPairMap.set(owner, [post]) | ||
} | ||
} else { | ||
postsWithNoOwners.push(post) | ||
} | ||
} | ||
|
||
return { | ||
ownerPostPairs: Array.from(postOwnerPairMap.entries()), | ||
postsWithNoOwners | ||
} | ||
} | ||
} | ||
|
||
module.exports = Array; | ||
module.exports = ArrayClass; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const Upvote = require('../models/Upvote') | ||
const Downvote = require('../models/Downvote') | ||
|
||
class CommentLibrary { | ||
processOneCommentFromOneOwner(commentOwner, comment, userRequesting) { | ||
delete comment.commenterId | ||
delete comment.__v | ||
return new Promise((resolve, reject) => { | ||
Promise.all([ | ||
Upvote.countDocuments({postId: {$eq: comment._id}, postFormat: "Comment"}), | ||
Downvote.countDocuments({postId: {$eq: comment._id}, postFormat: "Comment"}), | ||
Upvote.findOne({postId: {$eq: comment._id}, postFormat: "Comment", userPublicId: userRequesting.secondId}), | ||
Downvote.findOne({postId: {$eq: comment._id}, postFormat: "Comment", userPublicId: userRequesting.secondId}) | ||
]).then(([upvotes, downvotes, isUpvoted, isDownvoted]) => { | ||
const commentObject = { | ||
...comment, | ||
votes: upvotes - downvotes, | ||
commenterName: commentOwner.name, | ||
commenterDisplayName: commentOwner.displayName, | ||
profileImageKey: commentOwner.profileImageKey, | ||
upvoted: !!isUpvoted, | ||
downvoted: !!isDownvoted, | ||
isOwner: commentOwner._id.toString() === userRequesting._id.toString(), | ||
interacted: !!isUpvoted || !!isDownvoted, | ||
_id: String(comment._id), | ||
commenterId: String(comment.commenterId) | ||
} | ||
|
||
if (isUpvoted) { | ||
commentObject.voteId = isUpvoted._id.toString() | ||
} | ||
|
||
if (isDownvoted) { | ||
commentObject.voteId = isDownvoted._id.toString() | ||
} | ||
|
||
if (comment.parentCommentId) { | ||
commentObject.parentCommentId = String(commentObject.parentCommentId) | ||
} | ||
|
||
resolve(commentObject) | ||
}).catch(error => { | ||
reject(`An error occured while executing Promise.all in CommentLibrary.processMultipleCommentsFromOneOwner: ${error}`) | ||
}) | ||
}) | ||
} | ||
|
||
processMultipleCommentsFromOneOwner(commentOwner, comments, userRequesting) { | ||
return new Promise((resolve, reject) => { | ||
|
||
Promise.all( | ||
comments.map(comment => this.processOneCommentFromOneOwner(commentOwner, comment, userRequesting)) | ||
).then(images => { | ||
resolve(images) | ||
}).catch(error => { | ||
reject(`An error occured in the final Promise.all in CommentLibrary.processMultipleCommentsFromOneOwner: ${error}`) | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
module.exports = CommentLibrary; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.