Skip to content

Commit

Permalink
diaspora#8239 Enhance Reports
Browse files Browse the repository at this point in the history
  • Loading branch information
tclaus committed Nov 9, 2021
1 parent bc94438 commit e875832
Show file tree
Hide file tree
Showing 12 changed files with 262 additions and 61 deletions.
29 changes: 25 additions & 4 deletions app/controllers/report_controller.rb
Expand Up @@ -9,12 +9,16 @@ class ReportController < ApplicationController
before_action :redirect_unless_moderator, except: [:create]

def index
@reports = Report.where(reviewed: false)
@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
end

def update
if report = Report.where(id: params[:id]).first
report.mark_as_reviewed
report.update(action: "No Action")
end
redirect_to :action => :index
end
Expand All @@ -30,6 +34,7 @@ def destroy

def create
report = current_user.reports.new(report_params)
report.originator_diaspora_handle = report.reported_author.diaspora_handle
if report.save
render json: true, status: 200
else
Expand All @@ -38,7 +43,23 @@ def create
end

private
def report_params
params.require(:report).permit(:item_id, :item_type, :text)
end

def report_params
params.require(:report).permit(:item_id, :item_type, :text)
end

def statistics_by_reporter
sql = "select count(*), diaspora_handle, guid from reports
join people on reports.user_id = people.owner_id
group by diaspora_handle, guid order by 1 desc"
ActiveRecord::Base.connection.exec_query sql
end

def statistics_by_originator
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
group by originator_diaspora_handle, guid order by 1 desc"
ActiveRecord::Base.connection.exec_query sql
end
end
19 changes: 19 additions & 0 deletions app/helpers/report_helper.rb
Expand Up @@ -19,6 +19,25 @@ def report_content(report)
end
end

def link_to_content(report)
case (item = report.item)
when Post
link_to("", post_path(item.id),
{title: "View reported element",
class: "entypo-eye",
target: "_blank",
rel: "noopener"})
when Comment
link_to("", post_path(item.post.id, anchor: item.guid),
{title: "View reported comment",
class: "entypo-eye",
target: "_blank",
rel: "noopener"})
else
t("report.not_found")
end
end

def unreviewed_reports_count
@unreviewed_reports_count ||= Report.where(reviewed: false).size
end
Expand Down
18 changes: 16 additions & 2 deletions app/models/report.rb
Expand Up @@ -17,6 +17,11 @@ class Report < ApplicationRecord

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
end
Expand Down Expand Up @@ -50,12 +55,21 @@ def destroy_reported_item
item.destroy
end
end
mark_as_reviewed
mark_as_reviewed_and_deleted
end

# rubocop:disable Rails/SkipsModelValidations

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

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

def send_report_notification
Workers::Mail::ReportWorker.perform_async(id)
Expand Down
39 changes: 39 additions & 0 deletions app/views/report/_checked.haml
@@ -0,0 +1,39 @@
%table.table
%tr
%th
= t("report.created_at")
%th
= t("report.reported_by")
%th
= t("report.reason")
%th
= t("report.type")
%th
= t("report.originator")
%th
= t("report.decision")
%th
= t("report.action")
- @reviewed_reports.each do |report|
%tr
%td
= I18n.l(report.created_at, format: :short)
%td
= link_to(report.user.username, user_profile_path(report.user.username))
%td
= report.text
%td
= report.item_type
%td
- unless report.originator_diaspora_handle.nil?
= link_to(report.originator_diaspora_handle, "/people/#{report.originator_guid}")
%td
= report.action
%td
- unless report.item.nil?
= link_to_content(report)
= link_to(report_path(report.id, type: report.item_type),
data: {confirm: t("report.confirm_deletion")},
title: t("report.delete_link"),
class: "delete", method: :delete) do
%i.entypo-trash
43 changes: 43 additions & 0 deletions app/views/report/_pending.haml
@@ -0,0 +1,43 @@
- @unreviewed_reports.each do |report|
- if report.item
.panel.panel-default
- username = report.user.username
.panel-heading
.reporter.pull-right
!= t("report.reported_label", person: link_to(username, user_profile_path(username)))
.reason
%span.reason-label
!= "#{t('report.created_at')}:"
%span
= I18n.l(report.created_at, format: :short)
.reason
%span.reason-label
= t("report.reason_label")
%span
= report.text
.panel-body
.content
= report_content(report)
.segment-selection
= button_to t("report.reported_user_details"),
user_search_path(admins_controller_user_search: {guid: report.reported_author.guid}),
class: "btn pull-left btn-info btn-small col-md-3 col-xs-12", method: :post
= button_to t("report.review_link"), report_path(report.id, type: report.item_type),
class: "btn pull-left btn-info btn-small col-md-3 col-xs-12", method: :put
= button_to t("report.delete_link"), report_path(report.id, type: report.item_type),
data: {confirm: t("report.confirm_deletion")},
class: "btn pull-right btn-danger btn-small col-md-3 col-xs-12", method: :delete
- else
.panel.panel-default
- username = report.user.username
.panel-heading
.reporter.pull-right
!= t("report.reported_label", person: link_to(username, user_profile_path(username)))
.title
= report_content(report)
.panel-body
= button_to t("report.review_link"), report_path(report.id, type: report.item_type),
class: "btn pull-left btn-info btn-small", method: :put
- if @unreviewed_reports.empty?
%h3
= t("report.no_pending_reports")
60 changes: 21 additions & 39 deletions app/views/report/_reports.haml
@@ -1,44 +1,26 @@

