Commit 60b6225e authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Hiding of recent comments marked as read.

parent 0c1feb2c
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -97,7 +97,8 @@ app.get('/auth/:sdk/:version/comments_recent', function(req, res) {
        offset: parseInt(req.query.offset, 10),
        limit: parseInt(req.query.limit, 10),
        orderBy: req.query.sortByScore ? "vote" : "created_at",
        hideCurrentUser: req.query.hideCurrentUser
        hideCurrentUser: req.query.hideCurrentUser,
        hideRead: req.query.hideRead
    };
    new Request(req).getRecentComments(query, function(comments) {
        res.json(comments);
+11 −2
Original line number Diff line number Diff line
@@ -52,8 +52,13 @@ Comments.prototype = {
     * @param {Number} user_id The ID of the user who's readings to inspect.
     */
    showReadBy: function(user_id) {
        var sql = "(SELECT COUNT(*) FROM readings WHERE user_id = ? AND comment_id = comments.id) AS `read`";
        this.fields.push(this.db.format(sql, [user_id]));
        this.readBy = user_id;
        this.fields.push(this.getReadExpression() + " AS `read`");
    },

    getReadExpression: function() {
        var sql = "(SELECT COUNT(*) FROM readings WHERE user_id = ? AND comment_id = comments.id)";
        return this.db.format(sql, [this.readBy]);
    },

    /**
@@ -108,6 +113,7 @@ Comments.prototype = {
     * @param {Number} [opts.orderBy="created_at"] By which column to sort the results.
     * Two possible options here: "created_at" and "vote".
     * @param {Number} [opts.hideUser=undefined] A user_id to hide.
     * @param {Number} [opts.hideRead=false] True to hide comments marked as read.
     *
     * @param {Function} callback Called with the result.
     * @param {Error} callback.err The error object.
@@ -155,6 +161,9 @@ Comments.prototype = {
        if (opts.hideUser) {
            where.push(this.db.format("user_id <> ?", [opts.hideUser]));
        }
        if (opts.hideRead) {
            where.push(this.getReadExpression() + " = 0");
        }
        return where.join(" AND ");
    },

+24 −0
Original line number Diff line number Diff line
@@ -158,6 +158,14 @@ describe("Comments", function() {
        });
    });

    it("#findRecent with hideRead:true excludes comments that have been read by current user", function(done) {
        comments.showReadBy(1);
        comments.findRecent({hideRead: true}, function(err, rows) {
            expect(rows.every(function(r){return r.read === false;})).toEqual(true);
            done();
        });
    });

    it("#count gets total number of comments in current domain", function(done) {
        comments.count({}, function(err, cnt) {
            expect(cnt).toEqual(24);
@@ -185,6 +193,22 @@ describe("Comments", function() {
        });
    });

    it("#count with hideRead:false counts all comments", function(done) {
        comments.showReadBy(1);
        comments.count({hideRead: false}, function(err, cnt) {
            expect(cnt).toEqual(24);
            done();
        });
    });

    it("#count with hideRead:true excludes comments that have been read by current user", function(done) {
        comments.showReadBy(1);
        comments.count({hideRead: true}, function(err, cnt) {
            expect(cnt).toEqual(19);
            done();
        });
    });

    it("#countPerTarget gets number of comments for each target", function(done) {
        comments.countsPerTarget(function(err, counts) {
            var line = counts.filter(function(row) { return row._id === "class__Ext__"; })[0];