mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +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),
|
||||
addOldCategory(topicData, userPrivileges),
|
||||
addTags(topicData, req, res, currentPage),
|
||||
incrementViewCount(req, tid),
|
||||
topics.increaseViewCount(req, tid),
|
||||
markAsRead(req, tid),
|
||||
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) };
|
||||
}
|
||||
|
||||
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) {
|
||||
if (req.loggedIn) {
|
||||
const markedRead = await topics.markAsRead([tid], req.uid);
|
||||
|
||||
@@ -296,13 +296,22 @@ module.exports = function (Topics) {
|
||||
incrementFieldAndUpdateSortedSet(tid, 'postcount', -1, 'topics:posts');
|
||||
};
|
||||
|
||||
Topics.increaseViewCount = async function (tid) {
|
||||
const cid = await Topics.getTopicField(tid, 'cid');
|
||||
incrementFieldAndUpdateSortedSet(tid, 'viewcount', 1, ['topics:views', `cid:${cid}:tids:views`]);
|
||||
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');
|
||||
incrementFieldAndUpdateSortedSet(tid, 'viewcount', 1, ['topics:views', `cid:${cid}:tids:views`]);
|
||||
req.session.tids_viewed[tid] = now;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user