mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-27 17:16:14 +01:00 
			
		
		
		
	refactor: suggest topics, use strings for tids
limit search results
This commit is contained in:
		| @@ -95,7 +95,7 @@ | |||||||
|         "nconf": "0.12.1", |         "nconf": "0.12.1", | ||||||
|         "nodebb-plugin-2factor": "7.5.3", |         "nodebb-plugin-2factor": "7.5.3", | ||||||
|         "nodebb-plugin-composer-default": "10.2.36", |         "nodebb-plugin-composer-default": "10.2.36", | ||||||
|         "nodebb-plugin-dbsearch": "6.2.3", |         "nodebb-plugin-dbsearch": "6.2.4", | ||||||
|         "nodebb-plugin-emoji": "5.1.15", |         "nodebb-plugin-emoji": "5.1.15", | ||||||
|         "nodebb-plugin-emoji-android": "4.0.0", |         "nodebb-plugin-emoji-android": "4.0.0", | ||||||
|         "nodebb-plugin-markdown": "12.2.6", |         "nodebb-plugin-markdown": "12.2.6", | ||||||
|   | |||||||
| @@ -6,23 +6,27 @@ const _ = require('lodash'); | |||||||
| const db = require('../database'); | const db = require('../database'); | ||||||
| const user = require('../user'); | const user = require('../user'); | ||||||
| const privileges = require('../privileges'); | const privileges = require('../privileges'); | ||||||
| const search = require('../search'); | const plugins = require('../plugins'); | ||||||
|  |  | ||||||
| module.exports = function (Topics) { | module.exports = function (Topics) { | ||||||
| 	Topics.getSuggestedTopics = async function (tid, uid, start, stop, cutoff = 0) { | 	Topics.getSuggestedTopics = async function (tid, uid, start, stop, cutoff = 0) { | ||||||
| 		let tids; | 		let tids; | ||||||
| 		tid = parseInt(tid, 10); | 		tid = parseInt(tid, 10); | ||||||
| 		cutoff = cutoff === 0 ? cutoff : (cutoff * 2592000000); | 		cutoff = cutoff === 0 ? cutoff : (cutoff * 2592000000); | ||||||
|  | 		const { cid, title, tags } = await Topics.getTopicFields(tid, [ | ||||||
|  | 			'cid', 'title', 'tags', | ||||||
|  | 		]); | ||||||
|  |  | ||||||
| 		const [tagTids, searchTids] = await Promise.all([ | 		const [tagTids, searchTids] = await Promise.all([ | ||||||
| 			getTidsWithSameTags(tid, cutoff), | 			getTidsWithSameTags(tid, tags.map(t => t.value), cutoff), | ||||||
| 			getSearchTids(tid, uid, cutoff), | 			getSearchTids(tid, title, cid, cutoff), | ||||||
| 		]); | 		]); | ||||||
|  |  | ||||||
| 		tids = _.uniq(tagTids.concat(searchTids)); | 		tids = _.uniq(tagTids.concat(searchTids)); | ||||||
|  |  | ||||||
| 		let categoryTids = []; | 		let categoryTids = []; | ||||||
| 		if (stop !== -1 && tids.length < stop - start + 1) { | 		if (stop !== -1 && tids.length < stop - start + 1) { | ||||||
| 			categoryTids = await getCategoryTids(tid, cutoff); | 			categoryTids = await getCategoryTids(tid, cid, cutoff); | ||||||
| 		} | 		} | ||||||
| 		tids = _.shuffle(_.uniq(tids.concat(categoryTids))); | 		tids = _.shuffle(_.uniq(tids.concat(categoryTids))); | ||||||
| 		tids = await privileges.topics.filterTids('topics:read', tids, uid); | 		tids = await privileges.topics.filterTids('topics:read', tids, uid); | ||||||
| @@ -36,36 +40,37 @@ module.exports = function (Topics) { | |||||||
| 		return topicData; | 		return topicData; | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	async function getTidsWithSameTags(tid, cutoff) { | 	async function getTidsWithSameTags(tid, tags, cutoff) { | ||||||
| 		const tags = await Topics.getTopicTags(tid); |  | ||||||
| 		let tids = cutoff === 0 ? | 		let tids = cutoff === 0 ? | ||||||
| 			await db.getSortedSetRevRange(tags.map(tag => `tag:${tag}:topics`), 0, -1) : | 			await db.getSortedSetRevRange(tags.map(tag => `tag:${tag}:topics`), 0, -1) : | ||||||
| 			await db.getSortedSetRevRangeByScore(tags.map(tag => `tag:${tag}:topics`), 0, -1, '+inf', Date.now() - cutoff); | 			await db.getSortedSetRevRangeByScore(tags.map(tag => `tag:${tag}:topics`), 0, -1, '+inf', Date.now() - cutoff); | ||||||
| 		tids = tids.filter(_tid => _tid !== tid); // remove self | 		tids = tids.filter(_tid => _tid !== tid); // remove self | ||||||
| 		return _.shuffle(_.uniq(tids)).slice(0, 10).map(Number); | 		return _.shuffle(_.uniq(tids)).slice(0, 10); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	async function getSearchTids(tid, uid, cutoff) { | 	async function getSearchTids(tid, title, cid, cutoff) { | ||||||
| 		const topicData = await Topics.getTopicFields(tid, ['title', 'cid']); | 		let { ids: tids } = await plugins.hooks.fire('filter:search.query', { | ||||||
| 		const data = await search.search({ | 			index: 'topic', | ||||||
| 			query: topicData.title, | 			content: title, | ||||||
| 			searchIn: 'titles', |  | ||||||
| 			matchWords: 'any', | 			matchWords: 'any', | ||||||
| 			categories: [topicData.cid], | 			cid: [cid], | ||||||
| 			uid: uid, | 			limit: 20, | ||||||
| 			returnIds: true, | 			ids: [], | ||||||
| 			timeRange: cutoff !== 0 ? cutoff / 1000 : 0, |  | ||||||
| 			timeFilter: 'newer', |  | ||||||
| 		}); | 		}); | ||||||
| 		data.tids = data.tids.filter(_tid => _tid !== tid); // remove self | 		tids = tids.filter(_tid => _tid !== tid); // remove self | ||||||
| 		return _.shuffle(data.tids).slice(0, 10).map(Number); | 		if (cutoff) { | ||||||
|  | 			const topicData = await Topics.getTopicsFields(tids, ['tid', 'timestamp']); | ||||||
|  | 			const now = Date.now(); | ||||||
|  | 			tids = topicData.filter(t => t && t.timestamp > now - cutoff).map(t => t.tid); | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		return _.shuffle(tids).slice(0, 10).map(String); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	async function getCategoryTids(tid, cutoff) { | 	async function getCategoryTids(tid, cid, cutoff) { | ||||||
| 		const cid = await Topics.getTopicField(tid, 'cid'); |  | ||||||
| 		const tids = cutoff === 0 ? | 		const tids = cutoff === 0 ? | ||||||
| 			await db.getSortedSetRevRange(`cid:${cid}:tids:lastposttime`, 0, 9) : | 			await db.getSortedSetRevRange(`cid:${cid}:tids:lastposttime`, 0, 9) : | ||||||
| 			await db.getSortedSetRevRangeByScore(`cid:${cid}:tids:lastposttime`, 0, 10, '+inf', Date.now() - cutoff); | 			await db.getSortedSetRevRangeByScore(`cid:${cid}:tids:lastposttime`, 0, 10, '+inf', Date.now() - cutoff); | ||||||
| 		return _.shuffle(tids.map(Number).filter(_tid => _tid !== tid)); | 		return _.shuffle(tids.filter(_tid => _tid !== tid)); | ||||||
| 	} | 	} | ||||||
| }; | }; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user