From c38edb1fd4a8e78480ff1f81277ba8280a392de4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 2 Feb 2018 11:55:55 -0500 Subject: [PATCH] more changes to suggested topics --- src/search.js | 12 +++++--- src/topics/suggested.js | 62 ++++++++++++++++++++++++++++++----------- 2 files changed, 53 insertions(+), 21 deletions(-) diff --git a/src/search.js b/src/search.js index 4d2560e6cf..893731d473 100644 --- a/src/search.js +++ b/src/search.js @@ -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); } diff --git a/src/topics/suggested.js b/src/topics/suggested.js index 0c6ae1a661..5a89968342 100644 --- a/src/topics/suggested.js +++ b/src/topics/suggested.js @@ -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); }