From 6b1af76f080160cd93adea03d96d74fed2697b4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Tue, 31 Oct 2017 09:43:11 -0400 Subject: [PATCH] closes #6015 --- src/posts/queue.js | 6 +++--- src/user/posts.js | 23 ++++++++++++++--------- test/posts.js | 8 +++++++- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/posts/queue.js b/src/posts/queue.js index 01836abba0..4cdedeca03 100644 --- a/src/posts/queue.js +++ b/src/posts/queue.js @@ -50,7 +50,7 @@ module.exports = function (Posts) { }, next); }, function (next) { - user.setUserField(data.uid, 'lastposttime', Date.now(), next); + user.setUserField(data.uid, 'lastqueuetime', Date.now(), next); }, function (next) { notifications.create({ @@ -95,8 +95,8 @@ module.exports = function (Posts) { privileges.categories.can('topics:reply', cid, data.uid, next); } }, - isReadyToPost: function (next) { - user.isReadyToPost(data.uid, cid, next); + isReadyToQueue: function (next) { + user.isReadyToQueue(data.uid, cid, next); }, }, next); }, diff --git a/src/user/posts.js b/src/user/posts.js index cba8c31008..419747b276 100644 --- a/src/user/posts.js +++ b/src/user/posts.js @@ -7,6 +7,14 @@ var privileges = require('../privileges'); module.exports = function (User) { User.isReadyToPost = function (uid, cid, callback) { + isReady(uid, cid, 'lastposttime', callback); + }; + + User.isReadyToQueue = function (uid, cid, callback) { + isReady(uid, cid, 'lastqueuetime', callback); + }; + + function isReady(uid, cid, field, callback) { if (parseInt(uid, 10) === 0) { return callback(); } @@ -14,10 +22,7 @@ module.exports = function (User) { function (next) { async.parallel({ userData: function (next) { - User.getUserFields(uid, ['banned', 'lastposttime', 'joindate', 'email', 'email:confirmed', 'reputation'], next); - }, - exists: function (next) { - db.exists('user:' + uid, next); + User.getUserFields(uid, ['uid', 'banned', 'joindate', 'email', 'email:confirmed', 'reputation'].concat([field]), next); }, isAdminOrMod: function (next) { privileges.categories.isAdminOrMod(cid, uid, next); @@ -25,7 +30,7 @@ module.exports = function (User) { }, next); }, function (results, next) { - if (!results.exists) { + if (!parseInt(results.userData.uid, 10)) { return next(new Error('[[error:no-user]]')); } @@ -48,18 +53,18 @@ module.exports = function (User) { return next(new Error('[[error:user-too-new, ' + meta.config.initialPostDelay + ']]')); } - var lastposttime = userData.lastposttime || 0; + var lasttime = userData[field] || 0; - if (parseInt(meta.config.newbiePostDelay, 10) > 0 && parseInt(meta.config.newbiePostDelayThreshold, 10) > parseInt(userData.reputation, 10) && now - parseInt(lastposttime, 10) < parseInt(meta.config.newbiePostDelay, 10) * 1000) { + if (parseInt(meta.config.newbiePostDelay, 10) > 0 && parseInt(meta.config.newbiePostDelayThreshold, 10) > parseInt(userData.reputation, 10) && now - parseInt(lasttime, 10) < parseInt(meta.config.newbiePostDelay, 10) * 1000) { return next(new Error('[[error:too-many-posts-newbie, ' + meta.config.newbiePostDelay + ', ' + meta.config.newbiePostDelayThreshold + ']]')); - } else if (now - parseInt(lastposttime, 10) < parseInt(meta.config.postDelay, 10) * 1000) { + } else if (now - parseInt(lasttime, 10) < parseInt(meta.config.postDelay, 10) * 1000) { return next(new Error('[[error:too-many-posts, ' + meta.config.postDelay + ']]')); } next(); }, ], callback); - }; + } User.onNewPostMade = function (postData, callback) { async.series([ diff --git a/test/posts.js b/test/posts.js index 7b67b3ee8c..7eb96d1cde 100644 --- a/test/posts.js +++ b/test/posts.js @@ -726,7 +726,6 @@ describe('Post\'s', function () { }); }); - describe('filterPidsByCid', function () { it('should return pids as is if cid is falsy', function (done) { posts.filterPidsByCid([1, 2, 3], null, function (err, pids) { @@ -753,6 +752,13 @@ describe('Post\'s', function () { }); }); + it('should error if user does not exist', function (done) { + user.isReadyToPost(21123123, 1, function (err) { + assert.equal(err.message, '[[error:no-user]]'); + done(); + }); + }); + describe('post queue', function () { var uid; before(function (done) {