Skip to content

Commit

Permalink
Fixing conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
caiosba committed Apr 3, 2024
2 parents d00e367 + 008b540 commit f36c49d
Show file tree
Hide file tree
Showing 58 changed files with 4,960 additions and 3,999 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ gem 'bootsnap', require: false
gem 'sidekiq-cron', '~> 1.1'
gem 'parse-cron'
gem 'streamio-ffmpeg'
gem 'rdoc', '6.3.2'
gem 'rdoc', '6.3.4.1'
gem 'geocoder', '1.6.4'
gem 'parallel'
gem 'fx'
Expand Down
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ GEM
ffi (~> 1.0)
rblineprof (0.3.7)
debugger-ruby_core_source (~> 1.3)
rdoc (6.3.2)
rdoc (6.3.4.1)
redis (4.5.1)
redis-actionpack (5.3.0)
actionpack (>= 5, < 8)
Expand Down Expand Up @@ -727,7 +727,7 @@ GEM
netrc (~> 0.8)
retriable (3.1.2)
rexml (3.2.5)
rotp (6.2.2)
rotp (6.3.0)
rqrcode (2.1.1)
chunky_png (~> 1.0)
rqrcode_core (~> 1.0)
Expand Down Expand Up @@ -994,7 +994,7 @@ DEPENDENCIES
rails-graphql-generator
rails-html-sanitizer (= 1.4.4)
rails-i18n!
rdoc (= 6.3.2)
rdoc (= 6.3.4.1)
redis-namespace
redis-objects
redis-rails
Expand Down
11 changes: 10 additions & 1 deletion app/graph/types/claim_description_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,14 @@ class ClaimDescriptionType < DefaultObject
field :context, GraphQL::Types::String, null: true, resolver_method: :claim_context
field :user, UserType, null: true
field :project_media, ProjectMediaType, null: true
field :fact_check, FactCheckType, null: true
field :fact_check, FactCheckType, null: true do
argument :report_status, GraphQL::Types::String, required: false, camelize: false
end

def fact_check(report_status: nil)
ability = context[:ability] || Ability.new
status = object.project_media.report_status
can_read = ability.can?(:read, object) || status == 'published'
(can_read && (!report_status || status == report_status)) ? object.fact_check : nil
end
end
13 changes: 13 additions & 0 deletions app/graph/types/cluster_team_fact_check_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ClusterTeamFactCheckType < DefaultObject
description "Cluster team fact-check type: Data about a fact-check in cluster but under the scope of a team"

implements GraphQL::Types::Relay::Node

field :claim, GraphQL::Types::String, null: true
field :fact_check_title, GraphQL::Types::String, null: true
field :fact_check_summary, GraphQL::Types::String, null: true
field :rating, GraphQL::Types::String, null: true
field :media_count, GraphQL::Types::Int, null: true
field :requests_count, GraphQL::Types::Int, null: true
field :claim_description, ClaimDescriptionType, null: true
end
12 changes: 12 additions & 0 deletions app/graph/types/cluster_team_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class ClusterTeamType < DefaultObject
description "Cluster team type: Data about a cluster but under the scope of a team"

implements GraphQL::Types::Relay::Node

field :team, PublicTeamType, null: true
field :last_request_date, GraphQL::Types::Int, null: true
field :media_count, GraphQL::Types::Int, null: true
field :requests_count, GraphQL::Types::Int, null: true

field :fact_checks, ClusterTeamFactCheckType.connection_type, null: true
end
23 changes: 23 additions & 0 deletions app/graph/types/cluster_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,27 @@ def last_fact_check_date
def teams
Team.where(id: object.team_ids)
end

field :cluster_teams, ClusterTeamType.connection_type, null: true

def cluster_teams
Team.where(id: object.team_ids).all.collect { |team| ClusterTeam.new(object, team) }
end

field :project_medias, ProjectMediaType.connection_type, null: true do
argument :team_id, GraphQL::Types::Int, required: true
end

def project_medias(team_id:)
return ProjectMedia.none unless object.team_ids.include?(team_id)
object.project_medias.where(team_id: team_id.to_i)
end

field :project_media, ProjectMediaType, null: true do
argument :id, GraphQL::Types::Int, required: true
end

