From a3604331c45fb44bb16a9c2f3041de2a6af2d44e Mon Sep 17 00:00:00 2001 From: Kristaps Fabians Geikins Date: Fri, 25 Oct 2024 14:21:58 +0300 Subject: [PATCH 1/3] commitCreated --- .../modules/core/graph/resolvers/commits.js | 16 -------------- .../core/graph/resolvers/commitsNew.ts | 22 ++++++++++++++++++- .../modules/shared/utils/subscriptions.ts | 11 +++++++++- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/packages/server/modules/core/graph/resolvers/commits.js b/packages/server/modules/core/graph/resolvers/commits.js index 4ddcd88f4..5f10d0526 100644 --- a/packages/server/modules/core/graph/resolvers/commits.js +++ b/packages/server/modules/core/graph/resolvers/commits.js @@ -11,28 +11,12 @@ const { Roles } = require('@speckle/shared') */ // subscription events -const COMMIT_CREATED = CommitPubsubEvents.CommitCreated const COMMIT_UPDATED = CommitPubsubEvents.CommitUpdated const COMMIT_DELETED = CommitPubsubEvents.CommitDeleted /** @type {import('@/modules/core/graph/generated/graphql').Resolvers} */ module.exports = { Subscription: { - commitCreated: { - subscribe: withFilter( - () => pubsub.asyncIterator([COMMIT_CREATED]), - async (payload, variables, context) => { - await authorizeResolver( - context.userId, - payload.streamId, - Roles.Stream.Reviewer, - context.resourceAccessRules - ) - return payload.streamId === variables.streamId - } - ) - }, - commitUpdated: { subscribe: withFilter( () => pubsub.asyncIterator([COMMIT_UPDATED]), diff --git a/packages/server/modules/core/graph/resolvers/commitsNew.ts b/packages/server/modules/core/graph/resolvers/commitsNew.ts index 0dd85644d..177eb1212 100644 --- a/packages/server/modules/core/graph/resolvers/commitsNew.ts +++ b/packages/server/modules/core/graph/resolvers/commitsNew.ts @@ -1,5 +1,9 @@ import { CommitNotFoundError } from '@/modules/core/errors/commit' -import { publish } from '@/modules/shared/utils/subscriptions' +import { + CommitSubscriptions, + filteredSubscribe, + publish +} from '@/modules/shared/utils/subscriptions' import { authorizeResolver } from '@/modules/shared' import { @@ -381,5 +385,21 @@ export = { await batchDeleteCommits(args.input, ctx.userId!) return true } + }, + Subscription: { + commitCreated: { + subscribe: filteredSubscribe( + CommitSubscriptions.CommitCreated, + 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 d97e991b9..f5992ccfc 100644 --- a/packages/server/modules/shared/utils/subscriptions.ts +++ b/packages/server/modules/shared/utils/subscriptions.ts @@ -39,7 +39,9 @@ import { StreamUpdateInput, ProjectUpdateInput, SubscriptionStreamUpdatedArgs, - SubscriptionStreamDeletedArgs + SubscriptionStreamDeletedArgs, + SubscriptionCommitCreatedArgs, + CommitCreateInput } from '@/modules/core/graph/generated/graphql' import { Merge } from 'type-fest' import { @@ -301,6 +303,13 @@ type SubscriptionTypeMap = { payload: { streamDeleted: { streamId: string }; streamId: string } variables: SubscriptionStreamDeletedArgs } + [CommitSubscriptions.CommitCreated]: { + payload: { + commitCreated: CommitCreateInput & { id: string; authorId: string } + streamId: string + } + variables: SubscriptionCommitCreatedArgs + } } & { [k in SubscriptionEvent]: { payload: unknown; variables: unknown } } type SubscriptionEvent = From cbbf9b6ad4b3cb6250168faa92ba67b20aa2bafa Mon Sep 17 00:00:00 2001 From: Kristaps Fabians Geikins Date: Fri, 25 Oct 2024 15:03:15 +0300 Subject: [PATCH 2/3] commitUpdated --- .../modules/core/graph/resolvers/commits.js | 22 ------------------- .../core/graph/resolvers/commitsNew.ts | 20 +++++++++++++++++ .../modules/shared/utils/subscriptions.ts | 8 ++++++- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/packages/server/modules/core/graph/resolvers/commits.js b/packages/server/modules/core/graph/resolvers/commits.js index 5f10d0526..1eb4db88f 100644 --- a/packages/server/modules/core/graph/resolvers/commits.js +++ b/packages/server/modules/core/graph/resolvers/commits.js @@ -11,33 +11,11 @@ const { Roles } = require('@speckle/shared') */ // subscription events -const COMMIT_UPDATED = CommitPubsubEvents.CommitUpdated const COMMIT_DELETED = CommitPubsubEvents.CommitDeleted /** @type {import('@/modules/core/graph/generated/graphql').Resolvers} */ module.exports = { Subscription: { - commitUpdated: { - subscribe: withFilter( - () => pubsub.asyncIterator([COMMIT_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.commitId) { - return payload.commitId === variables.commitId - } - - return streamMatch - } - ) - }, - commitDeleted: { subscribe: withFilter( () => pubsub.asyncIterator([COMMIT_DELETED]), diff --git a/packages/server/modules/core/graph/resolvers/commitsNew.ts b/packages/server/modules/core/graph/resolvers/commitsNew.ts index 177eb1212..e1ca65170 100644 --- a/packages/server/modules/core/graph/resolvers/commitsNew.ts +++ b/packages/server/modules/core/graph/resolvers/commitsNew.ts @@ -401,5 +401,25 @@ export = { } ) } + }, + commitUpdated: { + subscribe: filteredSubscribe( + CommitSubscriptions.CommitUpdated, + async (payload, variables, context) => { + await authorizeResolver( + context.userId, + payload.streamId, + Roles.Stream.Reviewer, + context.resourceAccessRules + ) + + const streamMatch = payload.streamId === variables.streamId + if (streamMatch && variables.commitId) { + return payload.commitId === variables.commitId + } + + return streamMatch + } + ) } } as Resolvers diff --git a/packages/server/modules/shared/utils/subscriptions.ts b/packages/server/modules/shared/utils/subscriptions.ts index f5992ccfc..8d98b4182 100644 --- a/packages/server/modules/shared/utils/subscriptions.ts +++ b/packages/server/modules/shared/utils/subscriptions.ts @@ -41,7 +41,9 @@ import { SubscriptionStreamUpdatedArgs, SubscriptionStreamDeletedArgs, SubscriptionCommitCreatedArgs, - CommitCreateInput + CommitCreateInput, + SubscriptionCommitUpdatedArgs, + CommitUpdateInput } from '@/modules/core/graph/generated/graphql' import { Merge } from 'type-fest' import { @@ -310,6 +312,10 @@ type SubscriptionTypeMap = { } variables: SubscriptionCommitCreatedArgs } + [CommitSubscriptions.CommitUpdated]: { + payload: { commitUpdated: CommitUpdateInput; streamId: string; commitId: string } + variables: SubscriptionCommitUpdatedArgs + } } & { [k in SubscriptionEvent]: { payload: unknown; variables: unknown } } type SubscriptionEvent = From 6cf31becadebd279ef6ab612e5abf76235c76661 Mon Sep 17 00:00:00 2001 From: Kristaps Fabians Geikins Date: Fri, 25 Oct 2024 15:16:31 +0300 Subject: [PATCH 3/3] commitDeleted --- .../modules/core/graph/resolvers/commits.js | 35 ------------- .../resolvers/{commitsNew.ts => commits.ts} | 51 ++++++++++++------- .../modules/shared/utils/subscriptions.ts | 8 +++ 3 files changed, 41 insertions(+), 53 deletions(-) delete mode 100644 packages/server/modules/core/graph/resolvers/commits.js rename packages/server/modules/core/graph/resolvers/{commitsNew.ts => commits.ts} (92%) diff --git a/packages/server/modules/core/graph/resolvers/commits.js b/packages/server/modules/core/graph/resolvers/commits.js deleted file mode 100644 index 1eb4db88f..000000000 --- a/packages/server/modules/core/graph/resolvers/commits.js +++ /dev/null @@ -1,35 +0,0 @@ -const { withFilter } = require('graphql-subscriptions') -const { - pubsub, - CommitSubscriptions: CommitPubsubEvents -} = require('@/modules/shared/utils/subscriptions') -const { authorizeResolver } = require('@/modules/shared') -const { Roles } = require('@speckle/shared') - -/** - * TODO: Clean up and move to commitsNew.ts - */ - -// subscription events -const COMMIT_DELETED = CommitPubsubEvents.CommitDeleted - -/** @type {import('@/modules/core/graph/generated/graphql').Resolvers} */ -module.exports = { - Subscription: { - commitDeleted: { - subscribe: withFilter( - () => pubsub.asyncIterator([COMMIT_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/commitsNew.ts b/packages/server/modules/core/graph/resolvers/commits.ts similarity index 92% rename from packages/server/modules/core/graph/resolvers/commitsNew.ts rename to packages/server/modules/core/graph/resolvers/commits.ts index e1ca65170..edb359e67 100644 --- a/packages/server/modules/core/graph/resolvers/commitsNew.ts +++ b/packages/server/modules/core/graph/resolvers/commits.ts @@ -400,26 +400,41 @@ export = { return payload.streamId === variables.streamId } ) - } - }, - commitUpdated: { - subscribe: filteredSubscribe( - CommitSubscriptions.CommitUpdated, - async (payload, variables, context) => { - await authorizeResolver( - context.userId, - payload.streamId, - Roles.Stream.Reviewer, - context.resourceAccessRules - ) + }, + commitUpdated: { + subscribe: filteredSubscribe( + CommitSubscriptions.CommitUpdated, + async (payload, variables, context) => { + await authorizeResolver( + context.userId, + payload.streamId, + Roles.Stream.Reviewer, + context.resourceAccessRules + ) - const streamMatch = payload.streamId === variables.streamId - if (streamMatch && variables.commitId) { - return payload.commitId === variables.commitId + const streamMatch = payload.streamId === variables.streamId + if (streamMatch && variables.commitId) { + return payload.commitId === variables.commitId + } + + return streamMatch } + ) + }, + commitDeleted: { + subscribe: filteredSubscribe( + CommitSubscriptions.CommitDeleted, + async (payload, variables, context) => { + await authorizeResolver( + context.userId, + payload.streamId, + Roles.Stream.Reviewer, + context.resourceAccessRules + ) - return streamMatch - } - ) + 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 8d98b4182..fde9afb50 100644 --- a/packages/server/modules/shared/utils/subscriptions.ts +++ b/packages/server/modules/shared/utils/subscriptions.ts @@ -58,6 +58,7 @@ import { ProjectAutomationsUpdatedMessageGraphQLReturn } from '@/modules/automate/helpers/graphTypes' import { CommentRecord } from '@/modules/comments/helpers/types' +import { CommitRecord } from '@/modules/core/helpers/types' /** * GraphQL Subscription PubSub instance @@ -316,6 +317,13 @@ type SubscriptionTypeMap = { payload: { commitUpdated: CommitUpdateInput; streamId: string; commitId: string } variables: SubscriptionCommitUpdatedArgs } + [CommitSubscriptions.CommitDeleted]: { + payload: { + commitDeleted: CommitRecord & { streamId: string; branchId: string } + streamId: string + } + variables: SubscriptionCommitUpdatedArgs + } } & { [k in SubscriptionEvent]: { payload: unknown; variables: unknown } } type SubscriptionEvent =