Make upvotes public the same way favorites are public: * Rename the "Private favorites" account setting to "Private favorites and upvotes". * Make upvotes public, unless the user has private upvotes enabled. Note that private upvotes are still visible to admins. Downvotes are still hidden to everyone except for admins. * Make https://danbooru.donmai.us/post_votes visible to all users. This page shows all public upvotes. Private upvotes and downvotes are only visible on the page to admins and to the voter themselves. * Make votes searchable with the `upvote:username` and `downvote:username` metatags. These already existed before, but they were only usable by admins and by people searching for their own votes. Upvotes are public to discourage users from upvoting with multiple accounts. Upvote abuse is obvious to everyone when upvotes are public. The other reason is to make upvotes consistent with favorites, which are already public.
53 lines
1.3 KiB
Ruby
53 lines
1.3 KiB
Ruby
class PostVote < ApplicationRecord
|
|
belongs_to :post
|
|
belongs_to :user
|
|
|
|
validates :user_id, uniqueness: { scope: :post_id, message: "have already voted for this post" }
|
|
validates :score, inclusion: { in: [1, -1], message: "must be 1 or -1" }
|
|
|
|
after_create :update_score_after_create
|
|
after_destroy :update_score_after_destroy
|
|
|
|
scope :positive, -> { where("post_votes.score > 0") }
|
|
scope :negative, -> { where("post_votes.score < 0") }
|
|
scope :public_votes, -> { positive.where(user: User.has_public_favorites) }
|
|
|
|
def self.visible(user)
|
|
user.is_admin? ? all : where(user: user).or(public_votes)
|
|
end
|
|
|
|
def self.search(params)
|
|
q = search_attributes(params, :id, :created_at, :updated_at, :score, :user, :post)
|
|
|
|
q.apply_default_order(params)
|
|
end
|
|
|
|
def is_positive?
|
|
score > 0
|
|
end
|
|
|
|
def is_negative?
|
|
score < 0
|
|
end
|
|
|
|
def update_score_after_create
|
|
if is_positive?
|
|
Post.update_counters(post_id, { score: score, up_score: score })
|
|
else
|
|
Post.update_counters(post_id, { score: score, down_score: score })
|
|
end
|
|
end
|
|
|
|
def update_score_after_destroy
|
|
if is_positive?
|
|
Post.update_counters(post_id, { score: -score, up_score: -score })
|
|
else
|
|
Post.update_counters(post_id, { score: -score, down_score: -score })
|
|
end
|
|
end
|
|
|
|
def self.available_includes
|
|
[:user, :post]
|
|
end
|
|
end
|