diff --git a/comments/app.js b/comments/app.js index 734d5d68e1314e327aec1a34475e6db2e24a4812..70eb14a1a30af7ec553780fedc2bc8bcd322f3d7 100644 --- a/comments/app.js +++ b/comments/app.js @@ -96,7 +96,8 @@ app.get('/auth/:sdk/:version/comments_recent', function(req, res) { var query = { offset: parseInt(req.query.offset, 10), limit: parseInt(req.query.limit, 10), - orderBy: req.query.sortByScore ? "vote" : "created_at" + orderBy: req.query.sortByScore ? "vote" : "created_at", + hideCurrentUser: req.query.hideCurrentUser }; new Request(req).getRecentComments(query, function(comments) { res.json(comments); diff --git a/comments/comments.js b/comments/comments.js index a84a0beceaf2ddc4d299f6b6392b00d41ad220f9..b73a8bfb9c44d70a393239be4a24360ffc216775 100644 --- a/comments/comments.js +++ b/comments/comments.js @@ -107,6 +107,7 @@ Comments.prototype = { * @param {Number} [opts.offset=0] The starting index. * @param {Number} [opts.orderBy="created_at"] By which column to sort the results. * Two possible options here: "created_at" and "vote". + * @param {Number} [opts.hideUser=undefined] A user_id to hide. * * @param {Function} callback Called with the result. * @param {Error} callback.err The error object. @@ -118,18 +119,19 @@ Comments.prototype = { var sql = [ 'SELECT ', this.fields.join(", "), 'FROM', this.view, - 'WHERE domain = ?', + 'WHERE ', this.buildWhere(opts), 'ORDER BY '+opts.orderBy+' DESC', 'LIMIT ? OFFSET ?' ]; - this.db.query(sql, [this.domain, opts.limit||100, opts.offset||0], this.fixFields(callback)); + this.db.query(sql, [opts.limit||100, opts.offset||0], this.fixFields(callback)); }, /** * Counts number of comments in the current domain. * - * @param {Object} opts Reserved for future. + * @param {Object} opts Options for the query: + * @param {Number} [opts.hideUser=undefined] A user_id to hide. * * @param {Function} callback Called with the result. * @param {Error} callback.err The error object. @@ -139,14 +141,23 @@ Comments.prototype = { var sql = [ 'SELECT COUNT(*) as count', 'FROM', this.view, - 'WHERE domain = ?' + 'WHERE ', this.buildWhere(opts) ]; - this.db.queryOne(sql, [this.domain], function(err, row) { + this.db.queryOne(sql, [], function(err, row) { callback(err, +row.count); }); }, + // helper for building the WHERE expression in #findRecent and #count. + buildWhere: function(opts) { + var where = [this.db.format("domain = ?", [this.domain])]; + if (opts.hideUser) { + where.push(this.db.format("user_id <> ?", [opts.hideUser])); + } + return where.join(" AND "); + }, + /** * Returns number of comments for each target in the current * domain. diff --git a/comments/comments.spec.js b/comments/comments.spec.js index ff58aaa0de04f2b9ad004d722352f9aa9c921caf..4e9681f4c1f94f48c0367c4b42791ef7193d131d 100644 --- a/comments/comments.spec.js +++ b/comments/comments.spec.js @@ -151,6 +151,13 @@ describe("Comments", function() { }); }); + it("#findRecent with hideUser:1 excludes user with ID 1 from results", function(done) { + comments.findRecent({hideUser: 1}, function(err, rows) { + expect(rows.every(function(r){return r.user_id !== 1;})).toEqual(true); + done(); + }); + }); + it("#count gets total number of comments in current domain", function(done) { comments.count({}, function(err, cnt) { expect(cnt).toEqual(24); @@ -171,6 +178,13 @@ describe("Comments", function() { }); }); + it("#count with hideUser:1 excludes comments by that user from the count", function(done) { + comments.count({hideUser: 1}, function(err, cnt) { + expect(cnt).toEqual(19); + done(); + }); + }); + it("#countPerTarget gets number of comments for each target", function(done) { comments.countsPerTarget(function(err, counts) { var line = counts.filter(function(row) { return row._id === "class__Ext__"; })[0]; diff --git a/comments/request.js b/comments/request.js index 5bcf6687be0a0737e14636e69a87637c0234bf30..33c326c73bf3530ae45dbcf53ced5c9f21e83885 100644 --- a/comments/request.js +++ b/comments/request.js @@ -22,6 +22,10 @@ Request.prototype = { }, getRecentComments: function(query, callback) { + if (query.hideCurrentUser) { + query.hideUser = this.getUserId(); + } + this.db.comments().findRecent(query, function(err, comments) { this.db.comments().count(query, function(err, total) { var commentsOut = comments.map(ApiAdapter.commentToJson, ApiAdapter);