From 8cebe61f3dc18818358a7d22df39026218b38e97 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Sat, 13 Jun 2020 18:07:51 -0300 Subject: [PATCH 1/2] Do not send email if user online --- .../server/functions/notifications/email.js | 6 ++++++ .../server/lib/sendNotificationsOnMessage.js | 1 + .../server/NotificationQueue.ts | 19 ++++++++----------- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/app/lib/server/functions/notifications/email.js b/app/lib/server/functions/notifications/email.js index 1640977bd0ed..8aa1fceedccc 100644 --- a/app/lib/server/functions/notifications/email.js +++ b/app/lib/server/functions/notifications/email.js @@ -166,6 +166,7 @@ export function sendEmail({ message, user, subscription, room, emailAddress, has export function shouldNotifyEmail({ disableAllMessageNotifications, + statusConnection, emailNotifications, isHighlighted, hasMentionToUser, @@ -183,6 +184,11 @@ export function shouldNotifyEmail({ return false; } + // user connected (don't need to send him an email) + if (statusConnection === 'online') { + return false; + } + // no user or room preference if (emailNotifications == null) { if (disableAllMessageNotifications && !isHighlighted && !hasMentionToUser && !hasReplyToThread) { diff --git a/app/lib/server/lib/sendNotificationsOnMessage.js b/app/lib/server/lib/sendNotificationsOnMessage.js index ce64d65a9203..bb7f51c33133 100644 --- a/app/lib/server/lib/sendNotificationsOnMessage.js +++ b/app/lib/server/lib/sendNotificationsOnMessage.js @@ -164,6 +164,7 @@ export const sendNotification = async ({ if (queueItems.length) { Notification.scheduleItem({ + user: receiver, uid: subscription.u._id, rid: room._id, mid: message._id, diff --git a/app/notification-queue/server/NotificationQueue.ts b/app/notification-queue/server/NotificationQueue.ts index 4daf190fd59f..d6a3db8fc219 100644 --- a/app/notification-queue/server/NotificationQueue.ts +++ b/app/notification-queue/server/NotificationQueue.ts @@ -4,6 +4,7 @@ import { INotification, INotificationItemPush, INotificationItemEmail, Notificat import { NotificationQueue, Users } from '../../models/server/raw'; import { sendEmailFromData } from '../../lib/server/functions/notifications/email'; import { PushNotification } from '../../push-notifications/server'; +import { IUser } from '../../../definition/IUser'; const { NOTIFICATIONS_WORKER_TIMEOUT = 2000, @@ -97,31 +98,27 @@ class NotificationClass { sendEmailFromData(item.data); } - async scheduleItem({ uid, rid, mid, items }: {uid: string; rid: string; mid: string; items: NotificationItem[]}): Promise { - const user = await Users.findOneById(uid, { + async scheduleItem({ uid, rid, mid, items, user }: { uid: string; rid: string; mid: string; items: NotificationItem[]; user?: Partial }): Promise { + const receiver = user || await Users.findOneById(uid, { projection: { statusConnection: 1, - _updatedAt: 1, }, }); - if (!user) { + if (!receiver) { return; } + const { statusConnection } = receiver; + const delay = this.maxScheduleDelaySeconds; let schedule: Date | undefined; - if (user.statusConnection === 'online') { + // schedule the notification to be sent after some time if user is current online + if (statusConnection === 'online') { schedule = new Date(); schedule.setSeconds(schedule.getSeconds() + delay); - } else if (user.statusConnection === 'away') { - const elapsedSeconds = Math.floor((Date.now() - user._updatedAt) / 1000); - if (elapsedSeconds < delay) { - schedule = new Date(); - schedule.setSeconds(schedule.getSeconds() + delay - elapsedSeconds); - } } await NotificationQueue.insertOne({ From 94d7adf28853873f787c770929c32197e25d4bbe Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Sat, 13 Jun 2020 18:43:39 -0300 Subject: [PATCH 2/2] Remove delay when away --- .../server/NotificationQueue.ts | 44 +++++++------------ 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/app/notification-queue/server/NotificationQueue.ts b/app/notification-queue/server/NotificationQueue.ts index 9b7139c1c58d..1927d888c228 100644 --- a/app/notification-queue/server/NotificationQueue.ts +++ b/app/notification-queue/server/NotificationQueue.ts @@ -9,8 +9,8 @@ import { IUser } from '../../../definition/IUser'; const { NOTIFICATIONS_WORKER_TIMEOUT = 2000, NOTIFICATIONS_BATCH_SIZE = 100, - NOTIFICATIONS_SCHEDULE_DELAY_ONLINE = -1, - NOTIFICATIONS_SCHEDULE_DELAY_AWAY = 120, + NOTIFICATIONS_SCHEDULE_DELAY_ONLINE = 120, + NOTIFICATIONS_SCHEDULE_DELAY_AWAY = 0, NOTIFICATIONS_SCHEDULE_DELAY_OFFLINE = 0, } = process.env; @@ -21,11 +21,11 @@ class NotificationClass { private maxBatchSize = Number(NOTIFICATIONS_BATCH_SIZE); - private maxScheduleDelaySecondsOnline = Number(NOTIFICATIONS_SCHEDULE_DELAY_ONLINE); - - private maxScheduleDelaySecondsAway = Number(NOTIFICATIONS_SCHEDULE_DELAY_AWAY); - - private maxScheduleDelaySecondsOffline = Number(NOTIFICATIONS_SCHEDULE_DELAY_OFFLINE); + private maxScheduleDelaySeconds: {[key: string]: number} = { + online: Number(NOTIFICATIONS_SCHEDULE_DELAY_ONLINE), + away: Number(NOTIFICATIONS_SCHEDULE_DELAY_AWAY), + offline: Number(NOTIFICATIONS_SCHEDULE_DELAY_OFFLINE), + }; initWorker(): void { this.running = true; @@ -115,32 +115,18 @@ class NotificationClass { return; } - let schedule: Date | undefined; - - if (receiver.statusConnection === 'online') { - if (this.maxScheduleDelaySecondsOnline === -1) { - return; - } + const { statusConnection } = receiver; - schedule = new Date(); - schedule.setSeconds(schedule.getSeconds() + this.maxScheduleDelaySecondsOnline); - } else if (receiver.statusConnection === 'away') { - if (this.maxScheduleDelaySecondsAway === -1) { - return; - } + let schedule: Date | undefined; - const elapsedSeconds = Math.floor((Date.now() - receiver._updatedAt) / 1000); - if (elapsedSeconds < this.maxScheduleDelaySecondsAway) { - schedule = new Date(); - schedule.setSeconds(schedule.getSeconds() + this.maxScheduleDelaySecondsAway - elapsedSeconds); - } - } else if (receiver.statusConnection === 'offline') { - if (this.maxScheduleDelaySecondsOffline === -1) { - return; - } + const delay = this.maxScheduleDelaySeconds[statusConnection]; + if (delay < 0) { + return; + } + if (delay > 0) { schedule = new Date(); - schedule.setSeconds(schedule.getSeconds() + this.maxScheduleDelaySecondsOffline); + schedule.setSeconds(schedule.getSeconds() + delay); } await NotificationQueue.insertOne({