Skip to content

Node.js based forum software built for the modern web

feat: generic bodyShort for merged notifications with a plugin-registered mergeId (#14886)

palmoni5d3c5e1d5cf75
diff --git a/public/language/en-GB/notifications.json b/public/language/en-GB/notifications.json
index 2226ac5356..be1b7e6358 100644
--- a/public/language/en-GB/notifications.json
+++ b/public/language/en-GB/notifications.json
@@ -87,6 +87,7 @@
"post-queue-rejected-for-reason": "Your queued post has been rejected for the following reason: \"%1\"",
"post-queue-notify": "Queued post received a notification: \"%1\"",
"post-queue-multiple": "There are <strong>%1</strong> posts awaiting review.",
+ "merged-notifications": "You have <strong>%1</strong> new notifications.",
"email-confirmed": "Email Confirmed",
"email-confirmed-message": "Thank you for validating your email. Your account is now fully activated.",
diff --git a/src/notifications.js b/src/notifications.js
index 32c4e4672f..1f7121a9c8 100644
--- a/src/notifications.js
+++ b/src/notifications.js
@@ -595,6 +595,10 @@ Notifications.merge = async function (notifications) {
case 'post-queue':
notifObj.bodyShort = `[[notifications:${mergeId}-multiple, ${set.length}]]`;
break;
+
+ default:
+ notifObj.bodyShort = `[[notifications:merged-notifications, ${set.length}]]`;
+ break;
}
// Filter out duplicates
diff --git a/test/notifications.js b/test/notifications.js
index e136fcf1a0..bea30e7332 100644
--- a/test/notifications.js
+++ b/test/notifications.js
@@ -429,7 +429,7 @@ describe('Notifications', () => {
assert.strictEqual(merged.length, 2);
const survivor = merged.find(n => n.mergeId === 'plugin-merge|7');
- assert.strictEqual(survivor.bodyShort, 'one');
+ assert.strictEqual(survivor.bodyShort, '[[notifications:merged-notifications, 2]]');
assert.strictEqual(survivor.mergeCount, 2);
});
@@ -454,10 +454,64 @@ describe('Notifications', () => {
assert.strictEqual(merged.length, 2);
const bare = merged.find(n => n.mergeId === 'plugin-merge');
- assert.strictEqual(bare.bodyShort, 'bare one');
+ assert.strictEqual(bare.bodyShort, '[[notifications:merged-notifications, 2]]');
assert.strictEqual(bare.mergeCount, 2);
const differentiated = merged.find(n => n.mergeId === 'plugin-merge|7');
assert.strictEqual(differentiated.mergeCount, 2);
});
+
+ it('should give a plugin-registered mergeId a generic bodyShort with the merged count', 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: 'g1', mergeId: 'plugin-merge|3', bodyShort: 'first' },
+ { nid: 'g2', mergeId: 'plugin-merge|3', bodyShort: 'second' },
+ { nid: 'g3', mergeId: 'plugin-merge|3', bodyShort: 'third' },
+ ]);
+ } finally {
+ plugins.hooks.unregister('notifications-merge-test', 'filter:notifications.mergeIds', method);
+ }
+
+ assert.strictEqual(merged.length, 1);
+ assert.strictEqual(merged[0].mergeCount, 3);
+ assert.strictEqual(merged[0].bodyShort, '[[notifications:merged-notifications, 3]]');
+ });
+
+ it('should let filter:notifications.merge override the generic bodyShort', async () => {
+ const mergeIdsMethod = async (data) => {
+ data.mergeIds.push('plugin-merge');
+ return data;
+ };
+ const mergeMethod = async (data) => {
+ data.notifications.forEach((n) => {
+ if (n.mergeId === 'plugin-merge') {
+ n.bodyShort = `custom ${n.mergeCount}`;
+ }
+ });
+ return data;
+ };
+ plugins.hooks.register('notifications-merge-test', { hook: 'filter:notifications.mergeIds', method: mergeIdsMethod });
+ plugins.hooks.register('notifications-merge-test', { hook: 'filter:notifications.merge', method: mergeMethod });
+
+ let merged;
+ try {
+ merged = await notifications.merge([
+ { nid: 'o1', mergeId: 'plugin-merge', bodyShort: 'first' },
+ { nid: 'o2', mergeId: 'plugin-merge', bodyShort: 'second' },
+ ]);
+ } finally {
+ plugins.hooks.unregister('notifications-merge-test', 'filter:notifications.mergeIds', mergeIdsMethod);
+ plugins.hooks.unregister('notifications-merge-test', 'filter:notifications.merge', mergeMethod);
+ }
+
+ assert.strictEqual(merged.length, 1);
+ assert.strictEqual(merged[0].bodyShort, 'custom 2');
+ });
});
});