From 14bb0a446917e2f45bdb4c42f8d22e5e3a0df3df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Mon, 30 Nov 2020 18:09:30 -0500 Subject: [PATCH] feat: #8960, update view count after merge --- src/topics/merge.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/topics/merge.js b/src/topics/merge.js index b3f5718422..2688ef69fc 100644 --- a/src/topics/merge.js +++ b/src/topics/merge.js @@ -32,7 +32,14 @@ module.exports = function (Topics) { }); }); - plugins.hooks.fire('action:topic.merge', { uid: uid, tids: tids, mergeIntoTid: mergeIntoTid, otherTids: otherTids }); + await updateViewCount(mergeIntoTid, tids); + + plugins.hooks.fire('action:topic.merge', { + uid: uid, + tids: tids, + mergeIntoTid: mergeIntoTid, + otherTids: otherTids, + }); return mergeIntoTid; }; @@ -46,6 +53,14 @@ module.exports = function (Topics) { return tid; } + async function updateViewCount(mergeIntoTid, tids) { + const topicData = await Topics.getTopicsFields(tids, ['viewcount']); + const totalViewCount = topicData.reduce( + (count, topic) => count + parseInt(topic.viewcount, 10), 0 + ); + await Topics.setTopicField(mergeIntoTid, 'viewcount', totalViewCount); + } + function findOldestTopic(tids) { return Math.min.apply(null, tids); }