diff --git a/comments/api_adapter.js b/comments/api_adapter.js index 3b09de799d9b978f1249c88a561176508486896d..4fc68996391c1a67b43566bcacd207138333eea4 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 2a89a19b961e9402f68c3af75836af455cefde00..c367c20d34a3780f92e8714816c412efeff4e6d5 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 cd3ab82b265c9db56c4f5273184a5282372c1b93..5bcf6687be0a0737e14636e69a87637c0234bf30 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)); }); },