Skip to content

Commit

Permalink
Changing sendTiplineMessage mutation so that it requires only one a…
Browse files Browse the repository at this point in the history
…rgument besides the message

For now the only use case is to allow custom messages to be sent in reply to previous user requests. So, this PR changes the `sendTiplineMessage` mutation... instead of requiring `uid`, `timestamp` and `language`, it just requires the ID of the request (`smooch` annotation).

References: CV2-3643 and CV2-3677.
  • Loading branch information
caiosba authored Sep 28, 2023
1 parent c01ad0e commit 11879ca
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 45 deletions.
11 changes: 5 additions & 6 deletions app/graph/mutations/tipline_message_mutations.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions app/graph/types/dynamic_annotation_field_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions app/models/concerns/smooch_messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions lib/relay.idl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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!
}

"""
Expand Down
50 changes: 16 additions & 34 deletions public/relay.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand All @@ -62828,7 +62810,7 @@
"deprecationReason": null
},
{
"name": "language",
"name": "message",
"description": null,
"type": {
"kind": "NON_NULL",
Expand Down
8 changes: 6 additions & 2 deletions test/controllers/graphql_controller_10_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 11879ca

Please sign in to comment.