Merge remote-tracking branch 'refs/remotes/origin/master' into develop

v1.18.x
Barış Soner Uşaklı 7 years ago
commit 958350fa6f

@ -42,7 +42,7 @@ function searchInContent(data, callback) {
var matchCount = 0;
var pids;
var metadata;
var itemsPerPage = data.itemsPerPage || 10;
async.waterfall([
function (next) {
async.parallel({
@ -99,8 +99,8 @@ function searchInContent(data, callback) {
matchCount = metadata.pids.length;
if (data.page) {
var start = Math.max(0, (data.page - 1)) * 10;
metadata.pids = metadata.pids.slice(start, start + 10);
var start = Math.max(0, (data.page - 1)) * itemsPerPage;
metadata.pids = metadata.pids.slice(start, start + itemsPerPage);
}
posts.getPostSummaryByPids(metadata.pids, data.uid, {}, next);
@ -108,7 +108,11 @@ function searchInContent(data, callback) {
function (posts, next) {
// Append metadata to returned payload (without pids)
delete metadata.pids;
next(null, Object.assign({ posts: posts, matchCount: matchCount, pageCount: Math.max(1, Math.ceil(parseInt(matchCount, 10) / 10)) }, metadata));
next(null, Object.assign({
posts: posts,
matchCount: matchCount,
pageCount: Math.max(1, Math.ceil(parseInt(matchCount, 10) / 10)),
}, metadata));
},
], callback);
}

@ -4,43 +4,50 @@
var async = require('async');
var _ = require('lodash');
var categories = require('../categories');
var privileges = require('../privileges');
var search = require('../search');
module.exports = function (Topics) {
Topics.getSuggestedTopics = function (tid, uid, start, stop, callback) {
var tids;
async.waterfall([
function (next) {
async.parallel({
tagTids: function (next) {
getTidsWithSameTags(tid, next);
getTidsWithSameTags(tid, uid, next);
},
searchTids: function (next) {
getSearchTids(tid, next);
},
categoryTids: function (next) {
getCategoryTids(tid, next);
getSearchTids(tid, uid, next);
},
}, next);
},
function (results, next) {
var tids = _.shuffle(_.uniq(results.tagTids.concat(results.searchTids).concat(results.categoryTids)));
tids = results.tagTids.concat(results.searchTids);
tids = tids.filter(function (_tid) {
return parseInt(_tid, 10) !== parseInt(tid, 10);
});
tids = _.shuffle(_.uniq(tids));
if (stop !== -1 && tids.length < stop - start + 1) {
getCategoryTids(tid, uid, next);
} else {
next(null, []);
}
},
function (categoryTids, next) {
tids = _.uniq(tids.concat(categoryTids));
if (stop === -1) {
tids = tids.slice(start);
} else {
tids = tids.slice(start, stop + 1);
}
Topics.getTopics(tids, uid, next);
Topics.getTopicsByTids(tids, uid, next);
},
], callback);
};
function getTidsWithSameTags(tid, callback) {
function getTidsWithSameTags(tid, uid, callback) {
async.waterfall([
function (next) {
Topics.getTopicTags(tid, next);
@ -53,31 +60,52 @@ module.exports = function (Topics) {
function (data, next) {
next(null, _.uniq(_.flatten(data)));
},
function (tids, next) {
tids = tids.map(Number);
privileges.topics.filterTids('read', tids, uid, next);
},
], callback);
}
function getSearchTids(tid, callback) {
function getSearchTids(tid, uid, callback) {
async.waterfall([
function (next) {
Topics.getTopicFields(tid, ['title', 'cid'], next);
},
function (topicData, next) {
search.searchQuery('topic', topicData.title, [topicData.cid], [], next);
search.search({
query: topicData.title,
searchIn: 'titles',
categories: [topicData.cid],
uid: uid,
page: 1,
itemsPerPage: 20,
}, next);
},
function (data, next) {
var tids = data.posts.map(function (post) {
return post && parseInt(post.tid, 10);
});
next(null, tids);
},
], callback);
}
function getCategoryTids(tid, callback) {
function getCategoryTids(tid, uid, callback) {
async.waterfall([
function (next) {
Topics.getTopicField(tid, 'cid', next);
},
function (cid, next) {
categories.getTopicIds({
cid: cid,
start: 0,
stop: 9,
}, next);
Topics.getRecentTopics(cid, uid, 0, 9, '', next);
},
function (data, next) {
var tids = data.topics.filter(function (topic) {
return topic && !topic.deleted;
}).map(function (topic) {
return topic && parseInt(topic.tid, 10);
});
next(null, tids);
},
], callback);
}

Loading…
Cancel
Save