From 0b498acdcfa09dac4dfc84a7c0987438714aebb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Mon, 22 Jul 2019 18:36:29 -0400 Subject: [PATCH] fix: #7765 --- src/user/digest.js | 2 +- src/user/notifications.js | 19 ++++++++++++++++--- test/notifications.js | 8 ++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/user/digest.js b/src/user/digest.js index c792ad8f83..cf4aff5f7d 100644 --- a/src/user/digest.js +++ b/src/user/digest.js @@ -72,7 +72,7 @@ Digest.send = async function (data) { async.eachLimit(users, 100, async function (userObj) { let [notifications, topics] = await Promise.all([ - user.notifications.getDailyUnread(userObj.uid), + user.notifications.getUnreadInterval(userObj.uid, data.interval), getTermTopics(data.interval, userObj.uid, 0, 9), ]); notifications = notifications.filter(Boolean); diff --git a/src/user/notifications.js b/src/user/notifications.js index 68c6dc879f..69e3997c76 100644 --- a/src/user/notifications.js +++ b/src/user/notifications.js @@ -103,12 +103,25 @@ UserNotifications.getNotifications = async function (nids, uid) { return result && result.notifications; }; -UserNotifications.getDailyUnread = async function (uid) { - const yesterday = Date.now() - (1000 * 60 * 60 * 24); // Approximate, can be more or less depending on time changes, makes no difference really. - const nids = await db.getSortedSetRevRangeByScore('uid:' + uid + ':notifications:unread', 0, 20, '+inf', yesterday); +UserNotifications.getUnreadInterval = async function (uid, interval) { + const dayInMs = 1000 * 60 * 60 * 24; + const times = { + day: dayInMs, + week: 7 * dayInMs, + month: 30 * dayInMs, + }; + if (!times[interval]) { + return []; + } + const min = Date.now() - times[interval]; + const nids = await db.getSortedSetRevRangeByScore('uid:' + uid + ':notifications:unread', 0, 20, '+inf', min); return await UserNotifications.getNotifications(nids, uid); }; +UserNotifications.getDailyUnread = async function (uid) { + return await UserNotifications.getUnreadInterval(uid, 'day'); +}; + UserNotifications.getUnreadCount = async function (uid) { if (parseInt(uid, 10) <= 0) { return 0; diff --git a/test/notifications.js b/test/notifications.js index eb03362b18..73af314fa9 100644 --- a/test/notifications.js +++ b/test/notifications.js @@ -380,6 +380,14 @@ describe('Notifications', function () { }); }); + it('should return empty array for invalid interval', function (done) { + user.notifications.getUnreadInterval(uid, '2 aeons', function (err, data) { + assert.ifError(err); + assert.deepEqual(data, []); + done(); + }); + }); + it('should return 0 for falsy uid', function (done) { user.notifications.getUnreadCount(0, function (err, count) { assert.ifError(err);