From b63e45136d03ceb46902aa43bfdcfa8f36656aae Mon Sep 17 00:00:00 2001 From: Sebastian-Webster Date: Tue, 10 Oct 2023 18:06:54 +1300 Subject: [PATCH] Removed userName check from temp/pollpostcommentreply --- controllers/Temp.js | 92 +++++++++++++++++++++------------------------ routes/Temp.js | 2 +- 2 files changed, 43 insertions(+), 51 deletions(-) diff --git a/controllers/Temp.js b/controllers/Temp.js index 1f5d4816..9137114f 100644 --- a/controllers/Temp.js +++ b/controllers/Temp.js @@ -744,16 +744,12 @@ class TempController { }) } - static #pollpostcommentreply = (userId, comment, userName, postId, commentId) => { + static #pollpostcommentreply = (userId, comment, postId, commentId) => { return new Promise(resolve => { if (typeof comment !== 'string') { return resolve(HTTPWTHandler.badInput(`comment must be a string. Provided type: ${typeof comment}`)) } - if (typeof userName !== 'string') { - return resolve(HTTPWTHandler.badInput(`userName must be a string. Provided type: ${typeof userName}`)) - } - if (typeof postId !== 'string') { return resolve(HTTPWTHandler.badInput(`postId must be a string. Provided type: ${typeof postId}`)) } @@ -779,52 +775,48 @@ class TempController { //Find User User.findOne({_id: {$eq: userId}}).lean().then(result => { if (result) { - if (result.name == userName) { - Poll.findOne({_id: {$eq: postId}}).lean().then(data => { - if (data) { - var comments = data.comments - async function findThreads(sentIndex) { - var objectId = new mongoose.Types.ObjectId() - console.log(objectId) - var commentForPost = {commentId: objectId, commenterId: userId, commentsText: comment, commentUpVotes: [], commentDownVotes: [], datePosted: Date.now()} - Poll.findOneAndUpdate({_id: {$eq: postId}}, { $push: { [`comments.${sentIndex}.commentReplies`]: commentForPost } }).then(function(){ - return resolve(HTTPWTHandler.OK('Comment upload successful')) - }) - .catch(err => { - console.error('An error occured while adding reply to poll comment. Comment reply was:', commentForPost, '. The error was:', err) - return resolve(HTTPWTHandler.serverError('An error occurred while adding comment reply. Please try again.')) - }); - } - var itemsProcessed = 0 - comments.forEach(function (item, index) { - console.log(comments[index].commentId) - console.log(commentId) - if (comments[index].commentId == commentId) { - if (itemsProcessed !== null) { - console.log("Found at index:") - console.log(index) - findThreads(index) - itemsProcessed = null - } - } else { - if (itemsProcessed !== null) { - itemsProcessed++; - if(itemsProcessed == comments.length) { - return resolve(HTTPWTHandler.notFound("Couldn't find comment")) - } - } - } + Poll.findOne({_id: {$eq: postId}}).lean().then(data => { + if (data) { + var comments = data.comments + async function findThreads(sentIndex) { + var objectId = new mongoose.Types.ObjectId() + console.log(objectId) + var commentForPost = {commentId: objectId, commenterId: userId, commentsText: comment, commentUpVotes: [], commentDownVotes: [], datePosted: Date.now()} + Poll.findOneAndUpdate({_id: {$eq: postId}}, { $push: { [`comments.${sentIndex}.commentReplies`]: commentForPost } }).then(function(){ + return resolve(HTTPWTHandler.OK('Comment upload successful')) + }) + .catch(err => { + console.error('An error occured while adding reply to poll comment. Comment reply was:', commentForPost, '. The error was:', err) + return resolve(HTTPWTHandler.serverError('An error occurred while adding comment reply. Please try again.')) }); - } else { - return resolve(HTTPWTHandler.notFound('Could not find poll')) } - }).catch(error => { - console.error('An error occurred while finding one poll with id:', postId, '. The error was:', error) - return resolve(HTTPWTHandler.serverError('An error occurred while finding poll. Please try again.')) + var itemsProcessed = 0 + comments.forEach(function (item, index) { + console.log(comments[index].commentId) + console.log(commentId) + if (comments[index].commentId == commentId) { + if (itemsProcessed !== null) { + console.log("Found at index:") + console.log(index) + findThreads(index) + itemsProcessed = null + } + } else { + if (itemsProcessed !== null) { + itemsProcessed++; + if(itemsProcessed == comments.length) { + return resolve(HTTPWTHandler.notFound("Couldn't find comment")) + } + } + } + }); + } else { + return resolve(HTTPWTHandler.notFound('Could not find poll')) + } + }).catch(error => { + console.error('An error occurred while finding one poll with id:', postId, '. The error was:', error) + return resolve(HTTPWTHandler.serverError('An error occurred while finding poll. Please try again.')) }) - } else { - return resolve(HTTPWTHandler.badInput('Provided userName does not match username in database.')) - } } else { return resolve(HTTPWTHandler.badInput('Could not find user from userId provided')) } @@ -6475,8 +6467,8 @@ class TempController { return await this.#pollpostcomment(userId, comment, userName, postId) } - static pollpostcommentreply = async (userId, comment, userName, postId, commentId) => { - return await this.#pollpostcommentreply(userId, comment, userName, postId, commentId) + static pollpostcommentreply = async (userId, comment, postId, commentId) => { + return await this.#pollpostcommentreply(userId, comment, postId, commentId) } static searchforpollcomments = async (userId, pollId) => { diff --git a/routes/Temp.js b/routes/Temp.js index 77e487b2..5762bbdb 100644 --- a/routes/Temp.js +++ b/routes/Temp.js @@ -1082,7 +1082,7 @@ router.post('/pollpostcommentreply', rateLimiters['/pollpostcommentreply'], (req const worker = new Worker(workerPath, { workerData: { functionName: 'pollpostcommentreply', - functionArgs: [req.tokenData, req.body.comment, req.body.userName, req.body.postId, req.body.commentId] + functionArgs: [req.tokenData, req.body.comment, req.body.postId, req.body.commentId] } })