feat: only mark notifications read that match current filter

closes #13574
This commit is contained in:
Barış Soner Uşaklı
2025-08-05 17:51:53 -04:00
parent e1423636a5
commit 9d39ed512f
4 changed files with 29 additions and 10 deletions

View File

@@ -13,7 +13,7 @@ define('forum/notifications', ['components', 'notifications'], function (compone
notifications.handleUnreadButton(listEl); notifications.handleUnreadButton(listEl);
components.get('notifications/mark_all').on('click', function () { components.get('notifications/mark_all').on('click', function () {
notifications.markAllRead(function () { notifications.markAllRead(ajaxify.data.selectedFilter.filter, function () {
components.get('notifications/item').removeClass('unread'); components.get('notifications/item').removeClass('unread');
}); });
}); });

View File

@@ -105,7 +105,7 @@ define('notifications', [
}); });
if (!unreadNotifs[notifData.nid]) { if (!unreadNotifs[notifData.nid]) {
unreadNotifs[notifData.nid] = true; unreadNotifs[notifData.nid] = notifData;
} }
}; };
@@ -165,12 +165,21 @@ define('notifications', [
} }
}; };
Notifications.markAllRead = function () { Notifications.markAllRead = function (filter = '') {
socket.emit('notifications.markAllRead', function (err) { socket.emit('notifications.markAllRead', { filter }, function (err) {
if (err) { if (err) {
alerts.error(err); alerts.error(err);
} }
unreadNotifs = {}; if (filter) {
Object.keys(unreadNotifs).forEach(nid => {
if (unreadNotifs[nid].type === filter) {
delete unreadNotifs[nid];
}
});
} else {
unreadNotifs = {};
}
const notifEls = $('[component="notifications/list"] [data-nid]'); const notifEls = $('[component="notifications/list"] [data-nid]');
notifEls.removeClass('unread'); notifEls.removeClass('unread');
notifEls.find('.mark-read .unread').addClass('hidden'); notifEls.find('.mark-read .unread').addClass('hidden');

View File

@@ -383,10 +383,20 @@ Notifications.markReadMultiple = async function (nids, uid) {
]); ]);
}; };
Notifications.markAllRead = async function (uid) { Notifications.markAllRead = async function (uid, filter = '') {
await batch.processSortedSet(`uid:${uid}:notifications:unread`, async (unreadNotifs) => { await batch.processSortedSet(`uid:${uid}:notifications:unread`, async (unreadNotifs) => {
const nids = unreadNotifs.map(n => n && n.value); let nids = unreadNotifs.map(n => n && n.value);
const datetimes = unreadNotifs.map(n => n && n.score); let datetimes = unreadNotifs.map(n => n && n.score);
if (filter !== '') {
const notificationKeys = nids.map(nid => `notifications:${nid}`);
let notificationData = await db.getObjectsFields(notificationKeys, ['nid', 'type', 'datetime']);
notificationData = notificationData.filter(n => n && n.nid && n.type === filter);
if (!notificationData.length) {
return;
}
nids = notificationData.map(n => n.nid);
datetimes = notificationData.map(n => n.datetime || Date.now());
}
await Promise.all([ await Promise.all([
db.sortedSetRemove(`uid:${uid}:notifications:unread`, nids), db.sortedSetRemove(`uid:${uid}:notifications:unread`, nids),
db.sortedSetAdd(`uid:${uid}:notifications:read`, datetimes, nids), db.sortedSetAdd(`uid:${uid}:notifications:read`, datetimes, nids),

View File

@@ -34,8 +34,8 @@ SocketNotifs.markUnread = async function (socket, nid) {
user.notifications.pushCount(socket.uid); user.notifications.pushCount(socket.uid);
}; };
SocketNotifs.markAllRead = async function (socket) { SocketNotifs.markAllRead = async function (socket, data) {
await notifications.markAllRead(socket.uid); await notifications.markAllRead(socket.uid, data.filter);
user.notifications.pushCount(socket.uid); user.notifications.pushCount(socket.uid);
}; };