Commit 354f83f2 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Add comment counts to list of all tags.

Rename Comments.getAllTags to .getTopTags, so it's in line with
.getTopUsers and .getTopTargets.

Ensure tags are sorted alphabetically in TagEditor (the backend
now returns them sorted by comment count).
parent 1912ff25
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -136,6 +136,13 @@ app.get('/auth/:sdk/:version/targets', function(req, res) {
    });
});

// Returns the most used tags.
app.get('/auth/:sdk/:version/tags', function(req, res) {
    new Request(req).getTopTags(function(tags) {
        res.send({ success: true, tags: tags });
    });
});

// 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) {
@@ -221,13 +228,6 @@ app.post('/auth/:sdk/:version/comments/:commentId/remove_tag', Auth.isLoggedIn,
    });
});

// Returns list of all available tags
app.get('/auth/:sdk/:version/tags', function(req, res) {
    new Request(req).getAllTags(function(tags) {
        res.send({ success: true, tags: tags });
    });
});

// Marks a comment 'read'
app.post('/auth/:sdk/:version/comments/:commentId/read', Auth.isLoggedIn, function(req, res) {
    new Request(req).markRead(req.params.commentId, function() {
+7 −7
Original line number Diff line number Diff line
@@ -378,13 +378,6 @@ Comments.prototype = {
        });
    },

    /**
     * @inheritdoc Tags#getAll
     */
    getAllTags: function(callback) {
        this.tags.getAll(callback);
    },

    /**
     * @inheritdoc Tags#add
     */
@@ -399,6 +392,13 @@ Comments.prototype = {
        this.tags.remove(tag, callback);
    },

    /**
     * @inheritdoc Tags#getTop
     */
    getTopTags: function(callback) {
        this.tags.getTop(callback);
    },

    /**
     * Retrieves users ordered by number of upvotes or number of comments.
     * @param {String} sortBy Either "votes" or "comments"
+9 −9
Original line number Diff line number Diff line
@@ -71,6 +71,15 @@ Request.prototype = {
        });
    },

    /**
     * Retrieves most used tags.
     */
    getTopTags: function(callback) {
        this.db.comments().getTopTags(function(err, tags) {
            callback(tags);
        });
    },

    /**
     * Provides the comments_meta request data.
     */
@@ -198,15 +207,6 @@ Request.prototype = {
        });
    },

    /**
     * Retrieves array of all tags.
     */
    getAllTags: function(callback) {
        this.db.comments().getAllTags(function(err, tags) {
            callback(tags);
        });
    },

    /**
     * Adds tag to comment.
     */
+22 −5
Original line number Diff line number Diff line
@@ -15,20 +15,37 @@ function Tags(db, domain) {

Tags.prototype = {
    /**
     * Retrieves all available tags in current domain.
     * Retrieves all available tags in current domain ordered by nr of
     * comments tagged with them.
     *
     * @param {Function} callback
     * @param {Error} callback.err
     * @param {Object[]} callback.tags Array of object with single `tagname` field.
     * @param {Object[]} callback.tags Array of objects with fields `tagname` and `score`.
     */
    getAll: function(callback) {
        var sql = "SELECT tagname FROM tags WHERE domain = ? ORDER BY tagname";
    getTop: function(callback) {
        var sql = [
            "SELECT",
                "tagname,",
                "COUNT(*) AS score",
            "FROM tags",
                "JOIN comment_tags ON tags.id = comment_tags.tag_id",
            "WHERE domain = ?",
            "GROUP BY tagname",
            "ORDER BY score DESC"
        ];
        this.db.query(sql, [this.domain], function(err, rows) {
            if (err) {
                callback(err);
                return;
            }
            callback(null, rows.map(function(r) { return {tagname: r.tagname}; }));
            // The rows var contains some extra fields we don't want,
            // plus the score field is a string, so convert it.
            callback(null, rows.map(function(r) {
                return {
                    tagname: r.tagname,
                    score: parseInt(r.score, 10)
                };
            }));
        });
    },

+5 −5
Original line number Diff line number Diff line
@@ -19,17 +19,17 @@ describe("Tags", function() {
        connection.end();
    });

    it("#getAllTags returns all tags in current domain", function(done) {
        comments.getAllTags(function(err, tags) {
            expect(tags).toEqual([{tagname: "bug"}, {tagname: "feature"}]);
    it("#getTopTags returns all tags in current domain", function(done) {
        comments.getTopTags(function(err, tags) {
            expect(tags).toEqual([{tagname: "feature", score: 2}, {tagname: "bug", score: 1}]);
            done();
        });
    });

    it("#getAllTags returns empty array when no tags in current domain", function(done) {
    it("#getTopTags returns empty array when no tags in current domain", function(done) {
        comments = new Comments(new DbFacade(connection), "blabla");

        comments.getAllTags(function(err, tags) {
        comments.getTopTags(function(err, tags) {
            expect(tags).toEqual([]);
            done();
        });
Loading