2024-02-25 12:15:55 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-10-08 06:04:17 -04:00
|
|
|
class CommentVote < UserVote
|
2011-04-01 19:01:17 -04:00
|
|
|
validate :validate_user_can_vote
|
2022-05-16 07:34:25 -04:00
|
|
|
validate :validate_comment_can_be_voted
|
2019-04-10 20:21:27 -04:00
|
|
|
|
|
|
|
def self.for_comments_and_user(comment_ids, user_id)
|
|
|
|
return {} unless user_id
|
|
|
|
CommentVote.where(comment_id: comment_ids, user_id: user_id).index_by(&:comment_id)
|
|
|
|
end
|
|
|
|
|
2022-10-08 17:59:33 -04:00
|
|
|
def self.model_creator_column
|
|
|
|
:creator
|
|
|
|
end
|
|
|
|
|
2011-04-01 19:01:17 -04:00
|
|
|
def validate_user_can_vote
|
2019-09-14 20:55:54 -04:00
|
|
|
allowed = user.can_comment_vote_with_reason
|
2019-09-04 00:56:41 -04:00
|
|
|
if allowed != true
|
2019-09-14 20:55:54 -04:00
|
|
|
errors.add(:user, User.throttle_reason(allowed))
|
2019-09-04 00:56:41 -04:00
|
|
|
return false
|
2011-04-01 19:01:17 -04:00
|
|
|
end
|
2019-09-04 00:56:41 -04:00
|
|
|
true
|
2011-04-01 19:01:17 -04:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2022-05-16 07:34:25 -04:00
|
|
|
def validate_comment_can_be_voted
|
2022-04-30 06:48:53 -04:00
|
|
|
if (is_positive? || is_negative?) && comment.creator == CurrentUser.user
|
|
|
|
errors.add :base, "You cannot vote on your own comments"
|
2022-05-16 08:42:44 -04:00
|
|
|
end
|
|
|
|
if comment.is_sticky
|
|
|
|
errors.add :base, "You cannot vote on sticky comments"
|
2011-04-01 19:01:17 -04:00
|
|
|
end
|
|
|
|
end
|
2010-02-15 13:59:58 -05:00
|
|
|
end
|