diff --git a/app/models/concerns/smooch_messages.rb b/app/models/concerns/smooch_messages.rb index 88913b58c..f23daea9c 100644 --- a/app/models/concerns/smooch_messages.rb +++ b/app/models/concerns/smooch_messages.rb @@ -395,6 +395,8 @@ def default_archived_flag end def save_message(message_json, app_id, author = nil, request_type = 'default_requests', associated_id = nil, associated_class = nil) + # associated: is the the "first media" and will be the source of the Relationship + # associated_obj: is used for TiplineRequest (smooch_resource_id field) message = JSON.parse(message_json) return if TiplineRequest.where(smooch_message_id: message['_id']).exists? associated_obj = nil @@ -413,13 +415,13 @@ def save_message(message_json, app_id, author = nil, request_type = 'default_req associated = self.create_project_media_from_message(message) end unless associated.nil? - self.smoooch_post_save_message_actions(message, associated, app_id, author, request_type, associated_obj) + self.smooch_post_save_message_actions(message, associated, app_id, author, request_type, associated_obj) self.smooch_relate_items_for_same_message(message, associated, app_id, author, request_type, associated_obj) end end end - def smoooch_post_save_message_actions(message, associated, app_id, author, request_type, associated_obj) + def smooch_post_save_message_actions(message, associated, app_id, author, request_type, associated_obj) # Remember that we received this message. hash = self.message_hash(message) Rails.cache.write("smooch:message:#{hash}", associated.id) @@ -430,6 +432,7 @@ def smoooch_post_save_message_actions(message, associated, app_id, author, reque end def smooch_relate_items_for_same_message(message, associated, app_id, author, request_type, associated_obj) + return unless associated.is_a?(ProjectMedia) if !message['caption'].blank? # Check if message contains caption then create an item and force relationship self.relate_item_and_text(message, associated, app_id, author, request_type, associated_obj, Relationship.confirmed_type) @@ -455,7 +458,7 @@ def relate_item_and_text(message, associated, app_id, author, request_type, asso message.delete('mediaUrl') target = self.create_project_media_from_message(message) unless target.nil? - smoooch_post_save_message_actions(message, target, app_id, author, request_type, associated_obj) + smooch_post_save_message_actions(message, target, app_id, author, request_type, associated_obj) Relationship.create_unless_exists(associated.id, target.id, relationship_type) end end diff --git a/test/models/bot/smooch_7_test.rb b/test/models/bot/smooch_7_test.rb index 10217674c..a2f17ed9f 100644 --- a/test/models/bot/smooch_7_test.rb +++ b/test/models/bot/smooch_7_test.rb @@ -632,4 +632,32 @@ def teardown end TiplineRequest.any_instance.unstub(:save!) end + + test "should not try to create relationship between media and tipline resource" do + t2 = create_team + pm = create_project_media team: t2 + + t = create_team + tipline_resource = create_tipline_resource team: t + tipline_resource.update_column(:id, pm.id) + + # It should not try to match at all, so we should never get to this notification + CheckSentry.expects(:notify).never + Rails.logger.expects(:notify).never + + Sidekiq::Testing.inline! do + message = { + type: 'video', + source: { type: "whatsapp" }, + text: 'Something', + caption: 'For this to happen, it needs a caption', + mediaUrl: @video_url, + '_id': random_string, + language: 'en', + } + assert_no_difference 'Relationship.count' do + Bot::Smooch.save_message(message.to_json, @app_id, @bot, 'resource_requests', tipline_resource.id, 'TiplineResource') + end + end + end end