Commit 938acfe7 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

New recent comments page.

The page is now also visible to normal users, with the only difference
that hideRead checkbox is only shown for moderators.

The page is divided into two sections: comments & users.

The comments section has two tabs which determine how the comments are
sorted (instead of a checkbox determining it).

The users section has a list of all users sorted by votes.
parent d2e30512
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -106,6 +106,13 @@ app.get('/auth/:sdk/:version/comments_recent', function(req, res) {
    });
});

// Returns the most upvoted users.
app.get('/auth/:sdk/:version/users', function(req, res) {
    new Request(req).getTopUsers(function(users) {
        res.json(users);
    });
});

// Returns number of comments for each class/member,
// and when user is logged in, all his subscriptions.
app.get('/auth/:sdk/:version/comments_meta', function(req, res) {
+13 −0
Original line number Diff line number Diff line
@@ -26,6 +26,19 @@ module.exports = {
        };
    },

    /**
     * Turns user row into JSON response.
     */
    userToJson: function(user) {
        return {
            id: user.id,
            username: user.username,
            vote: user.vote,
            moderator: user.moderator,
            emailHash: crypto.createHash('md5').update(user.email).digest("hex")
        };
    },

    /**
     * Turns target array in JSON into {type,cls,member} object.
     */
+22 −0
Original line number Diff line number Diff line
@@ -360,6 +360,28 @@ Comments.prototype = {
        });
    },

    /**
     * Retrieves users ordered by number of upvotes.
     * @param {Function} callback Called when done.
     * @param {String} callback.err Error message when login failed.
     * @param {Object} callback.users The top users.
     */
    getTopUsers: function(callback) {
        var sql = [
            "SELECT",
                "user_id AS id,",
                "username,",
                "email,",
                "moderator,",
                "COALESCE(SUM(vote), 0) AS vote",
            "FROM ", this.view,
            "WHERE domain = ?",
            "GROUP BY user_id",
            "ORDER BY vote DESC"
        ];
        this.db.query(sql, [this.domain], callback);
    },

    // 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
+9 −0
Original line number Diff line number Diff line
@@ -53,6 +53,15 @@ Request.prototype = {
        }.bind(this));
    },

    /**
     * Retrieves most upvoted users.
     */
    getTopUsers: function(callback) {
        this.db.comments().getTopUsers(function(err, users) {
            callback(users.map(ApiAdapter.userToJson, ApiAdapter));
        });
    },

    /**
     * Provides the comments_meta request data.
     */
+14 −0
Original line number Diff line number Diff line
@@ -214,6 +214,20 @@ describe("Comments", function() {
        });
    });

    it("#getTopUsers gives all users who have posted to this domain", function(done) {
        comments.getTopUsers(function(err, users) {
            expect(users.length).toEqual(5);
            done();
        });
    });

    it("#getTopUsers gives users sorted by votes", function(done) {
        comments.getTopUsers(function(err, users) {
            expect(users[0].vote).toBeGreaterThan(users[1].vote);
            done();
        });
    });

    it("#add adds a new comment and returns its ID which we can then use to retrieve the comment", function(done) {
        var com = {
            user_id: 1,
Loading