Skip to content

Node.js based forum software built for the modern web

fix: unread notification count includes notifications hidden from the list (#14997)

palmoni53a613d2ecf05
diff --git a/src/user/notifications.js b/src/user/notifications.js
index 4a4bb03b70..5b2eb64ea3 100644
--- a/src/user/notifications.js
+++ b/src/user/notifications.js
@@ -184,7 +184,12 @@ UserNotifications.getUnreadCount = async function (uid) {
let nids = await db.getSortedSetRevRange(`uid:${uid}:notifications:unread`, 0, 99);
nids = await notifications.filterExists(nids);
const keys = nids.map(nid => `notifications:${nid}`);
- const notifData = await db.getObjectsFields(keys, ['mergeId']);
+ let notifData = await db.getObjectsFields(keys, ['nid', 'mergeId', 'pid']);
+ notifData = notifData.filter(n => n && n.nid);
+
+ const pids = _.uniq(notifData.filter(n => n.pid).map(n => String(n.pid)));
+ const visiblePids = new Set(await privileges.posts.filter('topics:read', pids, uid));
+ notifData = notifData.filter(n => !n.pid || visiblePids.has(String(n.pid)));
const mergeIds = notifData.map(n => n.mergeId);
// Collapse any notifications with identical mergeIds
diff --git a/test/notifications.js b/test/notifications.js
index bea30e7332..28167735de 100644
--- a/test/notifications.js
+++ b/test/notifications.js
@@ -22,9 +22,17 @@ const sleep = util.promisify(setTimeout);
describe('Notifications', () => {
let uid;
let notification;
+ let pid;
before(async () => {
uid = await user.create({ username: 'poster' });
+ const { cid } = await categories.create({ name: 'Notification Category' });
+ ({ postData: { pid } } = await topics.post({
+ uid: uid,
+ cid: cid,
+ title: 'Notification Topic Title',
+ content: 'The content of notification topic',
+ }));
});
it('should fail to create notification without a nid', (done) => {
@@ -39,7 +47,7 @@ describe('Notifications', () => {
bodyShort: 'bodyShort',
nid: 'notification_id',
path: '/notification/path',
- pid: 1,
+ pid: pid,
});
assert(notification);
@@ -83,7 +91,7 @@ describe('Notifications', () => {
bodyShort: 'bodyShort',
nid: 'notification_id',
path: '/notification/path',
- pid: 1,
+ pid: pid,
importance: 1,
});
assert.strictEqual(notification, null);
@@ -296,6 +304,28 @@ describe('Notifications', () => {
assert.equal(count, 0);
});
+ it('should not count unread notifications that are not shown in the list', async () => {
+ const ownerUid = await user.create({ username: 'countowner' });
+ const { cid } = await categories.create({ name: 'Count Category' });
+ const { postData } = await topics.post({
+ uid: uid,
+ cid: cid,
+ title: 'Count Topic Title',
+ content: 'The content of count topic',
+ });
+ const visible = await notifications.create({ bodyShort: 'visible', nid: 'count:visible' });
+ const purged = await notifications.create({ bodyShort: 'purged', nid: 'count:purged', pid: postData.pid });
+ await notifications.push(visible, [ownerUid]);
+ await notifications.push(purged, [ownerUid]);
+ await sleep(2000);
+ assert.strictEqual(await user.notifications.getUnreadCount(ownerUid), 2);
+
+ await topics.purgePostsAndTopic(postData.tid, uid);
+ const { unread } = await user.notifications.get(ownerUid);
+ assert.strictEqual(unread.length, 1);
+ assert.strictEqual(await user.notifications.getUnreadCount(ownerUid), 1);
+ });
+
it('should not do anything if uid is falsy', async () => {
await user.notifications.deleteAll(0);
});