Files
speckle-server/packages/server/modules/notifications/index.ts
T
Daniel Gak Anagrov 3ca4a11ca3 feat(notifications): basic listener structure, notification record, delayed mechanism (#5432)
* feat: basic notification listener sturcuture

* feat: clean up generated gql

* chore: edited structure

* feat: added basic repo

* feat: ported comment email to job queue

* feat: ported stream access request accepted

* feat: added notification insertion

* fix: minor typings

* feat: delayed notifications

* updated types

* feat: fixed gql

* notifications are listed

* index on notifications

* feat: while loop skiping for update locked

* delayed notification for access request

* take into account user prefrences

* on comment view, notification is marked as read

* feat: added gql notifications

* feat: avoid raising errors

* fix: error added scopes

* fix: mr comments

* fix: cursor and service method

* feat: added stronger types to notifications and versioning logic

* minor: rows updated
2025-10-06 12:19:12 +01:00

75 lines
3.0 KiB
TypeScript

import {
initializePublicationQueue,
consumeIncomingNotifications,
registerNotificationHandlers,
shutdownPublicationQueue
} from '@/modules/notifications/services/publication/queue'
import type { NotificationTypeHandlers } from '@/modules/notifications/helpers/types'
import type { SpeckleModule } from '@/modules/shared/helpers/typeHelper'
import { shouldDisableNotificationsConsumption } from '@/modules/shared/helpers/envHelper'
import { moduleLogger, notificationsLogger } from '@/observability/logging'
import MentionedInCommentHandler from '@/modules/notifications/services/publication/handlers/mentionedInComment'
import NewStreamAccessRequestHandler from '@/modules/notifications/services/publication/handlers/newStreamAccessRequest'
import StreamAccessRequestApprovedHandler from '@/modules/notifications/services/publication/handlers/streamAccessRequestApproved'
import ActivityDigestHandler from '@/modules/notifications/services/publication/handlers/activityDigest'
import {
initializeNotificationEventsConsumption,
initializeNotificationEventsQueue,
shutdownEventQueue
} from '@/modules/notifications/services/events/queue'
import { notificationListenersFactory } from '@/modules/notifications/events/notificationListener'
import { getEventBus } from '@/modules/shared/services/eventBus'
import { scheduleDelayedEmailNotifications } from '@/modules/notifications/tasks/delayedNotifications'
import type cron from 'node-cron'
import { NotificationType } from '@speckle/shared/notifications'
let scheduledTasks: cron.ScheduledTask[] = []
export async function initializePublicationConsumption(
customHandlers?: Partial<NotificationTypeHandlers>
) {
moduleLogger.info('📞 Initializing notification queue consumption...')
const allHandlers: Partial<NotificationTypeHandlers> = {
[NotificationType.MentionedInComment]: MentionedInCommentHandler,
[NotificationType.NewStreamAccessRequest]: NewStreamAccessRequestHandler,
[NotificationType.StreamAccessRequestApproved]: StreamAccessRequestApprovedHandler,
[NotificationType.ActivityDigest]: ActivityDigestHandler
}
registerNotificationHandlers(customHandlers || allHandlers)
await initializePublicationQueue()
if (shouldDisableNotificationsConsumption()) {
moduleLogger.info('Skipping notification consumption...')
} else {
await consumeIncomingNotifications()
}
}
export const init: SpeckleModule['init'] = async ({ isInitial }) => {
moduleLogger.info('📞 Init notifications module')
if (isInitial) {
await initializePublicationConsumption()
await initializeNotificationEventsQueue()
await initializeNotificationEventsConsumption()
notificationListenersFactory({
eventBus: getEventBus(),
logger: notificationsLogger
})()
scheduledTasks = [await scheduleDelayedEmailNotifications()]
}
}
export const shutdown: SpeckleModule['shutdown'] = async () => {
await shutdownPublicationQueue()
await shutdownEventQueue()
scheduledTasks.forEach((task) => {
task.stop()
})
}