Skip to content

Commit

Permalink
Fixing conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
caiosba committed Feb 5, 2025
2 parents df3b09a + f513954 commit 7b3264a
Show file tree
Hide file tree
Showing 101 changed files with 3,411 additions and 1,408 deletions.
4 changes: 2 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,10 @@ Metrics/ModuleLength:
Max: 250

Metrics/ParameterLists:
Description: 'Avoid parameter lists longer than 9 parameters.'
Description: 'Avoid parameter lists longer than 10 parameters.'
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#too-many-params'
Enabled: true
Max: 9
Max: 10

Metrics/PerceivedComplexity:
Description: >-
Expand Down
6 changes: 5 additions & 1 deletion CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
* @caiosba @melsawy @DGaffney @jayjay-w
* @caiosba @vasconsaurus @jayjay-w @melsawy
/.github/ @dmou
/production/ @dmou
Dockerfile @dmou
/docker* @dmou
24 changes: 20 additions & 4 deletions app/controllers/api/v1/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ def create
begin
duplicate_user = User.get_duplicate_user(resource.email, [])[:user]
user = resource
error = [
{
message: I18n.t(:email_exists)
}
]
if !duplicate_user.nil? && duplicate_user.invited_to_sign_up?
duplicate_user.accept_invitation_or_confirm
duplicate_user.password = resource.password
Expand All @@ -25,13 +30,24 @@ def create
resource.last_accepted_terms_at = Time.now
resource.save!
end

User.current = user
sign_up(resource_name, user)
render_success 'user', user
render_success user, 'user', 401, error
rescue ActiveRecord::RecordInvalid => e
clean_up_passwords resource
set_minimum_password_length
render_error e.message.gsub(/^Validation failed: Email /, ''), 'INVALID_VALUE'
# Check if the error is specifically related to the email being taken
if resource.errors.details[:email].any? { |email_error| email_error[:error] == :taken } && resource.errors.details.except(:email).empty?
# Treat as successful sign-up if only the email is taken
duplicate_user = User.get_duplicate_user(resource.email, [])[:user]
User.current = duplicate_user if duplicate_user
sign_up(resource_name, duplicate_user)
render_success nil, 'user', 401, error
else
# For other errors, show the error message in the form
clean_up_passwords resource
set_minimum_password_length
render_error e.message.gsub("Email #{I18n.t(:email_exists)}<br />", '').strip, 'INVALID_VALUE', 401
end
end
end

Expand Down
7 changes: 4 additions & 3 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ def add_info_to_trace

private

def render_success(type = 'success', object = nil)
def render_success(type = 'success', object = nil, status = 200, errors = nil)
json = { type: type }
json[:data] = object unless object.nil?
logger.info message: json, status: 200
render json: json, status: 200
json[:errors] = errors unless errors.nil?
logger.info message: json, status: status
render json: json, status: status
end

def render_error(message, code, status = 400)
Expand Down
36 changes: 36 additions & 0 deletions app/controllers/test_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,42 @@ def suggest_similarity_item
render_success 'suggest_similarity', pm1
end

def create_imported_standalone_fact_check
team = Team.current = Team.find(params[:team_id])
user = User.where(email: params[:email]).last
description = params[:description]
context = params[:context]
title = params[:title]
summary = params[:summary]
url = params[:url]
language = params[:language] || 'en'

project_media = ProjectMedia.create!(media: Blank.create!, team: team, user: user)

# Create ClaimDescription
claim_description = ClaimDescription.create!(
description: description,
context: context,
user: user,
team: team,
project_media: project_media
)

# Set up FactCheck
fact_check = FactCheck.new(
claim_description: claim_description,
title: title,
summary: summary,
url: url,
language: language,
user: user,
publish_report: true,
report_status: 'published'
)
fact_check.save!
render_success 'fact_check', fact_check
end

def random
render html: "<!doctype html><html><head><title>Test #{rand(100000).to_i}</title></head><body>Test</body></html>".html_safe
end
Expand Down
1 change: 1 addition & 0 deletions app/graph/mutations/claim_description_mutations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module SharedCreateAndUpdateFields
argument :description, GraphQL::Types::String, required: false
argument :context, GraphQL::Types::String, required: false, as: :claim_context
argument :project_media_id, GraphQL::Types::Int, required: false, camelize: false
argument :enable_create_blank_media, GraphQL::Types::Boolean, required: false, camelize: false
end
end

Expand Down
1 change: 1 addition & 0 deletions app/graph/types/about_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ class AboutType < BaseObject

field :channels, JsonStringType, "List check channels", null: true
field :countries, JsonStringType, "List of workspace countries", null: true
field :media_cluster_origins, JsonStringType, "List of media cluster origins", null: true
end
32 changes: 32 additions & 0 deletions app/graph/types/project_media_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,36 @@ def articles_count
count += 1 if object.fact_check
count
end

