Files
danbooru/app/models/favorite.rb
evazion ee638f976f Add /user_actions page.
Add a /user_actions page. This page shows you a global timeline of
(almost) all activity on the site, including uploads, comments, votes,
edits, forum posts, and so on.

The main things it doesn't include are post edits, pool edits, and
favorites (posts and pools live in a separate database, and favorites
don't have the timestamps we need for ordering).

This page is useful for moderation purposes because it lets you see a
history of almost all of a user's activity on a single page.

Currently this page is mod-only. In the future it will be open to all
users, so you can view the history of your own site activity, or the
activity of others.
2022-09-16 05:39:25 -05:00

43 lines
1.1 KiB
Ruby

# frozen_string_literal: true
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.not(user: User.has_private_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