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 @@
-
- <%= link_to generic_share_link(post) do %> - <%= post.post_type.is_top_level ? post.title : post.parent.title %> - <% end %> -
-
- <%= post.post_type.name %> - <%= post.category.name %> -
+ <% if post.present? %> +
+ <%= link_to generic_share_link(post) do %> + <%= post.post_type.is_top_level ? post.title : post.parent.title %> + <% end %> +
+
+ <%= post.post_type.name %> + <%= post.category.name %> +
+ <% else %> +
+ <%= I18n.t('votes.summary.post_missing') %> +
+ <% end %>
diff --git a/config/locales/strings/en.votes.yml b/config/locales/strings/en.votes.yml new file mode 100644 index 000000000..485ef2f6f --- /dev/null +++ b/config/locales/strings/en.votes.yml @@ -0,0 +1,4 @@ +en: + votes: + summary: + post_missing: 'Post not found' \ No newline at end of file diff --git a/config/schedule.rb b/config/schedule.rb index 39d5eee0b..8b0ea73db 100644 --- a/config/schedule.rb +++ b/config/schedule.rb @@ -18,6 +18,10 @@ runner 'scripts/cleanup_drafts.rb' end +every 1.day, ay: '02.25' do + runner 'scripts/cleanup_votes.rb' +end + every 6.hours do runner 'scripts/recalc_abilities.rb' end diff --git a/scripts/cleanup_votes.rb b/scripts/cleanup_votes.rb new file mode 100644 index 000000000..67707824e --- /dev/null +++ b/scripts/cleanup_votes.rb @@ -0,0 +1 @@ +CleanupVotesJob.perform_later \ No newline at end of file