From badf179df4ed73b287c3eab992fdc926d0a4e236 Mon Sep 17 00:00:00 2001 From: Alan Witkowski Date: Sun, 17 May 2020 09:41:58 -0600 Subject: [PATCH] Split NOTIFICATIONS_SCHEDULE_DELAY into three separate configurable variables for the statuses 'online', 'away', and 'offline'. This change also allows disabling the scheduled notification for each status by setting the delay to -1. --- .../server/NotificationQueue.ts | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/app/notification-queue/server/NotificationQueue.ts b/app/notification-queue/server/NotificationQueue.ts index 4daf190fd59f..241d98387614 100644 --- a/app/notification-queue/server/NotificationQueue.ts +++ b/app/notification-queue/server/NotificationQueue.ts @@ -8,7 +8,9 @@ import { PushNotification } from '../../push-notifications/server'; const { NOTIFICATIONS_WORKER_TIMEOUT = 2000, NOTIFICATIONS_BATCH_SIZE = 100, - NOTIFICATIONS_SCHEDULE_DELAY = 120, + NOTIFICATIONS_SCHEDULE_DELAY_ONLINE = -1, + NOTIFICATIONS_SCHEDULE_DELAY_AWAY = 120, + NOTIFICATIONS_SCHEDULE_DELAY_OFFLINE = 0, } = process.env; class NotificationClass { @@ -18,7 +20,11 @@ class NotificationClass { private maxBatchSize = Number(NOTIFICATIONS_BATCH_SIZE); - private maxScheduleDelaySeconds = Number(NOTIFICATIONS_SCHEDULE_DELAY); + private maxScheduleDelaySecondsOnline = Number(NOTIFICATIONS_SCHEDULE_DELAY_ONLINE); + + private maxScheduleDelaySecondsAway = Number(NOTIFICATIONS_SCHEDULE_DELAY_AWAY); + + private maxScheduleDelaySecondsOffline = Number(NOTIFICATIONS_SCHEDULE_DELAY_OFFLINE); initWorker(): void { this.running = true; @@ -109,19 +115,32 @@ class NotificationClass { return; } - const delay = this.maxScheduleDelaySeconds; - let schedule: Date | undefined; if (user.statusConnection === 'online') { + if (this.maxScheduleDelaySecondsOnline === -1) { + return; + } + schedule = new Date(); - schedule.setSeconds(schedule.getSeconds() + delay); + schedule.setSeconds(schedule.getSeconds() + this.maxScheduleDelaySecondsOnline); } else if (user.statusConnection === 'away') { + if (this.maxScheduleDelaySecondsAway === -1) { + return; + } + const elapsedSeconds = Math.floor((Date.now() - user._updatedAt) / 1000); - if (elapsedSeconds < delay) { + if (elapsedSeconds < this.maxScheduleDelaySecondsAway) { schedule = new Date(); - schedule.setSeconds(schedule.getSeconds() + delay - elapsedSeconds); + schedule.setSeconds(schedule.getSeconds() + this.maxScheduleDelaySecondsAway - elapsedSeconds); + } + } else if (user.statusConnection === 'offline') { + if (this.maxScheduleDelaySecondsOffline === -1) { + return; } + + schedule = new Date(); + schedule.setSeconds(schedule.getSeconds() + this.maxScheduleDelaySecondsOffline); } await NotificationQueue.insertOne({