Skip to content

Commit

Permalink
chore(server): getting rid of module-scoped eventBuses - batch #4 - c…
Browse files Browse the repository at this point in the history
…omments (#3812)
  • Loading branch information
fabis94 authored Jan 14, 2025
1 parent 91460ee commit b41a98d
Show file tree
Hide file tree
Showing 19 changed files with 800 additions and 425 deletions.
5 changes: 2 additions & 3 deletions packages/server/modules/cli/commands/download/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import {
markCommentUpdatedFactory,
markCommentViewedFactory
} from '@/modules/comments/repositories/comments'
import { CommentsEmitter } from '@/modules/comments/events/emitter'
import {
addCommentCreatedActivityFactory,
addReplyAddedActivityFactory
Expand Down Expand Up @@ -144,7 +143,7 @@ const command: CommandModule<
insertComments,
insertCommentLinks,
markCommentViewed,
commentsEventsEmit: CommentsEmitter.emit,
emitEvent: getEventBus().emit,
addCommentCreatedActivity: addCommentCreatedActivityFactory({
getViewerResourcesFromLegacyIdentifiers,
getViewerResourceItemsUngrouped,
Expand All @@ -159,7 +158,7 @@ const command: CommandModule<
insertComments,
insertCommentLinks,
markCommentUpdated: markCommentUpdatedFactory({ db: projectDb }),
commentsEventsEmit: CommentsEmitter.emit,
emitEvent: getEventBus().emit,
addReplyAddedActivity: addReplyAddedActivityFactory({
getViewerResourcesForComment: getViewerResourcesForCommentFactory({
getCommentsResources: getCommentsResourcesFactory({ db: projectDb }),
Expand Down
5 changes: 2 additions & 3 deletions packages/server/modules/cli/commands/download/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
createCommentThreadAndNotifyFactory
} from '@/modules/comments/services/management'
import { createBranchAndNotifyFactory } from '@/modules/core/services/branch/management'
import { CommentsEmitter } from '@/modules/comments/events/emitter'
import {
addCommentCreatedActivityFactory,
addReplyAddedActivityFactory
Expand Down Expand Up @@ -170,7 +169,7 @@ const command: CommandModule<
insertComments,
insertCommentLinks,
markCommentViewed,
commentsEventsEmit: CommentsEmitter.emit,
emitEvent: getEventBus().emit,
addCommentCreatedActivity: addCommentCreatedActivityFactory({
getViewerResourcesFromLegacyIdentifiers,
getViewerResourceItemsUngrouped,
Expand All @@ -184,7 +183,7 @@ const command: CommandModule<
insertComments,
insertCommentLinks,
markCommentUpdated: markCommentUpdatedFactory({ db: projectDb }),
commentsEventsEmit: CommentsEmitter.emit,
emitEvent: getEventBus().emit,
addReplyAddedActivity: addReplyAddedActivityFactory({
getViewerResourcesForComment: getViewerResourcesForCommentFactory({
getCommentsResources: getCommentsResourcesFactory({ db: projectDb }),
Expand Down
16 changes: 16 additions & 0 deletions packages/server/modules/comments/domain/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { CommentRecord } from '@/modules/comments/helpers/types'

export const commentEventsNamespace = 'comments' as const

export const CommentEvents = {
Created: `${commentEventsNamespace}.created`,
Updated: `${commentEventsNamespace}.updated`
} as const

export type CommentEventsPayloads = {
[CommentEvents.Created]: { comment: CommentRecord }
[CommentEvents.Updated]: {
previousComment: CommentRecord
newComment: CommentRecord
}
}
22 changes: 0 additions & 22 deletions packages/server/modules/comments/events/emitter.ts

This file was deleted.

14 changes: 7 additions & 7 deletions packages/server/modules/comments/graph/resolvers/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ import { Resolvers, ResourceType } from '@/modules/core/graph/generated/graphql'
import { GraphQLContext } from '@/modules/shared/helpers/typeHelper'
import { CommentRecord } from '@/modules/comments/helpers/types'
import { db, mainDb } from '@/db/knex'
import { CommentsEmitter } from '@/modules/comments/events/emitter'
import { getBlobsFactory } from '@/modules/blobstorage/repositories'
import { ResourceIdentifier } from '@/modules/comments/domain/types'
import {
Expand All @@ -99,6 +98,7 @@ import { getStreamFactory } from '@/modules/core/repositories/streams'
import { saveActivityFactory } from '@/modules/activitystream/repositories'
import { getProjectDbClient } from '@/modules/multiregion/utils/dbSelector'
import { Knex } from 'knex'
import { getEventBus } from '@/modules/shared/services/eventBus'

// We can use the main DB for these
const getStream = getStreamFactory({ db })
Expand Down Expand Up @@ -549,7 +549,7 @@ export = {
insertComments,
insertCommentLinks,
markCommentViewed,
commentsEventsEmit: CommentsEmitter.emit,
emitEvent: getEventBus().emit,
addCommentCreatedActivity: addCommentCreatedActivityFactory({
getViewerResourcesFromLegacyIdentifiers,
getViewerResourceItemsUngrouped,
Expand Down Expand Up @@ -587,7 +587,7 @@ export = {
insertComments,
insertCommentLinks,
markCommentUpdated: markCommentUpdatedFactory({ db: projectDb }),
commentsEventsEmit: CommentsEmitter.emit,
emitEvent: getEventBus().emit,
addReplyAddedActivity: addReplyAddedActivityFactory({
getViewerResourcesForComment: getViewerResourcesForCommentFactory({
getCommentsResources: getCommentsResourcesFactory({ db: projectDb }),
Expand Down Expand Up @@ -624,7 +624,7 @@ export = {
getComment,
validateInputAttachments,
updateComment,
commentsEventsEmit: CommentsEmitter.emit
emitEvent: getEventBus().emit
})

return await editCommentAndNotify(args.input, ctx.userId!)
Expand Down Expand Up @@ -752,7 +752,7 @@ export = {
insertCommentLinks: insertCommentLinksFactory({ db: projectDb }),
deleteComment: deleteCommentFactory({ db: projectDb }),
markCommentViewed: markCommentViewedFactory({ db: projectDb }),
commentsEventsEmit: CommentsEmitter.emit
emitEvent: getEventBus().emit
})
const comment = await createComment({
userId: context.userId,
Expand Down Expand Up @@ -796,7 +796,7 @@ export = {
getBlobs: getBlobsFactory({ db: projectDb })
}),
updateComment: updateCommentFactory({ db: projectDb }),
commentsEventsEmit: CommentsEmitter.emit
emitEvent: getEventBus().emit
})

await editComment({ userId: context.userId!, input: args.input, matchUser })
Expand Down Expand Up @@ -878,7 +878,7 @@ export = {
}),
deleteComment: deleteCommentFactory({ db: projectDb }),
markCommentUpdated: markCommentUpdatedFactory({ db: projectDb }),
commentsEventsEmit: CommentsEmitter.emit
emitEvent: getEventBus().emit
})
const reply = await createCommentReply({
authorId: context.userId,
Expand Down
4 changes: 2 additions & 2 deletions packages/server/modules/comments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { db } from '@/db/knex'
import { moduleLogger } from '@/logging/logging'
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'
import { Optional, SpeckleModule } from '@/modules/shared/helpers/typeHelper'
import { getEventBus } from '@/modules/shared/services/eventBus'

let unsubFromEvents: Optional<() => void> = undefined

Expand All @@ -15,7 +15,7 @@ const commentsModule: SpeckleModule = {

if (isInitial) {
const notifyUsersOnCommentEvents = notifyUsersOnCommentEventsFactory({
commentsEventsListen: CommentsEmitter.listen,
eventBus: getEventBus(),
publish: publishNotification,
addStreamCommentMentionActivity: addStreamCommentMentionActivityFactory({
saveActivity: saveActivityFactory({ db })
Expand Down
12 changes: 11 additions & 1 deletion packages/server/modules/comments/services/commentTextService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import {
convertBasicStringToDocument,
isSerializedTextEditorValueSchema,
SmartTextEditorValueSchema,
isDocEmpty
isDocEmpty,
documentToBasicString
} from '@/modules/core/services/richTextEditorService'
import { isString, uniq } from 'lodash'
import { InvalidAttachmentsError } from '@/modules/comments/errors'
import { JSONContent } from '@tiptap/core'
import { ValidateInputAttachments } from '@/modules/comments/domain/operations'
import { GetBlobs } from '@/modules/blobstorage/domain/operations'
import { Nullable } from '@speckle/shared'

const COMMENT_SCHEMA_VERSION = '1.0.0'
const COMMENT_SCHEMA_TYPE = 'stream_comment'
Expand Down Expand Up @@ -76,3 +78,11 @@ export function ensureCommentSchema(

throw new RichTextParseError('Unexpected comment schema format')
}

export const commentTextToRawString = (
text: Nullable<SmartTextEditorValueSchema | string>
) => {
if (!text) return null
const schema = ensureCommentSchema(text)
return documentToBasicString(schema.doc)
}
38 changes: 24 additions & 14 deletions packages/server/modules/comments/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import crs from 'crypto-random-string'
import { ForbiddenError } from '@/modules/shared/errors'
import { buildCommentTextFromInput } from '@/modules/comments/services/commentTextService'
import { CommentsEvents, CommentsEventsEmit } from '@/modules/comments/events/emitter'
import { isNonNullable, Roles } from '@speckle/shared'
import {
ResourceIdentifier,
CommentCreateInput,
CommentEditInput,
SmartTextEditorValue
CommentEditInput
} from '@/modules/core/graph/generated/graphql'
import { CommentLinkRecord, CommentRecord } from '@/modules/comments/helpers/types'
import { SmartTextEditorValueSchema } from '@/modules/core/services/richTextEditorService'
Expand All @@ -25,6 +23,9 @@ import {
} from '@/modules/comments/domain/operations'
import { ResourceType } from '@/modules/comments/domain/types'
import { GetStream } from '@/modules/core/domain/streams/operations'
import { EventBusEmit } from '@/modules/shared/services/eventBus'
import { CommentEvents } from '@/modules/comments/domain/events'
import { JSONContent } from '@tiptap/core'

export const streamResourceCheckFactory =
(deps: {
Expand Down Expand Up @@ -54,7 +55,7 @@ export const createCommentFactory =
insertCommentLinks: InsertCommentLinks
deleteComment: DeleteComment
markCommentViewed: MarkCommentViewed
commentsEventsEmit: CommentsEventsEmit
emitEvent: EventBusEmit
}) =>
async ({ userId, input }: { userId: string; input: CommentCreateInput }) => {
if (input.resources.length < 1)
Expand Down Expand Up @@ -115,8 +116,11 @@ export const createCommentFactory =

await deps.markCommentViewed(id, userId) // so we don't self mark a comment as unread the moment it's created

await deps.commentsEventsEmit(CommentsEvents.Created, {
comment: newComment
await deps.emitEvent({
eventName: CommentEvents.Created,
payload: {
comment: newComment
}
})

return newComment
Expand All @@ -133,7 +137,7 @@ export const createCommentReplyFactory =
checkStreamResourcesAccess: CheckStreamResourcesAccess
deleteComment: DeleteComment
markCommentUpdated: MarkCommentUpdated
commentsEventsEmit: CommentsEventsEmit
emitEvent: EventBusEmit
}) =>
async ({
authorId,
Expand All @@ -146,7 +150,7 @@ export const createCommentReplyFactory =
authorId: string
parentCommentId: string
streamId: string
text: SmartTextEditorValue
text: JSONContent
data: CommentRecord['data']
blobIds: string[]
}) => {
Expand Down Expand Up @@ -184,8 +188,11 @@ export const createCommentReplyFactory =

await deps.markCommentUpdated(parentCommentId)

await deps.commentsEventsEmit(CommentsEvents.Created, {
comment: newComment
await deps.emitEvent({
eventName: CommentEvents.Created,
payload: {
comment: newComment
}
})

return newComment
Expand All @@ -199,7 +206,7 @@ export const editCommentFactory =
getComment: GetComment
validateInputAttachments: ValidateInputAttachments
updateComment: UpdateComment
commentsEventsEmit: CommentsEventsEmit
emitEvent: EventBusEmit
}) =>
async ({
userId,
Expand All @@ -223,9 +230,12 @@ export const editCommentFactory =
})
const updatedComment = await deps.updateComment(input.id, { text: newText })

await deps.commentsEventsEmit(CommentsEvents.Updated, {
previousComment: editedComment,
newComment: updatedComment!
await deps.emitEvent({
eventName: CommentEvents.Updated,
payload: {
previousComment: editedComment,
newComment: updatedComment!
}
})

return updatedComment
Expand Down
Loading

0 comments on commit b41a98d

Please sign in to comment.