Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start to use Comment collection schema #204

Merged
merged 58 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
69e3905
Added Comment class file
Sebastian-Webster Oct 14, 2023
8db161d
Partially add temp/deletecomment APIs
Sebastian-Webster Oct 14, 2023
2d52bff
Added temp/deletecomment API
Sebastian-Webster Oct 14, 2023
a72d38c
Added Comment library
Sebastian-Webster Oct 14, 2023
b36a720
temp/pollpostcomment now uses Comment collection
Sebastian-Webster Oct 14, 2023
d0b0346
Added poll check to temp/pollpostcomment
Sebastian-Webster Oct 14, 2023
c386daf
Added to temp/pollpostcomment
Sebastian-Webster Oct 14, 2023
dfc4624
temp/pollpostcommentreply now uses Comment collection
Sebastian-Webster Oct 14, 2023
7df7e7a
temp/searchforpollcomments now uses Comment collection
Sebastian-Webster Oct 15, 2023
86ffe42
temp/getsinglepollcomment now uses Comment collection
Sebastian-Webster Oct 15, 2023
738df48
Fixed issue with Comment library
Sebastian-Webster Oct 15, 2023
1adeb73
temp/pollpostcommentreply now doesn't allow nested replies
Sebastian-Webster Oct 15, 2023
26e4f99
temp/searchforpollcommentreplies now uses Comment collection
Sebastian-Webster Oct 15, 2023
be13156
temp/imagepostcomment now uses Comment collection
Sebastian-Webster Oct 15, 2023
3bcf241
temp/imagepostcommentreply now uses Comment collection
Sebastian-Webster Oct 15, 2023
a03195b
temp/getimagepostcomments now uses Comment collection
Sebastian-Webster Oct 15, 2023
5589980
Fixed issues with getting comments
Sebastian-Webster Oct 15, 2023
3fe9f88
Flipped order of arguments for commentHandler comment processor
Sebastian-Webster Oct 15, 2023
8dd8c78
Removed get single comment and search for comment replies APIs
Sebastian-Webster Oct 15, 2023
81f881e
Added temp/getsinglecomment API
Sebastian-Webster Oct 15, 2023
9073bb5
Created temp/getcommentreplies API
Sebastian-Webster Oct 15, 2023
04c161a
temp/threadpostcomment now uses Comment collection
Sebastian-Webster Oct 15, 2023
74ef86c
temp/threadpostcommentreply now uses Comment collection
Sebastian-Webster Oct 15, 2023
62e2ff7
temp/searchforthreadcomments now uses Comment collection
Sebastian-Webster Oct 15, 2023
756c759
temp/deletecomment is now complete
Sebastian-Webster Oct 16, 2023
6307739
temp/upvotecomment now uses Comment collection
Sebastian-Webster Oct 16, 2023
119d91c
Replaced temp/upvotecomment and temp/downvotecomment with temp/voteon…
Sebastian-Webster Oct 16, 2023
52d2f85
Replaced post specific comment reply APIs with temp/replytocomment
Sebastian-Webster Oct 16, 2023
a366f77
temp/deleteaccount now uses Comment collection
Sebastian-Webster Oct 16, 2023
2cdb884
Await db operations
Sebastian-Webster Oct 16, 2023
c146136
Modify post libraries to use Comment collection
Sebastian-Webster Oct 16, 2023
f30884d
Added OneTimeExecuteCode
Sebastian-Webster Oct 16, 2023
8d40207
Replaced post specific post comment APIs with temp/postcomment
Sebastian-Webster Oct 17, 2023
7879426
Added more safety features to temp/getcommentreplies
Sebastian-Webster Oct 17, 2023
fd04824
Replaced post specific search comments API with temp/searchforpostcom…
Sebastian-Webster Oct 17, 2023
468f02b
CommentCollection OTEC now unsets comments field
Sebastian-Webster Oct 17, 2023
5ac2df2
Fixed issue with OETC CommentCollection
Sebastian-Webster Oct 17, 2023
4bd5058
Promise.all now receives an array instead of many parameters
Sebastian-Webster Oct 17, 2023
b3a512f
Removed update query for unsetting comments documents in OTEC Comment…
Sebastian-Webster Oct 17, 2023
47e1842
Fixed OETC CommentCollection so it unsets comments field from all posts
Sebastian-Webster Oct 17, 2023
726ff9b
Removed OETC in server.js
Sebastian-Webster Oct 17, 2023
056b05d
Fixed issue with Array.returnOwnerPostPairs
Sebastian-Webster Oct 17, 2023
b37630d
Fixed comments not being sent correctly
Sebastian-Webster Oct 18, 2023
1abcf1d
Removed unneccesary database query
Sebastian-Webster Oct 18, 2023
f40623b
Added process one comment library method
Sebastian-Webster Oct 18, 2023
f7c5dd5
Stringify ObjectIds in Comment library
Sebastian-Webster Oct 18, 2023
f0176da
Fixed posting comment failing
Sebastian-Webster Oct 18, 2023
1585e13
temp/voteoncomment now ensures only one vote per comment per person c…
Sebastian-Webster Oct 18, 2023
279b049
temp/deleteaccount now deletes comments made on deleted posts
Sebastian-Webster Oct 18, 2023
b244409
temp/searchforpostcomments no longer returns comment replies
Sebastian-Webster Oct 18, 2023
218575a
temp/deleteaccount now deletes votes from comments
Sebastian-Webster Oct 18, 2023
8049a28
temp/deletepoll now deletes associated comments and comment votes
Sebastian-Webster Oct 18, 2023
87bd39a
temp/deleteimage now deletes associated comments and comment votes
Sebastian-Webster Oct 18, 2023
1069734
temp/deleteimage now deletes image file
Sebastian-Webster Oct 18, 2023
076c040
temp/deletepoll now correctly gets comment ids
Sebastian-Webster Oct 18, 2023
b1670ab
Fixed incorrect Promise.alls
Sebastian-Webster Oct 18, 2023
21cfbc5
temp/deletethread now deletes associated comments and comment votes
Sebastian-Webster Oct 18, 2023
5e19cf3
Image file now gets deleted after committing transaction in temp/dele…
Sebastian-Webster Oct 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions OneTimeExecuteCode/CommentCollection.js
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)
})
1 change: 1 addition & 0 deletions constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const CONSTANTS = {
NUM_THREAD_POSTS_TO_SEND_PER_API_CALL: 10, //Used for temp/getthreadsfromprofile API - Determines how many thread posts to send per API call
NUM_CATEGORIES_TO_SEND_PER_API_CALL: 10, //Used for temp/findcategoryfromprofile, temp/getCategoriesUserIsAPartOf, and temp/searchpagesearchcategories APIs - Determines how many categories to send per API call
NUM_USERS_TO_SEND_PER_PROFILE_STATS_API_CALL: 10, //Used for temp/getProfileStats API - Determines how many users they are following or users that follow them to send per API call
COMMENT_API_ALLOWED_POST_FORMATS: ["Image", "Poll", "Thread"]
}

