diff --git a/app/graph/mutations/tipline_message_mutations.rb b/app/graph/mutations/tipline_message_mutations.rb index 9d84b7cb37..686444dbd9 100644 --- a/app/graph/mutations/tipline_message_mutations.rb +++ b/app/graph/mutations/tipline_message_mutations.rb @@ -1,17 +1,16 @@ module TiplineMessageMutations class Send < Mutations::BaseMutation - argument :uid, GraphQL::Types::ID, required: true + argument :in_reply_to_id, GraphQL::Types::Int, required: true # Database ID of a tipline request ("smooch" annotation) argument :message, GraphQL::Types::String, required: true - argument :timestamp, GraphQL::Types::Int, required: true - argument :language, GraphQL::Types::String, required: true field :success, GraphQL::Types::Boolean, null: true - def resolve(uid: nil, message: nil, timestamp: nil, language: nil) + def resolve(in_reply_to_id: nil, message: nil) + request = Annotation.find(in_reply_to_id).load ability = context[:ability] || Ability.new success = false - if Team.current&.id && User.current&.id && ability.can?(:send, TiplineMessage.new(team: Team.current)) - success = Bot::Smooch.send_custom_message_to_user(Team.current, uid, timestamp, message, language) + if Team.current&.id && User.current&.id && ability.can?(:send, TiplineMessage.new(team: Team.current)) && request.annotated.team_id == Team.current.id + success = Bot::Smooch.reply_to_request_with_custom_message(request, message) end { success: success } end diff --git a/app/graph/types/dynamic_annotation_field_type.rb b/app/graph/types/dynamic_annotation_field_type.rb index ac9ac4e782..3e2d92fd69 100644 --- a/app/graph/types/dynamic_annotation_field_type.rb +++ b/app/graph/types/dynamic_annotation_field_type.rb @@ -6,6 +6,7 @@ class DynamicAnnotationFieldType < DefaultObject field :dbid, GraphQL::Types::Int, null: true field :value_json, JsonStringType, null: true field :annotation, AnnotationType, null: true + field :annotation_id, GraphQL::Types::Int, null: true field :associated_graphql_id, GraphQL::Types::String, null: true field :smooch_user_slack_channel_url, GraphQL::Types::String, null: true field :smooch_user_external_identifier, GraphQL::Types::String, null: true diff --git a/app/models/concerns/smooch_messages.rb b/app/models/concerns/smooch_messages.rb index 73314da90a..c8f69a9461 100644 --- a/app/models/concerns/smooch_messages.rb +++ b/app/models/concerns/smooch_messages.rb @@ -437,6 +437,11 @@ def send_message_to_user_on_timeout(uid, language) sm.reset end + def reply_to_request_with_custom_message(request, message) + data = JSON.parse(request.get_field_value('smooch_data')) + self.send_custom_message_to_user(request.annotated.team, data['authorId'], data['received'], message, data['language']) + end + def send_custom_message_to_user(team, uid, timestamp, message, language) platform = self.get_user_platform(uid) RequestStore.store[:smooch_bot_platform] = platform diff --git a/lib/relay.idl b/lib/relay.idl index 9bdc3acc76..0f5887a61d 100644 --- a/lib/relay.idl +++ b/lib/relay.idl @@ -4576,6 +4576,7 @@ DynamicAnnotation::Field type """ type DynamicAnnotationField implements Node { annotation: Annotation + annotation_id: Int associated_graphql_id: String created_at: String dbid: Int @@ -11966,10 +11967,8 @@ input SendInput { A unique identifier for the client performing the mutation. """ clientMutationId: String - language: String! + inReplyToId: Int! message: String! - timestamp: Int! - uid: ID! } """ diff --git a/public/relay.json b/public/relay.json index 577cc43b79..5459ac477c 100644 --- a/public/relay.json +++ b/public/relay.json @@ -25854,6 +25854,20 @@ "isDeprecated": false, "deprecationReason": null }, + { + "name": "annotation_id", + "description": null, + "args": [ + + ], + "type": { + "kind": "SCALAR", + "name": "Int", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, { "name": "associated_graphql_id", "description": null, @@ -62780,39 +62794,7 @@ "fields": null, "inputFields": [ { - "name": "uid", - "description": null, - "type": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "SCALAR", - "name": "ID", - "ofType": null - } - }, - "defaultValue": null, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "message", - "description": null, - "type": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "SCALAR", - "name": "String", - "ofType": null - } - }, - "defaultValue": null, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "timestamp", + "name": "inReplyToId", "description": null, "type": { "kind": "NON_NULL", @@ -62828,7 +62810,7 @@ "deprecationReason": null }, { - "name": "language", + "name": "message", "description": null, "type": { "kind": "NON_NULL", diff --git a/test/controllers/graphql_controller_10_test.rb b/test/controllers/graphql_controller_10_test.rb index 50d4a414d8..0193d1efb8 100644 --- a/test/controllers/graphql_controller_10_test.rb +++ b/test/controllers/graphql_controller_10_test.rb @@ -784,9 +784,11 @@ def setup u = create_user t = create_team create_team_user user: u, team: t, role: 'editor' + pm = create_project_media team: t + a = create_dynamic_annotation annotation_type: 'smooch', set_fields: { smooch_data: { authorId: '123', language: 'en', received: Time.now.to_f }.to_json }.to_json, annotated: pm authenticate_with_user(u) - query = 'mutation { sendTiplineMessage(input: { clientMutationId: "1", uid: "123456", message: "Hello", language: "en", timestamp: 1695692221 }) { success } }' + query = "mutation { sendTiplineMessage(input: { clientMutationId: \"1\", message: \"Hello\", inReplyToId: #{a.id} }) { success } }" post :create, params: { query: query, team: t.slug } assert_response :success @@ -797,9 +799,11 @@ def setup Bot::Smooch.stubs(:send_message_to_user).returns(OpenStruct.new(code: 200)).never u = create_user t = create_team + pm = create_project_media team: t + a = create_dynamic_annotation annotation_type: 'smooch', set_fields: { smooch_data: { authorId: '123', language: 'en', received: Time.now.to_f }.to_json }.to_json, annotated: pm authenticate_with_user(u) - query = 'mutation { sendTiplineMessage(input: { clientMutationId: "1", uid: "123456", message: "Hello", language: "en", timestamp: 1695692221 }) { success } }' + query = "mutation { sendTiplineMessage(input: { clientMutationId: \"1\", message: \"Hello\", inReplyToId: #{a.id} }) { success } }" post :create, params: { query: query, team: t.slug } assert_response :success