mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-30 18:46:01 +01:00 
			
		
		
		
	fix: #11531, fix teasers
1. with scheduled topics, pid is no longer reliable, lower pid can have higher timestamp(scheduled in the future) so use timestamp for sorting teasers 2. when restoring/deleting topics, update the teaser tid as the last step because it checks topicData.deleted
This commit is contained in:
		| @@ -131,7 +131,7 @@ module.exports = function (Categories) { | |||||||
| 		categories.forEach((category) => { | 		categories.forEach((category) => { | ||||||
| 			if (category) { | 			if (category) { | ||||||
| 				category.posts = topics.filter(t => t.cid && (t.cid === category.cid || t.parentCids.includes(category.cid))) | 				category.posts = topics.filter(t => t.cid && (t.cid === category.cid || t.parentCids.includes(category.cid))) | ||||||
| 					.sort((a, b) => b.pid - a.pid) | 					.sort((a, b) => b.timestamp - a.timestamp) | ||||||
| 					.slice(0, parseInt(category.numRecentReplies, 10)); | 					.slice(0, parseInt(category.numRecentReplies, 10)); | ||||||
| 			} | 			} | ||||||
| 		}); | 		}); | ||||||
| @@ -147,7 +147,7 @@ module.exports = function (Categories) { | |||||||
| 				const posts = []; | 				const posts = []; | ||||||
| 				getPostsRecursive(category, posts); | 				getPostsRecursive(category, posts); | ||||||
|  |  | ||||||
| 				posts.sort((a, b) => b.pid - a.pid); | 				posts.sort((a, b) => b.timestamp - a.timestamp); | ||||||
| 				if (posts.length) { | 				if (posts.length) { | ||||||
| 					category.posts = [posts[0]]; | 					category.posts = [posts[0]]; | ||||||
| 				} | 				} | ||||||
|   | |||||||
| @@ -11,44 +11,40 @@ const batch = require('../batch'); | |||||||
|  |  | ||||||
| module.exports = function (Topics) { | module.exports = function (Topics) { | ||||||
| 	Topics.delete = async function (tid, uid) { | 	Topics.delete = async function (tid, uid) { | ||||||
| 		await removeTopicPidsFromCid(tid); | 		const cid = await Topics.getTopicField(tid, 'cid'); | ||||||
|  | 		await removeTopicPidsFromCid(tid, cid); | ||||||
| 		await Topics.setTopicFields(tid, { | 		await Topics.setTopicFields(tid, { | ||||||
| 			deleted: 1, | 			deleted: 1, | ||||||
| 			deleterUid: uid, | 			deleterUid: uid, | ||||||
| 			deletedTimestamp: Date.now(), | 			deletedTimestamp: Date.now(), | ||||||
| 		}); | 		}); | ||||||
|  | 		await categories.updateRecentTidForCid(cid); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	async function removeTopicPidsFromCid(tid) { | 	async function removeTopicPidsFromCid(tid, cid) { | ||||||
| 		const [cid, pids] = await Promise.all([ | 		const pids = await Topics.getPids(tid); | ||||||
| 			Topics.getTopicField(tid, 'cid'), |  | ||||||
| 			Topics.getPids(tid), |  | ||||||
| 		]); |  | ||||||
| 		await db.sortedSetRemove(`cid:${cid}:pids`, pids); | 		await db.sortedSetRemove(`cid:${cid}:pids`, pids); | ||||||
| 		await categories.updateRecentTidForCid(cid); |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	async function addTopicPidsToCid(tid) { | 	async function addTopicPidsToCid(tid, cid) { | ||||||
| 		const [cid, pids] = await Promise.all([ | 		const pids = await Topics.getPids(tid); | ||||||
| 			Topics.getTopicField(tid, 'cid'), |  | ||||||
| 			Topics.getPids(tid), |  | ||||||
| 		]); |  | ||||||
| 		let postData = await posts.getPostsFields(pids, ['pid', 'timestamp', 'deleted']); | 		let postData = await posts.getPostsFields(pids, ['pid', 'timestamp', 'deleted']); | ||||||
| 		postData = postData.filter(post => post && !post.deleted); | 		postData = postData.filter(post => post && !post.deleted); | ||||||
| 		const pidsToAdd = postData.map(post => post.pid); | 		const pidsToAdd = postData.map(post => post.pid); | ||||||
| 		const scores = postData.map(post => post.timestamp); | 		const scores = postData.map(post => post.timestamp); | ||||||
| 		await db.sortedSetAdd(`cid:${cid}:pids`, scores, pidsToAdd); | 		await db.sortedSetAdd(`cid:${cid}:pids`, scores, pidsToAdd); | ||||||
| 		await categories.updateRecentTidForCid(cid); |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	Topics.restore = async function (tid) { | 	Topics.restore = async function (tid) { | ||||||
|  | 		const cid = await Topics.getTopicField(tid, 'cid'); | ||||||
| 		await Promise.all([ | 		await Promise.all([ | ||||||
| 			Topics.deleteTopicFields(tid, [ | 			Topics.deleteTopicFields(tid, [ | ||||||
| 				'deleterUid', 'deletedTimestamp', | 				'deleterUid', 'deletedTimestamp', | ||||||
| 			]), | 			]), | ||||||
| 			addTopicPidsToCid(tid), | 			addTopicPidsToCid(tid, cid), | ||||||
| 		]); | 		]); | ||||||
| 		await Topics.setTopicField(tid, 'deleted', 0); | 		await Topics.setTopicField(tid, 'deleted', 0); | ||||||
|  | 		await categories.updateRecentTidForCid(cid); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	Topics.purgePostsAndTopic = async function (tid, uid) { | 	Topics.purgePostsAndTopic = async function (tid, uid) { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user