def project_media(id:)
object.project_medias.where(id: id.to_i).first
end
end
1 change: 1 addition & 0 deletions app/graph/types/feed_team_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ def requests_filters
end

field :saved_search, SavedSearchType, null: true
field :saved_search_was, SavedSearchType, null: true
end
15 changes: 15 additions & 0 deletions app/graph/types/feed_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ class FeedType < DefaultObject
field :licenses, [GraphQL::Types::Int, null: true], null: true
field :saved_search_id, GraphQL::Types::Int, null: true
field :discoverable, GraphQL::Types::Boolean, null: true
field :last_clusterized_at, GraphQL::Types::String, null: true
field :user, UserType, null: true

field :team, PublicTeamType, null: true
field :saved_search, SavedSearchType, null: true
field :saved_search_was, SavedSearchType, null: true

field :requests, RequestType.connection_type, null: true do
argument :request_id, GraphQL::Types::Int, required: false, camelize: false
Expand Down Expand Up @@ -91,4 +93,17 @@ def clusters(**args)
order_type = args[:sort_type].to_s.downcase.to_sym == :desc ? :desc : :asc
object.filtered_clusters(args).offset(args[:offset].to_i).order(order => order_type)
end

# Given a project media ID, return the cluster it belongs to, in the scope of this feed
field :cluster, ClusterType, null: true do
argument :project_media_id, GraphQL::Types::Int, required: true, camelize: false
end

def cluster(project_media_id:)
cluster = ClusterProjectMedia.joins(:cluster).where('clusters.feed_id' => object.id, 'cluster_project_medias.project_media_id' => project_media_id.to_i).first&.cluster
return nil if cluster.nil?
ability = context[:ability] || Ability.new
return nil unless ability.can?(:read, object)
cluster
end
end
2 changes: 1 addition & 1 deletion app/graph/types/saved_search_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def filters
field :is_part_of_feeds, GraphQL::Types::Boolean, null: true

def is_part_of_feeds
Feed.where(saved_search_id: object.id).exists?
Feed.where(saved_search_id: object.id).exists? || FeedTeam.where(saved_search_id: object.id).exists?
end

field :feeds, FeedType.connection_type, null: true
Expand Down
19 changes: 16 additions & 3 deletions app/graph/types/tipline_request_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,27 @@ class TiplineRequestType < DefaultObject
field :dbid, GraphQL::Types::Int, null: true
field :associated_id, GraphQL::Types::Int, null: true
field :associated_type, GraphQL::Types::String, null: true
field :smooch_data, JsonStringType, null: true
field :smooch_user_slack_channel_url, GraphQL::Types::String, null: true
field :smooch_user_external_identifier, GraphQL::Types::String, null: true
field :smooch_report_received_at, GraphQL::Types::Int, null: true
field :smooch_report_update_received_at, GraphQL::Types::Int, null: true
field :smooch_user_request_language, GraphQL::Types::String, null: true
field :smooch_report_sent_at, GraphQL::Types::Int, null: true
field :smooch_report_correction_sent_at, GraphQL::Types::Int, null: true
field :smooch_request_type, GraphQL::Types::String, null: true
field :associated_graphql_id, GraphQL::Types::String, null: true

field :smooch_user_external_identifier, GraphQL::Types::String, null: true

def smooch_user_external_identifier
ability = context[:ability] || Ability.new
# Mask the user identifier when this request is displayed in a feed context
ability.can?(:read, object) ? object.smooch_user_external_identifier : SecureRandom.hex.first(5)
end

field :smooch_data, JsonStringType, null: true

def smooch_data
ability = context[:ability] || Ability.new
# Mask user information when this request is displayed in a feed context
ability.can?(:read, object) ? object.smooch_data : object.smooch_data.to_h.merge({ 'name' => SecureRandom.hex.first(5), 'authorId' => SecureRandom.hex.first(5) })
end
end
11 changes: 11 additions & 0 deletions app/mailers/updated_terms_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class UpdatedTermsMailer < ApplicationMailer
layout nil

