Node.js based forum software built for the modern web
feat: only mark notifications read that match current filter
Barış Soner Uşaklı9d39ed512fa9
diff --git a/public/src/client/notifications.js b/public/src/client/notifications.jsindex d42b23d51e..2b82174ff4 100644--- a/public/src/client/notifications.js+++ b/public/src/client/notifications.js@@ -13,7 +13,7 @@ define('forum/notifications', ['components', 'notifications'], function (componenotifications.handleUnreadButton(listEl);components.get('notifications/mark_all').on('click', function () {- notifications.markAllRead(function () {+ notifications.markAllRead(ajaxify.data.selectedFilter.filter, function () {components.get('notifications/item').removeClass('unread');});});diff --git a/public/src/modules/notifications.js b/public/src/modules/notifications.jsindex 891ca17fe1..712b99ce98 100644--- a/public/src/modules/notifications.js+++ b/public/src/modules/notifications.js@@ -105,7 +105,7 @@ define('notifications', [});if (!unreadNotifs[notifData.nid]) {- unreadNotifs[notifData.nid] = true;+ unreadNotifs[notifData.nid] = notifData;}};@@ -165,12 +165,21 @@ define('notifications', [}};- Notifications.markAllRead = function () {- socket.emit('notifications.markAllRead', function (err) {+ Notifications.markAllRead = function (filter = '') {+ socket.emit('notifications.markAllRead', { filter }, function (err) {if (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]');notifEls.removeClass('unread');notifEls.find('.mark-read .unread').addClass('hidden');diff --git a/src/notifications.js b/src/notifications.jsindex 4e6e7873f3..bcf27c7d66 100644--- a/src/notifications.js+++ b/src/notifications.js@@ -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) => {- const nids = unreadNotifs.map(n => n && n.value);- const datetimes = unreadNotifs.map(n => n && n.score);+ let nids = unreadNotifs.map(n => n && n.value);+ 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([db.sortedSetRemove(`uid:${uid}:notifications:unread`, nids),db.sortedSetAdd(`uid:${uid}:notifications:read`, datetimes, nids),diff --git a/src/socket.io/notifications.js b/src/socket.io/notifications.jsindex 2b0df88114..c861f6c725 100644--- a/src/socket.io/notifications.js+++ b/src/socket.io/notifications.js@@ -34,8 +34,8 @@ SocketNotifs.markUnread = async function (socket, nid) {user.notifications.pushCount(socket.uid);};-SocketNotifs.markAllRead = async function (socket) {- await notifications.markAllRead(socket.uid);+SocketNotifs.markAllRead = async function (socket, data) {+ await notifications.markAllRead(socket.uid, data.filter);user.notifications.pushCount(socket.uid);};