Commit 1c931e6e authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Refactor adding new comment.

Created a #saveNew method directly on model, which ensures that
when we add the comment, we automatically mark it as read for
moderators.
parent 75cfeadf
Loading
Loading
Loading
Loading
+1 −16
Original line number Diff line number Diff line
@@ -256,25 +256,10 @@ app.post('/auth/:sdk/:version/comments', util.requireLoggedInUser, function(req,
        url: req.body.url
    });

    var afterSave = function() {
    comment.saveNew(req.session.user, function(err) {
        res.json({ success: true, id: comment._id, action: req.body.action });

        util.sendEmailUpdates(comment);
    };

    comment.save(function(err) {
        if (util.isModerator(req.session.user)) {
            // When moderator posts comment, mark it automatically as read.
            var meta = new Meta({
                userId: req.session.user.userid,
                commentId: comment._id,
                metaType: 'read'
            });
            meta.save(afterSave);
        }
        else {
            afterSave();
        }
    });

});
+24 −2
Original line number Diff line number Diff line
@@ -4,9 +4,10 @@
 */

var mongoose = require('mongoose'),
    util = require('./util'),
    config = require('./config');

Comment = mongoose.model('Comment', new mongoose.Schema({
CommentSchema = new mongoose.Schema({
    sdk:         String,
    version:     String,

@@ -26,7 +27,28 @@ Comment = mongoose.model('Comment', new mongoose.Schema({
    mod:         Boolean,
    title:       String,
    url:         String
}));
});

// Helper method for adding new comments.
// When moderator posts comment, mark it automatically as read.
CommentSchema.methods.saveNew = function(user, next) {
    var comment = this;
    if (util.isModerator(user)) {
        comment.save(function(err) {
            var meta = new Meta({
                userId: user.userid,
                commentId: comment._id,
                metaType: 'read'
            });
            meta.save(next);
        });
    }
    else {
        comment.save(next);
    }
};

Comment = mongoose.model('Comment', CommentSchema);

Subscription = mongoose.model('Subscription', new mongoose.Schema({
    sdk:         String,