Skip to content

Node.js based forum software built for the modern web

fix: dont allow returning notifications by nid

Barış Soner Uşaklıfb3a90506fb6
diff --git a/src/user/notifications.js b/src/user/notifications.js
index fd47cf1122..b56668b21f 100644
--- a/src/user/notifications.js
+++ b/src/user/notifications.js
@@ -103,9 +103,10 @@ UserNotifications.getNotifications = async function (nids, uid) {
return [];
}
- const [notifObjs, hasRead, userSettings] = await Promise.all([
+ const [notifObjs, isRead, isUnread, userSettings] = await Promise.all([
notifications.getMultiple(nids),
db.isSortedSetMembers(`uid:${uid}:notifications:read`, nids),
+ db.isSortedSetMembers(`uid:${uid}:notifications:unread`, nids),
user.getSettings(uid),
]);
@@ -115,11 +116,11 @@ UserNotifications.getNotifications = async function (nids, uid) {
deletedNids.push(nids[index]);
}
if (notification) {
- notification.read = hasRead[index];
+ notification.read = isRead[index];
notification.readClass = !notification.read ? 'unread' : '';
}
-
- return notification;
+ const isUsersNotification = isRead[index] || isUnread[index];
+ return notification && isUsersNotification;
});
await deleteUserNids(deletedNids, uid);
diff --git a/test/notifications.js b/test/notifications.js
index dcfb5ad588..3730a9886c 100644
--- a/test/notifications.js
+++ b/test/notifications.js
@@ -12,6 +12,7 @@ const topics = require('../src/topics');
const categories = require('../src/categories');
const notifications = require('../src/notifications');
const socketNotifications = require('../src/socket.io/notifications');
+const api = require('../src/api');
const sleep = util.promisify(setTimeout);
@@ -212,10 +213,15 @@ describe('Notifications', () => {
});
it('should get notification by nid', async () => {
- const data = await socketNotifications.get({ uid: uid }, { nids: [notification.nid] });
- assert.equal(data[0].bodyShort, 'bodyShort');
- assert.equal(data[0].nid, 'notification_id');
- assert.equal(data[0].path, `${nconf.get('relative_path')}/notification/path`);
+ const [notifObj] = await socketNotifications.get({ uid: uid }, { nids: [notification.nid] });
+ assert.equal(notifObj.bodyShort, 'bodyShort');
+ assert.equal(notifObj.nid, 'notification_id');
+ assert.equal(notifObj.path, `${nconf.get('relative_path')}/notification/path`);
+ });
+
+ it('should not return another user\'s notification by nid', async () => {
+ const notifObj = await api.notifications.get({ uid: 0 }, { nid: notification.nid });
+ assert.deepStrictEqual(notifObj, { notification: undefined });
});
it('should get user\'s notifications', async () => {