Commit 59dc2abf authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Implement comment posting.

Added requireLogin service.
Added _id field to comments response.
parent b0b5ec26
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ module.exports = {
     */
    commentToJson: function(comment) {
        return {
            _id: comment.id,
            author: comment.username,
            contentHtml: comment.content_html,
            createdAt: String(comment.created_at),
+17 −0
Original line number Diff line number Diff line
@@ -121,6 +121,23 @@ app.get('/auth/:sdk/:version/comments', services.comments, function(req, res) {
    });
});

// Adds new comment
app.post('/auth/:sdk/:version/comments', services.requireLogin, services.comments, function(req, res) {
    var comment = {
        user_id: req.session.user.id,
        target: ApiAdapter.targetFromJson(JSON.parse(req.body.target)),
        content: req.body.comment
    };

    req.comments.add(comment, function(err, comment_id) {
        res.json({
            id: comment_id,
            success: true
        });
    });
});


// Returns all subscriptions for logged in user
// For now does nothing.
app.get('/auth/:sdk/:version/subscriptions', function(req, res) {
+12 −0
Original line number Diff line number Diff line
@@ -25,5 +25,17 @@ module.exports = {
        var forumAuth = new ForumAuth(new DbFacade(config.forumDb));
        req.users = new Users(new DbFacade(config.mysql), forumAuth);
        next();
    },

    /**
     * Requires that user is logged in.
     */
    requireLogin: function(req, res, next) {
        if (!req.session || !req.session.user) {
            res.json({success: false, reason: 'Forbidden'}, 403);
        }
        else {
            next();
        }
    }
};