|
|
|
@ -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);
|
|
|
|
|
}
|
|
|
|
|