diff --git a/server/controllers/comment.controller.js b/server/controllers/comment.controller.js index 648ed9c1..a6445d3f 100644 --- a/server/controllers/comment.controller.js +++ b/server/controllers/comment.controller.js @@ -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' }); @@ -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({}); } diff --git a/server/models/forumThread.model.js b/server/models/forumThread.model.js index 22a1bf51..c897ea02 100644 --- a/server/models/forumThread.model.js +++ b/server/models/forumThread.model.js @@ -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(); }); diff --git a/server/routes/post.route.js b/server/routes/post.route.js index b10f66c4..9a9b7dfc 100644 --- a/server/routes/post.route.js +++ b/server/routes/post.route.js @@ -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'; @@ -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,