Skip to content

Commit

Permalink
Fixing conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
caiosba committed Jan 21, 2025
2 parents 3b444e0 + 29488b5 commit dcc9922
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
6 changes: 3 additions & 3 deletions app/models/project_media.rb
Original file line number Diff line number Diff line change
Expand Up @@ -517,17 +517,17 @@ def log_relevant_results(klass, id, author_id, actor_session_id)
data = begin JSON.parse(Rails.cache.read("relevant-items-#{self.id}")) rescue {} end
type = klass.underscore
unless data[type].blank?
user_action = data[type].include?(article.id) ? 'relevant_articles' : 'article_search'
user_action = data[type].keys.map(&:to_i).include?(article.id) ? 'relevant_articles' : 'article_search'
tbi = Bot::Alegre.get_alegre_tbi(self.team_id)
similarity_settings = tbi&.settings&.to_h || {}
# Retrieve the user's selection, which can be either FactCheck or Explainer,
# as this type encompasses the user's choice, and then define the shared field based on this type.
# i.e selected_count either 0/1
items = data[type]
items.keys.each_with_index do |value, index|
selected_count = (value == article.id).to_i
selected_count = (value.to_i == article.id).to_i
fields = {
article_id: article.id,
article_id: value,
article_type: article.class.name,
matched_media_id: items[value],
selected_count: selected_count,
Expand Down
16 changes: 14 additions & 2 deletions app/models/team.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Team < ApplicationRecord
class RelevantArticlesError < StandardError; end

# These two callbacks must be in the top
after_create :create_team_partition
before_destroy :delete_created_bots, :remove_is_default_project_flag
Expand Down Expand Up @@ -563,10 +565,15 @@ def filter_by_keywords(query, filters, type = 'FactCheck')
query.where(Arel.sql("#{tsvector} @@ #{tsquery}"))
end

def search_for_similar_articles(query, pm = nil, settings = nil)
def similar_articles_search_limit(pm = nil)
pm.nil? ? CheckConfig.get('most_relevant_team_limit', 3, :integer) : CheckConfig.get('most_relevant_item_limit', 10, :integer)
end

def search_for_similar_articles(query, pm = nil)
# query: expected to be text
# pm: to request a most relevant to specific item and also include both FactCheck & Explainer
limit = pm.nil? ? CheckConfig.get('most_relevant_team_limit', 3, :integer) : CheckConfig.get('most_relevant_item_limit', 10, :integer)
limit = self.similar_articles_search_limit(pm)
threads = []
fc_items = []
# FIXME: Threads approach not working locally - requests from GraphiQL hang forever.
# ex_items = []
Expand Down Expand Up @@ -594,7 +601,12 @@ def search_for_similar_articles(query, pm = nil, settings = nil)
items = fc_items
# Get Explainers if no fact-check returned or get similar_articles for a ProjectMedia
items += ex_items if items.blank? || !pm.nil?
Rails.logger.info("Relevant articles found for team slug #{self.slug}, project media with ID #{pm&.id} and query #{query}: #{items.map(&:graphql_id)}")
items
rescue StandardError => e
Rails.logger.warn("Error when trying to retrieve relevant articles for team slug #{self.slug}, project media with ID #{pm&.id} and query #{query}.")
CheckSentry.notify(RelevantArticlesError.new('Error when trying to retrieve relevant articles'), team_slug: self.slug, project_media_id: pm&.id, query: query, exception_message: e.message, exception: e)
[]
end

def get_shorten_outgoing_urls
Expand Down
5 changes: 5 additions & 0 deletions test/models/relevant_results_item_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ class RelevantResultsItemTest < ActiveSupport::TestCase
assert_equal 2, RelevantResultsItem.where(query_media_parent_id: pm1.id, article_type: 'Explainer').count
assert_equal 2, RelevantResultsItem.where(query_media_parent_id: pm1.id, article_type: 'FactCheck').count
assert_equal 4, RelevantResultsItem.where(query_media_parent_id: pm1.id, relevant_results_render_id: Digest::MD5.hexdigest("#{actor_session_id}-#{now.to_i}")).count
# Verify user action value
assert_equal ['relevant_articles'], RelevantResultsItem.where(query_media_parent_id: pm1.id).map(&:user_action).uniq
# Verified selected_count value
assert_equal [1], RelevantResultsItem.where(query_media_parent_id: pm1.id, article_type: 'Explainer', article_id: ex2.id).map(&:selected_count)
assert_equal [0], RelevantResultsItem.where(query_media_parent_id: pm1.id, article_type: 'Explainer').where.not(article_id: ex2.id).map(&:selected_count).uniq
Time.unstub(:now)
RequestStore.unstub(:[])
assert_nil Rails.cache.read("relevant-items-#{pm1.id}")
Expand Down
7 changes: 7 additions & 0 deletions test/models/team_2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1583,4 +1583,11 @@ def setup
end
Bot::Smooch.unstub(:search_for_explainers)
end

test "should notify Sentry if it fails to retrieve relevant articles" do
Bot::Smooch.stubs(:search_for_similar_published_fact_checks_no_cache).raises(StandardError)
CheckSentry.expects(:notify).once
t = create_team
assert_equal [], t.search_for_similar_articles('Test')
end
end

0 comments on commit dcc9922

Please sign in to comment.