Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(server): getting rid of module-scoped event emitters - batch #1 #3766

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions packages/server/modules/activitystream/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
acquireTaskLockFactory,
releaseTaskLockFactory
} from '@/modules/core/repositories/scheduledTasks'
import { UsersEmitter, UsersEvents } from '@/modules/core/events/usersEmitter'
import { Knex } from 'knex'
import {
onServerAccessRequestCreatedFactory,
Expand All @@ -40,7 +39,8 @@ import { isProjectResourceTarget } from '@/modules/serverinvites/helpers/core'
import { publish } from '@/modules/shared/utils/subscriptions'
import { isStreamAccessRequest } from '@/modules/accessrequests/repositories'
import { ServerInvitesEvents } from '@/modules/serverinvites/domain/events'
import { ProjectEvents, ProjectsEmitter } from '@/modules/core/events/projectsEmitter'
import { ProjectEvents } from '@/modules/core/domain/projects/events'
import { UserEvents } from '@/modules/core/domain/users/events'

let scheduledTask: ReturnType<ScheduleExecution> | null = null
let quitEventListeners: Optional<() => void> = undefined
Expand All @@ -57,8 +57,8 @@ const initializeEventListeners = ({
db: Knex
}) => {
const quitCbs = [
UsersEmitter.listen(
UsersEvents.Created,
eventBus.listen(
UserEvents.Created,
// this activity will always go in the main DB
onUserCreatedFactory({ saveActivity: saveActivityFactory({ db }) })
),
Expand Down Expand Up @@ -90,12 +90,20 @@ const initializeEventListeners = ({
getStream: getStreamFactory({ db })
})(payload)
}),
ProjectsEmitter.listen(ProjectEvents.Created, async ({ ownerId, project }) => {
await addStreamCreatedActivityFactory({
saveActivity: saveActivityFactory({ db }),
publish
})({ streamId: project.id, creatorId: ownerId, stream: project, input: project })
})
eventBus.listen(
ProjectEvents.Created,
async ({ payload: { ownerId, project } }) => {
await addStreamCreatedActivityFactory({
saveActivity: saveActivityFactory({ db }),
publish
})({
streamId: project.id,
creatorId: ownerId,
stream: project,
input: project
})
}
)
]

return () => quitCbs.forEach((quit) => quit())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
SaveActivity
} from '@/modules/activitystream/domain/operations'
import { GetStream } from '@/modules/core/domain/streams/operations'
import { UsersEvents, UsersEventsPayloads } from '@/modules/core/events/usersEmitter'
import { UserEvents } from '@/modules/core/domain/users/events'
import {
ServerInvitesEvents,
ServerInvitesEventsPayloads
Expand All @@ -23,11 +23,12 @@ import {
isProjectResourceTarget,
resolveTarget
} from '@/modules/serverinvites/helpers/core'
import { EventPayload } from '@/modules/shared/services/eventBus'

export const onUserCreatedFactory =
({ saveActivity }: { saveActivity: SaveActivity }) =>
async (payload: UsersEventsPayloads[UsersEvents.Created]) => {
const { user } = payload
async (payload: EventPayload<typeof UserEvents.Created>) => {
const { user } = payload.payload

await saveActivity({
streamId: null,
Expand Down
4 changes: 2 additions & 2 deletions packages/server/modules/activitystream/tests/activity.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ const {
deleteServerOnlyInvitesFactory,
updateAllInviteTargetsFactory
} = require('@/modules/serverinvites/repositories/serverInvites')
const { UsersEmitter } = require('@/modules/core/events/usersEmitter')
const { createPersonalAccessTokenFactory } = require('@/modules/core/services/tokens')
const {
storePersonalApiTokenFactory,
Expand All @@ -65,6 +64,7 @@ const {
storeSingleObjectIfNotFoundFactory,
storeClosuresIfNotFoundFactory
} = require('@/modules/core/repositories/objects')
const { getEventBus } = require('@/modules/shared/services/eventBus')

const getUser = getUserFactory({ db })
const getUserActivity = getUserActivityFactory({ db })
Expand Down Expand Up @@ -109,7 +109,7 @@ const createUser = createUserFactory({
}),
requestNewEmailVerification
}),
usersEventsEmitter: UsersEmitter.emit
emitEvent: getEventBus().emit
})

const createPersonalAccessToken = createPersonalAccessTokenFactory({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import {
import { collectAndValidateCoreTargetsFactory } from '@/modules/serverinvites/services/coreResourceCollection'
import { buildCoreInviteEmailContentsFactory } from '@/modules/serverinvites/services/coreEmailContents'
import { getEventBus } from '@/modules/shared/services/eventBus'
import { ProjectsEmitter } from '@/modules/core/events/projectsEmitter'
import { createBranchFactory } from '@/modules/core/repositories/branches'
import { getUserFactory, getUsersFactory } from '@/modules/core/repositories/users'
import { getServerInfoFactory } from '@/modules/core/repositories/server'
Expand Down Expand Up @@ -78,7 +77,7 @@ const createStream = legacyCreateStreamFactory({
}),
createStream: createStreamFactory({ db }),
createBranch: createBranchFactory({ db }),
projectsEventsEmitter: ProjectsEmitter.emit
emitEvent: getEventBus().emit
})
})
const deleteStream = deleteStreamFactory({ db })
Expand Down
6 changes: 3 additions & 3 deletions packages/server/modules/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ import {
findEmailFactory,
findPrimaryEmailForUserFactory
} from '@/modules/core/repositories/userEmails'
import { UsersEmitter } from '@/modules/core/events/usersEmitter'
import { validateAndCreateUserEmailFactory } from '@/modules/core/services/userEmails'
import { requestNewEmailVerificationFactory } from '@/modules/emails/services/verification/request'
import { deleteOldAndInsertNewVerificationFactory } from '@/modules/emails/repositories'
import { renderEmail } from '@/modules/emails/services/emailRendering'
import { sendEmail } from '@/modules/emails/services/sending'
import { getServerInfoFactory } from '@/modules/core/repositories/server'
import { initializeEventListenerFactory } from '@/modules/auth/services/postAuth'
import { getEventBus } from '@/modules/shared/services/eventBus'

const findEmail = findEmailFactory({ db })
const requestNewEmailVerification = requestNewEmailVerificationFactory({
Expand All @@ -86,7 +86,7 @@ const createUser = createUserFactory({
}),
requestNewEmailVerification
}),
usersEventsEmitter: UsersEmitter.emit
emitEvent: getEventBus().emit
})