- content_for :head do
= stylesheet_link_tag :admin

.reports
%h1
= t("report.title")
- if @reports.empty?
%p
= t("report.unreviewed_reports", count: 0)
- @reports.each do |report|
- if report.item
.panel.panel-default
- username = report.user.username
.panel-heading
.reporter.pull-right
!= t("report.reported_label", person: link_to(username, user_profile_path(username)))
.reason
%span.reason-label
= t("report.reason_label")
%span
= report.text
.panel-body
.content
= report_content(report)
.segment-selection
= button_to t("report.reported_user_details"),
user_search_path(admins_controller_user_search: {guid: report.reported_author.guid}),
class: "btn pull-left btn-info btn-small col-md-3 col-xs-12", method: :post
= button_to t("report.review_link"), report_path(report.id, type: report.item_type),
class: "btn pull-left btn-info btn-small col-md-3 col-xs-12", method: :put
= button_to t("report.delete_link"), report_path(report.id, type: report.item_type),
data: {confirm: t("report.confirm_deletion")},
class: "btn pull-right btn-danger btn-small col-md-3 col-xs-12", method: :delete
- else
.panel.panel-default
- username = report.user.username
.panel-heading
.reporter.pull-right
!= t("report.reported_label", person: link_to(username, user_profile_path(username)))
.title
= report_content(report)
.panel-body
= button_to t("report.review_link"), report_path(report.id, type: report.item_type),
class: "btn pull-left btn-info btn-small", method: :put
%ul.nav.nav-tabs{role: "tablist"}
%li.nav-item.active{role: "presentation"}
%a{href: "#reports", title: t("report.tooltip_incomming"),
aria: {controls: "reports", selected: true}, data: {toggle: "tab"}}
= t("report.reported_tab")
%li.nav-item{role: "presentation"}
%a{href: "#checked", title: t("report.tooltip_reviewed"),
aria: {controls: "checked", selected: false}, data: {toggle: "tab"}}
= t("report.reviewed_tab")
%li.nav-item{role: "presentation"}
%a{href: "#statistics", title: t("report.tooltip_statistics"),
aria: {controls: "statistics", selected: false}, data: {toggle: "tab"}}
= t("report.statistics_tab")
.tab-content
.tab-pane.active#reports{role: "tabpanel"}
= render partial: "report/pending"
.tab-pane#checked{role: "tabpanel"}
= render partial: "report/checked"
.tab-pane#statistics{role: "tabpanel"}
= render partial: "report/statistics"
28 changes: 28 additions & 0 deletions app/views/report/_statistics.haml
@@ -0,0 +1,28 @@
%h3
= t("report.by_reporter")
%table.table
%tr
%th
= t("report.count")
%th
= t("report.reported_by")
- @statistics_by_reporter.rows.each do |count, diaspora_handle, guid|
%tr
%td
= count
%td
= link_to diaspora_handle, "/people/#{guid}"
%h3
= t("report.by_originator")
%table.table
%tr
%th
= t("report.count")
%th
= t("report.originator")
- @statistics_by_originator.rows.each do |count, originator_diaspora_handle, guid|
%tr
%td
= count
%td
= link_to originator_diaspora_handle, "people/#{guid}"
19 changes: 18 additions & 1 deletion config/locales/diaspora/en.yml
Expand Up @@ -1054,6 +1054,21 @@ en:
post_label: "<strong>Post</strong>: %{content}"
comment_label: "<strong>Comment</strong>: %{data}"
reported_label: "<strong>Reported by</strong> %{person}"
no_pending_reports: "No pending reports"
reported_tab: "Reported"
reviewed_tab: "Reviewed"
originator: "Originator"
tooltip_incomming: "New incomming reports"
tooltip_reviewed: "Already reviewed reports"
tooltip_statistics: "Statistics about reporter and reporting items"
reported_by: "Reported by"
statistics_tab: "Statistics"
created_at: "Created at"
reason: "Reason"
type: "Type"
decision: "Decision"
action: "Action"
count: "Count"
reason_label: "Reason:"
review_link: "Mark as reviewed"
delete_link: "Delete item"
Expand All @@ -1067,7 +1082,9 @@ en:
zero: "There are no unreviewed reports."
one: "There is one unreviewed report."
other: "There are %{count} unreviewed reports."

by_reporter: "By Reporter"
by_originator: "By Originator"
originator: "Originator"
profiles:
edit:
basic: "My basic profile"
Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20210421064041_add_action_to_report.rb
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddActionToReport < ActiveRecord::Migration[5.2]
def change
add_column :reports, :action, :string
end
end
19 changes: 19 additions & 0 deletions db/migrate/20210424054604_add_user_fields_to_reports.rb
@@ -0,0 +1,19 @@
# frozen_string_literal: true

class AddUserFieldsToReports < ActiveRecord::Migration[5.2]
def change
add_column :reports, :originator_diaspora_handle, :string, index: true

Report.find_each do |report|
# get originator author from item before item gets deleted
unless report.reported_author.nil?
report.originator_diaspora_handle = report.reported_author.diaspora_handle
report.save(validate: false, touch: false)
end
if report.item.nil?
report.action = "Deleted"
report.save(validate: false, touch: false)
end
end
end
end

0 comments on commit e875832

Please sign in to comment.