Skip to content

Node.js based forum software built for the modern web

feat(crossposts): reconcile stale sort scores on topic read (#14681)

palmoni5aa52e783936d
diff --git a/src/controllers/topics.js b/src/controllers/topics.js
index c9af94546b..c3df881eab 100644
--- a/src/controllers/topics.js
+++ b/src/controllers/topics.js
@@ -2,6 +2,7 @@
const nconf = require('nconf');
const path = require('path');
+const winston = require('winston');
const qs = require('querystring');
const validator = require('validator');
@@ -155,6 +156,11 @@ topicsController.get = async function getTopic(req, res, next) {
topicData.author = author;
topicData.crossposts = crossposts;
topicData.canCrosspost = canCrosspost;
+
+ // not awaited on purpose so topic loading is not blocked
+ topics.crossposts.syncCrosspostedTopicCids(crossposts, topicData)
+ .catch(err => winston.error(err.stack));
+
topicData.pagination = pagination.create(currentPage, pageCount, req.query);
topicData.pagination.rel.forEach((rel) => {
rel.href = `${url}/topic/${topicData.slug}${rel.href}`;
diff --git a/src/topics/crossposts.js b/src/topics/crossposts.js
index 493413c7d1..7a2aa54309 100644
--- a/src/topics/crossposts.js
+++ b/src/topics/crossposts.js
@@ -55,6 +55,45 @@ Crossposts.get = async function (tids, uid) {
return isArray ? crossposts : crossposts[0];
};
+Crossposts.syncCrosspostedTopicCids = async function (crossposts, topicData) {
+ // `:tids:posts`, `:tids:votes` and `:tids:views` are only written for the topic's own
+ // category, so crossposted categories keep the values they were seeded with. Reconcile
+ // them when the topic is read, which is cheap and only runs for crossposted topics.
+ // Pinned topics are absent from these zsets by design, so they are skipped.
+ if (!crossposts.length || topicData.pinned) {
+ return;
+ }
+
+ const cids = crossposts.map(crosspost => crosspost.cid);
+ const count = cids.length;
+ const scores = await db.sortedSetsScore([
+ ...cids.map(cid => `cid:${cid}:tids:posts`),
+ ...cids.map(cid => `cid:${cid}:tids:votes`),
+ ...cids.map(cid => `cid:${cid}:tids:views`),
+ ], topicData.tid);
+
+ const postcounts = scores.slice(0, count);
+ const votes = scores.slice(count, count * 2);
+ const viewcounts = scores.slice(count * 2);
+
+ const bulkAdd = [];
+ cids.forEach((cid, index) => {
+ if (topicData.postcount !== postcounts[index]) {
+ bulkAdd.push([`cid:${cid}:tids:posts`, topicData.postcount, topicData.tid]);
+ }
+ if (topicData.votes !== votes[index]) {
+ bulkAdd.push([`cid:${cid}:tids:votes`, topicData.votes, topicData.tid]);
+ }
+ if (topicData.viewcount !== viewcounts[index]) {
+ bulkAdd.push([`cid:${cid}:tids:views`, topicData.viewcount, topicData.tid]);
+ }
+ });
+
+ if (bulkAdd.length) {
+ await db.sortedSetAddBulk(bulkAdd);
+ }
+};
+
Crossposts.add = async function (tid, cid, uid) {
/**
* NOTE: If uid is 0, the assumption is that it is a "system" crosspost, not a guest!
diff --git a/test/crossposts.js b/test/crossposts.js
index 712ea5101d..d34ffe0366 100644
--- a/test/crossposts.js
+++ b/test/crossposts.js
@@ -102,6 +102,51 @@ describe('Crossposts', () => {
assert.strictEqual(after, timestamp);
});
+ it('should reconcile stale post, vote and view scores in crossposted categories', async () => {
+ const { tid } = await createTopic();
+ await topics.crossposts.add(tid, targetCategory.cid, uid);
+
+ // posts/votes/views are only ever written for the topic's own category,
+ // so stand in for the drift that accumulates there
+ await db.sortedSetAddBulk([
+ [`cid:${targetCategory.cid}:tids:posts`, 0, tid],
+ [`cid:${targetCategory.cid}:tids:votes`, -5, tid],
+ [`cid:${targetCategory.cid}:tids:views`, 0, tid],
+ ]);
+
+ const [crossposts, topicData] = await Promise.all([
+ topics.crossposts.get(tid),
+ topics.getTopicData(tid),
+ ]);
+ await topics.crossposts.syncCrosspostedTopicCids(crossposts, topicData);
+
+ const scores = await db.sortedSetsScore([
+ `cid:${targetCategory.cid}:tids:posts`,
+ `cid:${targetCategory.cid}:tids:votes`,
+ `cid:${targetCategory.cid}:tids:views`,
+ ], tid);
+ assert.deepStrictEqual(scores, [topicData.postcount, topicData.votes, topicData.viewcount]);
+ });
+
+ it('should leave a pinned topic out of the reconciled zsets', async () => {
+ const { tid } = await createTopic();
+ await topics.tools.pin(tid, adminUid);
+ await topics.crossposts.add(tid, targetCategory.cid, uid);
+
+ const [crossposts, topicData] = await Promise.all([
+ topics.crossposts.get(tid),
+ topics.getTopicData(tid),
+ ]);
+ await topics.crossposts.syncCrosspostedTopicCids(crossposts, topicData);
+
+ const scores = await db.sortedSetsScore([
+ `cid:${targetCategory.cid}:tids:posts`,
+ `cid:${targetCategory.cid}:tids:votes`,
+ `cid:${targetCategory.cid}:tids:views`,
+ ], tid);
+ assert(scores.every(score => score === null), 'a pinned topic should not be re-added to these sets');
+ });
+
it('should remove the topic from the destination sets when uncrossposted', async () => {
const { tid } = await createTopic();
await topics.crossposts.add(tid, targetCategory.cid, uid);