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

WIP: Erase comments that have been hidden for a while #8319

Closed
wants to merge 6 commits into from
Closed
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
4 changes: 3 additions & 1 deletion app/helpers/admin/link_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ def comment_both_links(comment)
title = 'View comment on public website'
icon = prominence_icon(comment)

body = comment.body.present? ? comment.body : tag.tt('[ERASED]')

link_to(icon, comment_path(comment), title: title) + ' ' +
link_to(truncate(comment.body, length: 60), edit_admin_comment_path(comment),
link_to(truncate(body, length: 60), edit_admin_comment_path(comment),
title: admin_title)
end

Expand Down
6 changes: 5 additions & 1 deletion app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
# Email: [email protected]; WWW: http://www.mysociety.org/

class Comment < ApplicationRecord
include Comment::Erasable

include Rails.application.routes.url_helpers
include LinkToHelper

Expand Down Expand Up @@ -52,14 +54,16 @@ class Comment < ApplicationRecord

# validates_presence_of :user # breaks during construction of new ones :(
validate :check_body_has_content,
:check_body_uses_mixed_capitals
:check_body_uses_mixed_capitals, unless: :hidden?

scope :visible, -> {
joins(:info_request).
merge(InfoRequest.is_searchable.except(:select)).
where(visible: true)
}

scope :hidden, -> { where(visible: false) }

scope :embargoed, -> {
joins(info_request: :embargo).
where('embargoes.id IS NOT NULL').
Expand Down
31 changes: 31 additions & 0 deletions app/models/comment/erasable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Comment::Erasable

Check warning on line 1 in app/models/comment/erasable.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Missing top-level documentation comment for `module Comment::Erasable`. Raw Output: app/models/comment/erasable.rb:1:1: C: Style/Documentation: Missing top-level documentation comment for `module Comment::Erasable`.
extend ActiveSupport::Concern

included do
cattr_accessor :old_age_in_days,
instance_reader: false,
instance_writer: false,
instance_accessor: false,
default: 30
end

class_methods do
def erase_old_hidden(editor: User.internal_admin_user)
old_hidden = hidden.where('updated_at > ?', old_age_in_days.days.ago)
reason = "Hidden for longer than #{old_age_in_days} days"

old_hidden.find_each do |comment|
comment.erase(editor: editor, reason: reason)
end
end
end

def erase(**kwargs)
return false unless hidden?

Check warning on line 24 in app/models/comment/erasable.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Add empty line after guard clause. Raw Output: app/models/comment/erasable.rb:24:5: C: Layout/EmptyLineAfterGuardClause: Add empty line after guard clause.
Comment::Erasure.new(self, **kwargs).erase
end

def erased?
info_request_events.erase_comment_events.any?
end
end
43 changes: 43 additions & 0 deletions app/models/comment/erasure.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Comment::Erasure

Check warning on line 1 in app/models/comment/erasure.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Missing top-level documentation comment for `class Comment::Erasure`. Raw Output: app/models/comment/erasure.rb:1:1: C: Style/Documentation: Missing top-level documentation comment for `class Comment::Erasure`.
def initialize(comment, editor: User.internal_admin_user, reason:)
@comment = comment
@editor = editor
@reason = reason
end

def erase
ActiveRecord::Base.transaction do
erase_comment
erase_comment_events
end
end

protected

attr_reader :comment, :editor, :reason

private

def erase_comment
event_params = {
comment_id: comment.id,
editor: editor.url_name,
reason: "Erased: #{reason}"
}

comment.update!(body: '')
comment.info_request.log_event('erase_comment', event_params)
end

def erase_comment_events
comment.info_request_events.edit_comment_events.find_each do |event|
params = event.params.dup

params.each do |key, _|
params[key] = "[ERASED]" if key =~ /body/
end

event.update(params: params)
end
end
end
1 change: 1 addition & 0 deletions app/models/info_request_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class InfoRequestEvent < ApplicationRecord
'edit_comment', # comment edited (in admin interface)
'hide_comment', # comment hidden by admin
'report_comment', # comment reported for admin attention by user
'erase_comment', # comment is erased
'report_request', # a request reported for admin attention by user
'destroy_incoming', # deleted an incoming message (in admin interface)
'destroy_outgoing', # deleted an outgoing message (in admin interface)
Expand Down
12 changes: 9 additions & 3 deletions app/views/admin_comment/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@
</div>

<%= form_tag admin_comment_path(@comment), :method => 'put' do %>
<% if @comment.erased? %>
<p class="text-warning">
<%= icon('ban-circle') %> <strong>Editing is restricted for erased comments</strong>
</p>
<% end %>

<p><label for="comment_body">Body of annotation</label><br/>
<%= text_area 'comment', 'body', :rows => 10, :cols => 60 %></p>
<%= text_area 'comment', 'body', :rows => 10, :cols => 60, disabled: @comment.erased? %></p>

<p><label for="comment_visible">Visible</label>
<%= select('comment', "visible", [["Yes – show comment",true],["No – hide comment",false]]) %>
<%= select('comment', "visible", [["Yes – show comment",true],["No – hide comment",false]], disabled: @comment.erased?) %>
</p>

<p><label for="comment_attention_requested">Admin attention requested</label>
Expand All @@ -44,8 +49,9 @@

<hr>
<h2>Events</h2>

<div class="accordion" id="events">
<% @comment.info_request_events.each do |info_request_event| %>
<% @comment.info_request_events.order(created_at: :asc).each do |info_request_event| %>
<div class="accordion-group">
<div class="accordion-heading">
<span class="item-title">
Expand Down
Loading