Files
danbooru/app/models/favorite.rb
evazion 353e708538 votes: allow admins to remove post votes.
Allow admins to remove votes on posts. This is for fixing vote abuse.

Votes can be removed by going to the vote list on the /post_votes page,
or by clicking on a post's score, then using the "Remove" option in the
"..." dropdown menu next to the vote.

Votes are soft-deleted - they're marked as deleted in the database, but
not fully deleted. Removed votes are only visible to admins, not to
regular users. When a vote is removed by an admin, it leaves a mod
action.

Technically it's possible to undelete votes, but there's no UI for it.
2021-11-23 23:18:54 -06:00

41 lines
1.1 KiB
Ruby

class Favorite < ApplicationRecord
belongs_to :post, counter_cache: :fav_count
belongs_to :user, counter_cache: :favorite_count
validates :user_id, uniqueness: { scope: :post_id, message: "have already favorited this post" }
after_create :upvote_post_on_create
after_destroy :unvote_post_on_destroy
scope :public_favorites, -> { where(user: User.has_public_favorites) }
def self.visible(user)
if user.is_admin?
all
elsif user.is_anonymous?
public_favorites
else
where(user: user).or(public_favorites)
end
end
def self.search(params)
q = search_attributes(params, :id, :post, :user)
q.apply_default_order(params)
end
def self.available_includes
[:post, :user]
end
def upvote_post_on_create
if Pundit.policy!(user, PostVote).create? && !PostVote.active.exists?(post: post, user: user, score: 1)
PostVote.create!(post: post, user: user, score: 1)
end
end
def unvote_post_on_destroy
vote = PostVote.active.positive.find_by(post: post, user: user)
vote&.soft_delete!(updater: user)
end
end