|
|
|
@ -4,12 +4,13 @@ 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, |
|
|
|
|
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; |
|
|
|
|
|
|
|
|
@ -20,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; |
|
|
|
@ -103,44 +104,29 @@ class NotificationClass { |
|
|
|
|
sendEmailFromData(item.data); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async scheduleItem({ uid, rid, mid, items }: {uid: string; rid: string; mid: string; items: NotificationItem[]}): Promise<void> { |
|
|
|
|
const user = await Users.findOneById(uid, { |
|
|
|
|
async scheduleItem({ uid, rid, mid, items, user }: { uid: string; rid: string; mid: string; items: NotificationItem[]; user?: Partial<IUser> }): Promise<void> { |
|
|
|
|
const receiver = user || await Users.findOneById(uid, { |
|
|
|
|
projection: { |
|
|
|
|
statusConnection: 1, |
|
|
|
|
_updatedAt: 1, |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
if (!user) { |
|
|
|
|
if (!receiver) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let schedule: Date | undefined; |
|
|
|
|
|
|
|
|
|
if (user.statusConnection === 'online') { |
|
|
|
|
if (this.maxScheduleDelaySecondsOnline === -1) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
const { statusConnection } = receiver; |
|
|
|
|
|
|
|
|
|
schedule = new Date(); |
|
|
|
|
schedule.setSeconds(schedule.getSeconds() + this.maxScheduleDelaySecondsOnline); |
|
|
|
|
} else if (user.statusConnection === 'away') { |
|
|
|
|
if (this.maxScheduleDelaySecondsAway === -1) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
let schedule: Date | undefined; |
|
|
|
|
|
|
|
|
|
const elapsedSeconds = Math.floor((Date.now() - user._updatedAt) / 1000); |
|
|
|
|
if (elapsedSeconds < this.maxScheduleDelaySecondsAway) { |
|
|
|
|
schedule = new Date(); |
|
|
|
|
schedule.setSeconds(schedule.getSeconds() + this.maxScheduleDelaySecondsAway - elapsedSeconds); |
|
|
|
|
} |
|
|
|
|
} else if (user.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({ |
|
|
|
|