diff --git a/packages/server/modules/activitystream/services/branchActivity.ts b/packages/server/modules/activitystream/services/branchActivity.ts index bbebbf7e14..22ef8c0a23 100644 --- a/packages/server/modules/activitystream/services/branchActivity.ts +++ b/packages/server/modules/activitystream/services/branchActivity.ts @@ -1,6 +1,5 @@ import { ActionTypes, ResourceTypes } from '@/modules/activitystream/helpers/types' import { - pubsub, BranchSubscriptions as BranchPubsubEvents, PublishSubscription } from '@/modules/shared/utils/subscriptions' @@ -38,8 +37,7 @@ export const addBranchCreatedActivityFactory = info: { branch }, message: `Branch created: ${branch.name} (${branch.id})` }), - // @deprecated - pubsub.publish(BranchPubsubEvents.BranchCreated, { + publish(BranchPubsubEvents.BranchCreated, { branchCreated: { ...branch }, streamId: branch.streamId }), @@ -76,8 +74,7 @@ export const addBranchUpdatedActivityFactory = info: { old: oldBranch, new: update }, message: `Branch metadata changed for branch ${update.id}` }), - // @deprecated - pubsub.publish(BranchPubsubEvents.BranchUpdated, { + publish(BranchPubsubEvents.BranchUpdated, { branchUpdated: { ...update }, streamId, branchId: update.id @@ -115,7 +112,7 @@ export const addBranchDeletedActivityFactory = info: { branch: { ...input, name: branchName } }, message: `Branch deleted: '${branchName}' (${input.id})` }), - pubsub.publish(BranchPubsubEvents.BranchDeleted, { + publish(BranchPubsubEvents.BranchDeleted, { branchDeleted: input, streamId }), diff --git a/packages/server/modules/core/graph/resolvers/branches.js b/packages/server/modules/core/graph/resolvers/branches.js deleted file mode 100644 index 69eeb7766b..0000000000 --- a/packages/server/modules/core/graph/resolvers/branches.js +++ /dev/null @@ -1,74 +0,0 @@ -const { withFilter } = require('graphql-subscriptions') -const { - pubsub, - BranchSubscriptions: BranchPubsubEvents -} = require('@/modules/shared/utils/subscriptions') -const { authorizeResolver } = require('@/modules/shared') -const { Roles } = require('@speckle/shared') - -/** - * TODO: Clean up and move to branchesNew.ts - */ - -// subscription events -const BRANCH_CREATED = BranchPubsubEvents.BranchCreated -const BRANCH_UPDATED = BranchPubsubEvents.BranchUpdated -const BRANCH_DELETED = BranchPubsubEvents.BranchDeleted - -/** @type {import('@/modules/core/graph/generated/graphql').Resolvers} */ -module.exports = { - Subscription: { - branchCreated: { - subscribe: withFilter( - () => pubsub.asyncIterator([BRANCH_CREATED]), - async (payload, variables, context) => { - await authorizeResolver( - context.userId, - payload.streamId, - Roles.Stream.Reviewer, - context.resourceAccessRules - ) - - return payload.streamId === variables.streamId - } - ) - }, - - branchUpdated: { - subscribe: withFilter( - () => pubsub.asyncIterator([BRANCH_UPDATED]), - async (payload, variables, context) => { - await authorizeResolver( - context.userId, - payload.streamId, - Roles.Stream.Reviewer, - context.resourceAccessRules - ) - - const streamMatch = payload.streamId === variables.streamId - if (streamMatch && variables.branchId) { - return payload.branchId === variables.branchId - } - - return streamMatch - } - ) - }, - - branchDeleted: { - subscribe: withFilter( - () => pubsub.asyncIterator([BRANCH_DELETED]), - async (payload, variables, context) => { - await authorizeResolver( - context.userId, - payload.streamId, - Roles.Stream.Reviewer, - context.resourceAccessRules - ) - - return payload.streamId === variables.streamId - } - ) - } - } -} diff --git a/packages/server/modules/core/graph/resolvers/branchesNew.ts b/packages/server/modules/core/graph/resolvers/branches.ts similarity index 74% rename from packages/server/modules/core/graph/resolvers/branchesNew.ts rename to packages/server/modules/core/graph/resolvers/branches.ts index d0afd44d80..a3b068c0df 100644 --- a/packages/server/modules/core/graph/resolvers/branchesNew.ts +++ b/packages/server/modules/core/graph/resolvers/branches.ts @@ -1,4 +1,4 @@ -import { authorizeResolver } from '@/modules/shared' +import { authorizeResolver, BranchPubsubEvents } from '@/modules/shared' import { createBranchAndNotifyFactory, updateBranchAndNotifyFactory, @@ -30,7 +30,7 @@ import { legacyGetUserFactory } from '@/modules/core/repositories/users' import { Resolvers } from '@/modules/core/graph/generated/graphql' import { getPaginatedStreamBranchesFactory } from '@/modules/core/services/branch/retrieval' import { saveActivityFactory } from '@/modules/activitystream/repositories' -import { publish } from '@/modules/shared/utils/subscriptions' +import { filteredSubscribe, publish } from '@/modules/shared/utils/subscriptions' const markBranchStreamUpdated = markBranchStreamUpdatedFactory({ db }) const getStream = getStreamFactory({ db }) @@ -137,5 +137,57 @@ export = { const deleted = await deleteBranchAndNotify(args.branch, context.userId!) return deleted } + }, + Subscription: { + branchCreated: { + subscribe: filteredSubscribe( + BranchPubsubEvents.BranchCreated, + async (payload, variables, context) => { + await authorizeResolver( + context.userId, + payload.streamId, + Roles.Stream.Reviewer, + context.resourceAccessRules + ) + + return payload.streamId === variables.streamId + } + ) + }, + branchUpdated: { + subscribe: filteredSubscribe( + BranchPubsubEvents.BranchUpdated, + async (payload, variables, context) => { + await authorizeResolver( + context.userId, + payload.streamId, + Roles.Stream.Reviewer, + context.resourceAccessRules + ) + + const streamMatch = payload.streamId === variables.streamId + if (streamMatch && variables.branchId) { + return payload.branchId === variables.branchId + } + + return streamMatch + } + ) + }, + branchDeleted: { + subscribe: filteredSubscribe( + BranchPubsubEvents.BranchDeleted, + async (payload, variables, context) => { + await authorizeResolver( + context.userId, + payload.streamId, + Roles.Stream.Reviewer, + context.resourceAccessRules + ) + + return payload.streamId === variables.streamId + } + ) + } } } as Resolvers diff --git a/packages/server/modules/shared/utils/subscriptions.ts b/packages/server/modules/shared/utils/subscriptions.ts index d97e991b96..36ea1f9812 100644 --- a/packages/server/modules/shared/utils/subscriptions.ts +++ b/packages/server/modules/shared/utils/subscriptions.ts @@ -39,7 +39,14 @@ import { StreamUpdateInput, ProjectUpdateInput, SubscriptionStreamUpdatedArgs, - SubscriptionStreamDeletedArgs + SubscriptionStreamDeletedArgs, + SubscriptionBranchCreatedArgs, + SubscriptionBranchUpdatedArgs, + BranchUpdateInput, + UpdateModelInput, + SubscriptionBranchDeletedArgs, + BranchDeleteInput, + DeleteModelInput } from '@/modules/core/graph/generated/graphql' import { Merge } from 'type-fest' import { @@ -54,6 +61,7 @@ import { ProjectAutomationsUpdatedMessageGraphQLReturn } from '@/modules/automate/helpers/graphTypes' import { CommentRecord } from '@/modules/comments/helpers/types' +import { BranchRecord } from '@/modules/core/helpers/types' /** * GraphQL Subscription PubSub instance @@ -301,6 +309,22 @@ type SubscriptionTypeMap = { payload: { streamDeleted: { streamId: string }; streamId: string } variables: SubscriptionStreamDeletedArgs } + [BranchSubscriptions.BranchCreated]: { + payload: { branchCreated: BranchRecord; streamId: string } + variables: SubscriptionBranchCreatedArgs + } + [BranchSubscriptions.BranchUpdated]: { + payload: { + branchUpdated: BranchUpdateInput | UpdateModelInput + streamId: string + branchId: string + } + variables: SubscriptionBranchUpdatedArgs + } + [BranchSubscriptions.BranchDeleted]: { + payload: { branchDeleted: BranchDeleteInput | DeleteModelInput; streamId: string } + variables: SubscriptionBranchDeletedArgs + } } & { [k in SubscriptionEvent]: { payload: unknown; variables: unknown } } type SubscriptionEvent = @@ -311,6 +335,7 @@ type SubscriptionEvent = | StreamSubscriptions | UserSubscriptions | ViewerSubscriptions + | BranchSubscriptions /** * Publish a GQL subscription event