From c1cbe8ff446c029274e960d76006de4e95247d10 Mon Sep 17 00:00:00 2001 From: Oleg Valter Date: Wed, 18 Dec 2024 16:37:47 +0300 Subject: [PATCH] added audit log for when the cleanup_votes job successfully deletes orphaned votes --- app/jobs/cleanup_votes_job.rb | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/app/jobs/cleanup_votes_job.rb b/app/jobs/cleanup_votes_job.rb index 11ef53808..b54b26be7 100644 --- a/app/jobs/cleanup_votes_job.rb +++ b/app/jobs/cleanup_votes_job.rb @@ -5,12 +5,27 @@ def perform Community.all.each do |c| RequestContext.community = c orphan_votes = Vote.all.reject { |v| v.post.present? } + puts "[#{c.name}] destroying #{orphan_votes.length} #{'orphan vote'.pluralize(orphan_votes.length)}" - failed = orphan_votes.reject(&:destroy) - failed.each do |v| - puts "[#{c.name}] failed to destroy vote \"#{v.id}\"" - v.errors.each { |e| puts e.full_message } + system_user = User.find(-1) + + orphan_votes.each do |v| + result = v.destroy + + if result + AuditLog.admin_audit( + comment: "Deleted orphaned vote for user ##{v.recv_user_id} " \ + "on post ##{v.post_id} " \ + "in community ##{c.id} (#{c.name})", + event_type: 'vote_delete', + related: v, + user: system_user + ) + else + puts "[#{c.name}] failed to destroy vote \"#{v.id}\"" + v.errors.each { |e| puts e.full_message } + end end end end