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): refactor activityStream invocations - batch #3 - branches #3874

Open
wants to merge 4 commits into
base: fabians/web-2415-2
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
97 changes: 97 additions & 0 deletions packages/server/modules/activitystream/events/branchListeners.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import {
AddBranchCreatedActivity,
AddBranchDeletedActivity,
AddBranchUpdatedActivity,
SaveActivity
} from '@/modules/activitystream/domain/operations'
import { ActionTypes, ResourceTypes } from '@/modules/activitystream/helpers/types'
import { ModelEvents } from '@/modules/core/domain/branches/events'
import { isBranchDeleteInput, isBranchUpdateInput } from '@/modules/core/helpers/branch'
import { EventBusListen } from '@/modules/shared/services/eventBus'

/**
* Save "branch created" activity
*/
const addBranchCreatedActivityFactory =
({ saveActivity }: { saveActivity: SaveActivity }): AddBranchCreatedActivity =>
async (params) => {
const { branch } = params

await saveActivity({
streamId: branch.streamId,
resourceType: ResourceTypes.Branch,
resourceId: branch.id,
actionType: ActionTypes.Branch.Create,
userId: branch.authorId,
info: { branch },
message: `Branch created: ${branch.name} (${branch.id})`
})
}

const addBranchUpdatedActivityFactory =
({ saveActivity }: { saveActivity: SaveActivity }): AddBranchUpdatedActivity =>
async (params) => {
const { update, userId, oldBranch } = params

const streamId = isBranchUpdateInput(update) ? update.streamId : update.projectId
await saveActivity({
streamId,
resourceType: ResourceTypes.Branch,
resourceId: update.id,
actionType: ActionTypes.Branch.Update,
userId,
info: { old: oldBranch, new: update },
message: `Branch metadata changed for branch ${update.id}`
})
}

const addBranchDeletedActivityFactory =
({ saveActivity }: { saveActivity: SaveActivity }): AddBranchDeletedActivity =>
async (params) => {
const { input, userId, branchName } = params

const streamId = isBranchDeleteInput(input) ? input.streamId : input.projectId
await Promise.all([
saveActivity({
streamId,
resourceType: ResourceTypes.Branch,
resourceId: input.id,
actionType: ActionTypes.Branch.Delete,
userId,
info: { branch: { ...input, name: branchName } },
message: `Branch deleted: '${branchName}' (${input.id})`
})
])
}

export const reportBranchActivityFactory =
(deps: { eventListen: EventBusListen; saveActivity: SaveActivity }) => () => {
const addBranchCreatedActivity = addBranchCreatedActivityFactory(deps)
const addBranchUpdatedActivity = addBranchUpdatedActivityFactory(deps)
const addBranchDeletedActivity = addBranchDeletedActivityFactory(deps)

const quitters = [
deps.eventListen(ModelEvents.Created, async (payload) => {
await addBranchCreatedActivity({ branch: payload.payload.model })
}),
deps.eventListen(ModelEvents.Updated, async ({ payload }) => {
await addBranchUpdatedActivity({
update: payload.update,
userId: payload.userId,
oldBranch: payload.oldModel,
newBranch: payload.newModel
})
}),
deps.eventListen(ModelEvents.Deleted, async ({ payload }) => {
await addBranchDeletedActivity({
userId: payload.userId,
input: payload.input,
branchName: payload.model.name
})
})
]

return () => {
quitters.forEach((quit) => quit())
}
}
6 changes: 6 additions & 0 deletions packages/server/modules/activitystream/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { ServerInvitesEvents } from '@/modules/serverinvites/domain/events'
import { ProjectEvents } from '@/modules/core/domain/projects/events'
import { reportUserActivityFactory } from '@/modules/activitystream/events/userListeners'
import { reportAccessRequestActivityFactory } from '@/modules/activitystream/events/accessRequestListeners'
import { reportBranchActivityFactory } from '@/modules/activitystream/events/branchListeners'

let scheduledTask: ReturnType<ScheduleExecution> | null = null
let quitEventListeners: Optional<() => void> = undefined
Expand All @@ -52,10 +53,15 @@ const initializeEventListeners = ({
eventListen: eventBus.listen,
saveActivity
})
const reportBranchActivity = reportBranchActivityFactory({
eventListen: eventBus.listen,
saveActivity
})

const quitCbs = [
reportUserActivity(),
reportAccessRequestActivity(),
reportBranchActivity(),
eventBus.listen(ServerInvitesEvents.Created, async ({ payload }) => {
if (!isProjectResourceTarget(payload.invite.resource)) return
await onServerInviteCreatedFactory({
Expand Down
128 changes: 0 additions & 128 deletions packages/server/modules/activitystream/services/branchActivity.ts

This file was deleted.

6 changes: 1 addition & 5 deletions packages/server/modules/cli/commands/download/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ import { publish } from '@/modules/shared/utils/subscriptions'
import { addCommitCreatedActivityFactory } from '@/modules/activitystream/services/commitActivity'
import { getUserFactory } from '@/modules/core/repositories/users'
import { createObjectFactory } from '@/modules/core/services/objects/management'
import { addBranchCreatedActivityFactory } from '@/modules/activitystream/services/branchActivity'
import { authorizeResolver } from '@/modules/shared'
import { Roles } from '@speckle/shared'
import { getDefaultRegionFactory } from '@/modules/workspaces/repositories/regions'
Expand Down Expand Up @@ -247,10 +246,7 @@ const command: CommandModule<
createBranchAndNotify: createBranchAndNotifyFactory({
getStreamBranchByName,
createBranch: createBranchFactory({ db: projectDb }),
addBranchCreatedActivity: addBranchCreatedActivityFactory({
saveActivity: saveActivityFactory({ db: mainDb }),
publish
})
eventEmit: getEventBus().emit
})
})
await downloadProject({ ...argv, regionKey }, { logger: cliLogger })
Expand Down
25 changes: 23 additions & 2 deletions packages/server/modules/core/domain/branches/events.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
import { Model } from '@/modules/core/domain/branches/types'
import {
BranchDeleteInput,
BranchUpdateInput,
DeleteModelInput,
UpdateModelInput
} from '@/modules/core/graph/generated/graphql'

export const modelEventsNamespace = 'models' as const

export const ModelEvents = {
Deleted: `${modelEventsNamespace}.deleted`
Deleted: `${modelEventsNamespace}.deleted`,
Created: `${modelEventsNamespace}.created`,
Updated: `${modelEventsNamespace}.updated`
} as const

export type ModelEventsPayloads = {
[ModelEvents.Deleted]: { projectId: string; modelId: string; model: Model }
[ModelEvents.Deleted]: {
projectId: string
modelId: string
model: Model
userId: string
input: BranchDeleteInput | DeleteModelInput
}
[ModelEvents.Created]: { projectId: string; model: Model }
[ModelEvents.Updated]: {
update: BranchUpdateInput | UpdateModelInput
userId: string
oldModel: Model
newModel: Model
}
}
Loading