Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Articles fixes #1967

Merged
merged 4 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions app/models/claim_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class ClaimDescription < ApplicationRecord
validates_presence_of :team
validates_uniqueness_of :project_media_id, allow_nil: true
after_commit :update_fact_check, on: [:update]
after_update :update_report_status
after_update :replace_media

# To avoid GraphQL conflict with name `context`
alias_attribute :claim_context, :context
Expand Down Expand Up @@ -48,6 +50,39 @@ def update_fact_check
if fact_check && self.project_media_id
fact_check.updated_at = Time.now
fact_check.save!
fact_check.update_item_status
end
end

# Pause report when claim/fact-check is removed
def update_report_status
if self.project_media_id.nil? && !self.project_media_id_before_last_save.nil?
# Update report status
pm = ProjectMedia.find(self.project_media_id_before_last_save)
report = Annotation.where(annotation_type: 'report_design', annotated_type: 'ProjectMedia', annotated_id: pm.id).last
unless report.nil?
report = report.load
data = report.data.clone.with_indifferent_access
data[:state] = 'paused'
report.data = data
report.save!
end

# Update fact-check report status
fact_check = self.fact_check
if fact_check
fact_check.report_status = 'paused'
fact_check.save!
end
end
end

# Replace item if fact-check is from a blank media
def replace_media
if !self.project_media_id_before_last_save.nil? && ProjectMedia.find_by_id(self.project_media_id_before_last_save)&.type_of_media == 'Blank'
old_pm = ProjectMedia.find(self.project_media_id_before_last_save)
new_pm = self.project_media
old_pm.replace_by(new_pm)
end
end
end
3 changes: 3 additions & 0 deletions app/models/concerns/project_media_creators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ def create_claim_description_and_fact_check
publish_report: !!fact_check['publish_report'],
signature: Digest::MD5.hexdigest([self.set_fact_check.to_json, self.team_id].join(':')),
claim_description: cd,
report_status: (fact_check['publish_report'] ? 'published' : 'unpublished'),
rating: self.set_status,
tags: self.set_tags,
skip_check_ability: true
})
end
Expand Down
20 changes: 10 additions & 10 deletions app/models/fact_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ def team
self.claim_description&.team
end

def update_item_status
pm = self.project_media
s = pm&.last_status_obj
if !s.nil? && s.status != self.rating
s.skip_check_ability = true
s.status = self.rating
s.save!
end
end

private

def set_language
Expand Down Expand Up @@ -102,16 +112,6 @@ def update_report
reports.save!
end

def update_item_status
pm = self.project_media
s = pm&.last_status_obj
if !s.nil? && s.status != self.rating
s.skip_check_ability = true
s.status = self.rating
s.save!
end
end

def article_elasticsearch_data(action = 'create_or_update')
return if self.disable_es_callbacks || RequestStore.store[:disable_es_callbacks]
data = action == 'destroy' ? {
Expand Down
2 changes: 1 addition & 1 deletion app/models/project_media.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def replace_by(new_pm, skip_send_report = false)
new_pm.skip_check_ability = true
new_pm.channel = { main: CheckChannels::ChannelCodes::FETCH }
# Point the claim and consequently the fact-check
new_pm.claim_description = self.claim_description
new_pm.claim_description = self.claim_description if self.claim_description
new_pm.save(validate: false) # To skip channel validation
RequestStore.store[:skip_check_ability] = false

Expand Down
2 changes: 1 addition & 1 deletion app/models/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ def filtered_fact_checks(filters = {})
query = FactCheck.includes(:claim_description).where('claim_descriptions.team_id' => self.id)

# Filter by standalone
query = query.where('claim_descriptions.project_media_id' => nil) if filters[:standalone]
query = query.left_joins(claim_description: { project_media: :media }).where('claim_descriptions.project_media_id IS NULL OR medias.type = ?', 'Blank') if filters[:standalone]

# Filter by language
query = query.where('fact_checks.language' => filters[:language].to_a) unless filters[:language].blank?
Expand Down
Loading