Commit d1154176 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Implement sorting recent comments by score.

Recent comments page now has two checkboxes:

  - hide read
  - sort by score

Not an efficient implementation, but should be good enough as only
moderators use the feature.
parent f8b5cea9
Loading
Loading
Loading
Loading
+20 −11
Original line number Diff line number Diff line
@@ -198,20 +198,29 @@ app.get('/auth/:sdk/:version/comments_recent', util.getCommentReads, function(re
        filter._id = { $nin: req.commentMeta.reads };
    }

    Comment.find(filter).sort('createdAt', -1).skip(offset).limit(limit).run(function(err, comments) {
    Comment.find(filter).sort("createdAt", -1).run(function(err, comments) {
        var total_rows = comments.length;

        comments = util.scoreComments(comments, req);
        // Count all comments, store count to last comment
        Comment.count(filter).run(function(err, count) {

        // For now this is the simplest solution to enable sorting by score.
        // A bit inefficient to select all records, but it's only for
        // moderators, so it shouldn't pose much of a performance problem.
        if (req.query.sortByScore) {
            util.sortByField(comments, "score", "DESC");
        }
        comments = comments.slice(offset, offset+limit);

        // store total count to last comment
        var last = comments[comments.length-1];
        if (last) {
                last.total_rows = count;
            last.total_rows = total_rows;
            last.offset = offset;
            last.limit = limit;
        }
        res.json(comments);
    });
});
});

/**
 * Returns number of comments for each class/member,
+24 −0
Original line number Diff line number Diff line
@@ -58,6 +58,30 @@ exports.scoreComments = function(comments, req) {
    });
};

/**
 * Sorts array of objects by the value of given field.
 *
 * @param {Array} arr
 * @param {String} field
 * @param {String} [direction="ASC"] either "ASC" or "DESC".
 */
exports.sortByField = function(arr, field, direction) {
    if (direction === "DESC") {
        var more = -1;
        var less = 1;
    }
    else {
        var more = 1;
        var less = -1;
    }

    arr.sort(function(aObj, bObj) {
        var a = aObj[field];
        var b = bObj[field];
        return a > b ? more : a < b ? less : 0;
    });
};

/**
 * Performs voting on comment.
 *
+8 −4
Original line number Diff line number Diff line
@@ -121,7 +121,8 @@ Ext.define('Docs.controller.Comments', {
                        [ '.fetchMoreComments',    'click', this.fetchMoreComments],
                        [ '.voteCommentUp',        'click', this.voteUp],
                        [ '.voteCommentDown',      'click', this.voteDown],
                        [ '#hideRead',            'change', function() { this.fetchRecentComments(); }]
                        [ '#hideRead',            'change', function() { this.fetchRecentComments(); }],
                        [ '#sortByScore',         'change', function() { this.fetchRecentComments(); }]
                    ], function(delegate) {
                        cmp.el.addListener(delegate[1], delegate[2], this, {
                            preventDefault: true,
@@ -278,9 +279,12 @@ Ext.define('Docs.controller.Comments', {
            limit: 100
        };

        if (this.isHideReadChecked()) {
        if (this.isChecked('hideRead')) {
            params.hideRead = 1;
        }
        if (this.isChecked('sortByScore')) {
            params.sortByScore = 1;
        }

        this.request("jsonp", {
            url: '/comments_recent',
@@ -297,8 +301,8 @@ Ext.define('Docs.controller.Comments', {
        });
    },

    isHideReadChecked: function() {
        var cb = Ext.get('hideRead');
    isChecked: function(id) {
        var cb = Ext.get(id);
        return cb && cb.dom.checked;
    },

+14 −2
Original line number Diff line number Diff line
@@ -11,8 +11,20 @@ Ext.define('Docs.view.comments.Index', {
    autoScroll: true,

    items: [
        { xtype: 'container', html: '<h1>Recent Comments</h1> Hide read: <input type="checkbox" name="hideRead" id="hideRead" />' },
        { xtype: 'container', id: 'recentcomments' }
        {
            xtype: 'container',
            html: [
                '<h1>Recent Comments</h1>',
                '<ul id="comment-index-controls">',
                    '<li><label><input type="checkbox" name="hideRead" id="hideRead" /> Hide read</label></li>',
                    '<li><label><input type="checkbox" name="sortByScore" id="sortByScore" /> Sort by score</label></li>',
                '</ul>'
            ].join(" ")
        },
        {
            xtype: 'container',
            id: 'recentcomments'
        }
    ],

    /**
+6 −0
Original line number Diff line number Diff line
@@ -298,3 +298,9 @@
    border: 1px solid #e1ba53;
    @include border-radius(4px);
    font-weight: bold; } }

#comment-index-controls {
  position: absolute;
  padding-top: 1em;
  li {
    list-style-type: none; } }