Node.js based forum software built for the modern web
closes #6932
Barış Soner Uşaklı973b5fcfe0be
diff --git a/public/language/en-GB/search.json b/public/language/en-GB/search.jsonindex 35f9c40625..061cd0423c 100644--- a/public/language/en-GB/search.json+++ b/public/language/en-GB/search.json@@ -17,6 +17,7 @@"at-most": "At most","relevance": "Relevance","post-time": "Post time",+ "votes": "Votes","newer-than": "Newer than","older-than": "Older than","any-date": "Any date",@@ -30,6 +31,7 @@"sort-by": "Sort by","last-reply-time": "Last reply time","topic-title": "Topic title",+ "topic-votes": "Topic votes","number-of-replies": "Number of replies","number-of-views": "Number of views","topic-start-date": "Topic start date",diff --git a/public/src/client/search.js b/public/src/client/search.jsindex 754f66d733..b021c562ec 100644--- a/public/src/client/search.js+++ b/public/src/client/search.js@@ -116,8 +116,8 @@ define('forum/search', ['search', 'autocomplete', 'storage'], function (searchMoif (formData.sortBy || ajaxify.data.searchDefaultSortBy) {$('#post-sort-by').val(formData.sortBy || ajaxify.data.searchDefaultSortBy);- $('#post-sort-direction').val(formData.sortDirection);}+ $('#post-sort-direction').val(formData.sortDirection || 'desc');if (formData.showAs) {var isTopic = formData.showAs === 'topics';diff --git a/src/search.js b/src/search.jsindex b0201b2240..db420c6ee5 100644--- a/src/search.js+++ b/src/search.js@@ -46,12 +46,8 @@ function searchInContent(data, callback) {async.waterfall([function (next) {async.parallel({- searchCids: function (next) {- getSearchCids(data, next);- },- searchUids: function (next) {- getSearchUids(data, next);- },+ searchCids: async.apply(getSearchCids, data),+ searchUids: async.apply(getSearchUids, data),}, next);},function (results, next) {@@ -70,12 +66,8 @@ function searchInContent(data, callback) {}}async.parallel({- pids: function (next) {- doSearch('post', ['posts', 'titlesposts'], next);- },- tids: function (next) {- doSearch('topic', ['titles', 'titlesposts'], next);- },+ pids: async.apply(doSearch, 'post', ['posts', 'titlesposts']),+ tids: async.apply(doSearch, 'topic', ['titles', 'titlesposts']),}, next);},function (results, next) {@@ -150,16 +142,16 @@ function filterAndSort(pids, data, callback) {}function getMatchedPosts(pids, data, callback) {- var postFields = ['pid', 'uid', 'tid', 'timestamp', 'deleted'];+ var postFields = ['pid', 'uid', 'tid', 'timestamp', 'deleted', 'upvotes', 'downvotes'];var categoryFields = [];- if (data.sortBy && data.sortBy !== 'relevance') {- if (data.sortBy.startsWith('category.')) {- categoryFields.push(data.sortBy.split('.')[1]);- }+ if (data.sortBy && data.sortBy.startsWith('category.')) {+ categoryFields.push(data.sortBy.split('.')[1]);}var postsData;+ let tids;+ let uids;async.waterfall([function (next) {posts.getPostsFields(pids, postFields, next);@@ -170,7 +162,7 @@ function getMatchedPosts(pids, data, callback) {async.parallel({users: function (next) {if (data.sortBy && data.sortBy.startsWith('user')) {- var uids = postsData.map(post => post.uid);+ uids = _.uniq(postsData.map(post => post.uid));user.getUsersFields(uids, ['username'], next);} else {next();@@ -178,7 +170,8 @@ function getMatchedPosts(pids, data, callback) {},topics: function (next) {var topicsData;- const tids = postsData.map(post => post.tid);+ tids = _.uniq(postsData.map(post => post.tid));+ let cids;async.waterfall([function (next) {topics.getTopicsData(tids, next);@@ -186,20 +179,13 @@ function getMatchedPosts(pids, data, callback) {function (_topics, next) {topicsData = _topics;async.parallel({- teasers: function (next) {- if (data.sortBy && data.sortBy.startsWith('teaser')) {- var teaserKeys = topicsData.map(topic => 'post:' + topic.teaserPid);- db.getObjectsFields(teaserKeys, ['timestamp'], next);- } else {- next();- }- },categories: function (next) {if (!categoryFields.length) {return next();}- var cids = topicsData.map(topic => 'category:' + topic.cid);- db.getObjectsFields(cids, categoryFields, next);++ cids = _.uniq(topicsData.map(topic => topic && topic.cid));+ db.getObjectsFields(cids.map(cid => 'category:' + cid), categoryFields, next);},tags: function (next) {if (Array.isArray(data.hasTags) && data.hasTags.length) {@@ -211,12 +197,10 @@ function getMatchedPosts(pids, data, callback) {}, next);},function (results, next) {+ const cidToCategory = _.zipObject(cids, results.categories);topicsData.forEach(function (topic, index) {- if (topic && results.categories && results.categories[index]) {- topic.category = results.categories[index];- }- if (topic && results.teasers && results.teasers[index]) {- topic.teaser = results.teasers[index];+ if (topic && results.categories && cidToCategory[topic.cid]) {+ topic.category = cidToCategory[topic.cid];}if (topic && results.tags && results.tags[index]) {topic.tags = results.tags[index];@@ -230,19 +214,18 @@ function getMatchedPosts(pids, data, callback) {}, next);},function (results, next) {- postsData.forEach(function (post, index) {- if (results.topics && results.topics[index]) {- post.topic = results.topics[index];- if (results.topics[index].category) {- post.category = results.topics[index].category;- }- if (results.topics[index].teaser) {- post.teaser = results.topics[index].teaser;+ const tidToTopic = _.zipObject(tids, results.topics);+ const uidToUser = _.zipObject(uids, results.users);+ postsData.forEach(function (post) {+ if (results.topics && tidToTopic[post.tid]) {+ post.topic = tidToTopic[post.tid];+ if (post.topic && post.topic.category) {+ post.category = post.topic.category;}}- if (results.users && results.users[index]) {- post.user = results.users[index];+ if (uidToUser[post.uid]) {+ post.user = uidToUser[post.uid];}});@@ -256,13 +239,9 @@ function filterByPostcount(posts, postCount, repliesFilter) {postCount = parseInt(postCount, 10);if (postCount) {if (repliesFilter === 'atleast') {- posts = posts.filter(function (post) {- return post.topic && post.topic.postcount >= postCount;- });+ posts = posts.filter(post => post.topic && post.topic.postcount >= postCount);} else {- posts = posts.filter(function (post) {- return post.topic && post.topic.postcount <= postCount;- });+ posts = posts.filter(post => post.topic && post.topic.postcount <= postCount);}}return posts;@@ -271,15 +250,11 @@ function filterByPostcount(posts, postCount, repliesFilter) {function filterByTimerange(posts, timeRange, timeFilter) {timeRange = parseInt(timeRange, 10) * 1000;if (timeRange) {- var time = Date.now() - timeRange;+ const time = Date.now() - timeRange;if (timeFilter === 'newer') {- posts = posts.filter(function (post) {- return post.timestamp >= time;- });+ posts = posts.filter(post => post.timestamp >= time);} else {- posts = posts.filter(function (post) {- return post.timestamp <= time;- });+ posts = posts.filter(post => post.timestamp <= time);}}return posts;@@ -290,9 +265,7 @@ function filterByTags(posts, hasTags) {posts = posts.filter(function (post) {var hasAllTags = false;if (post && post.topic && Array.isArray(post.topic.tags) && post.topic.tags.length) {- hasAllTags = hasTags.every(function (tag) {- return post.topic.tags.includes(tag);- });+ hasAllTags = hasTags.every(tag => post.topic.tags.includes(tag));}return hasAllTags;});@@ -307,18 +280,12 @@ function sortPosts(posts, data) {data.sortDirection = data.sortDirection || 'desc';var direction = data.sortDirection === 'desc' ? 1 : -1;-- if (data.sortBy === 'timestamp') {- posts.sort(function (p1, p2) {- return direction * (p2.timestamp - p1.timestamp);- });-- return;+ const fields = data.sortBy.split('.');+ if (fields.length === 1) {+ return posts.sort((p1, p2) => direction * (p2[fields[0]] - p1[fields[0]]));}var firstPost = posts[0];- var fields = data.sortBy.split('.');-if (!fields || fields.length !== 2 || !firstPost[fields[0]] || !firstPost[fields[0]][fields[1]]) {return;}@@ -326,9 +293,7 @@ function sortPosts(posts, data) {var isNumeric = utils.isNumber(firstPost[fields[0]][fields[1]]);if (isNumeric) {- posts.sort(function (p1, p2) {- return direction * (p2[fields[0]][fields[1]] - p1[fields[0]][fields[1]]);- });+ posts.sort((p1, p2) => direction * (p2[fields[0]][fields[1]] - p1[fields[0]][fields[1]]));} else {posts.sort(function (p1, p2) {if (p1[fields[0]][fields[1]] > p2[fields[0]][fields[1]]) {@@ -370,8 +335,7 @@ function getSearchCids(data, callback) {}, next);},function (results, next) {- var cids = results.watchedCids.concat(results.childrenCids).concat(data.categories).filter(Boolean);- cids = _.uniq(cids);+ const cids = _.uniq(results.watchedCids.concat(results.childrenCids).concat(data.categories).filter(Boolean));next(null, cids);},], callback);@@ -380,18 +344,10 @@ function getSearchCids(data, callback) {function getChildrenCids(cids, uid, callback) {async.waterfall([function (next) {- categories.getChildren(cids, uid, next);+ async.map(cids, categories.getChildrenCids, next);},- function (childrenCategories, next) {- var childrenCids = [];- var allCategories = [];-- childrenCategories.forEach(function (childrens) {- categories.flattenCategories(allCategories, childrens);- childrenCids = childrenCids.concat(allCategories.map(category => category && category.cid));- });-- next(null, childrenCids);+ function (childrenCids, next) {+ privileges.categories.filterCids('find', _.uniq(_.flatten(childrenCids)), uid, next);},], callback);}diff --git a/src/topics/tags.js b/src/topics/tags.jsindex 4ad2b2981d..564fbf034d 100644--- a/src/topics/tags.js+++ b/src/topics/tags.js@@ -255,9 +255,7 @@ module.exports = function (Topics) {};Topics.getTopicsTags = function (tids, callback) {- var keys = tids.map(function (tid) {- return 'topic:' + tid + ':tags';- });+ const keys = tids.map(tid => 'topic:' + tid + ':tags');db.getSetsMembers(keys, callback);};@@ -268,9 +266,7 @@ module.exports = function (Topics) {};Topics.getTopicsTagsObjects = function (tids, callback) {- var sets = tids.map(function (tid) {- return 'topic:' + tid + ':tags';- });+ const sets = tids.map(tid => 'topic:' + tid + ':tags');var uniqueTopicTags;var topicTags;async.waterfall([diff --git a/src/views/admin/settings/general.tpl b/src/views/admin/settings/general.tplindex d945c56e05..73f9365ab0 100644--- a/src/views/admin/settings/general.tpl+++ b/src/views/admin/settings/general.tpl@@ -125,10 +125,12 @@<select id="post-sort-by" class="form-control" data-field="searchDefaultSortBy"><option value="relevance">[[search:relevance]]</option><option value="timestamp">[[search:post-time]]</option>- <option value="teaser.timestamp">[[search:last-reply-time]]</option>+ <option value="votes">[[search:votes]]</option>+ <option value="topic.lastposttime">[[search:last-reply-time]]</option><option value="topic.title">[[search:topic-title]]</option><option value="topic.postcount">[[search:number-of-replies]]</option><option value="topic.viewcount">[[search:number-of-views]]</option>+ <option value="topic.votes">[[search:topic-votes]]</option><option value="topic.timestamp">[[search:topic-start-date]]</option><option value="user.username">[[search:username]]</option><option value="category.name">[[search:category]]</option>