diff --git a/src/database/mongo/sorted.js b/src/database/mongo/sorted.js index 8ecbb1b129..21a1a6e61c 100644 --- a/src/database/mongo/sorted.js +++ b/src/database/mongo/sorted.js @@ -290,7 +290,10 @@ module.exports = function (db, module) { module.sortedSetScores = function (key, values, callback) { if (!key) { - return callback(null, null); + return setImmediate(callback, null, null); + } + if (!values.length) { + return setImmediate(callback, null, []); } values = values.map(helpers.valueToString); db.collection('objects').find({ _key: key, value: { $in: values } }, { projection: { _id: 0, _key: 0 } }).toArray(function (err, result) { diff --git a/src/database/postgres/sorted.js b/src/database/postgres/sorted.js index 014bda1ba7..4b1dfa41c6 100644 --- a/src/database/postgres/sorted.js +++ b/src/database/postgres/sorted.js @@ -382,9 +382,11 @@ SELECT o."_key" k, module.sortedSetScores = function (key, values, callback) { if (!key) { - return callback(null, null); + return setImmediate(callback, null, null); + } + if (!values.length) { + return setImmediate(callback, null, []); } - values = values.map(helpers.valueToString); query({ diff --git a/src/database/redis/sorted.js b/src/database/redis/sorted.js index 72a522c8eb..6dac8955aa 100644 --- a/src/database/redis/sorted.js +++ b/src/database/redis/sorted.js @@ -185,6 +185,9 @@ module.exports = function (redisClient, module) { }; module.sortedSetScores = function (key, values, callback) { + if (!values.length) { + return setImmediate(callback, null, []); + } helpers.execKeyValues(redisClient, 'batch', 'zscore', key, values, function (err, scores) { if (err) { return callback(err); diff --git a/src/posts/bookmarks.js b/src/posts/bookmarks.js index 241c5b85c3..c609408821 100644 --- a/src/posts/bookmarks.js +++ b/src/posts/bookmarks.js @@ -86,12 +86,7 @@ module.exports = function (Posts) { Posts.hasBookmarked = function (pid, uid, callback) { if (parseInt(uid, 10) <= 0) { - if (Array.isArray(pid)) { - callback(null, pid.map(() => false)); - } else { - callback(null, false); - } - return; + return callback(null, Array.isArray(pid) ? pid.map(() => false) : false); } if (Array.isArray(pid)) { diff --git a/src/topics/index.js b/src/topics/index.js index 6828f8eae3..fb57fcd5d9 100644 --- a/src/topics/index.js +++ b/src/topics/index.js @@ -188,13 +188,15 @@ Topics.getTopicWithPosts = function (topicData, set, uid, start, stop, reverse, topicData.bookmark = results.bookmark; topicData.postSharing = results.postSharing; topicData.deleter = results.deleter; - topicData.deletedTimestampISO = utils.toISOString(topicData.deletedTimestamp); + if (results.deleter) { + topicData.deletedTimestampISO = utils.toISOString(topicData.deletedTimestamp); + } topicData.merger = results.merger; - topicData.mergedTimestampISO = utils.toISOString(topicData.mergedTimestamp); + if (results.merger) { + topicData.mergedTimestampISO = utils.toISOString(topicData.mergedTimestamp); + } topicData.related = results.related || []; - topicData.unreplied = topicData.postcount === 1; - topicData.icons = []; plugins.fireHook('filter:topic.get', { topic: topicData, uid: uid }, next); @@ -245,14 +247,14 @@ function getMainPostAndReplies(topic, set, uid, start, stop, reverse, callback) } function getDeleter(topicData, callback) { - if (!topicData.deleterUid) { + if (!parseInt(topicData.deleterUid, 10)) { return setImmediate(callback, null, null); } user.getUserFields(topicData.deleterUid, ['username', 'userslug', 'picture'], callback); } function getMerger(topicData, callback) { - if (!topicData.mergerUid) { + if (!parseInt(topicData.mergerUid, 10)) { return setImmediate(callback, null, null); } async.waterfall([ diff --git a/src/topics/posts.js b/src/topics/posts.js index 005e5d4eed..33dd80ef44 100644 --- a/src/topics/posts.js +++ b/src/topics/posts.js @@ -154,7 +154,7 @@ module.exports = function (Topics) { if (!parentPids.length) { return setImmediate(callback); } - + parentPids = _.uniq(parentPids); var parentPosts; async.waterfall([ async.apply(posts.getPostsFields, parentPids, ['uid']), diff --git a/src/topics/tags.js b/src/topics/tags.js index 4acc8810b5..2be85a2c73 100644 --- a/src/topics/tags.js +++ b/src/topics/tags.js @@ -232,13 +232,13 @@ module.exports = function (Topics) { }; Topics.getTagData = function (tags, callback) { - var keys = tags.map(function (tag) { - return 'tag:' + tag.value; - }); + if (!tags.length) { + return setImmediate(callback, null, []); + } async.waterfall([ function (next) { - db.getObjects(keys, next); + db.getObjects(tags.map(tag => 'tag:' + tag.value), next); }, function (tagData, next) { tags.forEach(function (tag, index) { diff --git a/test/database/sorted.js b/test/database/sorted.js index f0320962cd..35adcdfd8a 100644 --- a/test/database/sorted.js +++ b/test/database/sorted.js @@ -581,6 +581,15 @@ describe('Sorted Set methods', function () { }); }); + it('should return empty array if values is an empty array', function (done) { + db.sortedSetScores('sortedSetTest1', [], function (err, scores) { + assert.ifError(err); + assert.equal(arguments.length, 2); + assert.deepStrictEqual(scores, []); + done(); + }); + }); + it('should return scores properly', function (done) { db.sortedSetsScore(['zeroScore', 'sortedSetTest1', 'doesnotexist'], 'value1', function (err, scores) { assert.ifError(err);