Skip to content

Commit

Permalink
Activitystream IoC 4 addCommitCreatedActivity (#3239)
Browse files Browse the repository at this point in the history
* chore(activitystream): addStreamUpdatedActivity refactor multi region

* chore(activitystream): addStreamAccessRequestedActivity refactor multiregion

* chore(activitystream): addStreamAccessRequestDeclinedActivity refactor multiregion

* chore(activitystream): onUserCreated refactor multiregion

* chore(activitystream): addCommitCreatedActivity refactor multiregion

* chore(activitystream): fix types
  • Loading branch information
alemagio authored Oct 11, 2024
1 parent acf38e7 commit d489975
Show file tree
Hide file tree
Showing 17 changed files with 157 additions and 82 deletions.
17 changes: 16 additions & 1 deletion packages/server/modules/activitystream/domain/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ import {
StreamScopeActivity
} from '@/modules/activitystream/helpers/types'
import {
CommitCreateInput,
ProjectUpdateInput,
StreamUpdateInput
} from '@/modules/core/graph/generated/graphql'
import { StreamAclRecord, StreamRecord } from '@/modules/core/helpers/types'
import {
CommitRecord,
StreamAclRecord,
StreamRecord
} from '@/modules/core/helpers/types'

export type GetActivity = (
streamId: string,
Expand Down Expand Up @@ -197,3 +202,13 @@ export type AddStreamAccessRequestDeclinedActivity = (params: {
requesterId: string
declinerId: string
}) => Promise<void>

export type AddCommitCreatedActivity = (params: {
commitId: string
streamId: string
userId: string
input: CommitCreateInput
branchName: string
modelId: string
commit: CommitRecord
}) => Promise<void>
97 changes: 55 additions & 42 deletions packages/server/modules/activitystream/services/commitActivity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ActionTypes, ResourceTypes } from '@/modules/activitystream/helpers/types'
import {
CommitSubscriptions as CommitPubsubEvents,
PublishSubscription,
pubsub
} from '@/modules/shared/utils/subscriptions'
import {
Expand All @@ -15,53 +16,65 @@ import { ProjectSubscriptions, publish } from '@/modules/shared/utils/subscripti
import { has } from 'lodash'
import { saveActivityFactory } from '@/modules/activitystream/repositories'
import { db } from '@/db/knex'
import {
AddCommitCreatedActivity,
SaveActivity
} from '@/modules/activitystream/domain/operations'

/**
* Save "new commit created" activity item
*/
export async function addCommitCreatedActivity(params: {
commitId: string
streamId: string
userId: string
input: CommitCreateInput
branchName: string
modelId: string
commit: CommitRecord
}) {
const { commitId, input, streamId, userId, branchName, commit } = params
await Promise.all([
saveActivityFactory({ db })({
streamId,
resourceType: ResourceTypes.Commit,
resourceId: commitId,
actionType: ActionTypes.Commit.Create,
userId,
info: {
id: commitId,
commit: {
...input,
projectId: streamId,
modelId: params.modelId,
versionId: commit.id
export const addCommitCreatedActivityFactory =
({
saveActivity,
publish
}: {
saveActivity: SaveActivity
publish: PublishSubscription
}): AddCommitCreatedActivity =>
async (params: {
commitId: string
streamId: string
userId: string
input: CommitCreateInput
branchName: string
modelId: string
commit: CommitRecord
}) => {
const { commitId, input, streamId, userId, branchName, commit } = params
await Promise.all([
saveActivity({
streamId,
resourceType: ResourceTypes.Commit,
resourceId: commitId,
actionType: ActionTypes.Commit.Create,
userId,
info: {
id: commitId,
commit: {
...input,
projectId: streamId,
modelId: params.modelId,
versionId: commit.id
}
},
message: `Commit created on branch ${branchName}: ${commitId} (${input.message})`
}),
publish(CommitPubsubEvents.CommitCreated, {
commitCreated: { ...input, id: commitId, authorId: userId },
streamId
}),
publish(ProjectSubscriptions.ProjectVersionsUpdated, {
projectId: streamId,
projectVersionsUpdated: {
id: commit.id,
version: commit,
type: ProjectVersionsUpdatedMessageType.Created,
modelId: null
}
},
message: `Commit created on branch ${branchName}: ${commitId} (${input.message})`
}),
pubsub.publish(CommitPubsubEvents.CommitCreated, {
commitCreated: { ...input, id: commitId, authorId: userId },
streamId
}),
publish(ProjectSubscriptions.ProjectVersionsUpdated, {
projectId: streamId,
projectVersionsUpdated: {
id: commit.id,
version: commit,
type: ProjectVersionsUpdatedMessageType.Created,
modelId: null
}
})
])
}
})
])
}

const isOldVersionUpdateInput = (
i: CommitUpdateInput | UpdateVersionInput
Expand Down
9 changes: 7 additions & 2 deletions packages/server/modules/cli/commands/download/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ import {
import { validateInputAttachmentsFactory } from '@/modules/comments/services/commentTextService'
import { getBlobsFactory } from '@/modules/blobstorage/repositories'
import { createCommitByBranchIdFactory } from '@/modules/core/services/commit/management'
import { addCommitCreatedActivity } from '@/modules/activitystream/services/commitActivity'
import { VersionsEmitter } from '@/modules/core/events/versionsEmitter'
import { addCommitCreatedActivityFactory } from '@/modules/activitystream/services/commitActivity'
import { saveActivityFactory } from '@/modules/activitystream/repositories'
import { publish } from '@/modules/shared/utils/subscriptions'

const command: CommandModule<
unknown,
Expand Down Expand Up @@ -140,7 +142,10 @@ const command: CommandModule<
markCommitStreamUpdated,
markCommitBranchUpdated: markCommitBranchUpdatedFactory({ db }),
versionsEventEmitter: VersionsEmitter.emit,
addCommitCreatedActivity
addCommitCreatedActivity: addCommitCreatedActivityFactory({
saveActivity: saveActivityFactory({ db }),
publish
})
})

const getStreamCollaborators = getStreamCollaboratorsFactory({ db })
Expand Down
7 changes: 5 additions & 2 deletions packages/server/modules/cli/commands/download/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import { getBlobsFactory } from '@/modules/blobstorage/repositories'
import { validateInputAttachmentsFactory } from '@/modules/comments/services/commentTextService'
import { addBranchCreatedActivity } from '@/modules/activitystream/services/branchActivity'
import { VersionsEmitter } from '@/modules/core/events/versionsEmitter'
import { addCommitCreatedActivity } from '@/modules/activitystream/services/commitActivity'
import { createStreamReturnRecordFactory } from '@/modules/core/services/streams/management'
import { inviteUsersToProjectFactory } from '@/modules/serverinvites/services/projectInviteManagement'
import { createAndSendInviteFactory } from '@/modules/serverinvites/services/creation'
Expand All @@ -71,6 +70,7 @@ import { ProjectsEmitter } from '@/modules/core/events/projectsEmitter'
import { addStreamCreatedActivityFactory } from '@/modules/activitystream/services/streamActivity'
import { saveActivityFactory } from '@/modules/activitystream/repositories'
import { publish } from '@/modules/shared/utils/subscriptions'
import { addCommitCreatedActivityFactory } from '@/modules/activitystream/services/commitActivity'

const command: CommandModule<
unknown,
Expand Down Expand Up @@ -151,7 +151,10 @@ const command: CommandModule<
markCommitStreamUpdated,
markCommitBranchUpdated: markCommitBranchUpdatedFactory({ db }),
versionsEventEmitter: VersionsEmitter.emit,
addCommitCreatedActivity
addCommitCreatedActivity: addCommitCreatedActivityFactory({
saveActivity: saveActivityFactory({ db }),
publish
})
})

const createStreamReturnRecord = createStreamReturnRecordFactory({
Expand Down
11 changes: 7 additions & 4 deletions packages/server/modules/comments/tests/comments.graph.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ const {
grantStreamPermissionsFactory
} = require('@/modules/core/repositories/streams')
const { VersionsEmitter } = require('@/modules/core/events/versionsEmitter')
const {
addCommitCreatedActivity
} = require('@/modules/activitystream/services/commitActivity')
const { getObjectFactory } = require('@/modules/core/repositories/objects')
const {
legacyCreateStreamFactory,
Expand Down Expand Up @@ -88,6 +85,9 @@ const {
} = require('@/modules/activitystream/services/streamActivity')
const { saveActivityFactory } = require('@/modules/activitystream/repositories')
const { publish } = require('@/modules/shared/utils/subscriptions')
const {
addCommitCreatedActivityFactory
} = require('@/modules/activitystream/services/commitActivity')

const streamResourceCheck = streamResourceCheckFactory({
checkStreamResourceAccess: checkStreamResourceAccessFactory({ db })
Expand Down Expand Up @@ -115,7 +115,10 @@ const createCommitByBranchId = createCommitByBranchIdFactory({
markCommitStreamUpdated,
markCommitBranchUpdated: markCommitBranchUpdatedFactory({ db }),
versionsEventEmitter: VersionsEmitter.emit,
addCommitCreatedActivity
addCommitCreatedActivity: addCommitCreatedActivityFactory({
saveActivity: saveActivityFactory({ db }),
publish
})
})

const createCommitByBranchName = createCommitByBranchNameFactory({
Expand Down
11 changes: 7 additions & 4 deletions packages/server/modules/comments/tests/comments.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ const {
createBranchFactory
} = require('@/modules/core/repositories/branches')
const { VersionsEmitter } = require('@/modules/core/events/versionsEmitter')
const {
addCommitCreatedActivity
} = require('@/modules/activitystream/services/commitActivity')
const { getObjectFactory } = require('@/modules/core/repositories/objects')
const {
legacyCreateStreamFactory,
Expand Down Expand Up @@ -107,6 +104,9 @@ const {
} = require('@/modules/activitystream/services/streamActivity')
const { saveActivityFactory } = require('@/modules/activitystream/repositories')
const { publish } = require('@/modules/shared/utils/subscriptions')
const {
addCommitCreatedActivityFactory
} = require('@/modules/activitystream/services/commitActivity')

const getStream = getStreamFactory({ db })
const streamResourceCheck = streamResourceCheckFactory({
Expand Down Expand Up @@ -164,7 +164,10 @@ const createCommitByBranchId = createCommitByBranchIdFactory({
markCommitStreamUpdated,
markCommitBranchUpdated: markCommitBranchUpdatedFactory({ db }),
versionsEventEmitter: VersionsEmitter.emit,
addCommitCreatedActivity
addCommitCreatedActivity: addCommitCreatedActivityFactory({
saveActivity: saveActivityFactory({ db }),
publish
})
})

const createCommitByBranchName = createCommitByBranchNameFactory({
Expand Down
13 changes: 9 additions & 4 deletions packages/server/modules/core/graph/resolvers/commits.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const { CommitNotFoundError } = require('@/modules/core/errors/commit')
const { withFilter } = require('graphql-subscriptions')
const {
pubsub,
CommitSubscriptions: CommitPubsubEvents
CommitSubscriptions: CommitPubsubEvents,
publish
} = require('@/modules/shared/utils/subscriptions')
const { authorizeResolver } = require('@/modules/shared')

Expand Down Expand Up @@ -66,15 +67,16 @@ const {
} = require('@/modules/core/repositories/branches')
const {
addCommitDeletedActivity,
addCommitCreatedActivity,
addCommitUpdatedActivity,
addCommitMovedActivity
addCommitMovedActivity,
addCommitCreatedActivityFactory
} = require('@/modules/activitystream/services/commitActivity')
const { VersionsEmitter } = require('@/modules/core/events/versionsEmitter')
const { getObjectFactory } = require('@/modules/core/repositories/objects')
const {
validateStreamAccessFactory
} = require('@/modules/core/services/streams/access')
const { saveActivityFactory } = require('@/modules/activitystream/repositories')

// subscription events
const COMMIT_CREATED = CommitPubsubEvents.CommitCreated
Expand Down Expand Up @@ -102,7 +104,10 @@ const createCommitByBranchId = createCommitByBranchIdFactory({
markCommitStreamUpdated,
markCommitBranchUpdated: markCommitBranchUpdatedFactory({ db }),
versionsEventEmitter: VersionsEmitter.emit,
addCommitCreatedActivity
addCommitCreatedActivity: addCommitCreatedActivityFactory({
saveActivity: saveActivityFactory({ db }),
publish
})
})

const createCommitByBranchName = createCommitByBranchNameFactory({
Expand Down
11 changes: 8 additions & 3 deletions packages/server/modules/core/graph/resolvers/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { Resolvers } from '@/modules/core/graph/generated/graphql'
import { authorizeResolver } from '@/modules/shared'
import {
filteredSubscribe,
ProjectSubscriptions
ProjectSubscriptions,
publish
} from '@/modules/shared/utils/subscriptions'
import { getServerOrigin } from '@/modules/shared/helpers/envHelper'
import {
Expand Down Expand Up @@ -47,11 +48,12 @@ import {
} from '@/modules/core/repositories/streams'
import { VersionsEmitter } from '@/modules/core/events/versionsEmitter'
import {
addCommitCreatedActivity,
addCommitCreatedActivityFactory,
addCommitMovedActivity,
addCommitUpdatedActivity
} from '@/modules/activitystream/services/commitActivity'
import { getObjectFactory } from '@/modules/core/repositories/objects'
import { saveActivityFactory } from '@/modules/activitystream/repositories'

const getCommitStream = getCommitStreamFactory({ db })
const getStream = getStreamFactory({ db })
Expand All @@ -66,7 +68,10 @@ const createCommitByBranchId = createCommitByBranchIdFactory({
markCommitStreamUpdated,
markCommitBranchUpdated: markCommitBranchUpdatedFactory({ db }),
versionsEventEmitter: VersionsEmitter.emit,
addCommitCreatedActivity
addCommitCreatedActivity: addCommitCreatedActivityFactory({
saveActivity: saveActivityFactory({ db }),
publish
})
})

const updateCommitAndNotify = updateCommitAndNotifyFactory({
Expand Down
4 changes: 2 additions & 2 deletions packages/server/modules/core/services/commit/management.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { db } from '@/db/knex'
import { AddCommitCreatedActivity } from '@/modules/activitystream/domain/operations'
import {
addCommitCreatedActivity,
addCommitDeletedActivity,
addCommitReceivedActivity,
addCommitUpdatedActivity
Expand Down Expand Up @@ -89,7 +89,7 @@ export const createCommitByBranchIdFactory =
markCommitStreamUpdated: typeof markCommitStreamUpdated
markCommitBranchUpdated: MarkCommitBranchUpdated
versionsEventEmitter: VersionsEventEmitter
addCommitCreatedActivity: typeof addCommitCreatedActivity
addCommitCreatedActivity: AddCommitCreatedActivity
}): CreateCommitByBranchId =>
async (
params: {
Expand Down
11 changes: 7 additions & 4 deletions packages/server/modules/core/tests/branches.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ const {
insertBranchCommitsFactory
} = require('@/modules/core/repositories/commits')
const { VersionsEmitter } = require('@/modules/core/events/versionsEmitter')
const {
addCommitCreatedActivity
} = require('@/modules/activitystream/services/commitActivity')
const { getObjectFactory } = require('@/modules/core/repositories/objects')
const {
legacyCreateStreamFactory,
Expand Down Expand Up @@ -77,6 +74,9 @@ const {
} = require('@/modules/activitystream/services/streamActivity')
const { saveActivityFactory } = require('@/modules/activitystream/repositories')
const { publish } = require('@/modules/shared/utils/subscriptions')
const {
addCommitCreatedActivityFactory
} = require('@/modules/activitystream/services/commitActivity')

const db = knex
const Commits = () => knex('commits')
Expand Down Expand Up @@ -108,7 +108,10 @@ const createCommitByBranchId = createCommitByBranchIdFactory({
markCommitStreamUpdated,
markCommitBranchUpdated: markCommitBranchUpdatedFactory({ db }),
versionsEventEmitter: VersionsEmitter.emit,
addCommitCreatedActivity
addCommitCreatedActivity: addCommitCreatedActivityFactory({
saveActivity: saveActivityFactory({ db }),
publish
})
})

const createCommitByBranchName = createCommitByBranchNameFactory({
Expand Down
Loading

0 comments on commit d489975

Please sign in to comment.