const findOrCreateUser = findOrCreateUserFactory({
Expand Down Expand Up @@ -158,7 +158,7 @@ export const init: SpeckleModule['init'] = async (app, isInitial) => {
// Listen to event emitters
if (isInitial) {
const initializeEventListener = initializeEventListenerFactory({
usersEventsListener: UsersEmitter.listen,
eventBus: getEventBus(),
logger: authLogger
})
initializeEventListener()
Expand Down
16 changes: 6 additions & 10 deletions packages/server/modules/auth/services/postAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@ import {
addToMailchimpAudience,
triggerMailchimpCustomerJourney
} from '@/modules/auth/services/mailchimp'
import {
UsersEvents,
UsersEventsListener,
UsersEventsPayloads
} from '@/modules/core/events/usersEmitter'
import { UserEvents } from '@/modules/core/domain/users/events'
import {
enableMixpanel,
getMailchimpNewsletterIds,
getMailchimpOnboardingIds,
getMailchimpStatus
} from '@/modules/shared/helpers/envHelper'
import { EventBus, EventPayload } from '@/modules/shared/services/eventBus'
import { mixpanel } from '@/modules/shared/utils/mixpanel'

const onUserCreatedFactory =
(deps: { logger: Logger }) =>
async (payload: UsersEventsPayloads[UsersEvents.Created]) => {
const { user, signUpCtx } = payload
async (payload: EventPayload<typeof UserEvents.Created>) => {
const { user, signUpCtx } = payload.payload

try {
// Send event to MP
Expand Down Expand Up @@ -59,10 +56,9 @@ const onUserCreatedFactory =
}

export const initializeEventListenerFactory =
(deps: { usersEventsListener: UsersEventsListener; logger: Logger }) => () => {
(deps: { eventBus: EventBus; logger: Logger }) => () => {
const onUserCreated = onUserCreatedFactory(deps)

const cbs = [deps.usersEventsListener(UsersEvents.Created, onUserCreated)]
const cbs = [deps.eventBus.listen(UserEvents.Created, onUserCreated)]

return () => cbs.forEach((cb) => cb())
}
4 changes: 2 additions & 2 deletions packages/server/modules/auth/tests/apps.graphql.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ const {
deleteServerOnlyInvitesFactory,
updateAllInviteTargetsFactory
} = require('@/modules/serverinvites/repositories/serverInvites')
const { UsersEmitter } = require('@/modules/core/events/usersEmitter')
const {
storeApiTokenFactory,
storeTokenScopesFactory,
Expand All @@ -61,6 +60,7 @@ const {
storePersonalApiTokenFactory
} = require('@/modules/core/repositories/tokens')
const { getServerInfoFactory } = require('@/modules/core/repositories/server')
const { getEventBus } = require('@/modules/shared/services/eventBus')

let sendRequest
let server
Expand Down Expand Up @@ -109,7 +109,7 @@ const createUser = createUserFactory({
}),
requestNewEmailVerification
}),
usersEventsEmitter: UsersEmitter.emit
emitEvent: getEventBus().emit
})
const createPersonalAccessToken = createPersonalAccessTokenFactory({
storeApiToken: storeApiTokenFactory({ db }),
Expand Down
4 changes: 2 additions & 2 deletions packages/server/modules/auth/tests/apps.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ const {
deleteServerOnlyInvitesFactory,
updateAllInviteTargetsFactory
} = require('@/modules/serverinvites/repositories/serverInvites')
const { UsersEmitter } = require('@/modules/core/events/usersEmitter')
const {
storeApiTokenFactory,
storeTokenScopesFactory,
Expand All @@ -75,6 +74,7 @@ const {
updateApiTokenFactory
} = require('@/modules/core/repositories/tokens')
const { getServerInfoFactory } = require('@/modules/core/repositories/server')
const { getEventBus } = require('@/modules/shared/services/eventBus')

const db = knex
const getApp = getAppFactory({ db: knex })
Expand Down Expand Up @@ -141,7 +141,7 @@ const createUser = createUserFactory({
}),
requestNewEmailVerification
}),
usersEventsEmitter: UsersEmitter.emit
emitEvent: getEventBus().emit
})
const validateToken = validateTokenFactory({
revokeUserTokenById: revokeUserTokenByIdFactory({ db }),
Expand Down
6 changes: 2 additions & 4 deletions packages/server/modules/auth/tests/auth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const {
} = require('@/modules/serverinvites/services/coreEmailContents')
const { getEventBus } = require('@/modules/shared/services/eventBus')
const { createBranchFactory } = require('@/modules/core/repositories/branches')
const { ProjectsEmitter } = require('@/modules/core/events/projectsEmitter')
const {
getUsersFactory,
getUserFactory,
Expand Down Expand Up @@ -66,7 +65,6 @@ const {
const {
finalizeInvitedServerRegistrationFactory
} = require('@/modules/serverinvites/services/processing')
const { UsersEmitter } = require('@/modules/core/events/usersEmitter')
const {
getServerInfoFactory,
updateServerInfoFactory
Expand Down Expand Up @@ -102,7 +100,7 @@ const createStream = legacyCreateStreamFactory({
}),
createStream: createStreamFactory({ db }),
createBranch: createBranchFactory({ db }),
projectsEventsEmitter: ProjectsEmitter.emit
emitEvent: getEventBus().emit
})
})

Expand Down Expand Up @@ -131,7 +129,7 @@ const createUser = createUserFactory({
}),
requestNewEmailVerification
}),
usersEventsEmitter: UsersEmitter.emit
emitEvent: getEventBus().emit
})
const getUserByEmail = legacyGetUserByEmailFactory({ db })
const updateServerInfo = updateServerInfoFactory({ db })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const {
} = require('@/modules/serverinvites/services/coreEmailContents')
const { getEventBus } = require('@/modules/shared/services/eventBus')
const { createBranchFactory } = require('@/modules/core/repositories/branches')
const { ProjectsEmitter } = require('@/modules/core/events/projectsEmitter')
const {
getUsersFactory,
getUserFactory,
Expand Down Expand Up @@ -62,7 +61,6 @@ const {
const {
finalizeInvitedServerRegistrationFactory
} = require('@/modules/serverinvites/services/processing')
const { UsersEmitter } = require('@/modules/core/events/usersEmitter')
const { getServerInfoFactory } = require('@/modules/core/repositories/server')

const getServerInfo = getServerInfoFactory({ db })
Expand Down Expand Up @@ -93,7 +91,7 @@ const createStream = legacyCreateStreamFactory({
}),
createStream: createStreamFactory({ db }),
createBranch: createBranchFactory({ db }),
projectsEventsEmitter: ProjectsEmitter.emit
emitEvent: getEventBus().emit
})
})

