Skip to content

Node.js based forum software built for the modern web

use _.uniq

Barış Soner Uşaklı38af65107200
diff --git a/src/posts/category.js b/src/posts/category.js
index 42319fd6ed..d1de31850e 100644
--- a/src/posts/category.js
+++ b/src/posts/category.js
@@ -28,11 +28,9 @@ module.exports = function (Posts) {
},
function (_postData, next) {
postData = _postData;
- tids = postData.map(function (post) {
- return post.tid;
- }).filter(function (tid, index, array) {
- return tid && array.indexOf(tid) === index;
- });
+ tids = _.uniq(postData.map(function (post) {
+ return post && post.tid;
+ }).filter(Boolean));
topics.getTopicsFields(tids, ['cid'], next);
},
diff --git a/src/privileges/categories.js b/src/privileges/categories.js
index f01971b591..97889c2b2e 100644
--- a/src/privileges/categories.js
+++ b/src/privileges/categories.js
@@ -232,9 +232,7 @@ module.exports = function (privileges) {
return callback(null, []);
}
- cids = cids.filter(function (cid, index, array) {
- return array.indexOf(cid) === index;
- });
+ cids = _.uniq(cids);
async.waterfall([
function (next) {
diff --git a/src/privileges/posts.js b/src/privileges/posts.js
index 1855098927..89ef1e0f48 100644
--- a/src/privileges/posts.js
+++ b/src/privileges/posts.js
@@ -2,6 +2,7 @@
'use strict';
var async = require('async');
+var _ = require('lodash');
var meta = require('../meta');
var posts = require('../posts');
@@ -72,17 +73,18 @@ module.exports = function (privileges) {
var tids;
var tidToTopic = {};
+ pids = _.uniq(pids);
+
async.waterfall([
function (next) {
posts.getPostsFields(pids, ['uid', 'tid', 'deleted'], next);
},
function (_posts, next) {
postData = _posts;
- tids = _posts.map(function (post) {
+ tids = _.uniq(_posts.map(function (post) {
return post && post.tid;
- }).filter(function (tid, index, array) {
- return tid && array.indexOf(tid) === index;
- });
+ }).filter(Boolean));
+
topics.getTopicsFields(tids, ['deleted', 'cid'], next);
},
function (topicData, next) {
diff --git a/src/privileges/users.js b/src/privileges/users.js
index 4b04b3ccd2..5e48750dc9 100644
--- a/src/privileges/users.js
+++ b/src/privileges/users.js
@@ -2,6 +2,7 @@
'use strict';
var async = require('async');
+var _ = require('lodash');
var user = require('../user');
var groups = require('../groups');
@@ -51,9 +52,7 @@ module.exports = function (privileges) {
return filterIsModerator(cids, uid, cids.map(function () { return true; }), callback);
}
- uniqueCids = cids.filter(function (cid, index, array) {
- return array.indexOf(cid) === index;
- });
+ uniqueCids = _.uniq(cids);
helpers.isUserAllowedTo('moderate', uid, uniqueCids, next);
},
diff --git a/src/search.js b/src/search.js
index 599bd66c09..58b93c8a5a 100644
--- a/src/search.js
+++ b/src/search.js
@@ -1,6 +1,7 @@
'use strict';
var async = require('async');
+var _ = require('lodash');
var db = require('./database');
var posts = require('./posts');
@@ -79,9 +80,7 @@ function searchInContent(data, callback) {
function (mainPids, next) {
pids = mainPids.concat(pids).map(function (pid) {
return pid && pid.toString();
- }).filter(function (pid, index, array) {
- return pid && array.indexOf(pid) === index;
- });
+ }).filter(Boolean);
privileges.posts.filter('read', pids, data.uid, next);
},
@@ -392,9 +391,8 @@ function getSearchCids(data, callback) {
}, next);
},
function (results, next) {
- var cids = results.watchedCids.concat(results.childrenCids).concat(data.categories).filter(function (cid, index, array) {
- return cid && array.indexOf(cid) === index;
- });
+ var cids = results.watchedCids.concat(results.childrenCids).concat(data.categories).filter(Boolean);
+ cids = _.uniq(cids);
next(null, cids);
},
], callback);
diff --git a/src/topics.js b/src/topics.js
index 575199d400..3952f7b402 100644
--- a/src/topics.js
+++ b/src/topics.js
@@ -101,14 +101,14 @@ Topics.getTopicsByTids = function (tids, uid, callback) {
function mapFilter(array, field) {
return array.map(function (topic) {
return topic && topic[field] && topic[field].toString();
- }).filter(function (value, index, array) {
- return utils.isNumber(value) && array.indexOf(value) === index;
+ }).filter(function (value) {
+ return utils.isNumber(value);
});
}
topics = _topics;
- uids = mapFilter(topics, 'uid');
- cids = mapFilter(topics, 'cid');
+ uids = _.uniq(mapFilter(topics, 'uid'));
+ cids = _.uniq(mapFilter(topics, 'cid'));
async.parallel({
users: function (next) {
diff --git a/src/topics/suggested.js b/src/topics/suggested.js
index 33153a580c..e2b965fd4e 100644
--- a/src/topics/suggested.js
+++ b/src/topics/suggested.js
@@ -25,8 +25,8 @@ module.exports = function (Topics) {
},
function (results, next) {
var tids = results.tagTids.concat(results.searchTids).concat(results.categoryTids);
- tids = tids.filter(function (_tid, index, array) {
- return parseInt(_tid, 10) !== parseInt(tid, 10) && array.indexOf(_tid) === index;
+ tids = _.uniq(tids).filter(function (_tid) {
+ return parseInt(_tid, 10) !== parseInt(tid, 10);
});
if (stop === -1) {
diff --git a/src/topics/tags.js b/src/topics/tags.js
index dbfb092cc1..f936d7da4a 100644
--- a/src/topics/tags.js
+++ b/src/topics/tags.js
@@ -23,11 +23,12 @@ module.exports = function (Topics) {
plugins.fireHook('filter:tags.filter', { tags: tags, tid: tid }, next);
},
function (data, next) {
- tags = data.tags.slice(0, meta.config.maximumTagsPerTopic || 5);
+ tags = _.uniq(data.tags);
+ tags = tags.slice(0, meta.config.maximumTagsPerTopic || 5);
tags = tags.map(function (tag) {
return utils.cleanUpTag(tag, meta.config.maximumTagLength);
- }).filter(function (tag, index, array) {
- return tag && tag.length >= (meta.config.minimumTagLength || 3) && array.indexOf(tag) === index;
+ }).filter(function (tag) {
+ return tag && tag.length >= (meta.config.minimumTagLength || 3);
});
filterCategoryTags(tags, tid, next);
diff --git a/src/topics/teaser.js b/src/topics/teaser.js
index d62dc2fe72..09d4d048ae 100644
--- a/src/topics/teaser.js
+++ b/src/topics/teaser.js
@@ -2,6 +2,7 @@
'use strict';
var async = require('async');
+var _ = require('lodash');
var S = require('string');
var winston = require('winston');
@@ -57,11 +58,9 @@ module.exports = function (Topics) {
},
function (_postData, next) {
postData = _postData;
- var uids = postData.map(function (post) {
+ var uids = _.uniq(postData.map(function (post) {
return post.uid;
- }).filter(function (uid, index, array) {
- return array.indexOf(uid) === index;
- });
+ }));
user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
},
diff --git a/src/topics/unread.js b/src/topics/unread.js
index 073a680358..4a46e4c0c9 100644
--- a/src/topics/unread.js
+++ b/src/topics/unread.js
@@ -2,6 +2,7 @@
'use strict';
var async = require('async');
+var _ = require('lodash');
var db = require('../database');
var user = require('../user');
@@ -118,10 +119,10 @@ module.exports = function (Topics) {
}
}).map(function (topic) {
return topic.value;
- }).filter(function (tid, index, array) {
- return array.indexOf(tid) === index;
});
+ tids = _.uniq(tids);
+
if (params.filter === 'watched') {
Topics.filterWatchedTids(tids, uid, next);
} else {
@@ -222,8 +223,8 @@ module.exports = function (Topics) {
return setImmediate(callback, null, false);
}
- tids = tids.filter(function (tid, index, array) {
- return tid && utils.isNumber(tid) && array.indexOf(tid) === index;
+ tids = _.uniq(tids).filter(function (tid) {
+ return tid && utils.isNumber(tid);
});
if (!tids.length) {
@@ -260,9 +261,9 @@ module.exports = function (Topics) {
function (results, next) {
var cids = results.topicData.map(function (topic) {
return topic && topic.cid;
- }).filter(function (topic, index, array) {
- return topic && array.indexOf(topic) === index;
- });
+ }).filter(Boolean);
+
+ cids = _.uniq(cids);
categories.markAsRead(cids, uid, next);
},
diff --git a/src/user.js b/src/user.js
index e4ad24283c..db8f9774ed 100644
--- a/src/user.js
+++ b/src/user.js
@@ -64,9 +64,7 @@ User.getUsersWithFields = function (uids, fields, uid, callback) {
plugins.fireHook('filter:users.addFields', { fields: fields }, next);
},
function (data, next) {
- data.fields = data.fields.filter(function (field, index, array) {
- return array.indexOf(field) === index;
- });
+ data.fields = _.uniq(data.fields);
async.parallel({
userData: function (next) {
diff --git a/src/user/delete.js b/src/user/delete.js
index 7392192c8b..92fd8f27b9 100644
--- a/src/user/delete.js
+++ b/src/user/delete.js
@@ -1,6 +1,7 @@
'use strict';
var async = require('async');
+var _ = require('lodash');
var db = require('../database');
var posts = require('../posts');
@@ -152,9 +153,7 @@ module.exports = function (User) {
}, next);
},
function (pids, next) {
- pids = pids.upvotedPids.concat(pids.downvotedPids).filter(function (pid, index, array) {
- return pid && array.indexOf(pid) === index;
- });
+ pids = _.uniq(pids.upvotedPids.concat(pids.downvotedPids).filter(Boolean));
async.eachSeries(pids, function (pid, next) {
posts.unvote(pid, uid, next);