From de24b15719b10b97a94ef9301e39d7c5896f15da Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Wed, 5 Sep 2012 13:43:57 +0300 Subject: [PATCH] Excluding current user posts from recent comments. --- comments/app.js | 3 ++- comments/comments.js | 21 ++++++++++++++++----- comments/comments.spec.js | 14 ++++++++++++++ comments/request.js | 4 ++++ 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/comments/app.js b/comments/app.js index 734d5d68..70eb14a1 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 a84a0bec..b73a8bfb 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 ff58aaa0..4e9681f4 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 5bcf6687..33c326c7 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); -- GitLab