Skip to content

Node.js based forum software built for the modern web

feat: let plugins take part in notification merging (#14843)

palmoni56c9f017bc716
diff --git a/src/notifications.js b/src/notifications.js
index 48fc681572..14b6bee21f 100644
--- a/src/notifications.js
+++ b/src/notifications.js
@@ -509,18 +509,20 @@ Notifications.prune = async function () {
Notifications.merge = async function (notifications) {
// When passed a set of notification objects, merge any that can be merged
- const mergeIds = [
- 'notifications:upvoted-your-post-in',
- 'notifications:user-started-following-you',
- 'notifications:user-posted-to',
- 'notifications:user-flagged-post-in',
- 'notifications:user-flagged-user',
- 'new-chat',
- 'notifications:user-posted-in-public-room',
- 'new-register',
- 'post-queue',
- 'notifications:activitypub.announce',
- ];
+ const { mergeIds } = await plugins.hooks.fire('filter:notifications.mergeIds', {
+ mergeIds: [
+ 'notifications:upvoted-your-post-in',
+ 'notifications:user-started-following-you',
+ 'notifications:user-posted-to',
+ 'notifications:user-flagged-post-in',
+ 'notifications:user-flagged-user',
+ 'new-chat',
+ 'notifications:user-posted-in-public-room',
+ 'new-register',
+ 'post-queue',
+ 'notifications:activitypub.announce',
+ ],
+ });
notifications = mergeIds.reduce((notifications, mergeId) => {
const isolated = notifications.filter(n => n && n.hasOwnProperty('mergeId') && n.mergeId.split('|')[0] === mergeId);
@@ -551,6 +553,7 @@ Notifications.merge = async function (notifications) {
return notifications;
}
const notifObj = notifications[modifyIndex];
+ notifObj.mergeCount = set.length;
switch (mergeId) {
case 'new-chat': {
const { roomId, roomName, type, user } = set[0];
diff --git a/test/notifications.js b/test/notifications.js
index bd187430a9..b758139974 100644
--- a/test/notifications.js
+++ b/test/notifications.js
@@ -11,6 +11,7 @@ const user = require('../src/user');
const topics = require('../src/topics');
const categories = require('../src/categories');
const notifications = require('../src/notifications');
+const plugins = require('../src/plugins');
const socketNotifications = require('../src/socket.io/notifications');
const api = require('../src/api');
const utils = require('../src/utils');
@@ -395,4 +396,41 @@ describe('Notifications', () => {
const data = await notifications.get(notification.nid);
assert(!data);
});
+
+ describe('.merge()', () => {
+ it('should expose how many notifications were merged', async () => {
+ const merged = await notifications.merge([
+ { nid: 'n1', mergeId: 'new-register', bodyShort: 'one' },
+ { nid: 'n2', mergeId: 'new-register', bodyShort: 'two' },
+ { nid: 'n3', mergeId: 'new-register', bodyShort: 'three' },
+ ]);
+
+ assert.strictEqual(merged.length, 1);
+ assert.strictEqual(merged[0].mergeCount, 3);
+ });
+
+ it('should let a plugin register its own mergeId', async () => {
+ const method = async (data) => {
+ data.mergeIds.push('plugin-merge');
+ return data;
+ };
+ plugins.hooks.register('notifications-merge-test', { hook: 'filter:notifications.mergeIds', method });
+
+ let merged;
+ try {
+ merged = await notifications.merge([
+ { nid: 'p1', mergeId: 'plugin-merge|7', bodyShort: 'one' },
+ { nid: 'p2', mergeId: 'plugin-merge|7', bodyShort: 'two' },
+ { nid: 'p3', mergeId: 'unregistered-merge', bodyShort: 'three' },
+ ]);
+ } finally {
+ plugins.hooks.unregister('notifications-merge-test', 'filter:notifications.mergeIds', method);
+ }
+
+ assert.strictEqual(merged.length, 2);
+ const survivor = merged.find(n => n.mergeId === 'plugin-merge|7');
+ assert.strictEqual(survivor.bodyShort, 'one');
+ assert.strictEqual(survivor.mergeCount, 2);
+ });
+ });
});