Skip to content

Commit a4ee3d4

Browse files
Merge pull request #329 from SoftwareEngineeringDaily/fix-comment-counts
Fix comment counts
2 parents 640aa8d + c450c2f commit a4ee3d4

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

server/controllers/comment.controller.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ function load(req, res, next, id) {
5353
*/
5454
function remove(req, res, next) {
5555
const { comment, user } = req;
56+
5657
if (comment && user) {
5758
if (comment.author._id.toString() !== user._id.toString()) {
5859
return res.status(401).json({ Error: 'Please login' });
@@ -66,10 +67,14 @@ function remove(req, res, next) {
6667
// Sucess:
6768
res.json({ deleted: true });
6869
})
70+
.then(() => {
71+
ForumThread.increaseCommentCount(comment.rootEntity, -1);
72+
})
6973
.catch((e) => {
7074
next(e);
7175
});
7276
}
77+
7378
return res.status(500).json({});
7479
}
7580

server/models/forumThread.model.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ ForumThreadSchema.statics = {
5555
});
5656
},
5757

58-
increaseCommentCount(id) {
58+
increaseCommentCount(id, count = 1) {
5959
return this.get(id).then((thread) => {
6060
const forumThread = thread;
61-
forumThread.commentsCount += 1;
61+
forumThread.commentsCount += count;
62+
forumThread.commentsCount = Math.max(forumThread.commentsCount, 0);
6263
forumThread.dateLastAcitiy = new Date();
6364
return forumThread.save();
6465
});

server/routes/post.route.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import express from 'express';
22
import expressJwt from 'express-jwt';
33
import validate from 'express-validation';
4+
import omit from 'lodash/omit';
45
import paramValidation from '../../config/param-validation';
56
import postCtrl from '../controllers/post.controller';
67
import voteCtrl from '../controllers/vote.controller';
@@ -14,8 +15,17 @@ import loadFullUser from '../middleware/loadFullUser.middleware';
1415

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

18+
// @TODO: Remove this after it's fixed in iOS
19+
const jwtCleanUp = (req, res, next) => {
20+
if (req.headers.authorization && !req.headers.authorization.split(' ')[1]) {
21+
req.headers = omit(req.headers, 'authorization');
22+
}
23+
next();
24+
};
25+
1726
router.route('/')
1827
.get(
28+
jwtCleanUp,
1929
expressJwt({
2030
secret: config.jwtSecret,
2131
credentialsRequired: false,

0 commit comments

Comments
 (0)