Commit 3658cdb3 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Implement marking comments as read.

parent a7ac5dd6
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -161,6 +161,13 @@ app.post('/auth/:sdk/:version/comments/:commentId/undo_delete', validator.isLogg
    });
});

// Marks a comment 'read'
app.post('/auth/:sdk/:version/comments/:commentId/read', validator.isLoggedIn, function(req, res) {
    new Request(req).markRead(req.params.commentId, function() {
        res.send({ success: true });
    });
});

// Returns all subscriptions for logged in user
app.get('/auth/:sdk/:version/subscriptions', function(req, res) {
    new Request(req).getSubscriptions(function(subs) {
+21 −0
Original line number Diff line number Diff line
@@ -320,6 +320,27 @@ Comments.prototype = {
        }.bind(this));
    },

    /**
     * Marks comment as read.
     *
     * @param {Object} read
     * @param {Number} read.user_id The user who's marking it.
     * @param {Number} read.comment_id The comment he's marking.
     * @param {Function} callback
     * @param {Error} callback.err
     */
    markRead: function(read, callback) {
        read.created_at = new Date();
        this.db.insert("readings", read, function(err) {
            if (err && err.code === "ER_DUP_ENTRY") {
                callback();
            }
            else {
                callback(err);
            }
        });
    },

    // Helper that converts all `vote_dir` and `read` fields into
    // appropriate type. For some reason the vote_dir field is a
    // string by default, but we don't want that.  The `read` field is
+21 −0
Original line number Diff line number Diff line
@@ -309,4 +309,25 @@ describe("Comments", function() {
        });
    });

    it("#markRead marks comment as read", function(done) {
        comments.markRead({comment_id: 7, user_id: 1}, function(err) {
            if (err) throw err;
            comments.showReadBy(1);
            comments.getById(7, function(err, com) {
                expect(com.read).toEqual(true);
                done();
            });
        });
    });

    it("#markRead on already read comment keeps the comment as read", function(done) {
        comments.markRead({comment_id: 7, user_id: 1}, function(err) {
            if (err) throw err;
            comments.showReadBy(1);
            comments.getById(7, function(err, com) {
                expect(com.read).toEqual(true);
                done();
            });
        });
    });
});
+11 −0
Original line number Diff line number Diff line
@@ -102,6 +102,17 @@ Request.prototype = {
        });
    },

    markRead: function(comment_id, callback) {
        var read = {
            user_id: this.getUserId(),
            comment_id: comment_id
        };

        this.db.comments().markRead(read, function(err) {
            callback();
        });
    },

    getSubscriptions: function(callback) {
        if (!this.isLoggedIn()) {
            callback([]);