From 88bb5f6522398dc1947de92182d76fc5ff6054ca Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Thu, 11 Jul 2024 15:15:21 +0100 Subject: [PATCH] WIP Purge old & hidden comments --- app/models/comment.rb | 2 ++ app/models/comment/purgable.rb | 39 ++++++++++++++++++++++++++++++++ app/models/info_request_event.rb | 1 + 3 files changed, 42 insertions(+) create mode 100644 app/models/comment/purgable.rb diff --git a/app/models/comment.rb b/app/models/comment.rb index 7a373b0068..b6a36e57a3 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -60,6 +60,8 @@ class Comment < ApplicationRecord where(visible: true) } + scope :hidden, -> { where(visible: false) } + scope :embargoed, -> { joins(info_request: :embargo). where('embargoes.id IS NOT NULL'). diff --git a/app/models/comment/purgable.rb b/app/models/comment/purgable.rb new file mode 100644 index 0000000000..83eee1d003 --- /dev/null +++ b/app/models/comment/purgable.rb @@ -0,0 +1,39 @@ +module Comment::Purgable + 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 purge_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.purge(editor: editor, reason: reason) + end + end + end + + def purge(editor:, reason:) + return false unless hidden? + + event_params = { + comment_id: id, + comment_created_at: created_at, + comment_user: user, + editor: editor.url_name, + reason: "Purged: #{reason}" + } + + ActiveRecord::Base.transaction do + destroy! + info_request_events.create!('destroy_comment', event_params) + end + end +end diff --git a/app/models/info_request_event.rb b/app/models/info_request_event.rb index dc25750a7b..7537b2ea8b 100644 --- a/app/models/info_request_event.rb +++ b/app/models/info_request_event.rb @@ -34,6 +34,7 @@ class InfoRequestEvent < ApplicationRecord 'edit_outgoing', # outgoing message edited (in admin interface) 'edit_comment', # comment edited (in admin interface) 'hide_comment', # comment hidden by admin + 'destroy_comment', # comment destroyed by admin 'report_comment', # comment reported for admin attention by user 'report_request', # a request reported for admin attention by user 'destroy_incoming', # deleted an incoming message (in admin interface)