diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 973a8eb59..2940021fb 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -574,18 +574,21 @@ def my_vote_summary end def vote_summary - @votes = Vote.where(recv_user: @user) \ - .includes(:post).group(:date_of, :post_id, :vote_type) - @votes = @votes.select(:post_id, :vote_type) \ - .select('count(*) as vote_count') \ - .select('date(created_at) as date_of') + @votes = Vote.where(recv_user: @user) + .joins(:post) + .group(:date_of, :post_id, :vote_type) + + @votes = @votes.select(:post_id, :vote_type) + .select('count(*) as vote_count') + .select('date(votes.created_at) as date_of') + @votes = @votes.order(date_of: :desc, post_id: :desc).all \ .group_by(&:date_of).map do |k, vl| [k, vl.group_by(&:post), vl.sum { |v| v.vote_type * v.vote_count }] end \ .paginate(page: params[:page], per_page: 15) + render layout: 'without_sidebar' - @votes end def avatar diff --git a/app/jobs/cleanup_votes_job.rb b/app/jobs/cleanup_votes_job.rb new file mode 100644 index 000000000..b54b26be7 --- /dev/null +++ b/app/jobs/cleanup_votes_job.rb @@ -0,0 +1,32 @@ +class CleanupVotesJob < ApplicationJob + queue_as :default + + 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)}" + + 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 +end diff --git a/app/models/vote.rb b/app/models/vote.rb index a7a3505ab..a7c42dcb3 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -34,37 +34,45 @@ def reverse_rep_change end def rep_change(direction) + return unless post.present? + change = CategoryPostType.rep_changes[[post.category_id, post.post_type_id]][vote_type] || 0 recv_user.update!(reputation: recv_user.reputation + (direction * change)) end def post_not_deleted - if post.deleted? + if post&.deleted? errors.add(:base, 'Votes are locked on deleted posts') end end def check_valid - throw :abort unless valid? + throw :abort unless valid? || post.blank? end def add_counter + return unless post.present? + case vote_type when 1 post.update(upvote_count: post.upvote_count + 1) when -1 post.update(downvote_count: post.downvote_count + 1) end + post.recalc_score end def remove_counter + return unless post.present? + case vote_type when 1 post.update(upvote_count: [post.upvote_count - 1, 0].max) when -1 post.update(downvote_count: [post.downvote_count - 1, 0].max) end + post.recalc_score end end diff --git a/app/views/users/vote_summary.html.erb b/app/views/users/vote_summary.html.erb index 7fab92d3e..d1cf7dcaa 100644 --- a/app/views/users/vote_summary.html.erb +++ b/app/views/users/vote_summary.html.erb @@ -40,15 +40,21 @@