Skip to content

Commit

Permalink
Abstracting reports-action field
Browse files Browse the repository at this point in the history
  • Loading branch information
tclaus committed Aug 26, 2021
1 parent 7c5c7c4 commit 8706cc2
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 24 deletions.
4 changes: 2 additions & 2 deletions app/controllers/report_controller.rb
Expand Up @@ -12,7 +12,7 @@ def index
@unreviewed_reports = Report.join_originator.where(reviewed: false).order(created_at: :desc)
@reviewed_reports = Report.join_originator.where(reviewed: true).order(created_at: :desc)
@statistics_by_reporter = statistics_by_reporter
@statistics_by_originator = statistics_by_originator
@statistics_by_author = statistics_by_author
end

def update
Expand Down Expand Up @@ -55,7 +55,7 @@ def statistics_by_reporter
ActiveRecord::Base.connection.exec_query sql
end

def statistics_by_originator
def statistics_by_author
sql = "select count(*), originator_diaspora_handle, guid from reports
left join people on originator_diaspora_handle = people.diaspora_handle
where originator_diaspora_handle is not null
Expand Down
30 changes: 21 additions & 9 deletions app/models/report.rb
Expand Up @@ -4,37 +4,41 @@ class Report < ApplicationRecord
validates :user_id, presence: true
validates :item_id, presence: true
validates :item_type, presence: true, inclusion: {
in: %w(Post Comment), message: "Type should match `Post` or `Comment`!"}
in: %w[Post Comment], message: "Type should match `Post` or `Comment`!"
}
validates :text, presence: true

validate :entry_does_not_exist, :on => :create
validate :post_or_comment_does_exist, :on => :create
validate :entry_does_not_exist, on: :create
validate :post_or_comment_does_exist, on: :create

belongs_to :user
belongs_to :post, optional: true
belongs_to :comment, optional: true
belongs_to :item, polymorphic: true

after_commit :send_report_notification, :on => :create
STATUS_DELETED = "deleted"
STATUS_NO_ACTION = "no action"

after_commit :send_report_notification, on: :create

scope :join_originator, -> {
joins("LEFT JOIN people ON originator_diaspora_handle = people.diaspora_handle ")
.select("reports.*, people.guid as originator_guid")
}

def reported_author
item.author if item
item&.author
end

def entry_does_not_exist
if Report.where(item_id: item_id, item_type: item_type).exists?(user_id: user_id)
errors[:base] << 'You cannot report the same post twice.'
errors[:base] << "You cannot report the same post twice."
end
end

def post_or_comment_does_exist
if Post.find_by_id(item_id).nil? && Comment.find_by_id(item_id).nil?
errors[:base] << 'Post or comment was already deleted or doesn\'t exists.'
errors[:base] << "Post or comment was already deleted or doesn't exists."
end
end

Expand Down Expand Up @@ -62,15 +66,23 @@ def destroy_reported_item

def mark_as_reviewed_and_deleted
Report.where(item_id: item_id, item_type: item_type)
.update_all(reviewed: true, action: "Deleted")
.update_all(reviewed: true, action: STATUS_DELETED)
end

def mark_as_reviewed
Report.where(item_id: item_id, item_type: item_type)
.update_all(reviewed: true, action: "No Action")
.update_all(reviewed: true, action: STATUS_NO_ACTION)
end
# rubocop:enable Rails/SkipsModelValidations

def action_deleted?
action&.downcase == STATUS_DELETED.downcase
end

def action_no_action?
action&.downcase == STATUS_NO_ACTION.downcase
end

def send_report_notification
Workers::Mail::ReportWorker.perform_async(id)
end
Expand Down
5 changes: 3 additions & 2 deletions app/views/report/_checked.haml
Expand Up @@ -9,7 +9,7 @@
%th
= t("report.type")
%th
= t("report.originator")
= t("report.author")
%th
= t("report.decision")
%th
Expand All @@ -28,7 +28,8 @@
- unless report.originator_diaspora_handle.nil?
= link_to(report.originator_diaspora_handle, "/people/#{report.originator_guid}")
%td
= report.action
= t("report.item_deleted") if report.action_deleted?
= t("report.item_no_action") if report.action_no_action?
%td
- unless report.item.nil?
= link_to_content(report)
Expand Down
8 changes: 4 additions & 4 deletions app/views/report/_statistics.haml
Expand Up @@ -13,16 +13,16 @@
%td
= link_to diaspora_handle, "/people/#{guid}"
%h3
= t("report.by_originator")
= t("report.by_author")
%table.table
%tr
%th
= t("report.count")
%th
= t("report.originator")
- @statistics_by_originator.rows.each do |count, originator_diaspora_handle, guid|
= t("report.author")
- @statistics_by_author.rows.each do |count, author_diaspora_handle, guid|
%tr
%td
= count
%td
= link_to originator_diaspora_handle, "people/#{guid}"
= link_to author_diaspora_handle, "people/#{guid}"
7 changes: 4 additions & 3 deletions config/locales/diaspora/en.yml
Expand Up @@ -1046,7 +1046,7 @@ en:
no_pending_reports: "No pending reports"
reported_tab: "Reported"
reviewed_tab: "Reviewed"
originator: "Originator"
author: "Author"
tooltip_incomming: "New incomming reports"
tooltip_reviewed: "Already reviewed reports"
tooltip_statistics: "Statistics about reporter and reporting items"
Expand All @@ -1057,6 +1057,8 @@ en:
type: "Type"
decision: "Decision"
action: "Action"
item_deleted: "Deleted"
item_no_action: "No Action"
count: "Count"
reason_label: "Reason:"
review_link: "Mark as reviewed"
Expand All @@ -1072,8 +1074,7 @@ en:
one: "There is one unreviewed report."
other: "There are %{count} unreviewed reports."
by_reporter: "By Reporter"
by_originator: "By Originator"
originator: "Originator"
by_author: "By Author"
profiles:
edit:
basic: "My basic profile"
Expand Down
8 changes: 4 additions & 4 deletions spec/models/report_spec.rb
Expand Up @@ -77,8 +77,8 @@

it "should set an action text on destroy item" do
@post_report.destroy_reported_item
action = @post_report.reload.action
expect(action).to eq "Deleted"
@post_report.reload
expect(@post_report.action_deleted?).to be_truthy
end
end

Expand All @@ -97,8 +97,8 @@

it "should set an action text on destroy item" do
@post_report.destroy_reported_item
action = @post_report.reload.action
expect(action).to eq "Deleted"
@post_report.reload
expect(@post_report.action_deleted?).to be_truthy
end
end
end
Expand Down

0 comments on commit 8706cc2

Please sign in to comment.