def notify(recipient, name)
@name = name
@accept_terms_url = CheckConfig.get('tos_url')
subject = I18n.t("mails_notifications.updated_terms.subject")
Rails.logger.info "Sending ToS e-mail to #{recipient}"
mail(to: recipient, email_type: 'updated_terms', subject: subject)
end
end
2 changes: 0 additions & 2 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ def admin_perms
can :set_privacy, Project, :team_id => @context_team.id
can :read_feed_invitations, Feed, :team_id => @context_team.id
can :destroy, Feed, :team_id => @context_team.id
can :destroy, Cluster, { feed: { team_id: @context_team.id } }
can [:create, :update], FeedTeam, :team_id => @context_team.id
can [:create, :update], FeedInvitation, { feed: { team_id: @context_team.id } }
can :destroy, FeedTeam do |obj|
Expand Down Expand Up @@ -112,7 +111,6 @@ def editor_perms
can [:read], FeedTeam, :team_id => @context_team.id
can [:read], FeedInvitation, { feed: { team_id: @context_team.id } }
can [:read, :create, :update], Feed, :team_id => @context_team.id
can [:read, :create, :update], Cluster, { feed: { team_id: @context_team.id } }
end

def collaborator_perms
Expand Down
11 changes: 7 additions & 4 deletions app/models/bot/alegre.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Error < ::StandardError

REPORT_TEXT_SIMILARITY_FIELDS = ['report_text_title', 'report_text_content', 'report_visual_card_title', 'report_visual_card_content']
ALL_TEXT_SIMILARITY_FIELDS = REPORT_TEXT_SIMILARITY_FIELDS + ['original_title', 'original_description', 'extracted_text', 'transcription', 'claim_description_content', 'fact_check_title', 'fact_check_summary']

BAD_TITLE_REGEX = /^[a-z\-]+-[0-9\-]+$/
::ProjectMedia.class_eval do
attr_accessor :alegre_similarity_thresholds, :alegre_matched_fields

Expand Down Expand Up @@ -197,8 +197,8 @@ def self.get_number_of_words(text)

def self.get_items_from_similar_text(team_id, text, fields = nil, threshold = nil, models = nil, fuzzy = false)
team_ids = [team_id].flatten
if text.blank?
Rails.logger.info("[Alegre Bot] get_items_from_similar_text returning early due to blank text #{text}")
if text.blank? || BAD_TITLE_REGEX =~ text
Rails.logger.info("[Alegre Bot] get_items_from_similar_text returning early due to blank/bad text #{text}")
return {}
end
fields ||= ALL_TEXT_SIMILARITY_FIELDS
Expand Down Expand Up @@ -295,7 +295,10 @@ def self.get_language(pm)
end

