diff --git a/app/models/concerns/smooch_messages.rb b/app/models/concerns/smooch_messages.rb index 5170a06a98..3322ce3a50 100644 --- a/app/models/concerns/smooch_messages.rb +++ b/app/models/concerns/smooch_messages.rb @@ -333,29 +333,29 @@ def default_archived_flag def save_message(message_json, app_id, author = nil, request_type = 'default_requests', associated_obj = nil) message = JSON.parse(message_json) self.get_installation(self.installation_setting_id_keys, app_id) - Team.current = Team.where(id: self.config['team_id']).last - associated = nil - if ['default_requests', 'timeout_requests', 'irrelevant_search_result_requests'].include?(request_type) - message['archived'] = ['default_requests', 'irrelevant_search_result_requests'].include?(request_type) ? self.default_archived_flag : CheckArchivedFlags::FlagCodes::UNCONFIRMED - associated = self.create_project_media_from_message(message) - elsif ['menu_options_requests', 'resource_requests'].include?(request_type) - associated = associated_obj - elsif ['relevant_search_result_requests', 'timeout_search_requests'].include?(request_type) - message['archived'] = (request_type == 'relevant_search_result_requests' ? self.default_archived_flag : CheckArchivedFlags::FlagCodes::UNCONFIRMED) - associated = self.create_project_media_from_message(message) - end - - return if associated.nil? - - # Remember that we received this message. - hash = self.message_hash(message) - Rails.cache.write("smooch:message:#{hash}", associated.id) - - self.smooch_save_tipline_request(message, associated, app_id, author, request_type, associated_obj) + Team.current = Team.find self.config['team_id'].to_i + ApplicationRecord.transaction do + associated = nil + if ['default_requests', 'timeout_requests', 'irrelevant_search_result_requests'].include?(request_type) + message['archived'] = ['default_requests', 'irrelevant_search_result_requests'].include?(request_type) ? self.default_archived_flag : CheckArchivedFlags::FlagCodes::UNCONFIRMED + associated = self.create_project_media_from_message(message) + elsif ['menu_options_requests', 'resource_requests'].include?(request_type) + associated = associated_obj + elsif ['relevant_search_result_requests', 'timeout_search_requests'].include?(request_type) + message['archived'] = (request_type == 'relevant_search_result_requests' ? self.default_archived_flag : CheckArchivedFlags::FlagCodes::UNCONFIRMED) + associated = self.create_project_media_from_message(message) + end - # If item is published (or parent item), send a report right away - self.get_platform_from_message(message) - self.send_report_to_user(message['authorId'], message, associated, message['language'], 'fact_check_report') if self.should_try_to_send_report?(request_type, associated) + unless associated.nil? + # Remember that we received this message. + hash = self.message_hash(message) + Rails.cache.write("smooch:message:#{hash}", associated.id) + self.smooch_save_tipline_request(message, associated, app_id, author, request_type, associated_obj) + # If item is published (or parent item), send a report right away + self.get_platform_from_message(message) + self.send_report_to_user(message['authorId'], message, associated, message['language'], 'fact_check_report') if self.should_try_to_send_report?(request_type, associated) + end + end end def smooch_save_tipline_request(message, associated, app_id, author, request_type, associated_obj) diff --git a/test/models/bot/smooch_4_test.rb b/test/models/bot/smooch_4_test.rb index 34a4b37ece..0a1201b503 100644 --- a/test/models/bot/smooch_4_test.rb +++ b/test/models/bot/smooch_4_test.rb @@ -596,7 +596,9 @@ def teardown medias_count = Media.count assert_no_difference 'ProjectMedia.count' do - Bot::Smooch.save_message(message.to_json, @app_id) + assert_raises ActiveRecord::StatementInvalid do + Bot::Smooch.save_message(message.to_json, @app_id) + end end assert_equal medias_count, Media.count end diff --git a/test/models/bot/smooch_6_test.rb b/test/models/bot/smooch_6_test.rb index b1f22421c5..bcbd691027 100644 --- a/test/models/bot/smooch_6_test.rb +++ b/test/models/bot/smooch_6_test.rb @@ -440,6 +440,42 @@ def send_message_outside_24_hours_window(template, pm = nil) end end + test "should save only one item and one request for same tipline message" do + text = "This is message is so long that it is considered a media" + Sidekiq::Testing.inline! do + message = { + type: 'text', + text: text, + role: 'appUser', + received: 1573082583.219, + name: random_string, + authorId: random_string, + '_id': random_string, + source: { + originalMessageId: random_string, + originalMessageTimestamp: 1573082582, + type: 'whatsapp', + integrationId: random_string + }, + language: 'en', + } + author = BotUser.smooch_user + threads = [] + assert_difference 'ProjectMedia.count' do + assert_difference "TiplineRequest.count" do + assert_raises ActiveRecord::StatementInvalid do + 3.times do |i| + threads << Thread.new { + Bot::Smooch.save_message(message.to_json, @app_id, author, 'timeout_requests', nil) + } + end + threads.map(&:join) + end + end + end + end + end + test "should show main menu as buttons for non-WhatsApp platforms on tipline bot v2" do send_message_to_smooch_bot('Hello', @uid, { 'source' => { 'type' => 'telegram' } }) send_message_to_smooch_bot('1', @uid, { 'source' => { 'type' => 'telegram' } }) diff --git a/test/models/bot/smooch_test.rb b/test/models/bot/smooch_test.rb index 0bc83003ec..8724731980 100644 --- a/test/models/bot/smooch_test.rb +++ b/test/models/bot/smooch_test.rb @@ -429,7 +429,8 @@ def teardown '_id': random_string, authorId: uid, type: 'text', - text: text + text: text, + source: { type: "whatsapp" }, } ] payload = {