Skip to content

Commit

Permalink
CV2-4593: use ApplicationRecord.transaction to save message (#1898)
Browse files Browse the repository at this point in the history
* CV2-4593: try to reproduce the issue using unit test

* CV2-4593: fix tests
  • Loading branch information
melsawy committed May 28, 2024
1 parent 87e144a commit c9243fc
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 24 deletions.
44 changes: 22 additions & 22 deletions app/models/concerns/smooch_messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion test/models/bot/smooch_4_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions test/models/bot/smooch_6_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' } })
Expand Down
3 changes: 2 additions & 1 deletion test/models/bot/smooch_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,8 @@ def teardown
'_id': random_string,
authorId: uid,
type: 'text',
text: text
text: text,
source: { type: "whatsapp" },
}
]
payload = {
Expand Down

0 comments on commit c9243fc

Please sign in to comment.