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

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

Loading…
Cancel
Save