Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:meedan/check-api into refactorin…
Browse files Browse the repository at this point in the history
…g/CV2-2550-remove-code-related-to-tipline-bot-v-1
  • Loading branch information
danielevalverde committed Aug 30, 2024
2 parents 1e5bd59 + 9a30cf1 commit 1efeeb8
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ on:
branches:
- master
- develop
- epic*
- cv2*
pull_request:
branches:
- develop


env:
CC_TEST_REPORTER_ID: "${{ secrets.CC_TEST_REPORTER_ID }}"
Expand Down
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand Down
6 changes: 5 additions & 1 deletion app/models/bot/smooch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions app/models/concerns/smooch_resend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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/

Expand All @@ -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?
Expand Down
14 changes: 12 additions & 2 deletions app/models/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/workers/tipline_newsletter_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
6 changes: 3 additions & 3 deletions test/models/bot/smooch_6_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions test/models/bot/smooch_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 25 additions & 0 deletions test/models/team_2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1efeeb8

Please sign in to comment.