From 72503a0abebc22cafdad7577a65a1afbad9afa52 Mon Sep 17 00:00:00 2001 From: Chinelo Obitube <74656858+chinelo-obitube@users.noreply.github.com> Date: Tue, 27 Aug 2024 16:50:37 +0200 Subject: [PATCH 1/7] run tests(github actions) on other branches (#2007) Co-authored-by: chinelo-obitube --- .github/workflows/ci-tests.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci-tests.yml b/.github/workflows/ci-tests.yml index 9a35e688f9..3c8e3b8c38 100644 --- a/.github/workflows/ci-tests.yml +++ b/.github/workflows/ci-tests.yml @@ -10,6 +10,8 @@ on: pull_request: branches: - develop + - epic* + - cv2* env: CC_TEST_REPORTER_ID: "${{ secrets.CC_TEST_REPORTER_ID }}" From c1c9b20ab1efd1ad592844b459db6729ea6be3d6 Mon Sep 17 00:00:00 2001 From: Caio Almeida <117518+caiosba@users.noreply.github.com> Date: Tue, 27 Aug 2024 12:04:57 -0300 Subject: [PATCH 2/7] Adjust Smooch Bot provider when tipline is running both CAPI and Smooch. (#2004) Fixes CV2-5127. --- app/models/concerns/smooch_resend.rb | 2 ++ app/workers/tipline_newsletter_worker.rb | 1 + 2 files changed, 3 insertions(+) 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/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)) From 5257311c918bcf3f2a1366a345c38cbf54eac9ef Mon Sep 17 00:00:00 2001 From: Chinelo Obitube <74656858+chinelo-obitube@users.noreply.github.com> Date: Tue, 27 Aug 2024 17:36:01 +0200 Subject: [PATCH 3/7] run tests(github actions) on other branches (#2008) Co-authored-by: chinelo-obitube --- .github/workflows/ci-tests.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci-tests.yml b/.github/workflows/ci-tests.yml index 3c8e3b8c38..d5126ae9ed 100644 --- a/.github/workflows/ci-tests.yml +++ b/.github/workflows/ci-tests.yml @@ -7,11 +7,12 @@ on: branches: - master - develop + - epic* + - cv2* pull_request: branches: - develop - - epic* - - cv2* + env: CC_TEST_REPORTER_ID: "${{ secrets.CC_TEST_REPORTER_ID }}" From e9d823bc5dad2dd68c66546e760c00cfc40d8e03 Mon Sep 17 00:00:00 2001 From: Caio <117518+caiosba@users.noreply.github.com> Date: Tue, 27 Aug 2024 12:39:05 -0300 Subject: [PATCH 4/7] Updating Rubocop file --- .rubocop.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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.' From da88e2840d2480216744c72482a3d89dcd7e8283 Mon Sep 17 00:00:00 2001 From: Scott Hale Date: Wed, 28 Aug 2024 20:01:24 +0900 Subject: [PATCH 5/7] 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 From 0598a50c930dd135574a31052c0cb48819988643 Mon Sep 17 00:00:00 2001 From: Caio Almeida <117518+caiosba@users.noreply.github.com> Date: Wed, 28 Aug 2024 08:09:09 -0300 Subject: [PATCH 6/7] Search for fact-checks by multiple keywords regardless the order. (#2012) * Search by multiple keywords regardless the order. Fixes: CV2-5129. * CV2-5129: apply same search for Explainer * CV2-5129: apply PR comment * No need to split, strip, and join with websearch_to_tsquery * CV2-5129: fix CC --------- Co-authored-by: Sawy Co-authored-by: computermacgyver --- app/models/team.rb | 14 ++++++++++++-- test/models/team_2_test.rb | 25 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) 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/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 From 9a30cf1345ca3f5e2a86608e4e162b0d20f91929 Mon Sep 17 00:00:00 2001 From: Scott Hale Date: Thu, 29 Aug 2024 23:46:39 +0900 Subject: [PATCH 7/7] CV2-5148: Use more inclusive detection of URLs in tipline messages for submission shortcut URI.regexp.match... only matched fully-qualified urls (i.e., urls need to include http://, https://, or another protocol) Twitter::TwitterText::Extractor.extract_urls recognizes urls without the protocol. This is consistent with how WhatsApp and other platforms parse URLs and more in line with user expectations. Co-authored-by: computermacgyver --- app/models/bot/smooch.rb | 3 +-- test/models/bot/smooch_test.rb | 4 ++++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/models/bot/smooch.rb b/app/models/bot/smooch.rb index f63a200c4a..765a8363c7 100644 --- a/app/models/bot/smooch.rb +++ b/app/models/bot/smooch.rb @@ -1,5 +1,4 @@ require 'digest' -require 'uri' class Bot::Smooch < BotUser class MessageDeliveryError < StandardError; end @@ -546,7 +545,7 @@ 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) || - !URI.regexp.match(message['text'].to_s).nil? # URL in message? + !Twitter::TwitterText::Extractor.extract_urls(message['text'].to_s).blank? # URL in message? ) end diff --git a/test/models/bot/smooch_test.rb b/test/models/bot/smooch_test.rb index ee1a0eef8d..f825154547 100644 --- a/test/models/bot/smooch_test.rb +++ b/test/models/bot/smooch_test.rb @@ -806,6 +806,10 @@ def teardown 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