mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 11:05:54 +01:00 
			
		
		
		
	fix: simplify increasePostCount logic by combining methods
This commit is contained in:
		| @@ -126,7 +126,7 @@ topicsController.get = async function getTopic(req, res, next) { | |||||||
| 		buildBreadcrumbs(topicData), | 		buildBreadcrumbs(topicData), | ||||||
| 		addOldCategory(topicData, userPrivileges), | 		addOldCategory(topicData, userPrivileges), | ||||||
| 		addTags(topicData, req, res, currentPage), | 		addTags(topicData, req, res, currentPage), | ||||||
| 		incrementViewCount(req, tid), | 		topics.increaseViewCount(req, tid), | ||||||
| 		markAsRead(req, tid), | 		markAsRead(req, tid), | ||||||
| 		analytics.increment([`pageviews:byCid:${topicData.category.cid}`]), | 		analytics.increment([`pageviews:byCid:${topicData.category.cid}`]), | ||||||
| 	]); | 	]); | ||||||
| @@ -164,19 +164,6 @@ function calculateStartStop(page, postIndex, settings) { | |||||||
| 	return { start: Math.max(0, start), stop: Math.max(0, stop) }; | 	return { start: Math.max(0, start), stop: Math.max(0, stop) }; | ||||||
| } | } | ||||||
|  |  | ||||||
| async function incrementViewCount(req, tid) { |  | ||||||
| 	const allow = req.uid > 0 || (meta.config.guestsIncrementTopicViews && req.uid === 0); |  | ||||||
| 	if (allow) { |  | ||||||
| 		req.session.tids_viewed = req.session.tids_viewed || {}; |  | ||||||
| 		const now = Date.now(); |  | ||||||
| 		const interval = meta.config.incrementTopicViewsInterval * 60000; |  | ||||||
| 		if (!req.session.tids_viewed[tid] || req.session.tids_viewed[tid] < now - interval) { |  | ||||||
| 			await topics.increaseViewCount(tid); |  | ||||||
| 			req.session.tids_viewed[tid] = now; |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
|  |  | ||||||
| async function markAsRead(req, tid) { | async function markAsRead(req, tid) { | ||||||
| 	if (req.loggedIn) { | 	if (req.loggedIn) { | ||||||
| 		const markedRead = await topics.markAsRead([tid], req.uid); | 		const markedRead = await topics.markAsRead([tid], req.uid); | ||||||
|   | |||||||
| @@ -296,13 +296,22 @@ module.exports = function (Topics) { | |||||||
| 		incrementFieldAndUpdateSortedSet(tid, 'postcount', -1, 'topics:posts'); | 		incrementFieldAndUpdateSortedSet(tid, 'postcount', -1, 'topics:posts'); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	Topics.increaseViewCount = async function (tid) { | 	Topics.increaseViewCount = async function (req, tid) { | ||||||
|  | 		const allow = req.uid > 0 || (meta.config.guestsIncrementTopicViews && req.uid === 0); | ||||||
|  | 		if (allow) { | ||||||
|  | 			req.session.tids_viewed = req.session.tids_viewed || {}; | ||||||
|  | 			const now = Date.now(); | ||||||
|  | 			const interval = meta.config.incrementTopicViewsInterval * 60000; | ||||||
|  | 			if (!req.session.tids_viewed[tid] || req.session.tids_viewed[tid] < now - interval) { | ||||||
| 				const cid = await Topics.getTopicField(tid, 'cid'); | 				const cid = await Topics.getTopicField(tid, 'cid'); | ||||||
| 				incrementFieldAndUpdateSortedSet(tid, 'viewcount', 1, ['topics:views', `cid:${cid}:tids:views`]); | 				incrementFieldAndUpdateSortedSet(tid, 'viewcount', 1, ['topics:views', `cid:${cid}:tids:views`]); | ||||||
|  | 				req.session.tids_viewed[tid] = now; | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	async function incrementFieldAndUpdateSortedSet(tid, field, by, set) { | 	async function incrementFieldAndUpdateSortedSet(tid, field, by, set) { | ||||||
| 		const value = await db.incrObjectFieldBy(`topic:${tid}`, field, by); | 		const value = await db.incrObjectFieldBy(`${activitypub.helpers.isUri(tid) ? 'topicRemote' : 'topic'}:${tid}`, field, by); | ||||||
| 		await db[Array.isArray(set) ? 'sortedSetsAdd' : 'sortedSetAdd'](set, value, tid); | 		await db[Array.isArray(set) ? 'sortedSetsAdd' : 'sortedSetAdd'](set, value, tid); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user