Commit 4523e8fd authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Basic implementation of recent comments query.

parent efe6e7c8
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ module.exports = {
        return {
            _id: comment.id,
            author: comment.username,
            target: this.targetToJson(comment),
            content: comment.content,
            contentHtml: comment.content_html,
            createdAt: String(comment.created_at),
+12 −0
Original line number Diff line number Diff line
@@ -91,6 +91,18 @@ app.post('/auth/logout', validator.doLogout, function(req, res) {

// Requests for Comments

// Returns n most recent comments.
// Takes two parameters: offset and limit.
app.get('/auth/:sdk/:version/comments_recent', function(req, res) {
    var query = {
        offset: parseInt(req.query.offset, 10),
        limit: parseInt(req.query.limit, 10)
    };
    new Request(req).getRecentComments(query, function(comments) {
        res.json(comments);
    });
});

// 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) {
+21 −2
Original line number Diff line number Diff line
@@ -21,6 +21,25 @@ Request.prototype = {
        this.db.users().login(user, pass, callback);
    },

    getRecentComments: function(query, callback) {
        this.db.comments().findRecent(query, function(err, comments) {
            this.db.comments().count(query, function(err, total) {
                var commentsOut = comments.map(ApiAdapter.commentToJson, ApiAdapter);

                // store total count to last comment.
                // It's a hack, but that's what we're left with for now.
                var last = commentsOut[commentsOut.length-1];
                if (last) {
                    last.total_rows = total;
                    last.offset = query.offset;
                    last.limit = query.limit;
                }

                callback(commentsOut);
            });
        }.bind(this));
    },

    getCommentCountsPerTarget: function(callback) {
        this.db.comments().countsPerTarget(function(err, counts) {
            callback(counts);
@@ -38,7 +57,7 @@ Request.prototype = {
        }

        this.db.comments().find(targetObj, function(err, comments) {
            callback(comments.map(ApiAdapter.commentToJson));
            callback(comments.map(ApiAdapter.commentToJson, ApiAdapter));
        });
    },

@@ -127,7 +146,7 @@ Request.prototype = {
        }

        this.db.subscriptions().findTargetsByUser(this.getUserId(), function(err, targets) {
            callback(targets.map(ApiAdapter.targetToJson));
            callback(targets.map(ApiAdapter.targetToJson, ApiAdapter));
        });
    },