From dee14709c873511104deb99484d64ab12f00cc2a Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Mon, 24 Sep 2018 12:34:27 -0600 Subject: [PATCH] feat: added reaction adding mutation --- .../server/graph/tenant/mutators/comment.ts | 11 +++++- .../server/graph/tenant/resolvers/mutation.ts | 12 ++++--- .../server/graph/tenant/schema/schema.graphql | 36 +++++++++++++++++++ src/core/server/services/comments/index.ts | 29 +++++++++++++++ 4 files changed, 83 insertions(+), 5 deletions(-) diff --git a/src/core/server/graph/tenant/mutators/comment.ts b/src/core/server/graph/tenant/mutators/comment.ts index aeaac70ee..8a8e86066 100644 --- a/src/core/server/graph/tenant/mutators/comment.ts +++ b/src/core/server/graph/tenant/mutators/comment.ts @@ -1,9 +1,14 @@ import TenantContext from "talk-server/graph/tenant/context"; import { GQLCreateCommentInput, + GQLCreateCommentReactionInput, GQLEditCommentInput, } from "talk-server/graph/tenant/schema/__generated__/types"; -import { create, edit } from "talk-server/services/comments"; +import { + create, + createCommentReaction, + edit, +} from "talk-server/services/comments"; export default (ctx: TenantContext) => ({ create: (input: GQLCreateCommentInput) => @@ -30,4 +35,8 @@ export default (ctx: TenantContext) => ({ }, ctx.req ), + createReaction: (input: GQLCreateCommentReactionInput) => + createCommentReaction(ctx.mongo, ctx.tenant, ctx.user!, { + item_id: input.commentID, + }), }); diff --git a/src/core/server/graph/tenant/resolvers/mutation.ts b/src/core/server/graph/tenant/resolvers/mutation.ts index 5b7c65dc4..2ff79b8d2 100644 --- a/src/core/server/graph/tenant/resolvers/mutation.ts +++ b/src/core/server/graph/tenant/resolvers/mutation.ts @@ -9,10 +9,10 @@ const Mutation: GQLMutationTypeResolver = { const comment = await ctx.mutators.Comment.create(input); return { edge: { - // (cvle) - // Depending on the sort we can't determine the accurate cursor - // in a performant way, so we return null instead. - // It seems that Relay does not directly use this value... + // NOTE: (cvle) + // Depending on the sort we can't determine the accurate cursor in a + // performant way, so we return null instead. It seems that Relay does + // not directly use this value. cursor: null, node: comment, }, @@ -23,6 +23,10 @@ const Mutation: GQLMutationTypeResolver = { settings: await ctx.mutators.Settings.update(input.settings), clientMutationId: input.clientMutationId, }), + createCommentReaction: async (source, { input }, ctx) => ({ + comment: await ctx.mutators.Comment.createReaction(input), + clientMutationId: input.clientMutationId, + }), }; export default Mutation; diff --git a/src/core/server/graph/tenant/schema/schema.graphql b/src/core/server/graph/tenant/schema/schema.graphql index 2f275ca0f..f03de4ffc 100644 --- a/src/core/server/graph/tenant/schema/schema.graphql +++ b/src/core/server/graph/tenant/schema/schema.graphql @@ -1478,6 +1478,34 @@ type UpdateSettingsPayload { ## Mutation ################## +input CreateCommentReactionInput { + """ + commentID is the Comment's ID that we want to create a Reaction on. + """ + commentID: ID! + + """ + clientMutationId is required for Relay support. + """ + clientMutationId: String! +} + +type CreateCommentReactionPayload { + """ + comment is the Comment that the Reaction was created on. + """ + comment: Comment + + """ + clientMutationId is required for Relay support. + """ + clientMutationId: String! +} + +################## +## Mutation +################## + type Mutation { """ createComment will create a Comment as the current logged in User. @@ -1495,6 +1523,14 @@ type Mutation { """ updateSettings(input: UpdateSettingsInput!): UpdateSettingsPayload @auth(roles: [ADMIN, MODERATOR]) + + """ + createCommentReaction will create a Reaction authored by the current logged in + user on a Comment. + """ + createCommentReaction( + input: CreateCommentReactionInput! + ): CreateCommentReactionPayload @auth } ################################################################################ diff --git a/src/core/server/services/comments/index.ts b/src/core/server/services/comments/index.ts index 7acc5f5bb..ee1fb969d 100644 --- a/src/core/server/services/comments/index.ts +++ b/src/core/server/services/comments/index.ts @@ -3,6 +3,7 @@ import { Db } from "mongodb"; import { Omit } from "talk-common/types"; import { ACTION_ITEM_TYPE, + ACTION_TYPE, CreateActionInput, createActions, encodeActionCounts, @@ -220,3 +221,31 @@ async function addCommentActions( return comment; } + +export type CreateCommentReaction = Pick; + +export async function createCommentReaction( + mongo: Db, + tenant: Tenant, + author: User, + input: CreateCommentReaction +) { + // Get the Comment that we are leaving the Action on. + let comment = await retrieveComment(mongo, tenant.id, input.item_id); + if (!comment) { + // TODO: replace to match error returned by the models/comments.ts + throw new Error("comment not found"); + } + + // Add the comment actions, and return the Comment that we just updated. + comment = await addCommentActions(mongo, tenant, comment, [ + { + action_type: ACTION_TYPE.REACTION, + item_type: ACTION_ITEM_TYPE.COMMENTS, + item_id: input.item_id, + user_id: author.id, + }, + ]); + + return comment; +}