Skip to content

Node.js based forum software built for the modern web

feat(topic-events): clear out topic events when a topic is purged

Julian Lam0d4a3775589f
diff --git a/src/topics/delete.js b/src/topics/delete.js
index 5c3ec308be..38c6ec4473 100644
--- a/src/topics/delete.js
+++ b/src/topics/delete.js
@@ -98,6 +98,7 @@ module.exports = function (Topics) {
], tid),
deleteTopicFromCategoryAndUser(tid),
Topics.deleteTopicTags(tid),
+ Topics.events.purge(tid),
reduceCounters(tid),
]);
plugins.hooks.fire('action:topic.purge', { topic: deletedTopic, uid: uid });
diff --git a/src/topics/events.js b/src/topics/events.js
index dc5d80906f..64cae15ce8 100644
--- a/src/topics/events.js
+++ b/src/topics/events.js
@@ -129,3 +129,12 @@ Events.log = async (tid, payload) => {
({ events } = await plugins.hooks.fire('filter:topic.events.log', { events }));
return events;
};
+
+Events.purge = async (tid) => {
+ // Should only be called on topic purge
+ const keys = [`topic:${tid}:events`];
+ const eventIds = await db.getSortedSetRange(keys[0], 0, -1);
+ keys.push(...eventIds.map(id => `topicEvent:${id}`));
+
+ db.deleteAll(keys);
+};