Commit 73f663ef authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Implement editing of comments.

parent 984c1551
Loading
Loading
Loading
Loading
+34 −7
Original line number Diff line number Diff line
@@ -121,13 +121,6 @@ app.get('/auth/:sdk/:version/comments', services.comments, function(req, res) {
    });
});

// Returns plain markdown content of individual comment (used when editing a comment)
app.get('/auth/:sdk/:version/comments/:commentId', services.comments, function(req, res) {
    req.comments.getById(req.params.commentId, function(err, comment) {
        res.json({ success: true, content: comment.content });
    });
});

// Adds new comment
app.post('/auth/:sdk/:version/comments', services.requireLogin, services.comments, function(req, res) {
    var comment = {
@@ -144,6 +137,40 @@ app.post('/auth/:sdk/:version/comments', services.requireLogin, services.comment
    });
});

// Returns plain markdown content of individual comment (used when editing a comment)
app.get('/auth/:sdk/:version/comments/:commentId', services.comments, function(req, res) {
    req.comments.getById(req.params.commentId, function(err, comment) {
        res.json({ success: true, content: comment.content });
    });
});

// Updates an existing comment (for voting or updating contents)
app.post('/auth/:sdk/:version/comments/:commentId', services.requireLogin, services.comments, services.users, function(req, res) {
    req.comments.getById(req.params.commentId, function(err, comment) {
        if (req.body.vote) {
            // TODO: voting...
        }
        else {
            if (!req.users.canModify(req.session.user, comment)) {
                res.json({ success: false, reason: 'Forbidden' }, 403);
                return;
            }

            var update = {
                id: comment.id,
                user_id: req.session.user.id,
                content: req.body.content
            };

            req.comments.update(update, function(err) {
                req.comments.getById(comment.id, function(err, comment) {
                    res.json({ success: true, content: comment.content_html });
                });
            });
        }
    });
});


// Returns all subscriptions for logged in user
// For now does nothing.
+13 −0
Original line number Diff line number Diff line
@@ -32,6 +32,19 @@ Users.prototype = {
        }.bind(this));
    },

    /**
     * True if the user can modify the comment.  Normal user can
     * modify his own comments, but moderators can modify all
     * comments.
     *
     * @param {Object} user User record.
     * @param {Object} comment Comment record.
     * @return {Boolean}
     */
    canModify: function(user, comment) {
        return user.moderator || user.id == comment.user_id;
    },

    ensure: function(user, callback) {
        // first try to insert. If that fails, the user already
        // exists and we can instead look it up. If insert succeeds,