field :relevant_articles, ::ArticleUnion.connection_type, null: true

def relevant_articles
object.get_similar_articles
end

field :relevant_articles_count, GraphQL::Types::Int, null: true

def relevant_articles_count
object.get_similar_articles.count
end

field :media_cluster_origin, GraphQL::Types::Int, null: true
field :media_cluster_origin_timestamp, GraphQL::Types::Int, null: true
field :media_cluster_origin_user, UserType, null: true

def media_cluster_origin_user
RecordLoader
.for(User)
.load(object.media_cluster_origin_user_id)
.then do |user|
ability = context[:ability] || Ability.new
user if ability.can?(:read, user)
end
end

field :media_cluster_relationship, RelationshipType, null: true

def media_cluster_relationship
Relationship.where(target_id: object.id).last || Relationship.where(source_id: object.id).last
end
end
3 changes: 2 additions & 1 deletion app/graph/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def about
"#{SizeValidator.config("max_width")}x#{SizeValidator.config("max_height")}",
languages_supported: CheckCldr.localized_languages.to_json,
terms_last_updated_at: User.terms_last_updated_at,
channels: CheckChannels::ChannelCodes.all_channels
channels: CheckChannels::ChannelCodes.all_channels,
media_cluster_origins: CheckMediaClusterOrigins::OriginCodes.all_origins
}
)
end
Expand Down
12 changes: 12 additions & 0 deletions app/graph/types/relationship_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,19 @@ class RelationshipType < BaseObject
field :source_id, GraphQL::Types::Int, null: true
field :permissions, GraphQL::Types::String, null: true
field :relationship_type, GraphQL::Types::String, null: true
field :user_id, GraphQL::Types::Int, null: true
field :confirmed_at, GraphQL::Types::Int, null: true
field :weight, GraphQL::Types::Float, null: true
field :source_field, GraphQL::Types::String, null: true
field :target_field, GraphQL::Types::String, null: true
field :model, GraphQL::Types::String, null: true

field :target, ProjectMediaType, null: true
field :source, ProjectMediaType, null: true
field :user, UserType, null: true
field :confirmed_by, UserType, null: true

def confirmed_by
User.find_by_id(object.confirmed_by)
end
end
28 changes: 28 additions & 0 deletions app/graph/types/team_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,32 @@ def api_keys
def statistics(period:, language: nil, platform: nil)
TeamStatistics.new(object, period, language, platform)
end

field :bot_query, [TiplineSearchResultType], null: true do
argument :search_text, GraphQL::Types::String, required: true
argument :threshold, GraphQL::Types::Float, required: false
argument :max_number_of_words, GraphQL::Types::Int, required: false
argument :enable_language_detection, GraphQL::Types::Boolean, required: false
argument :should_restrict_by_language, GraphQL::Types::Boolean, required: false
argument :enable_link_shortening, GraphQL::Types::Boolean, required: false
argument :utm_code, GraphQL::Types::String, required: false
end

def bot_query(search_text:, threshold: nil, max_number_of_words: nil, enable_language_detection: nil, should_restrict_by_language: nil, enable_link_shortening: nil, utm_code: nil)
return nil unless User.current&.is_admin # Feature flag

settings = {
threshold: threshold,
max_number_of_words: max_number_of_words,
enable_language_detection: enable_language_detection,
should_restrict_by_language: should_restrict_by_language,
enable_link_shortening: enable_link_shortening,
utm_code: utm_code
}.with_indifferent_access

language = (enable_language_detection ? Bot::Smooch.get_language({ 'text' => search_text }, object.default_language) : object.default_language)

results = object.search_for_similar_articles(search_text, nil, language, settings)
results.collect{ |result| result.as_tipline_search_result(settings) }.select{ |result| result.should_send_in_language?(language, should_restrict_by_language) }
end
end
11 changes: 11 additions & 0 deletions app/graph/types/tipline_search_result_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class TiplineSearchResultType < DefaultObject
description "Represents a search result for the tipline"

field :title, GraphQL::Types::String, null: false
field :body, GraphQL::Types::String, null: true
field :image_url, GraphQL::Types::String, null: true
field :language, GraphQL::Types::String, null: true
field :url, GraphQL::Types::String, null: true
field :type, GraphQL::Types::String, null: false
field :format, GraphQL::Types::String, null: false
end
1 change: 0 additions & 1 deletion app/lib/check_elastic_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def create_elasticsearch_doc_bg(options)
# TODO: Sawy remove annotation_type field
ms.attributes[:annotation_type] = 'mediasearch'
ms.attributes[:team_id] = self.team_id
ms.attributes[:project_id] = self.project_id
ms.attributes[:annotated_type] = self.class.name
ms.attributes[:annotated_id] = self.id
ms.attributes[:parent_id] = self.id
Expand Down
20 changes: 20 additions & 0 deletions app/lib/check_media_cluster_origins.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module CheckMediaClusterOrigins
class OriginCodes
TIPLINE_SUBMITTED = 0
USER_ADDED = 1
USER_MERGED = 2
USER_MATCHED = 3
AUTO_MATCHED = 4
ALL = [TIPLINE_SUBMITTED, USER_ADDED, USER_MERGED, USER_MATCHED, AUTO_MATCHED]

