From 2eff539ac1d94ca6551946b7765fd0fed76d0bbc Mon Sep 17 00:00:00 2001 From: Alessandro Magionami Date: Fri, 27 Sep 2024 10:35:42 +0200 Subject: [PATCH] chore(activitystream): refactor addStreamCommentMentionActivity --- .../activitystream/domain/operations.ts | 8 ++++ .../activitystream/services/streamActivity.ts | 45 +++++++++---------- packages/server/modules/comments/index.ts | 8 +++- .../comments/services/notifications.ts | 4 +- 4 files changed, 38 insertions(+), 27 deletions(-) diff --git a/packages/server/modules/activitystream/domain/operations.ts b/packages/server/modules/activitystream/domain/operations.ts index a894eda7f..3a8a60088 100644 --- a/packages/server/modules/activitystream/domain/operations.ts +++ b/packages/server/modules/activitystream/domain/operations.ts @@ -146,3 +146,11 @@ export type CreateActivitySummary = (args: { start: Date end: Date }) => Promise + +export type AddStreamCommentMentionActivity = (params: { + streamId: string + mentionAuthorId: string + mentionTargetId: string + commentId: string + threadId: string +}) => Promise diff --git a/packages/server/modules/activitystream/services/streamActivity.ts b/packages/server/modules/activitystream/services/streamActivity.ts index 6bec7b826..651eba313 100644 --- a/packages/server/modules/activitystream/services/streamActivity.ts +++ b/packages/server/modules/activitystream/services/streamActivity.ts @@ -23,6 +23,10 @@ import { } from '@/modules/shared/utils/subscriptions' import { saveActivityFactory } from '@/modules/activitystream/repositories' import { db } from '@/db/knex' +import { + AddStreamCommentMentionActivity, + SaveActivity +} from '@/modules/activitystream/domain/operations' /** * Save "stream updated" activity @@ -408,26 +412,21 @@ export async function addStreamInviteDeclinedActivity(params: { /** * Save "user mentioned in stream comment" activity item */ -export async function addStreamCommentMentionActivity(params: { - streamId: string - mentionAuthorId: string - mentionTargetId: string - commentId: string - threadId: string -}) { - const { streamId, mentionAuthorId, mentionTargetId, commentId, threadId } = params - await saveActivityFactory({ db })({ - streamId, - resourceType: ResourceTypes.Comment, - resourceId: commentId, - actionType: ActionTypes.Comment.Mention, - userId: mentionAuthorId, - message: `User ${mentionAuthorId} mentioned user ${mentionTargetId} in comment ${commentId}`, - info: { - mentionAuthorId, - mentionTargetId, - commentId, - threadId - } - }) -} +export const addStreamCommentMentionActivityFactory = + ({ saveActivity }: { saveActivity: SaveActivity }): AddStreamCommentMentionActivity => + async ({ streamId, mentionAuthorId, mentionTargetId, commentId, threadId }) => { + await saveActivity({ + streamId, + resourceType: ResourceTypes.Comment, + resourceId: commentId, + actionType: ActionTypes.Comment.Mention, + userId: mentionAuthorId, + message: `User ${mentionAuthorId} mentioned user ${mentionTargetId} in comment ${commentId}`, + info: { + mentionAuthorId, + mentionTargetId, + commentId, + threadId + } + }) + } diff --git a/packages/server/modules/comments/index.ts b/packages/server/modules/comments/index.ts index 08fa0bef9..934658cdd 100644 --- a/packages/server/modules/comments/index.ts +++ b/packages/server/modules/comments/index.ts @@ -1,5 +1,7 @@ +import { db } from '@/db/knex' import { moduleLogger } from '@/logging/logging' -import { addStreamCommentMentionActivity } from '@/modules/activitystream/services/streamActivity' +import { saveActivityFactory } from '@/modules/activitystream/repositories' +import { addStreamCommentMentionActivityFactory } from '@/modules/activitystream/services/streamActivity' import { CommentsEmitter } from '@/modules/comments/events/emitter' import { notifyUsersOnCommentEventsFactory } from '@/modules/comments/services/notifications' import { publishNotification } from '@/modules/notifications/services/publication' @@ -15,7 +17,9 @@ const commentsModule: SpeckleModule = { const notifyUsersOnCommentEvents = notifyUsersOnCommentEventsFactory({ commentsEventsListen: CommentsEmitter.listen, publish: publishNotification, - addStreamCommentMentionActivity + addStreamCommentMentionActivity: addStreamCommentMentionActivityFactory({ + saveActivity: saveActivityFactory({ db }) + }) }) unsubFromEvents = await notifyUsersOnCommentEvents() } diff --git a/packages/server/modules/comments/services/notifications.ts b/packages/server/modules/comments/services/notifications.ts index 35a4562b2..52b105ffa 100644 --- a/packages/server/modules/comments/services/notifications.ts +++ b/packages/server/modules/comments/services/notifications.ts @@ -8,7 +8,7 @@ import { NotificationPublisher, NotificationType } from '@/modules/notifications/helpers/types' -import { addStreamCommentMentionActivity } from '@/modules/activitystream/services/streamActivity' +import { AddStreamCommentMentionActivity } from '@/modules/activitystream/domain/operations' function findMentionedUserIds(doc: JSONContent) { const mentionedUserIds = new Set() @@ -36,7 +36,7 @@ function collectMentionedUserIds(comment: CommentRecord): string[] { type SendNotificationsForUsersDeps = { publish: NotificationPublisher - addStreamCommentMentionActivity: typeof addStreamCommentMentionActivity + addStreamCommentMentionActivity: AddStreamCommentMentionActivity } const sendNotificationsForUsersFactory =