From e19a202ac69d0196bde648c7ce3ed78f4227eb45 Mon Sep 17 00:00:00 2001 From: Sebastian Webster Date: Sat, 16 Sep 2023 13:05:04 +1200 Subject: [PATCH 1/4] Added temp/removevoteonpoll API --- controllers/Temp.js | 38 ++++++++++++++++++++++++++++++++++++++ routes/Temp.js | 27 +++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/controllers/Temp.js b/controllers/Temp.js index ea6ddb42..44c79577 100644 --- a/controllers/Temp.js +++ b/controllers/Temp.js @@ -1093,6 +1093,40 @@ class TempController { }) } + static #removevoteonpoll = (userId, pollId) => { + return new Promise(resolve => { + if (typeof pollId !== 'string') { + return resolve(HTTPWTHandler.badInput(`pollId must be a string. Provided type: ${typeof pollId}`)) + } + + User.findOne({_id: {$eq: userId}}).then(userFound => { + if (!userFound) { + return resolve(HTTPWTHandler.notFound('Could not find user with provided userId')) + } + + Poll.findOne({_id: {$eq: pollId}}).then(pollFound => { + if (!pollFound) { + return resolve(HTTPWTHandler.notFound('Could not find poll with provided pollId')) + } + + //This is temporary - Soon there will be a PollVote collection that we will remove the poll vote from + Poll.findOneAndUpdate({_id: {$eq: pollId}}, {$pull: {optionOnesVotes: userId, optionTwosVotes: userId, optionThreesVotes: userId, optionFoursVotes: userId, optionFivesVotes: userId, optionSixesVotes: userId}}).then(() => { + return resolve(HTTPWTHandler.OK('Removed vote successfully')) + }).catch(error => { + console.error('An error occurred while pulling:', userId, 'from all vote arrays for poll with id:', pollId, '. The error was:', error) + return resolve(HTTPWTHandler.serverError('An error occurred while removing vote from poll. Please try again.')) + }) + }).catch(error => { + console.error('An error occurred while finding one poll with id:', pollId, '. The error was:', error) + return resolve(HTTPWTHandler.serverError('An error occurred while finding poll. Please try again.')) + }) + }).catch(error => { + console.error('An error occurred while finding one user with id:', userId, '. The error was:', error) + return resolve(HTTPWTHandler.serverError('An error occurred while finding user. Please try again.')) + }) + }) + } + static #searchforpollpostsbyid = (userId, pollId) => { return new Promise(resolve => { if (typeof pollId !== 'string') { @@ -6527,6 +6561,10 @@ class TempController { return await this.#voteonpoll(userId, optionSelected, pollId) } + static removevoteonpoll = async (userId, pollId) => { + return await this.#removevoteonpoll(userId, pollId) + } + static searchforpollpostsbyid = async (userId, pollId) => { return await this.#searchforpollpostsbyid(userId, pollId) } diff --git a/routes/Temp.js b/routes/Temp.js index 22da48b2..85a93a60 100644 --- a/routes/Temp.js +++ b/routes/Temp.js @@ -153,6 +153,15 @@ const rateLimiters = { skipFailedRequests: true, keyGenerator: (req, res) => req.tokenData //Use req.tokenData (account _id in MongoDB) to identify clients and rate limit }), + '/removevoteonpoll': rateLimit({ + windowMs: 1000 * 60, //1 minute + max: 30, + standardHeaders: false, + legacyHeaders: false, + message: {status: "FAILED", message: "You have removed your vote on too many poll posts in the last minute. Please try again in 60 seconds."}, + skipFailedRequests: true, + keyGenerator: (req, res) => req.tokenData //Use req.tokenData (account _id in MongoDB) to identify clients and rate limit + }), '/searchforpollpostsbyid': rateLimit({ windowMs: 1000 * 60, //1 minute max: 30, @@ -1123,6 +1132,24 @@ router.post('/voteonpoll', rateLimiters['/voteonpoll'], (req, res) => { }) }); +router.post('/removevoteonpoll', rateLimiters['/removevoteonpoll'], (req, res) => { + const worker = new Worker(workerPath, { + workerData: { + functionName: 'removevoteonpoll', + functionArgs: [req.tokenData, req.body.pollId] + } + }) + + worker.on('message', (result) => { + res.status(result.statusCode).json(result.data) + }) + + worker.on('error', (error) => { + console.error('An error occurred from TempWorker for POST /removevoteonpoll:', error) + HTTPHandler.serverError(res, String(error)) + }) +}); + router.post('/searchforpollpostsbyid', rateLimiters['/searchforpollpostsbyid'], (req, res) => { const worker = new Worker(workerPath, { workerData: { From cc1f3c9e71ed6e32c6cc049712426f801b74e7fe Mon Sep 17 00:00:00 2001 From: Sebastian Webster Date: Sat, 16 Sep 2023 13:09:19 +1200 Subject: [PATCH 2/4] Added lean to temp/removevoteonpoll --- controllers/Temp.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/controllers/Temp.js b/controllers/Temp.js index 44c79577..3723237f 100644 --- a/controllers/Temp.js +++ b/controllers/Temp.js @@ -1099,12 +1099,12 @@ class TempController { return resolve(HTTPWTHandler.badInput(`pollId must be a string. Provided type: ${typeof pollId}`)) } - User.findOne({_id: {$eq: userId}}).then(userFound => { + User.findOne({_id: {$eq: userId}}).lean().then(userFound => { if (!userFound) { return resolve(HTTPWTHandler.notFound('Could not find user with provided userId')) } - Poll.findOne({_id: {$eq: pollId}}).then(pollFound => { + Poll.findOne({_id: {$eq: pollId}}).lean().then(pollFound => { if (!pollFound) { return resolve(HTTPWTHandler.notFound('Could not find poll with provided pollId')) } From 1faec1e85cc026de1e7fe93283f776150495b4af Mon Sep 17 00:00:00 2001 From: Sebastian Webster Date: Sat, 16 Sep 2023 13:13:54 +1200 Subject: [PATCH 3/4] Removed unvoting functionality from temp/voteonpoll --- controllers/Temp.js | 127 ++++---------------------------------------- 1 file changed, 10 insertions(+), 117 deletions(-) diff --git a/controllers/Temp.js b/controllers/Temp.js index 3723237f..471d6a70 100644 --- a/controllers/Temp.js +++ b/controllers/Temp.js @@ -955,124 +955,17 @@ class TempController { //User exists Poll.findOne({_id: {$eq: pollId}}).lean().then(data => { if (data) { - var findUser = data; - console.log(findUser) - if (findUser.creatorId !== userId) { - if (findUser.optionOnesVotes.includes(userId)) { - Poll.findOneAndUpdate({_id: {$eq: pollId}}, { $pull: { optionOnesVotes: userId }}).then(function(){ - if (optionSelected !== "optionOnesVotes") { - Poll.findOneAndUpdate({_id: {$eq: pollId}}, { $push: { [optionSelected]: userId }}).then(function(){ - return resolve(HTTPWTHandler.OK('Vote successful', {lastVote: "One"})) - }) - .catch(err => { - console.error('An error occured while adding:', userId, 'to the list of votes in:', optionSelected, 'on poll with id:', pollId, '. The error was:', err) - return resolve(HTTPWTHandler.serverError('An error occurred while adding vote. Please try again.')) - }); - } else { - return resolve(HTTPWTHandler.OK('Pulled', {lastVote: 'One'})) - } - }) - .catch(err => { - console.error('An error occured while pulling:', userId, 'from optionOnesVotes on poll with id:', pollId, '. The error was:', err) - return resolve(HTTPWTHandler.serverError('An error occurred while pulling vote. Please try again.')) - }); - } else if (findUser.optionTwosVotes.includes(userId)) { - Poll.findOneAndUpdate({_id: {$eq: pollId}}, { $pull: { optionTwosVotes: userId }}).then(function(){ - if (optionSelected !== "optionTwosVotes") { - Poll.findOneAndUpdate({_id: {$eq: pollId}}, { $push: { [optionSelected]: userId }}).then(function(){ - return resolve(HTTPWTHandler.OK('Vote successful', {lastVote: 'Two'})) - }) - .catch(err => { - console.error('An error occured while adding:', userId, 'to:', optionSelected, 'in poll with id:', pollId, '. The error was:', err) - return resolve(HTTPWTHandler.serverError('An error occurred while adding vote. Please try again.')) - }); - } else { - return resolve(HTTPWTHandler.OK('Pulled', {lastVote: "Two"})) - } - }).catch(error => { - console.error('An error occurred while pulling:', userId, 'from optionTwosVotes from poll with id:', pollId, '. The error was:', error) - return resolve(HTTPWTHandler.serverError('An error occurred while removing existing vote. Please try again.')) - }) - } else if (findUser.optionThreesVotes.includes(userId)) { - Poll.findOneAndUpdate({_id: {$eq: pollId}}, { $pull: { optionThreesVotes: userId }}).then(function(){ - if (optionSelected !== "optionThreesVotes") { - Poll.findOneAndUpdate({_id: {$eq: pollId}}, { $push: { [optionSelected]: userId }}).then(function(){ - return resolve(HTTPWTHandler.OK('Vote successful', {lastVote: "Three"})) - }) - .catch(err => { - console.error('An error occurred while adding:', userId, 'to:', optionSelected, 'in poll with id:', pollId, '. The error was:', err) - return resolve(HTTPWTHandler.serverError('An error occurred while adding vote. Please try again.')) - }); - } else { - return resolve(HTTPWTHandler.OK('Pulled', {lastVote: 'Three'})) - } - }).catch(error => { - console.error('An error occurred while pulling:', userId, 'from optionThreesVotes from poll with id:', pollId, '. The error was:', error) - return resolve(HTTPWTHandler.serverError('An error occurred while removing existing vote. Please try again.')) - }) - } else if (findUser.optionFoursVotes.includes(userId)) { - Poll.findOneAndUpdate({_id: {$eq: pollId}}, { $pull: { optionFoursVotes: userId }}).then(function(){ - if (optionSelected !== "optionFoursVotes") { - Poll.findOneAndUpdate({_id: {$eq: pollId}}, { $push: { [optionSelected]: userId }}).then(function(){ - return resolve(HTTPWTHandler.OK('Vote successful', {lastVote: 'Four'})) - }) - .catch(err => { - console.error('An error occured while adding:', userId, 'to:', optionSelected, 'in poll with id:', pollId, '. The error was:', err) - return resolve(HTTPWTHandler.serverError('An error occurred while adding vote. Please try again.')) - }); - } else { - return resolve(HTTPWTHandler.OK('Pulled', {lastVote: "Four"})) - } - }).catch(error => { - console.error('An error occurred while pulling:', userId, 'from optionFoursVotes from poll with id:', pollId, '. The error was:', error) - return resolve(HTTPWTHandler.serverError('An error occurred while removing existing vote. Please try again.')) - }) - } else if (findUser.optionFivesVotes.includes(userId)) { - Poll.findOneAndUpdate({_id: {$eq: pollId}}, { $pull: { optionFivesVotes: userId }}).then(function(){ - if (optionSelected !== "optionFivesVotes") { - Poll.findOneAndUpdate({_id: {$eq: pollId}}, { $push: { [optionSelected]: userId }}).then(function(){ - return resolve(HTTPWTHandler.OK('Vote successful', {lastVote: 'Five'})) - }) - .catch(err => { - console.error('An error occured while adding:', userId, 'to:', optionSelected, 'in poll with id:', pollId, '. The error was:', err) - return resolve(HTTPWTHandler.serverError('An error occurred while adding vote. Please try again.')) - }); - } else { - return resolve(HTTPWTHandler.OK('Pulled', {lastVote: "Five"})) - } - }).catch(error => { - console.error('An error occurred while pulling:', userId, 'from optionFivesVotes from poll with id:', pollId, '. The error was:', error) - return resolve(HTTPWTHandler.serverError('An error occurred while removing existing vote. Please try again.')) - }) - } else if (findUser.optionSixesVotes.includes(userId)) { - Poll.findOneAndUpdate({_id: {$eq: pollId}}, { $pull: { optionSixesVotes: userId }}).then(function(){ - if (optionSelected !== "optionSixesVotes") { - Poll.findOneAndUpdate({_id: {$eq: pollId}}, { $push: { [optionSelected]: userId }}).then(function(){ - return resolve(HTTPWTHandler.OK('Vote successful', {lastVote: "Six"})) - }) - .catch(err => { - console.error('An error occured while adding:', userId, 'to:', optionSelected, 'in poll with id:', pollId, '. The error was:', err) - return resolve(HTTPWTHandler.serverError('An error occurred while adding vote. Please try again.')) - }); - } else { - return resolve(HTTPWTHandler.OK('Pulled', {lastVote: 'Six'})) - } - }).catch(error => { - console.error('An error occurred while pulling:', userId, 'from optionSixesVotes in poll with id:', pollId, '. The error was:', error) - return resolve(HTTPWTHandler.serverError('An error occurred while removing existing vote. Please try again.')) - }) - } else { - Poll.findOneAndUpdate({_id: {$eq: pollId}}, { $push: { [optionSelected] : userId }}).then(function(){ - return resolve(HTTPWTHandler.OK('Vote successful', {lastVote: "None"})) - }) - .catch(err => { - console.error('An error occurred while adding:', userId, 'to:', optionSelected, 'in poll with id:', pollId, '. The error was:', err) - return resolve(HTTPWTHandler.serverError('An error occurred while adding vote. Please try again.')) - }); - } - } else { - return resolve(HTTPWTHandler.forbidden("You can't vote on your own post")) + if (data.creatorId == userId) { + return resolve(HTTPWTHandler.forbidden('You cannot vote on your own poll')) } + + //Temporary until PollVote collection is made + Poll.findOneAndUpdate({_id: {$eq: pollId}}, {$addToSet: {[optionSelected]: userId}}).then(() => { + return resolve(HTTPWTHandler.OK('Successfully added vote')) + }).catch(error => { + console.error('An error occurred while adding:', pollId, 'to:', optionSelected, 'field on poll with id:', pollId, '. The error was:', error) + return resolve(HTTPWTHandler.serverError('An error occurred while adding vote. Please try again.')) + }) } else { return resolve(HTTPWTHandler.notFound('Could not find poll')) } From 3aba1cc3878b425bc91fc699c3ec96d8c201f366 Mon Sep 17 00:00:00 2001 From: Sebastian Webster Date: Sat, 16 Sep 2023 13:14:40 +1200 Subject: [PATCH 4/4] Temporarily disable temp/voteonpoll and temp/removevoteonpoll --- controllers/Temp.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/controllers/Temp.js b/controllers/Temp.js index 471d6a70..bae7c6cf 100644 --- a/controllers/Temp.js +++ b/controllers/Temp.js @@ -929,6 +929,8 @@ class TempController { static #voteonpoll = (userId, optionSelected, pollId) => { return new Promise(resolve => { + return resolve(HTTPWTHandler.notImplemented('API functionality is temporarily disabled')) + if (typeof optionSelected !== 'string') { return resolve(HTTPWTHandler.badInput(`optionSelected must be a string. Provided type: ${typeof optionSelected}`)) } @@ -988,6 +990,8 @@ class TempController { static #removevoteonpoll = (userId, pollId) => { return new Promise(resolve => { + return resolve(HTTPWTHandler.notImplemented('API functionality is temporarily disabled')) + if (typeof pollId !== 'string') { return resolve(HTTPWTHandler.badInput(`pollId must be a string. Provided type: ${typeof pollId}`)) }