module.exports = CONSTANTS
5,190 changes: 2,122 additions & 3,068 deletions controllers/Temp.js

Large diffs are not rendered by default.

34 changes: 32 additions & 2 deletions libraries/Array.js
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;
62 changes: 62 additions & 0 deletions libraries/Comment.js
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;
6 changes: 4 additions & 2 deletions libraries/ImagePost.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Upvote = require('../models/Upvote')
const Downvote = require('../models/Downvote')
const ImagePost = require('../models/ImagePost')
const Comment = require('../models/Comment')
const ImageLibrary = require('../libraries/Image')
const imageLib = new ImageLibrary()

Expand All @@ -17,7 +18,8 @@ class ImagePostClass {
Downvote.countDocuments({postId: {$eq: post._id}, postFormat: "Image"}),
Upvote.findOne({postId: {$eq: post._id}, postFormat: "Image", userPublicId: userRequesting.secondId}),
Downvote.findOne({postId: {$eq: post._id}, postFormat: "Image", userPublicId: userRequesting.secondId}),
]).then(([upvotes, downvotes, isUpvoted, isDownvoted]) => {
Comment.countDocuments({postId: {$eq: post._id}, postFormat: "Image"})
]).then(([upvotes, downvotes, isUpvoted, isDownvoted, comments]) => {
const postObject = {
...post,
votes: upvotes - downvotes,
Expand All @@ -29,7 +31,7 @@ class ImagePostClass {
isOwner: postOwner._id.toString() === userRequesting._id.toString(),
interacted: !!isUpvoted || !!isDownvoted,
_id: post._id.toString(),
comments: Array.isArray(post.comments) ? post.comments.length : 0
comments
}

if (isUpvoted) {
Expand Down
8 changes: 5 additions & 3 deletions libraries/PollPost.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const Upvote = require('../models/Upvote')
const Downvote = require('../models/Downvote')
const Poll = require('../models/Poll')
const PollVote = require('../models/PollVote')
const Comment = require('../models/Comment')

class PollPost {
processMultiplePostDataFromOneOwner(posts, postOwner, userRequesting) {
Expand All @@ -22,8 +23,9 @@ class PollPost {
PollVote.countDocuments({pollId: {$eq: post._id}, vote: 'Three'}),
PollVote.countDocuments({pollId: {$eq: post._id}, vote: 'Four'}),
PollVote.countDocuments({pollId: {$eq: post._id}, vote: 'Five'}),
PollVote.countDocuments({pollId: {$eq: post._id}, vote: 'Six'})
]).then(([upvotes, downvotes, isUpvoted, isDownvoted, pollVote, optionOnesVotes, optionTwosVotes, optionThreesVotes, optionFoursVotes, optionFivesVotes, optionSixesVotes]) => {
PollVote.countDocuments({pollId: {$eq: post._id}, vote: 'Six'}),
Comment.countDocuments({postId: {$eq: post._id}, postFormat: "Poll"})
]).then(([upvotes, downvotes, isUpvoted, isDownvoted, pollVote, optionOnesVotes, optionTwosVotes, optionThreesVotes, optionFoursVotes, optionFivesVotes, optionSixesVotes, comments]) => {
const postObject = {
...post,
optionOnesVotes,
Expand All @@ -42,7 +44,7 @@ class PollPost {
isOwner: postOwner._id.toString() === userRequesting._id.toString(),
interacted: !!isUpvoted || !!isDownvoted,
votedFor: pollVote?.vote || "None",
comments: Array.isArray(post.comments) ? post.comments.length : 0
comments
}

if (isUpvoted) {
Expand Down
8 changes: 5 additions & 3 deletions libraries/ThreadPost.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Downvote = require('../models/Downvote')
const Thread = require('../models/Thread')
const ImageLibrary = require('./Image')
const Category = require('../models/Category')
const Comment = require('../models/Comment')
const imageLib = new ImageLibrary()

class ThreadPost {
Expand All @@ -18,8 +19,9 @@ class ThreadPost {
Downvote.countDocuments({postId: {$eq: post._id}, postFormat: "Thread"}),
Upvote.findOne({postId: {$eq: post._id}, postFormat: "Thread", userPublicId: {$eq: userRequesting.secondId}}),
Downvote.findOne({postId: {$eq: post._id}, postFormat: "Thread", userPublicId: {$eq: userRequesting.secondId}}),
Category.findOne({_id: {$eq: post.threadCategoryId}}, {categoryTitle: 1})
]).then(([upvotes, downvotes, isUpvoted, isDownvoted, category]) => {
Category.findOne({_id: {$eq: post.threadCategoryId}}, {categoryTitle: 1}),
Comment.countDocuments({postId: {$eq: post._id}, postFormat: "Thread"})
]).then(([upvotes, downvotes, isUpvoted, isDownvoted, category, comments]) => {
const postObject = {
...post,
votes: upvotes - downvotes,
Expand All @@ -32,7 +34,7 @@ class ThreadPost {
interacted: !!isUpvoted || !!isDownvoted,
_id: String(post._id),
threadCategory: category.categoryTitle,
comments: Array.isArray(post.comments) ? post.comments.length : 0
comments
}

if (isUpvoted) {
Expand Down
4 changes: 3 additions & 1 deletion models/Comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ const CommentSchema = new Schema({
datePosted: Number,
parentCommentId: mongoose.Schema.Types.ObjectId,
postId: mongoose.Schema.Types.ObjectId,
postFormat: String
postFormat: String,
deleted: Boolean,
replies: {type: Number, default: 0}
});

const Comment = mongoose.model('Comment', CommentSchema);
Expand Down
Loading