Loading comments/app.js +2 −1 Original line number Diff line number Diff line Loading @@ -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); Loading comments/comments.js +16 −5 Original line number Diff line number Diff line Loading @@ -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. Loading @@ -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. Loading @@ -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. Loading comments/comments.spec.js +14 −0 Original line number Diff line number Diff line Loading @@ -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); Loading @@ -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]; Loading comments/request.js +4 −0 Original line number Diff line number Diff line Loading @@ -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); Loading Loading
comments/app.js +2 −1 Original line number Diff line number Diff line Loading @@ -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); Loading
comments/comments.js +16 −5 Original line number Diff line number Diff line Loading @@ -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. Loading @@ -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. Loading @@ -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. Loading
comments/comments.spec.js +14 −0 Original line number Diff line number Diff line Loading @@ -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); Loading @@ -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]; Loading
comments/request.js +4 −0 Original line number Diff line number Diff line Loading @@ -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); Loading