Expand Down Expand Up @@ -122,7 +120,7 @@ const createUser = createUserFactory({
}),
requestNewEmailVerification
}),
usersEventsEmitter: UsersEmitter.emit
emitEvent: getEventBus().emit
})

describe('Blobs graphql @blobstorage', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import { sendEmail } from '@/modules/emails/services/sending'
import { createUserFactory } from '@/modules/core/services/users/management'
import { validateAndCreateUserEmailFactory } from '@/modules/core/services/userEmails'
import { finalizeInvitedServerRegistrationFactory } from '@/modules/serverinvites/services/processing'
import { UsersEmitter } from '@/modules/core/events/usersEmitter'
import { createTokenFactory } from '@/modules/core/services/tokens'
import {
storeApiTokenFactory,
Expand All @@ -42,6 +41,7 @@ import { faker } from '@faker-js/faker'
import { BasicTestUser } from '@/test/authHelper'
import cryptoRandomString from 'crypto-random-string'
import type { BlobStorageItem } from '@/modules/blobstorage/domain/types'
import { getEventBus } from '@/modules/shared/services/eventBus'

const getServerInfo = getServerInfoFactory({ db })

Expand Down Expand Up @@ -70,7 +70,7 @@ const createUser = createUserFactory({
}),
requestNewEmailVerification
}),
usersEventsEmitter: UsersEmitter.emit
emitEvent: getEventBus().emit
})

