mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 02:55:58 +01:00 
			
		
		
		
	refactor: change incrementViewCount and markAsRead to async/await
This commit is contained in:
		| @@ -1,7 +1,6 @@ | |||||||
| 'use strict'; | 'use strict'; | ||||||
|  |  | ||||||
| const nconf = require('nconf'); | const nconf = require('nconf'); | ||||||
| const winston = require('winston'); |  | ||||||
|  |  | ||||||
| const user = require('../user'); | const user = require('../user'); | ||||||
| const meta = require('../meta'); | const meta = require('../meta'); | ||||||
| @@ -90,6 +89,9 @@ topicsController.get = async function getTopic(req, res, callback) { | |||||||
| 	await Promise.all([ | 	await Promise.all([ | ||||||
| 		buildBreadcrumbs(topicData), | 		buildBreadcrumbs(topicData), | ||||||
| 		addTags(topicData, req, res), | 		addTags(topicData, req, res), | ||||||
|  | 		incrementViewCount(req, tid), | ||||||
|  | 		markAsRead(req, tid), | ||||||
|  | 		analytics.increment(['pageviews:byCid:' + topicData.category.cid]), | ||||||
| 	]); | 	]); | ||||||
|  |  | ||||||
| 	topicData.pagination = pagination.create(currentPage, pageCount, req.query); | 	topicData.pagination = pagination.create(currentPage, pageCount, req.query); | ||||||
| @@ -98,12 +100,6 @@ topicsController.get = async function getTopic(req, res, callback) { | |||||||
| 		res.locals.linkTags.push(rel); | 		res.locals.linkTags.push(rel); | ||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
| 	incrementViewCount(req, tid); |  | ||||||
|  |  | ||||||
| 	markAsRead(req, tid); |  | ||||||
|  |  | ||||||
| 	analytics.increment(['pageviews:byCid:' + topicData.category.cid]); |  | ||||||
|  |  | ||||||
| 	res.render('topic', topicData); | 	res.render('topic', topicData); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| @@ -126,27 +122,24 @@ 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) }; | ||||||
| } | } | ||||||
|  |  | ||||||
| function incrementViewCount(req, tid) { | async function incrementViewCount(req, tid) { | ||||||
| 	if (req.uid >= 1) { | 	if (req.uid >= 1) { | ||||||
| 		req.session.tids_viewed = req.session.tids_viewed || {}; | 		req.session.tids_viewed = req.session.tids_viewed || {}; | ||||||
| 		if (!req.session.tids_viewed[tid] || req.session.tids_viewed[tid] < Date.now() - 3600000) { | 		if (!req.session.tids_viewed[tid] || req.session.tids_viewed[tid] < Date.now() - 3600000) { | ||||||
| 			topics.increaseViewCount(tid); | 			await topics.increaseViewCount(tid); | ||||||
| 			req.session.tids_viewed[tid] = Date.now(); | 			req.session.tids_viewed[tid] = Date.now(); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| function markAsRead(req, tid) { | async function markAsRead(req, tid) { | ||||||
| 	if (req.loggedIn) { | 	if (req.loggedIn) { | ||||||
| 		topics.markAsRead([tid], req.uid, function (err, markedRead) { | 		const markedRead = await topics.markAsRead([tid], req.uid); | ||||||
| 			if (err) { | 		const promises = [topics.markTopicNotificationsRead([tid], req.uid)]; | ||||||
| 				return winston.error(err.stack); | 		if (markedRead) { | ||||||
| 			} | 			promises.push(topics.pushUnreadCount(req.uid)); | ||||||
| 			if (markedRead) { | 		} | ||||||
| 				topics.pushUnreadCount(req.uid); | 		await Promise.all(promises); | ||||||
| 				topics.markTopicNotificationsRead([tid], req.uid); |  | ||||||
| 			} |  | ||||||
| 		}); |  | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -10,11 +10,11 @@ module.exports = function (SocketTopics) { | |||||||
| 			throw new Error('[[error:invalid-data]]'); | 			throw new Error('[[error:invalid-data]]'); | ||||||
| 		} | 		} | ||||||
| 		const hasMarked = await topics.markAsRead(tids, socket.uid); | 		const hasMarked = await topics.markAsRead(tids, socket.uid); | ||||||
|  | 		const promises = [topics.markTopicNotificationsRead(tids, socket.uid)]; | ||||||
| 		if (hasMarked) { | 		if (hasMarked) { | ||||||
| 			topics.pushUnreadCount(socket.uid); | 			promises.push(topics.pushUnreadCount(socket.uid)); | ||||||
|  |  | ||||||
| 			topics.markTopicNotificationsRead(tids, socket.uid); |  | ||||||
| 		} | 		} | ||||||
|  | 		await Promise.all(promises); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	SocketTopics.markTopicNotificationsRead = async function (socket, tids) { | 	SocketTopics.markTopicNotificationsRead = async function (socket, tids) { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user