Skip to content

Commit

Permalink
branchCreated sub
Browse files Browse the repository at this point in the history
  • Loading branch information
fabis94 committed Oct 25, 2024
1 parent 678c753 commit cedc1be
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,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
}),
Expand Down
17 changes: 0 additions & 17 deletions packages/server/modules/core/graph/resolvers/branches.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,12 @@ const { Roles } = require('@speckle/shared')
*/

// 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]),
Expand Down
21 changes: 19 additions & 2 deletions packages/server/modules/core/graph/resolvers/branchesNew.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { authorizeResolver } from '@/modules/shared'
import { authorizeResolver, BranchPubsubEvents } from '@/modules/shared'
import {
createBranchAndNotifyFactory,
updateBranchAndNotifyFactory,
Expand Down Expand Up @@ -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 })
Expand Down Expand Up @@ -137,5 +137,22 @@ 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
}
)
}
}
} as Resolvers
9 changes: 8 additions & 1 deletion packages/server/modules/shared/utils/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ import {
StreamUpdateInput,
ProjectUpdateInput,
SubscriptionStreamUpdatedArgs,
SubscriptionStreamDeletedArgs
SubscriptionStreamDeletedArgs,
SubscriptionBranchCreatedArgs
} from '@/modules/core/graph/generated/graphql'
import { Merge } from 'type-fest'
import {
Expand All @@ -54,6 +55,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
Expand Down Expand Up @@ -301,6 +303,10 @@ type SubscriptionTypeMap = {
payload: { streamDeleted: { streamId: string }; streamId: string }
variables: SubscriptionStreamDeletedArgs
}
[BranchSubscriptions.BranchCreated]: {
payload: { branchCreated: BranchRecord; streamId: string }
variables: SubscriptionBranchCreatedArgs
}
} & { [k in SubscriptionEvent]: { payload: unknown; variables: unknown } }

type SubscriptionEvent =
Expand All @@ -311,6 +317,7 @@ type SubscriptionEvent =
| StreamSubscriptions
| UserSubscriptions
| ViewerSubscriptions
| BranchSubscriptions

/**
* Publish a GQL subscription event
Expand Down

0 comments on commit cedc1be

Please sign in to comment.