diff --git a/.github/workflows/ci-tests.yml b/.github/workflows/ci-tests.yml index 9a35e688f9..d5126ae9ed 100644 --- a/.github/workflows/ci-tests.yml +++ b/.github/workflows/ci-tests.yml @@ -7,9 +7,12 @@ on: branches: - master - develop + - epic* + - cv2* pull_request: branches: - develop + env: CC_TEST_REPORTER_ID: "${{ secrets.CC_TEST_REPORTER_ID }}" diff --git a/.rubocop.yml b/.rubocop.yml index d9bb8d820f..2ff1fbf74d 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -224,7 +224,7 @@ Metrics/CyclomaticComplexity: A complexity metric that is strongly correlated to the number of test cases needed to validate a method. Enabled: true - Max: 12 + Max: 13 Metrics/LineLength: Description: 'Limit lines to 80 characters.' diff --git a/app/models/bot/smooch.rb b/app/models/bot/smooch.rb index 7c7fdb4987..ac98497347 100644 --- a/app/models/bot/smooch.rb +++ b/app/models/bot/smooch.rb @@ -539,7 +539,11 @@ def self.process_menu_option_value(value, option, message, language, workflow, a end def self.is_a_shortcut_for_submission?(state, message) - (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)) + (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) || + !Twitter::TwitterText::Extractor.extract_urls(message['text'].to_s).blank? # URL in message? + ) end def self.process_menu_option(message, state, app_id) diff --git a/app/models/concerns/smooch_resend.rb b/app/models/concerns/smooch_resend.rb index 883ed49cd1..455ce33ac8 100644 --- a/app/models/concerns/smooch_resend.rb +++ b/app/models/concerns/smooch_resend.rb @@ -159,6 +159,7 @@ def message_tags_payload(text, image = nil) def resend_facebook_messenger_message_after_window(message, original) original = JSON.parse(original) unless original.blank? uid = message['appUser']['_id'] + RequestStore.store[:smooch_bot_provider] = 'ZENDESK' return self.resend_facebook_messenger_report_after_window(message, original) if original&.dig('fallback_template') =~ /report/ @@ -184,6 +185,7 @@ def resend_facebook_messenger_message_after_window(message, original) end def resend_facebook_messenger_report_after_window(message, original) + RequestStore.store[:smooch_bot_provider] = 'ZENDESK' pm = ProjectMedia.where(id: original['project_media_id']).last report = self.get_report_data_to_be_resent(message, original) unless report.nil? diff --git a/app/models/team.rb b/app/models/team.rb index 6ba123bb6b..a3d8572e51 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -495,7 +495,7 @@ def filtered_explainers(filters = {}) query = query.where(updated_at: Range.new(*format_times_search_range_filter(JSON.parse(filters[:updated_at]), nil))) unless filters[:updated_at].blank? # Filter by text - query = query.where('(title ILIKE ? OR url ILIKE ? OR description ILIKE ?)', *["%#{filters[:text]}%"]*3) if filters[:text].to_s.size > 2 + query = self.filter_by_keywords(query, filters, 'Explainer') if filters[:text].to_s.size > 2 # Exclude the ones already applied to a target item target = ProjectMedia.find_by_id(filters[:target_id].to_i) @@ -535,7 +535,7 @@ def filtered_fact_checks(filters = {}) query = query.where('fact_checks.report_status' => filters[:report_status].to_a.map(&:to_s)) unless filters[:report_status].blank? # Filter by text - query = query.where('(fact_checks.title ILIKE ? OR fact_checks.url ILIKE ? OR fact_checks.summary ILIKE ?)', *["%#{filters[:text]}%"]*3) if filters[:text].to_s.size > 2 + query = self.filter_by_keywords(query, filters) if filters[:text].to_s.size > 2 # Exclude the ones already applied to a target item target = ProjectMedia.find_by_id(filters[:target_id].to_i) @@ -544,6 +544,16 @@ def filtered_fact_checks(filters = {}) query end + def filter_by_keywords(query, filters, type = 'FactCheck') + tsquery = Team.sanitize_sql_array(["websearch_to_tsquery(?)", filters[:text]]) # FIXME: May not work for all languages + if type == 'FactCheck' + tsvector = "to_tsvector('simple', coalesce(title, '') || ' ' || coalesce(summary, '') || coalesce(url, ''))" + else + tsvector = "to_tsvector('simple', coalesce(title, '') || ' ' || coalesce(description, '') || coalesce(url, ''))" + end + query.where(Arel.sql("#{tsvector} @@ #{tsquery}")) + end + # private # # Please add private methods to app/models/concerns/team_private.rb diff --git a/app/workers/tipline_newsletter_worker.rb b/app/workers/tipline_newsletter_worker.rb index 45adefa1ae..a0458ded23 100644 --- a/app/workers/tipline_newsletter_worker.rb +++ b/app/workers/tipline_newsletter_worker.rb @@ -39,6 +39,7 @@ def perform(team_id, language, job_created_at = 0) begin RequestStore.store[:smooch_bot_platform] = ts.platform Bot::Smooch.get_installation('team_bot_installation_id', tbi.id) { |i| i.id == tbi.id } + RequestStore.store[:smooch_bot_provider] = 'ZENDESK' if ts.platform != 'WhatsApp' # Adjustment for tiplines running CAPI and Smooch at the same time response = (ts.platform == 'WhatsApp' ? Bot::Smooch.send_message_to_user(ts.uid, newsletter.format_as_template_message, {}, false, true, 'newsletter') : Bot::Smooch.send_message_to_user(ts.uid, *newsletter.format_as_tipline_message)) diff --git a/test/models/bot/smooch_6_test.rb b/test/models/bot/smooch_6_test.rb index 305ed98930..e8c5c7f2e1 100644 --- a/test/models/bot/smooch_6_test.rb +++ b/test/models/bot/smooch_6_test.rb @@ -660,12 +660,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..f825154547 100644 --- a/test/models/bot/smooch_test.rb +++ b/test/models/bot/smooch_test.rb @@ -789,4 +789,27 @@ 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") + + # Should be a submission shortcut + message = {"text"=>"abc example.com"} + assert_equal(true, Bot::Smooch.is_a_shortcut_for_submission?(state,message), "Missed non-qualified URL shortcut") + + Bot::Smooch.unstub(:is_v2?) + end end diff --git a/test/models/team_2_test.rb b/test/models/team_2_test.rb index 53275867ab..1542c9491e 100644 --- a/test/models/team_2_test.rb +++ b/test/models/team_2_test.rb @@ -1527,4 +1527,29 @@ def setup tbi.save! assert_equal ['none', 'link_preview'], t.available_newsletter_header_types end + + test "should search for fact-checks and explainers by keywords" do + Sidekiq::Testing.fake! + t = create_team + # Fact-checks + create_fact_check title: 'Some Other Test', claim_description: create_claim_description(project_media: create_project_media(team: t)) + create_fact_check title: 'Bar Bravo Foo Test', claim_description: create_claim_description(project_media: create_project_media(team: t)) + create_fact_check title: 'Foo Alpha Bar Test', claim_description: create_claim_description(project_media: create_project_media(team: t)) + assert_equal 3, t.filtered_fact_checks.count + assert_equal 3, t.filtered_fact_checks(text: 'Test').count + assert_equal 2, t.filtered_fact_checks(text: 'Foo Bar').count + assert_equal 1, t.filtered_fact_checks(text: 'Foo Bar Bravo').count + assert_equal 1, t.filtered_fact_checks(text: 'Foo Bar Alpha').count + assert_equal 0, t.filtered_fact_checks(text: 'Foo Bar Delta').count + # Explainer + create_explainer title: 'Some Other Test', team: t + create_explainer title: 'Bar Bravo Foo Test', team: t + create_explainer title: 'Foo Alpha Bar Test', team: t + assert_equal 3, t.filtered_explainers.count + assert_equal 3, t.filtered_explainers(text: 'Test').count + assert_equal 2, t.filtered_explainers(text: 'Foo Bar').count + assert_equal 1, t.filtered_explainers(text: 'Foo Bar Bravo').count + assert_equal 1, t.filtered_explainers(text: 'Foo Bar Alpha').count + assert_equal 0, t.filtered_fact_checks(text: 'Foo Bar Delta').count + end end