Skip to content

Fix comment counts #329

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

Merged
merged 2 commits into from
Mar 6, 2020
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
5 changes: 5 additions & 0 deletions server/controllers/comment.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function load(req, res, next, id) {
*/
function remove(req, res, next) {
const { comment, user } = req;

if (comment && user) {
if (comment.author._id.toString() !== user._id.toString()) {
return res.status(401).json({ Error: 'Please login' });
Expand All @@ -66,10 +67,14 @@ function remove(req, res, next) {
// Sucess:
res.json({ deleted: true });
})
.then(() => {
ForumThread.increaseCommentCount(comment.rootEntity, -1);
})
.catch((e) => {
next(e);
});
}

return res.status(500).json({});
}

Expand Down
5 changes: 3 additions & 2 deletions server/models/forumThread.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ ForumThreadSchema.statics = {
});
},

increaseCommentCount(id) {
increaseCommentCount(id, count = 1) {
return this.get(id).then((thread) => {
const forumThread = thread;
forumThread.commentsCount += 1;
forumThread.commentsCount += count;
forumThread.commentsCount = Math.max(forumThread.commentsCount, 0);
forumThread.dateLastAcitiy = new Date();
return forumThread.save();
});
Expand Down
10 changes: 10 additions & 0 deletions server/routes/post.route.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express from 'express';
import expressJwt from 'express-jwt';
import validate from 'express-validation';
import omit from 'lodash/omit';
import paramValidation from '../../config/param-validation';
import postCtrl from '../controllers/post.controller';
import voteCtrl from '../controllers/vote.controller';
Expand All @@ -14,8 +15,17 @@ import loadFullUser from '../middleware/loadFullUser.middleware';

const router = express.Router(); // eslint-disable-line new-cap

// @TODO: Remove this after it's fixed in iOS
const jwtCleanUp = (req, res, next) => {
if (req.headers.authorization && !req.headers.authorization.split(' ')[1]) {
req.headers = omit(req.headers, 'authorization');
}
next();
};

router.route('/')
.get(
jwtCleanUp,
expressJwt({
secret: config.jwtSecret,
credentialsRequired: false,
Expand Down