diff --git a/controllers/Temp.js b/controllers/Temp.js index 8d7ea472..fccfd5d2 100644 --- a/controllers/Temp.js +++ b/controllers/Temp.js @@ -4695,10 +4695,12 @@ class TempController { return resolve(HTTPWTHandler.forbidden('You cannot follow yourself')) } + if (userGettingFollowed.followers.includes(userFollowingFound.secondId)) { + //Already following account + + mongoose.startSession().then(session => { + session.startTransaction(); - if (userGettingFollowed.privateAccount == true) { - if (userGettingFollowed.followers.includes(userFollowingFound.secondId)) { - //UnFollow private account const dbUpdates = [ { updateOne: { @@ -4713,14 +4715,37 @@ class TempController { } } ] - + User.bulkWrite(dbUpdates).then(() => { - return resolve(HTTPWTHandler.OK('UnFollowed user')) + session.commitTransaction().then(() => { + session.endSession().catch(error => { + console.error('An error occurred while ending Mongoose session:', error) + }).finally(() => { + return resolve(HTTPWTHandler.OK('UnFollowed user')) + }) + }).catch(error => { + console.error('An error occurred while commiting transaction and ending session. The error was:', error) + return resolve(HTTPWTHandler.serverError('An error occurred while unfollowing account. Please try again.')) + }) }).catch(error => { - console.error('An error occurred while unfollowing private account using bulkWrite on the User collection. The updates array was:', dbUpdates, '. The error was:', error) - return resolve(HTTPWTHandler.serverError('An error occurred while unfollowing user. Please try again.')) + session.abortTransaction().catch(error => { + console.error('An error occurred while aborting transaction:', error) + }).finally(() => { + session.endSession().catch(error => { + console.error('An error occurred while ending mongoose session:', error) + }).finally(() => { + console.error('An error occurred while unfollowing account using bulkWrite on the User collection. The updates array was:', dbUpdates, '. The error was:', error) + return resolve(HTTPWTHandler.serverError('An error occurred while unfollowing user. Please try again.')) + }) + }) }) - } else { + }).catch(error => { + console.error('An error occurred while starting mongoose session. The error was:', error) + return resolve(HTTPWTHandler.serverError('An error occurred while unfollowing account. Please try again.')) + }) + } else { + //Is not following account + if (userGettingFollowed.privateAccount == true) { if (!userGettingFollowed.accountFollowRequests.includes(userFollowingFound.secondId)) { //Request to follow the account User.findOneAndUpdate({_id: userGettingFollowed._id}, {$addToSet: {accountFollowRequests: userFollowingFound.secondId}}).then(function() { @@ -4750,65 +4775,61 @@ class TempController { return resolve(HTTPWTHandler.serverError('An error occurred while removing request to follow user. Please try again.')) }) } - } - } else { - if (!userGettingFollowed.followers.includes(userFollowingFound.secondId)) { - //Follow - - const dbUpdates = [ - { - updateOne: { - filter: {_id: {$eq: userGettingFollowed._id}}, - update: {$addToSet : {followers: userFollowingFound.secondId}} - } - }, - { - updateOne: { - filter: {_id: {$eq: userId}}, - update: { $addToSet : {following: userGettingFollowed.secondId}} - } - } - ] - - User.bulkWrite(dbUpdates).then(() => { - var notifMessage = { - title: "New Follower", - body: userFollowingFound[0].name + " has followed you." - } - var notifData = { - type: "Follow", - pubIdOfFollower: userFollowingFound[0].secondId - } - sendNotifications(userGettingFollowed[0]._id, notifMessage, notifData) - - return resolve(HTTPWTHandler.OK('Followed User')) - }).catch(error => { - console.error('An error occurred while following not-private account using bulkWrite on the User collection. The updates array was:', dbUpdates, '. The error was:', error) - return resolve(HTTPWTHandler.serverError('An error occurred while following user. Please try again.')) - }) } else { - //UnFollow - - const dbUpdates = [ - { - updateOne: { - filter: {_id: {$eq: userGettingFollowed._id}}, - update: {$pull : {followers: userFollowingFound.secondId}} + mongoose.startSession().then(session => { + session.startTransaction(); + + const dbUpdates = [ + { + updateOne: { + filter: {_id: {$eq: userGettingFollowed._id}}, + update: {$addToSet : {followers: userFollowingFound.secondId}} + } + }, + { + updateOne: { + filter: {_id: {$eq: userId}}, + update: { $addToSet : {following: userGettingFollowed.secondId}} + } } - }, - { - updateOne: { - filter: {_id: {$eq: userId}}, - update: { $pull : {following: userGettingFollowed.secondId}} + ] + + User.bulkWrite(dbUpdates).then(() => { + var notifMessage = { + title: "New Follower", + body: userFollowingFound[0].name + " has followed you." } - } - ] + var notifData = { + type: "Follow", + pubIdOfFollower: userFollowingFound[0].secondId + } + sendNotifications(userGettingFollowed[0]._id, notifMessage, notifData) - User.bulkWrite(dbUpdates).then(() => { - return resolve(HTTPWTHandler.OK('UnFollowed User')) + session.commitTransaction().then(() => { + session.endSession().catch(error => { + console.error('An error occurred while ending mongoose session:', error) + }).finally(() => { + return resolve(HTTPWTHandler.OK('Followed User')) + }) + }).catch(error => { + console.error('An error occurred while commiting transaction and ending session. The error was:', error) + return resolve(HTTPWTHandler.serverError('An error occurred while following account. Please try again.')) + }) + }).catch(error => { + session.abortTransaction().catch(error => { + console.error('An error occurred while aborting transaction:', error) + }).finally(() => { + session.endSession().catch(error => { + console.error('An error occurred while ending Mongoose session. The error was:', error) + }).finally(() => { + console.error('An error occurred while following not-private account using bulkWrite on the User collection. The updates array was:', dbUpdates, '. The error was:', error) + return resolve(HTTPWTHandler.serverError('An error occurred while following user. Please try again.')) + }) + }) + }) }).catch(error => { - console.error('An error occurred while unfollowing not-private account using bulkWrite on the User collection. The updates array was:', dbUpdates, '. The error was:', error) - return resolve(HTTPWTHandler.serverError('An error occurred while unfollowing user. Please try again.')) + console.error('An error occurred while starting a mongoose session. The error was:', error) + return resolve(HTTPWTHandler.serverError('An error occurred while following account. Please try again.')) }) } } @@ -5024,52 +5045,64 @@ class TempController { static #makeaccountpublic = (userId) => { return new Promise(resolve => { - const makeAccountPublic = () => { - User.findOneAndUpdate({_id: {$eq: userId}}, {privateAccount: false}).then(function() { - return resolve(HTTPWTHandler.OK('Account is now public.')) - }).catch((error) => { - console.error('An error occurred while setting privateAccount to false for user with id:', userId, '. The error was:', error) - return resolve(HTTPWTHandler.serverError('An error occurred while making the account public. Please try again.')) - }) - }; - User.findOne({_id: {$eq: userId}}).lean().then(userFound => { if (userFound) { //User found const accountsRequestingToFollow = userFound.accountFollowRequests + + const dbUpdates = [ + { + updateOne: { + filter: {_id: {$eq: userId}}, + update: {$set: {privateAccount: false}} + } + } + ] + if (accountsRequestingToFollow) { - const dbUpdates = [ - { - updateOne: { - filter: {_id: {$eq: userId}}, - update: {$push: {followers: {$each: accountsRequestingToFollow}}} - } - }, - { + //Add all accounts' public ids requesting to follow to the user's followers list, add user's public id to each account requesting to follow's following list, and empty accountFollowRequests array + dbUpdates.push({ + updateOne: { + filter: {_id: {$eq: userId}}, + update: {$push: {followers: {$each: accountsRequestingToFollow}}, $set: {accountFollowRequests: []}} + } + }) + + for (const accountPubId of accountsRequestingToFollow) { + dbUpdates.push({ updateOne: { - filter: {_id: {$eq: userId}}, - update: {accountFollowRequests: []} - } - }, - ...accountsRequestingToFollow.map(accountPubId => { - return { - updateOne: { - filter: {secondId: {$eq: accountPubId}}, - update: {$push: {following: userFound.secondId}} - } + filter: {secondId: {$eq: accountPubId}}, + update: {$push: {following: userFound.secondId}} } }) - ] - - User.bulkWrite(dbUpdates).then(() => { - makeAccountPublic() + } + } + + mongoose.startSession().then(session => { + session.startTransaction(); + + User.bulkWrite(dbUpdates).then(() => session.commitTransaction()).then(() => { + session.endSession().catch(error => { + console.error('An error occurred while ending the Mongoose session:', error) + }).finally(() => { + return resolve(HTTPWTHandler.OK('Account is now public.')) + }) }).catch(error => { - console.error('An error occurred while making bulkWrite database updates to the User collection. The updates were:', dbUpdates, '. The error was:', error) - return resolve(HTTPWTHandler.serverError('An error occurred while adding users that requested to follow you to your followers list. Please try again.')) + console.error('An error occurred while making bulkWrite database updates to the User collection and commiting transaction. The updates were:', dbUpdates, '. The error was:', error) + session.abortTransaction().catch(error => { + console.error('An error occurred while aborting a Mongoose transaction:', error) + }).finally(() => { + session.endSession().catch(error => { + console.error('An error occurred while ending a Mongoose session:', error) + }).finally(() => { + return resolve(HTTPWTHandler.serverError('An error occurred while adding users that requested to follow you to your followers list. Please try again.')) + }) + }) }) - } else { - makeAccountPublic() - } + }).catch(error => { + console.error('An error occurred while starting Mongoose session. The error was:', error) + return resolve(HTTPWTHandler.serverError('An error occurred while making your account public. Please try again.')) + }) } else { //User not found return resolve(HTTPWTHandler.notFound('User not found.')) @@ -5162,12 +5195,31 @@ class TempController { } } ] - - User.bulkWrite(dbUpdates).then(() => { - return resolve(HTTPWTHandler.OK('Follow request accepted.')) + + mongoose.startSession().then(session => { + session.startTransaction(); + + User.bulkWrite(dbUpdates).then(() => session.commitTransaction()).then(() => { + session.endSession().catch(error => { + console.error('An error occurred while ending Mongoose session. The error was:', error) + }).finally(() => { + return resolve(HTTPWTHandler.OK('Follow request accepted.')) + }) + }).catch(error => { + session.abortTransaction().catch(error => { + console.error('An error occurred while aborting Mongoose transaction. The error was:', error) + }).finally(() => { + session.endSession().catch(error => { + console.error('An error occurred while ending Mongoose session. The error was:', error) + }).finally(() => { + console.error('An error occurred while making the following bulkWrite database updates on the User collection:', dbUpdates, '. The error was:', error) + return resolve(HTTPWTHandler.serverError('An error occurred while accepting the follow request. Please try again.')) + }) + }) + }) }).catch(error => { - console.error('An error occurred while making the following bulkWrite database updates on the User collection:', dbUpdates, '. The error was:', error) - return resolve(HTTPWTHandler.serverError('An error occurred while accepting the follow request. Please try again.')) + console.error('An error occurred while starting a Mongoose session. The error was:', error) + return resolve(HTTPWTHandler.serverError('An error occurred while accepting follow request. Please try again.')) }) }).catch(error => { console.error('An error occurred while finding user with id:', userId, '. The error was:', error) @@ -5219,11 +5271,30 @@ class TempController { } } ] - - User.bulkWrite(dbUpdates).then(() => { - return resolve(HTTPWTHandler.OK('Follower has been removed.')) + + mongoose.startSession().then(session => { + session.startTransaction(); + + User.bulkWrite(dbUpdates).then(() => session.commitTransaction()).then(() => { + session.endSession().catch(error => { + console.error('An error occurred while ending Mongoose session. The error was:', error) + }).finally(() => { + return resolve(HTTPWTHandler.OK('Follower has been removed.')) + }) + }).catch(error => { + console.error('An error occurred while making a bulkWrite operation to the database on the User collection and committing transaction. The dbUpdates to be made were:', dbUpdates, '. The error was:', error) + session.abortTransaction().catch(error => { + console.error('An error occurred while aborting Mongoose transaction. The error was:', error) + }).finally(() => { + session.endSession().catch(error => { + console.error('An error occurred while ending Mongoose session. The error was:', error) + }).finally(() => { + return resolve(HTTPWTHandler.serverError('An error occurred while removing follower. Please try again.')) + }) + }) + }) }).catch(error => { - console.error('An error occurred while making a bulkWrite operatin to the database on the User collection. The dbUpdates to be made were:', dbUpdates, '. The error was:', error) + console.error('An error occurred while starting Mongoose session. The error was:', error) return resolve(HTTPWTHandler.serverError('An error occurred while removing follower. Please try again.')) }) }).catch(error => { @@ -5271,12 +5342,31 @@ class TempController { } } ] - - User.bulkWrite(dbUpdates).then(() => { - return resolve(HTTPWTHandler.OK('Blocked user.')) + + mongoose.startSession().then(session => { + session.startTransaction(); + + User.bulkWrite(dbUpdates).then(() => session.commitTransaction()).then(() => { + session.endSession().catch(error => { + console.error('An error occurred while ending Mongoose session. The error was:', error) + }).finally(() => { + return resolve(HTTPWTHandler.OK('Blocked user.')) + }) + }).catch(error => { + console.error('An error occurred while making a bulkWrite operation on the User collection. The database updates were:', dbUpdates, '. The error was:', error) + session.abortTransaction().catch(error => { + console.error('An error occurred while aborting Mongoose transaction. The error was:', error) + }).finally(() => { + session.endSession().catch(error => { + console.error('An error occurred while ending Mongoose session. The error was:', error) + }).finally(() => { + return resolve(HTTPWTHandler.serverError('An error occurred while blocking user. Please try again.')) + }) + }) + }) }).catch(error => { - console.error('An error occurred while making a bulkWrite operation on the User collection. The database updates were:', dbUpdates, '. The error was:', error) - return resolve(HTTPWTHandler.serverError('An error occurred while blocking user. Please try again.')) + console.error('An error occurred while starting Mongoose session. The error was:', error) + return resolve(HTTPWTHandler.serverError('An error occurred while blocking an account. Please try again.')) }) }).catch(error => { console.error('An error occurred while finding one user with secondId:', userToBlockPubId, '. The error was:', error) @@ -5461,21 +5551,6 @@ class TempController { mongoose.startSession().then(session => { session.startTransaction() - - const dbUpdates = [ - { - updateOne: { - filter: {_id: {$eq: userGettingFollowed._id}}, - update: {$pull : {followers: userFollowingFound.secondId}} - } - }, - { - updateOne: { - filter: {_id: {$eq: userId}}, - update: { $pull : {following: userGettingFollowed.secondId}} - } - } - ] Promise.all([ popularPosts.length !== newPopularPosts.length ? PopularPosts.findOneAndUpdate({}, {popularPosts: newPopularPosts}) : Promise.resolve('Popular posts do not need to be updated'), @@ -5491,19 +5566,27 @@ class TempController { updateMany: { filter: {followers: userFound.secondId}, update: {$pull: {followers: userFound.secondId}} - }, + } + }, + { updateMany: { filter: {following: userFound.secondId}, update: {$pull: {following: userFound.secondId}} - }, + } + }, + { updateMany: { filter: {blockedAccounts: userFound.secondId}, update: {$pull: {blockedAccounts: userFound.secondId}} - }, + } + }, + { updateMany: { filter: {accountFollowRequests: userFound.secondId}, update: {$pull: {accountFollowRequests: userFound.secondId}} - }, + } + }, + { deleteOne: { filter: {_id: {$eq: userId}}, } diff --git a/libraries/ImagePost.js b/libraries/ImagePost.js index 68696dbe..3738fbf9 100644 --- a/libraries/ImagePost.js +++ b/libraries/ImagePost.js @@ -13,8 +13,8 @@ class ImagePostClass { delete post.creatorId return new Promise((resolve, reject) => { Promise.all([ - Upvote.estimatedDocumentCount({postId: {$eq: post._id}, postFormat: "Image"}), - Downvote.estimatedDocumentCount({postId: {$eq: post._id}, postFormat: "Image"}), + Upvote.countDocuments({postId: {$eq: post._id}, postFormat: "Image"}), + 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]) => { diff --git a/libraries/PollPost.js b/libraries/PollPost.js index 510ff239..52293e6f 100644 --- a/libraries/PollPost.js +++ b/libraries/PollPost.js @@ -11,8 +11,8 @@ class PollPost { delete post.creatorId return new Promise((resolve, reject) => { Promise.all([ - Upvote.estimatedDocumentCount({postId: {$eq: post._id}, postFormat: "Poll"}), - Downvote.estimatedDocumentCount({postId: {$eq: post._id}, postFormat: "Poll"}), + Upvote.countDocuments({postId: {$eq: post._id}, postFormat: "Poll"}), + Downvote.countDocuments({postId: {$eq: post._id}, postFormat: "Poll"}), Upvote.findOne({postId: {$eq: post._id}, postFormat: "Poll", userPublicId: userRequesting.secondId}), Downvote.findOne({postId: {$eq: post._id}, postFormat: "Poll", userPublicId: userRequesting.secondId}), ]).then(([upvotes, downvotes, isUpvoted, isDownvoted]) => { diff --git a/libraries/ThreadPost.js b/libraries/ThreadPost.js index 64c16170..d36f8981 100644 --- a/libraries/ThreadPost.js +++ b/libraries/ThreadPost.js @@ -14,8 +14,8 @@ class ThreadPost { delete post.creatorId return new Promise((resolve, reject) => { Promise.all([ - Upvote.estimatedDocumentCount({postId: {$eq: post._id}, postFormat: "Thread"}), - Downvote.estimatedDocumentCount({postId: {$eq: post._id}, postFormat: "Thread"}), + Upvote.countDocuments({postId: {$eq: post._id}, postFormat: "Thread"}), + 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})