def self.get_language_from_text(pm, text)
lang = text.blank? ? 'und' : self.get_language_from_alegre(text)
lang = 'und'
if !text.blank? && BAD_TITLE_REGEX !~ text
lang = self.get_language_from_alegre(text)
end
self.save_annotation(pm, 'language', { language: lang })
lang
end
Expand Down
60 changes: 2 additions & 58 deletions app/models/bot/fetch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def self.import_claim_reviews(installation_id, language = nil, force = false, ma
end
end

def self.import_claim_review(claim_review, team_id, user_id, status_fallback, status_mapping, auto_publish_reports, force = false)
def self.import_claim_review(claim_review, team_id, user_id, status_fallback, status_mapping, _auto_publish_reports, force = false)
begin
user = User.find(user_id)
team = Team.find(team_id)
Expand All @@ -196,7 +196,6 @@ def self.import_claim_review(claim_review, team_id, user_id, status_fallback, st
self.set_status(claim_review, pm, status_fallback, status_mapping)
self.set_analysis(claim_review, pm)
self.set_claim_and_fact_check(claim_review, pm, user, team)
self.create_report(claim_review, pm, team, user, auto_publish_reports)
self.create_tags(claim_review, pm, user)
end
end
Expand Down Expand Up @@ -261,8 +260,8 @@ def self.set_claim_and_fact_check(claim_review, pm, user, team)
fc.url = claim_review['url'].to_s
fc.summary = self.get_summary(claim_review).to_s
fc.user = user
fc.skip_report_update = true
fc.language = fc_language
fc.publish_report = true
fc.save!
User.current = current_user
end
Expand Down Expand Up @@ -306,63 +305,8 @@ def self.set_status(claim_review, pm, status_fallback, status_mapping)
end
end

def self.get_image_file(image_url)
tmp = nil
unless image_url.blank?
tmp = File.join(Rails.root, 'tmp', "image-#{SecureRandom.hex}")
URI(Addressable::URI.escape(image_url)).open do |i|
File.open(tmp, 'wb') do |f|
f.write(i.read)
end
end
end
tmp
end

def self.parse_text(text)
CGI.unescapeHTML(ActionView::Base.full_sanitizer.sanitize(text.to_s))
end

def self.create_report(claim_review, pm, team, user, auto_publish_reports)
report = Dynamic.new
report.annotation_type = 'report_design'
report.annotated = pm
report.annotator = user
tmp_file_path = begin self.get_image_file(claim_review['image'].to_s) rescue nil end
if tmp_file_path
File.open(tmp_file_path) do |f|
report.file = [f]
end
end
date = claim_review['datePublished'].blank? ? Time.now : Time.parse(claim_review['datePublished'])
language = team.default_language
title = self.get_title(claim_review).truncate(140)
summary = self.parse_text(claim_review['text']).truncate(620)
fields = {
state: auto_publish_reports ? 'published' : 'paused',
options: {
language: language,
status_label: pm.status_i18n(pm.reload.last_verification_status, { locale: language }),
description: summary,
title: title,
published_article_url: claim_review['url'],
headline: title,
use_visual_card: false,
image: '',
use_introduction: !!report.report_design_team_setting_value('use_introduction', language),
introduction: report.report_design_team_setting_value('introduction', language).to_s,
theme_color: pm.reload.last_status_color,
url: '',
use_text_message: true,
text: summary,
date: report.report_design_date(date.to_date, language)
}
}
report.set_fields = fields.to_json
report.action = 'save'
report.skip_check_ability = true
report.save!
FileUtils.rm_f(tmp_file_path) if tmp_file_path
end
end
end
2 changes: 1 addition & 1 deletion app/models/claim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def media_type
end

def uuid
Media.where(type: 'Claim', quote: self.quote.to_s.strip).first&.id || self.id
Media.where(type: 'Claim', quote: self.quote.to_s.strip).joins("INNER JOIN project_medias pm ON pm.media_id = medias.id").first&.id || self.id
end

private
Expand Down
12 changes: 7 additions & 5 deletions app/models/concerns/alegre_similarity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,13 @@ def send_to_text_similarity_index_package(pm, field, text, doc_id)
end

def send_to_text_similarity_index(pm, field, text, doc_id)
self.request(
'post',
'/text/similarity/',
self.send_to_text_similarity_index_package(pm, field, text, doc_id)
)
if !text.blank? && Bot::Alegre::BAD_TITLE_REGEX !~ text
self.request(
'post',
'/text/similarity/',
self.send_to_text_similarity_index_package(pm, field, text, doc_id)
)
end
end

def delete_from_index(pm, fields=nil, quiet=false)
Expand Down
21 changes: 21 additions & 0 deletions app/models/concerns/smooch_blocking.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def block_user(uid)
block.save!
Rails.logger.info("[Smooch Bot] Blocked user #{uid}")
Rails.cache.write("smooch:banned:#{uid}", Time.now.to_i)
self.delay.apply_content_warning_to_user_content(uid)
rescue ActiveRecord::RecordNotUnique
# User already blocked
Rails.logger.info("[Smooch Bot] User #{uid} already blocked")
Expand All @@ -43,5 +44,25 @@ def user_banned?(payload)
uid = payload.dig('appUser', '_id')
self.user_blocked?(uid)
end

def apply_content_warning_to_user_content(uid)
RequestStore.store[:skip_rules] = true
ProjectMedia.joins(:tipline_requests)
.where(tipline_requests: { tipline_user_uid: uid }).find_each do |pm|
begin
flags = { 'spam': 1, 'adult': 0, 'spoof': 0, 'medical': 0, 'violence': 0, 'racy': 0 }
# Add a flag
flag = Dynamic.new
flag.annotation_type = 'flag'
flag.annotated = pm
flag.skip_check_ability = true
flag.set_fields = { show_cover: true, flags: flags }.to_json
flag.save!
rescue StandardError => e
Rails.logger.info "[Smooch Bot] Exception when flagging blocked user's media: #{e.message}"
end
end
RequestStore.store[:skip_rules] = false
end
end
end
Loading

0 comments on commit f36c49d

Please sign in to comment.