const createRandomUser = async (): Promise<BasicTestUser> => {
Expand Down
4 changes: 2 additions & 2 deletions packages/server/modules/cli/commands/download/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ import {
import { getBlobsFactory } from '@/modules/blobstorage/repositories'
import { validateInputAttachmentsFactory } from '@/modules/comments/services/commentTextService'
import { VersionsEmitter } from '@/modules/core/events/versionsEmitter'
import { ProjectsEmitter } from '@/modules/core/events/projectsEmitter'
import { saveActivityFactory } from '@/modules/activitystream/repositories'
import { publish } from '@/modules/shared/utils/subscriptions'
import { addCommitCreatedActivityFactory } from '@/modules/activitystream/services/commitActivity'
Expand All @@ -78,6 +77,7 @@ import {
storeProjectRoleFactory
} from '@/modules/core/repositories/projects'
import { storeModelFactory } from '@/modules/core/repositories/models'
import { getEventBus } from '@/modules/shared/services/eventBus'

const command: CommandModule<
unknown,
Expand Down Expand Up @@ -220,7 +220,7 @@ const command: CommandModule<
storeModel: storeModelFactory({ db: projectDb }),
// THIS MUST GO TO THE MAIN DB
storeProjectRole: storeProjectRoleFactory({ db }),
projectsEventsEmitter: ProjectsEmitter.emit
emitEvent: getEventBus().emit
})

const createObject = createObjectFactory({
Expand Down
6 changes: 2 additions & 4 deletions packages/server/modules/comments/tests/comments.graph.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ const {
buildCoreInviteEmailContentsFactory
} = require('@/modules/serverinvites/services/coreEmailContents')
const { getEventBus } = require('@/modules/shared/services/eventBus')
const { ProjectsEmitter } = require('@/modules/core/events/projectsEmitter')
const { saveActivityFactory } = require('@/modules/activitystream/repositories')
const { publish } = require('@/modules/shared/utils/subscriptions')
const {
Expand Down Expand Up @@ -115,7 +114,6 @@ const {
const {
finalizeInvitedServerRegistrationFactory
} = require('@/modules/serverinvites/services/processing')
const { UsersEmitter } = require('@/modules/core/events/usersEmitter')
const { getServerInfoFactory } = require('@/modules/core/repositories/server')
const { createObjectFactory } = require('@/modules/core/services/objects/management')

Expand Down Expand Up @@ -186,7 +184,7 @@ const createStream = legacyCreateStreamFactory({
}),
createStream: createStreamFactory({ db }),
createBranch: createBranchFactory({ db }),
projectsEventsEmitter: ProjectsEmitter.emit
emitEvent: getEventBus().emit
})
})

Expand Down Expand Up @@ -220,7 +218,7 @@ const createUser = createUserFactory({
}),
requestNewEmailVerification
}),
usersEventsEmitter: UsersEmitter.emit
emitEvent: getEventBus().emit
})
const createObject = createObjectFactory({
storeSingleObjectIfNotFoundFactory: storeSingleObjectIfNotFoundFactory({ db }),
Expand Down
Loading