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 e8f8093
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 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) }

Check failure on line 69 in app/models/comment.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 unexpected token tLAMBDA (Using Ruby 3.0 parser; configure using `TargetRubyVersion` parameter, under `AllCops`) Raw Output: app/models/comment.rb:69:17: F: Lint/Syntax: unexpected token tLAMBDA (Using Ruby 3.0 parser; configure using `TargetRubyVersion` parameter, under `AllCops`)

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,22 @@ def hide(editor:)
end
end

# TODO: Maybe add an erase_comment event
def erase(editor:, reason:)
return false unless hidden?

ActiveRecord::Base.transaction do
event_params = {
comment_id: id,
editor: editor.url_name,
reason: "Erased: #{reason}"
}

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

def cached_urls
[
request_path(info_request),
Expand Down

0 comments on commit e8f8093

Please sign in to comment.