comments: allow votes to be soft deleted.
Make it so that when a user removes their own vote, the vote is soft deleted (the is_deleted flag is set) instead of hard deleted. Changes: * Add is_deleted flag to comment votes. * Relax uniqueness constraint so you can have multiple deleted votes on the same comment. You can still only have one active vote on the comment. * Add `soft_delete` method to Deletable concern.
This commit is contained in:
@@ -28,12 +28,12 @@ class CommentComponent < ApplicationComponent
|
||||
|
||||
def upvoted?
|
||||
return false if current_user.is_anonymous?
|
||||
comment.votes.select(&:is_positive?).map(&:user_id).include?(current_user.id)
|
||||
comment.votes.active.select(&:is_positive?).map(&:user_id).include?(current_user.id)
|
||||
end
|
||||
|
||||
def downvoted?
|
||||
return false if current_user.is_anonymous?
|
||||
comment.votes.select(&:is_negative?).map(&:user_id).include?(current_user.id)
|
||||
comment.votes.active.select(&:is_negative?).map(&:user_id).include?(current_user.id)
|
||||
end
|
||||
|
||||
def reported?
|
||||
|
||||
@@ -13,7 +13,11 @@ class CommentVotesController < ApplicationController
|
||||
|
||||
@comment.with_lock do
|
||||
@comment_vote = authorize CommentVote.new(comment: @comment, score: params[:score], user: CurrentUser.user)
|
||||
CommentVote.where(comment: @comment, user: CurrentUser.user).destroy_all
|
||||
|
||||
CommentVote.active.where(comment: @comment, user: CurrentUser.user).each do |vote|
|
||||
vote.soft_delete!(updater: CurrentUser.user)
|
||||
end
|
||||
|
||||
@comment_vote.save
|
||||
end
|
||||
|
||||
@@ -22,8 +26,9 @@ class CommentVotesController < ApplicationController
|
||||
end
|
||||
|
||||
def destroy
|
||||
@comment_vote = authorize CommentVote.find_by!(comment_id: params[:comment_id], user: CurrentUser.user)
|
||||
@comment_vote.destroy
|
||||
# XXX should find by comment vote id.
|
||||
@comment_vote = authorize CommentVote.active.find_by!(comment_id: params[:comment_id], user: CurrentUser.user)
|
||||
@comment_vote.soft_delete(updater: CurrentUser.user)
|
||||
|
||||
respond_with(@comment_vote)
|
||||
end
|
||||
|
||||
@@ -6,6 +6,14 @@ module Deletable
|
||||
scope :active, -> { where(is_deleted: false) }
|
||||
scope :deleted, -> { where(is_deleted: true) }
|
||||
scope :undeleted, -> { where(is_deleted: false) }
|
||||
|
||||
define_method(:soft_delete) do |**options|
|
||||
update(is_deleted: true, **options)
|
||||
end
|
||||
|
||||
define_method(:soft_delete!) do |**options|
|
||||
update!(is_deleted: true, **options)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
class CommentVote < ApplicationRecord
|
||||
attr_accessor :updater
|
||||
|
||||
belongs_to :comment
|
||||
belongs_to :user
|
||||
|
||||
validates :user_id, uniqueness: { scope: :comment_id, message: "have already voted for this comment" }
|
||||
validate :validate_vote_is_unique, if: :is_deleted_changed?
|
||||
validates :score, inclusion: { in: [-1, 1], message: "must be 1 or -1" }
|
||||
|
||||
after_create :update_score_after_create
|
||||
after_destroy :update_score_after_destroy
|
||||
before_create :update_score_on_create
|
||||
before_save :update_score_on_delete_or_undelete, if: -> { !new_record? && is_deleted_changed? }
|
||||
|
||||
deletable
|
||||
|
||||
def self.visible(user)
|
||||
if user.is_moderator?
|
||||
@@ -19,7 +23,7 @@ class CommentVote < ApplicationRecord
|
||||
end
|
||||
|
||||
def self.search(params)
|
||||
q = search_attributes(params, :id, :created_at, :updated_at, :score, :comment, :user)
|
||||
q = search_attributes(params, :id, :created_at, :updated_at, :score, :is_deleted, :comment, :user)
|
||||
q.apply_default_order(params)
|
||||
end
|
||||
|
||||
@@ -31,15 +35,34 @@ class CommentVote < ApplicationRecord
|
||||
score == -1
|
||||
end
|
||||
|
||||
def update_score_after_create
|
||||
# allow duplicate deleted votes but not duplicate active votes
|
||||
def validate_vote_is_unique
|
||||
if !is_deleted? && CommentVote.active.where.not(id: id).exists?(comment_id: comment_id, user_id: user_id)
|
||||
errors.add(:user, "have already voted for this comment")
|
||||
end
|
||||
end
|
||||
|
||||
def update_score_on_create
|
||||
comment.with_lock do
|
||||
comment.update_columns(score: comment.score + score)
|
||||
end
|
||||
end
|
||||
|
||||
def update_score_after_destroy
|
||||
def update_score_on_delete_or_undelete
|
||||
comment.with_lock do
|
||||
comment.update_columns(score: comment.score - score)
|
||||
if is_deleted_changed?(from: false, to: true)
|
||||
comment.update_columns(score: comment.score - score)
|
||||
|
||||
if updater != user
|
||||
ModAction.log("#{updater.name} deleted comment vote ##{id} on comment ##{comment_id}", :comment_vote_delete, updater)
|
||||
end
|
||||
else
|
||||
comment.update_columns(score: comment.score + score)
|
||||
|
||||
if updater != user
|
||||
ModAction.log("#{updater.name} undeleted comment vote ##{id} on comment ##{comment_id}", :comment_vote_undelete, updater)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -37,6 +37,8 @@ class ModAction < ApplicationRecord
|
||||
artist_unban: 185,
|
||||
comment_update: 81,
|
||||
comment_delete: 82,
|
||||
comment_vote_delete: 92,
|
||||
comment_vote_undelete: 93,
|
||||
forum_topic_delete: 202,
|
||||
forum_topic_undelete: 203,
|
||||
forum_topic_lock: 206,
|
||||
|
||||
Reference in New Issue
Block a user