From da88e2840d2480216744c72482a3d89dcd7e8283 Mon Sep 17 00:00:00 2001 From: Scott Hale Date: Wed, 28 Aug 2024 20:01:24 +0900 Subject: [PATCH] CV2-5148: include URLs as shortcuts triggering tipline submission (#2011) --- app/models/bot/smooch.rb | 7 ++++++- test/models/bot/smooch_6_test.rb | 6 +++--- test/models/bot/smooch_test.rb | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/app/models/bot/smooch.rb b/app/models/bot/smooch.rb index 9177a69039..f63a200c4a 100644 --- a/app/models/bot/smooch.rb +++ b/app/models/bot/smooch.rb @@ -1,4 +1,5 @@ require 'digest' +require 'uri' class Bot::Smooch < BotUser class MessageDeliveryError < StandardError; end @@ -542,7 +543,11 @@ def self.process_menu_option_value(value, option, message, language, workflow, a end def self.is_a_shortcut_for_submission?(state, message) - self.is_v2? && (state == 'main' || state == 'waiting_for_message') && (!message['mediaUrl'].blank? || ::Bot::Alegre.get_number_of_words(message['text'].to_s) > CheckConfig.get('min_number_of_words_for_tipline_submit_shortcut', 10, :integer)) + self.is_v2? && (state == 'main' || state == 'waiting_for_message') && ( + !message['mediaUrl'].blank? || + ::Bot::Alegre.get_number_of_words(message['text'].to_s) > CheckConfig.get('min_number_of_words_for_tipline_submit_shortcut', 10, :integer) || + !URI.regexp.match(message['text'].to_s).nil? # URL in message? + ) end def self.process_menu_option(message, state, app_id) diff --git a/test/models/bot/smooch_6_test.rb b/test/models/bot/smooch_6_test.rb index 6473d95dc2..de5b67ff21 100644 --- a/test/models/bot/smooch_6_test.rb +++ b/test/models/bot/smooch_6_test.rb @@ -664,12 +664,12 @@ def send_message_outside_24_hours_window(template, pm = nil) test "should not duplicate messages when saving" do @team.set_languages ['en'] @team.save! - url = 'http://localhost' - send_message url, '1', url, '1' + message_text = 'not_a_url' #Not a URL, not media, and not longer than 'min_number_of_words_for_tipline_submit_shortcut' + send_message message_text, '1', message_text, '1' assert_state 'search' Sidekiq::Worker.drain_all tr = TiplineRequest.last - assert_equal 2, tr.smooch_data['text'].split("\n#{Bot::Smooch::MESSAGE_BOUNDARY}").select{ |x| x.chomp.strip == url }.size + assert_equal 2, tr.smooch_data['text'].split("\n#{Bot::Smooch::MESSAGE_BOUNDARY}").select{ |x| x.chomp.strip == message_text }.size end test "should get search results in different languages" do diff --git a/test/models/bot/smooch_test.rb b/test/models/bot/smooch_test.rb index 8724731980..ee1a0eef8d 100644 --- a/test/models/bot/smooch_test.rb +++ b/test/models/bot/smooch_test.rb @@ -789,4 +789,23 @@ def teardown tr = pm.tipline_requests.last assert_equal 'en', tr.smooch_user_request_language end + + test "should submit message for factchecking" do + Bot::Smooch.stubs(:is_v2?).returns(true) + state='main' + + # Should not be a submission shortcut + message = {"text"=>"abc"} + assert_equal(false, Bot::Smooch.is_a_shortcut_for_submission?(state,message), "Unexpected shortcut") + + # Should be a submission shortcut + message = {"text"=>"abc http://example.com"} + assert_equal(true, Bot::Smooch.is_a_shortcut_for_submission?(state,message), "Missed URL shortcut") + + # Should be a submission shortcut + message = {"text"=>"abc", "mediaUrl"=>"not blank"} + assert_equal(true, Bot::Smooch.is_a_shortcut_for_submission?(state,message), "Missed media shortcut") + + Bot::Smooch.unstub(:is_v2?) + end end