def self.all_origins
{
'TIPLINE_SUBMITTED' => TIPLINE_SUBMITTED, # First media of a cluster, submitted through a tipline
'USER_ADDED' => USER_ADDED, # First media of a cluster, uploaded manually using Check Web
'USER_MERGED' => USER_MERGED, # When a user manually-creates a relationship
'USER_MATCHED' => USER_MATCHED, # When a user confirms a suggestion
'AUTO_MATCHED' => AUTO_MATCHED # When a bot creates a relationship
}
end
end
end
32 changes: 26 additions & 6 deletions app/lib/tipline_search_result.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
class TiplineSearchResult
attr_accessor :team, :title, :body, :image_url, :language, :url, :type, :format
attr_accessor :id, :team, :image_url, :language, :type, :format, :link_settings
attr_writer :title, :body, :url

def initialize(team:, title:, body:, image_url:, language:, url:, type:, format:)
def initialize(id:, team:, title:, body:, image_url:, language:, url:, type:, format:, link_settings: nil)
self.id = id
self.team = team
self.title = title
self.body = body
Expand All @@ -10,12 +12,14 @@ def initialize(team:, title:, body:, image_url:, language:, url:, type:, format:
self.url = url
self.type = type # :explainer or :fact_check
self.format = format # :text or :image
self.link_settings = link_settings
end

def should_send_in_language?(language)
def should_send_in_language?(language, force_restrict_by_language = nil)
return true if self.team.get_languages.to_a.size < 2
tbi = TeamBotInstallation.where(team_id: self.team.id, user: BotUser.alegre_user).last
should_send_report_in_different_language = !tbi&.alegre_settings&.dig('single_language_fact_checks_enabled')
should_send_report_in_different_language = !force_restrict_by_language unless force_restrict_by_language.nil?
self.language == language || should_send_report_in_different_language
end

Expand Down Expand Up @@ -46,13 +50,29 @@ def text(language = nil, hide_body = false)
text << "*#{self.title.strip}*" unless self.title.blank?
text << self.body.to_s unless hide_body
text << self.url unless self.url.blank?
text = text.collect do |part|
self.team.get_shorten_outgoing_urls ? UrlRewriter.shorten_and_utmize_urls(part, self.team.get_outgoing_urls_utm_code) : part
end
unless language.nil?
footer = self.footer(language)
text << footer if !footer.blank? && self.team_report_setting_value('use_signature', language)
end
text.join("\n\n")
end

def title
self.formatted_value(@title)
end

def url
self.formatted_value(@url)
end

def body
self.formatted_value(@body)
end

def formatted_value(text)
link_settings = self.link_settings.to_h.with_indifferent_access
enable_link_shortening = link_settings[:enable_link_shortening].nil? ? self.team.get_shorten_outgoing_urls : link_settings[:enable_link_shortening]
utm_code = link_settings[:utm_code].nil? ? self.team.get_outgoing_urls_utm_code : link_settings[:utm_code]
enable_link_shortening ? UrlRewriter.shorten_and_utmize_urls(text, utm_code) : text
end
end
22 changes: 0 additions & 22 deletions app/models/annotations/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ class Comment < ApplicationRecord

before_save :extract_check_entities, unless: proc { |p| p.is_being_copied }
after_commit :send_slack_notification, on: [:create, :update]
after_commit :add_elasticsearch_comment, on: :create
after_commit :update_elasticsearch_comment, on: :update
after_commit :destroy_elasticsearch_comment, on: :destroy

notifies_pusher on: :destroy,
event: 'media_updated',
Expand Down Expand Up @@ -86,23 +83,4 @@ def extract_check_entities
end
self.entities = ids
end

def add_elasticsearch_comment
add_update_elasticsearch_comment('create')
end

def update_elasticsearch_comment
add_update_elasticsearch_comment('update')
end

def add_update_elasticsearch_comment(op)
# add item/task notes
if self.annotated_type == 'ProjectMedia'
add_update_nested_obj({ op: op, nested_key: 'comments', keys: ['text'], pm_id: self.annotated_id })
end
end

def destroy_elasticsearch_comment
destroy_es_items('comments', 'destroy_doc_nested', self.annotated_id) if self.annotated_type == 'ProjectMedia'
end
end
Loading

0 comments on commit 7b3264a

Please sign in to comment.