From 4523e8fd7cb6592a303b76a4b5c9fc35e37ca4a7 Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Wed, 5 Sep 2012 13:07:30 +0300 Subject: [PATCH] Basic implementation of recent comments query. --- comments/api_adapter.js | 1 + comments/app.js | 12 ++++++++++++ comments/request.js | 23 +++++++++++++++++++++-- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/comments/api_adapter.js b/comments/api_adapter.js index 3b09de79..4fc68996 100644 --- a/comments/api_adapter.js +++ b/comments/api_adapter.js @@ -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), diff --git a/comments/app.js b/comments/app.js index 2a89a19b..c367c20d 100644 --- a/comments/app.js +++ b/comments/app.js @@ -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) { diff --git a/comments/request.js b/comments/request.js index cd3ab82b..5bcf6687 100644 --- a/comments/request.js +++ b/comments/request.js @@ -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)); }); }, -- GitLab