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

[Backend] Limiting the Number of upvotes/downvotes a particular user can do Fixed #942

Merged
merged 7 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const responseTime = require('response-time');
const routes = require('./app/routes');
const { errorHandler } = require('./helpers/error');
const cookieParser = require('cookie-parser');
require('colors');
require('./helpers/dbConnection');

Expand All @@ -24,6 +25,9 @@
// Response time
app.use(responseTime({ suffix: false }));

// cookie
app.use(cookieParser());

Check failure

Code scanning / CodeQL

Missing CSRF middleware High

This cookie middleware is serving a
request handler
without CSRF protection.
This cookie middleware is serving a
request handler
without CSRF protection.

// Use routes
app.use('/', routes);

Expand Down
2 changes: 2 additions & 0 deletions backend/app/routes/Q&A/question/downvoteQuestion.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const to = require('await-to-js').default;
const question = require('../../../models/question');
const { ErrorHandler } = require('../../../../helpers/error');
const constants = require('../../../../constants');
const { getVoteCookieName } = require('../../../../helpers/middlewares/cookie');

module.exports = async (req, res, next) => {
const { questionId } = req.body;
Expand Down Expand Up @@ -35,6 +36,7 @@ module.exports = async (req, res, next) => {
return next(error);
}

res.cookie(getVoteCookieName('question', questionId), true, { maxAge: 20 * 365 * 24 * 60 * 60 * 1000 });
res.status(200).send({
message: 'Question has been down voted',
});
Expand Down
5 changes: 3 additions & 2 deletions backend/app/routes/Q&A/question/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const downvoteQuestion = require('./downvoteQuestion');
const updateQuestionStatus = require('./updateQuestionStatus');
const { authMiddleware } = require('../../../../helpers/middlewares/auth');
const deleteQuestion = require('./deleteQuestion');
const { checkVoteCookie } = require('../../../../helpers/middlewares/cookie');

router.post('/', validation(QuestionValidationSchema), postQuestion);

Expand All @@ -19,10 +20,10 @@ router.get('/getallquestions', getAllQuestion);
router.get('/getQuestionById/:questionId', getQuestionById);

// This route will increase upvote by one.
router.patch('/upvote', upvoteQuestion);
router.patch('/upvote',checkVoteCookie, upvoteQuestion);

// This route will decrease upvote by one.
router.patch('/downvote', downvoteQuestion);
router.patch('/downvote',checkVoteCookie, downvoteQuestion);

// route for updating the question status
router.patch('/updateStatus', validation(updateQuestionStatusSchema), updateQuestionStatus);
Expand Down
4 changes: 3 additions & 1 deletion backend/app/routes/Q&A/question/upvoteQuestion.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const to = require('await-to-js').default;
const question = require('../../../models/question');
const { ErrorHandler } = require('../../../../helpers/error');
const constants = require('../../../../constants');
const { getVoteCookieName } = require('../../../../helpers/middlewares/cookie');

module.exports = async (req, res, next) => {
const { questionId } = req.body;
Expand All @@ -12,10 +13,11 @@ module.exports = async (req, res, next) => {
message: 'Database Error',
errStack: err,
});

return next(error);
}

res.cookie(getVoteCookieName('question', questionId), true, { maxAge: 20 * 365 * 24 * 60 * 60 * 1000 });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the unit of this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept it to 20 years. why because no one can use a mobile for 20 years.


res.status(200).send({
message: 'Question has been upvoted',
});
Expand Down
16 changes: 16 additions & 0 deletions backend/helpers/middlewares/cookie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const getVoteCookieName = (type, id) => `${type}_voted_${id}`;

const checkVoteCookie = (req, res, next) => {
const { questionId, answerId } = req.body;
const voteType = questionId ? 'question' : 'answer';
const voteId = questionId || answerId;
const voteCookieName = getVoteCookieName(voteType, voteId);

if (req.cookies[voteCookieName]) {
return res.status(400).json({ error: 'You have already voted' });
}

next();
};

module.exports = { checkVoteCookie, getVoteCookieName }
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"chai-http": "^4.3.0",
"colors": "^1.4.0",
"config": "^3.3.3",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"ejs": "^3.1.6",
Expand Down
2 changes: 1 addition & 1 deletion frontend/.env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
REACT_APP_API="https://community-website-backend.onrender.com"
REACT_APP_API="https://community-website-backend.onrender.com"
Loading