Files
speckle-server/packages/server/modules/comments/services/notifications.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

117 lines
3.7 KiB
TypeScript

import type { CommentRecord } from '@/modules/comments/helpers/types'
import { ensureCommentSchema } from '@/modules/comments/services/commentTextService'
import { flatten } from 'lodash-es'
import type { NotificationPublisher } from '@/modules/notifications/helpers/types'
import { NotificationType } from '@speckle/shared/notifications'
import type {
AddStreamCommentMentionActivity,
SaveStreamActivity
} from '@/modules/activitystream/domain/operations'
import type { EventBus } from '@/modules/shared/services/eventBus'
import { CommentEvents } from '@/modules/comments/domain/events'
import {
StreamActionTypes,
StreamResourceTypes
} from '@/modules/activitystream/helpers/types'
import { processCommentMentions } from '@/modules/notifications/services/events/handlers/createdOrUpdatedComment'
/**
* Save "user mentioned in stream comment" activity item
*/
const addStreamCommentMentionActivityFactory =
({
saveActivity
}: {
saveActivity: SaveStreamActivity
}): AddStreamCommentMentionActivity =>
async ({ streamId, mentionAuthorId, mentionTargetId, commentId, threadId }) => {
await saveActivity({
streamId,
resourceType: StreamResourceTypes.Comment,
resourceId: commentId,
actionType: StreamActionTypes.Comment.Mention,
userId: mentionAuthorId,
message: `User ${mentionAuthorId} mentioned user ${mentionTargetId} in comment ${commentId}`,
info: {
mentionAuthorId,
mentionTargetId,
commentId,
threadId
}
})
}
type SendNotificationsForUsersDeps = {
publish: NotificationPublisher
addStreamCommentMentionActivity: AddStreamCommentMentionActivity
}
const sendNotificationsForUsersFactory =
(deps: SendNotificationsForUsersDeps) =>
async (userIds: string[], comment: CommentRecord) => {
const { id, streamId, authorId, parentComment } = comment
const threadId = parentComment || id
await Promise.all(
flatten(
userIds.map((uid) => {
return [
// Actually send out notification
deps.publish(NotificationType.MentionedInComment, {
targetUserId: uid,
data: {
threadId,
streamId,
authorId,
commentId: id
}
}),
// Create activity item
deps.addStreamCommentMentionActivity({
streamId,
mentionAuthorId: authorId,
mentionTargetId: uid,
commentId: id,
threadId
})
]
})
)
)
}
/**
* Hook into the comments lifecycle to generate notifications accordingly
* @returns Callback to invoke when you wish to stop listening for comments events
*/
export const notifyUsersOnCommentEventsFactory =
(deps: {
eventBus: EventBus
publish: NotificationPublisher
saveActivity: SaveStreamActivity
}) =>
async () => {
const addStreamCommentMentionActivity = addStreamCommentMentionActivityFactory(deps)
const sendNotificationsForUsers = sendNotificationsForUsersFactory({
...deps,
addStreamCommentMentionActivity
})
const exitCbs = [
deps.eventBus.listen(CommentEvents.Created, async ({ payload: { comment } }) => {
const newMentions = processCommentMentions(comment)
if (newMentions.length) await sendNotificationsForUsers(newMentions, comment)
}),
deps.eventBus.listen(
CommentEvents.Updated,
async ({ payload: { newComment, previousComment } }) => {
const newMentions = processCommentMentions(newComment, previousComment)
if (newMentions.length)
await sendNotificationsForUsers(newMentions, newComment)
}
)
]
return () => exitCbs.forEach((cb) => cb())
}