Skip to content

Commit

Permalink
WIP: Erase comments that have been hidden for a while
Browse files Browse the repository at this point in the history
TODO: Add a nightly cron task that calls `Comment.erase_old_hidden`
  • Loading branch information
garethrees committed Jul 11, 2024
1 parent 15c547e commit 28b246a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
24 changes: 23 additions & 1 deletion app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ class Comment < ApplicationRecord
instance_accessor: false,
default: DEFAULT_CREATION_RATE_LIMITS

cattr_accessor :old_age_in_days,
instance_reader: false,
instance_writer: false,
instance_accessor: false,
default: 30

strip_attributes allow_empty: true

belongs_to :user,
Expand All @@ -52,14 +58,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 All @@ -79,6 +87,15 @@ class Comment < ApplicationRecord

default_url_options[:host] = AlaveteliConfiguration.domain

def self.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

# When posting a new comment, use this to check user hasn't double
# submitted.
def self.find_existing(info_request_id, body)
Expand Down Expand Up @@ -194,6 +211,11 @@ def hide(editor:)
end
end

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

Check warning on line 215 in app/models/comment.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.rb:215:5: C: Layout/EmptyLineAfterGuardClause: Add empty line after guard clause.
Comment::Erasure.new(self, **kwargs).erase
end

def cached_urls
[
request_path(info_request),
Expand Down
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

0 comments on commit 28b246a

Please sign in to comment.