diff --git a/app/models/server/raw/NotificationQueue.ts b/app/models/server/raw/NotificationQueue.ts index 44a34a21be7f5..4732639a067f0 100644 --- a/app/models/server/raw/NotificationQueue.ts +++ b/app/models/server/raw/NotificationQueue.ts @@ -54,6 +54,23 @@ export class NotificationQueueRaw extends BaseRaw { return op.deletedCount; } + /** + * Expedite scheduled notifications for given user, to be scheduled now. + * Used when the user that was "online" becomes "away" or "offline". + * @param uid User ID + */ + async expediteSheduleByUserId(uid: string) { + const now = new Date(); + return this.col.updateMany({ + uid, + schedule: { $gt: now }, + }, { + $set: { + schedule: now, + }, + }); + } + async findNextInQueueOrExpired(expired: Date): Promise { const now = new Date(); diff --git a/app/notification-queue/server/NotificationQueue.ts b/app/notification-queue/server/NotificationQueue.ts index 7665a588c8ee0..e3c06194c0bb2 100644 --- a/app/notification-queue/server/NotificationQueue.ts +++ b/app/notification-queue/server/NotificationQueue.ts @@ -138,6 +138,19 @@ class NotificationClass { items, }); } + + /** + * Expedite scheduled notifications for given user, to be scheduled now. + * Used when the user that was "online" becomes "away" or "offline". + * @param uid User ID + */ + async expediteSheduleByUserId(uid: string) { + const updateResult = await NotificationQueue.expediteSheduleByUserId(uid); + if (updateResult.modifiedCount > 0) { + console.debug(`Expedited ${updateResult.modifiedCount} notifications for user ${uid}`); + } + return updateResult; + } } export const Notification = new NotificationClass(); diff --git a/imports/users-presence/server/activeUsers.js b/imports/users-presence/server/activeUsers.js index 53c5430090844..37bc6189c9c1e 100644 --- a/imports/users-presence/server/activeUsers.js +++ b/imports/users-presence/server/activeUsers.js @@ -2,6 +2,7 @@ import { UserPresenceEvents } from 'meteor/konecty:user-presence'; import { Notifications } from '../../../app/notifications/server'; import { settings } from '../../../app/settings/server'; +import { Notification } from '../../../app/notification-queue/server/NotificationQueue'; // mirror of object in /imports/startup/client/listenActiveUsers.js - keep updated export const STATUS_MAP = { @@ -26,6 +27,11 @@ export const setUserStatus = (user, status/* , statusConnection*/) => { STATUS_MAP[status], statusText, ]); + + if (status == 'offline' || status == 'away') { + // Expedite notification schedule for this user + Notification.expediteSheduleByUserId(_id); + } }; let TroubleshootDisablePresenceBroadcast;