posts: stop updating fav_string attribute.
Stop updating the fav_string attribute on posts. The column still exists on the table, but is no longer used or updated. Like the pool_string in7d503f08, the fav_string was used in the past to facilitate `fav:X` searches. Posts had a hidden fav_string column that contained a list of every user who favorited the post. These were treated like fake hidden tags on the post so that a search for `fav:X` was treated like a tag search. The fav_string attribute has been unused for search purposes for a while now. It was only kept because of technicalities that required departitioning the favorites table first (340e1008e) before it could be removed. Basically, removing favorites with `@favorite.destroy` was slow because Rails always deletes object by ID, but we didn't have an index on favorites.id, and we couldn't easily add one until the favorites table was departitioned. Fixes #4652. See https://github.com/danbooru/danbooru/issues/4652#issuecomment-754993802 for more discussion of issues caused by the fav_string (in short: write amplification, post table bloat, and favorite inconsistency problems).
This commit is contained in:
@@ -1,23 +1,19 @@
|
||||
class Favorite < ApplicationRecord
|
||||
class Error < StandardError; end
|
||||
belongs_to :post, counter_cache: :fav_count
|
||||
belongs_to :user, counter_cache: :favorite_count
|
||||
|
||||
belongs_to :post
|
||||
belongs_to :user
|
||||
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 :for_user, ->(user_id) { where(user_id: user_id) }
|
||||
scope :public_favorites, -> { where(user: User.bit_prefs_match(:enable_private_favorites, false)) }
|
||||
|
||||
def self.visible(user)
|
||||
user.is_admin? ? all : for_user(user.id).or(public_favorites)
|
||||
user.is_admin? ? all : where(user: user).or(public_favorites)
|
||||
end
|
||||
|
||||
def self.search(params)
|
||||
q = search_attributes(params, :id, :post)
|
||||
|
||||
if params[:user_id].present?
|
||||
q = q.for_user(params[:user_id])
|
||||
end
|
||||
|
||||
q = search_attributes(params, :id, :post, :user)
|
||||
q.apply_default_order(params)
|
||||
end
|
||||
|
||||
@@ -25,37 +21,16 @@ class Favorite < ApplicationRecord
|
||||
[:post, :user]
|
||||
end
|
||||
|
||||
def self.add(post:, user:)
|
||||
Favorite.transaction do
|
||||
User.where(id: user.id).select("id").lock("FOR UPDATE").first
|
||||
def upvote_post_on_create
|
||||
if Pundit.policy!(user, PostVote).create?
|
||||
PostVote.negative.destroy_by(post: post, user: user)
|
||||
|
||||
if Favorite.for_user(user.id).where(:user_id => user.id, :post_id => post.id).exists?
|
||||
raise Error, "You have already favorited this post"
|
||||
end
|
||||
|
||||
Favorite.create!(:user_id => user.id, :post_id => post.id)
|
||||
Post.where(:id => post.id).update_all("fav_count = fav_count + 1")
|
||||
post.append_user_to_fav_string(user.id)
|
||||
User.where(:id => user.id).update_all("favorite_count = favorite_count + 1")
|
||||
user.favorite_count += 1
|
||||
# Silently ignore the error if the user has already upvoted the post.
|
||||
PostVote.create(post: post, user: user, score: 1)
|
||||
end
|
||||
end
|
||||
|
||||
def self.remove(user:, post: nil, post_id: nil)
|
||||
Favorite.transaction do
|
||||
if post && post_id.nil?
|
||||
post_id = post.id
|
||||
end
|
||||
|
||||
User.where(id: user.id).select("id").lock("FOR UPDATE").first
|
||||
|
||||
return unless Favorite.for_user(user.id).where(:user_id => user.id, :post_id => post_id).exists?
|
||||
Favorite.for_user(user.id).where(post_id: post_id).delete_all
|
||||
Post.where(:id => post_id).update_all("fav_count = fav_count - 1")
|
||||
post&.delete_user_from_fav_string(user.id)
|
||||
User.where(:id => user.id).update_all("favorite_count = favorite_count - 1")
|
||||
user.favorite_count -= 1
|
||||
post.fav_count -= 1 if post
|
||||
end
|
||||
def unvote_post_on_destroy
|
||||
PostVote.positive.destroy_by(post: post, user: user)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -10,7 +10,7 @@ class Post < ApplicationRecord
|
||||
NOTE_COPY_TAGS = %w[translated partially_translated check_translation translation_request reverse_translation
|
||||
annotated partially_annotated check_annotation annotation_request]
|
||||
|
||||
self.ignored_columns = [:pool_string]
|
||||
self.ignored_columns = [:pool_string, :fav_string]
|
||||
|
||||
deletable
|
||||
|
||||
@@ -53,7 +53,7 @@ class Post < ApplicationRecord
|
||||
has_many :children, -> {order("posts.id")}, :class_name => "Post", :foreign_key => "parent_id"
|
||||
has_many :approvals, :class_name => "PostApproval", :dependent => :destroy
|
||||
has_many :disapprovals, :class_name => "PostDisapproval", :dependent => :destroy
|
||||
has_many :favorites
|
||||
has_many :favorites, dependent: :destroy
|
||||
has_many :favorited_users, through: :favorites, source: :user
|
||||
has_many :replacements, class_name: "PostReplacement", :dependent => :destroy
|
||||
|
||||
@@ -582,10 +582,10 @@ class Post < ApplicationRecord
|
||||
pool&.add!(self)
|
||||
|
||||
when /^fav:(.+)$/i
|
||||
add_favorite(CurrentUser.user)
|
||||
Favorite.create(post: self, user: CurrentUser.user)
|
||||
|
||||
when /^-fav:(.+)$/i
|
||||
remove_favorite(CurrentUser.user)
|
||||
Favorite.destroy_by(post: self, user: CurrentUser.user)
|
||||
|
||||
when /^(up|down)vote:(.+)$/i
|
||||
score = ($1 == "up" ? 1 : -1)
|
||||
@@ -695,56 +695,11 @@ class Post < ApplicationRecord
|
||||
end
|
||||
|
||||
module FavoriteMethods
|
||||
def clean_fav_string?
|
||||
true
|
||||
end
|
||||
|
||||
def clean_fav_string!
|
||||
array = fav_string.split.uniq
|
||||
self.fav_string = array.join(" ")
|
||||
self.fav_count = array.size
|
||||
update_column(:fav_string, fav_string)
|
||||
update_column(:fav_count, fav_count)
|
||||
end
|
||||
|
||||
def favorited_by?(user)
|
||||
return false if user.is_anonymous?
|
||||
Favorite.exists?(post: self, user: user)
|
||||
end
|
||||
|
||||
def append_user_to_fav_string(user_id)
|
||||
update_column(:fav_string, (fav_string + " fav:#{user_id}").strip)
|
||||
clean_fav_string! if clean_fav_string?
|
||||
end
|
||||
|
||||
def add_favorite(user)
|
||||
add_favorite!(user)
|
||||
true
|
||||
rescue Favorite::Error
|
||||
false
|
||||
end
|
||||
|
||||
def add_favorite!(user)
|
||||
Favorite.add(post: self, user: user)
|
||||
vote!(1, user)
|
||||
end
|
||||
|
||||
def delete_user_from_fav_string(user_id)
|
||||
update_column(:fav_string, fav_string.gsub(/(?:\A| )fav:#{user_id}(?:\Z| )/, " ").strip)
|
||||
end
|
||||
|
||||
def remove_favorite!(user)
|
||||
Favorite.remove(post: self, user: user)
|
||||
unvote!(user)
|
||||
end
|
||||
|
||||
def remove_favorite(user)
|
||||
remove_favorite!(user)
|
||||
true
|
||||
rescue Favorite::Error
|
||||
false
|
||||
end
|
||||
|
||||
# Users who publicly favorited this post, ordered by time of favorite.
|
||||
def visible_favorited_users(viewer)
|
||||
favorited_users.order("favorites.id DESC").select do |fav_user|
|
||||
@@ -756,13 +711,6 @@ class Post < ApplicationRecord
|
||||
FavoriteGroup.for_post(id)
|
||||
end
|
||||
|
||||
def remove_from_favorites
|
||||
Favorite.where(post_id: id).delete_all
|
||||
user_ids = fav_string.scan(/\d+/)
|
||||
User.where(:id => user_ids).update_all("favorite_count = favorite_count - 1")
|
||||
PostVote.where(post_id: id).delete_all
|
||||
end
|
||||
|
||||
def remove_from_fav_groups
|
||||
FavoriteGroup.for_post(id).find_each do |favgroup|
|
||||
favgroup.remove!(self)
|
||||
@@ -857,8 +805,8 @@ class Post < ApplicationRecord
|
||||
|
||||
transaction do
|
||||
favorites.each do |fav|
|
||||
remove_favorite!(fav.user)
|
||||
parent.add_favorite(fav.user)
|
||||
fav.destroy!
|
||||
Favorite.create(post: parent, user: fav.user)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -887,7 +835,6 @@ class Post < ApplicationRecord
|
||||
decrement_tag_post_counts
|
||||
remove_from_all_pools
|
||||
remove_from_fav_groups
|
||||
remove_from_favorites
|
||||
destroy
|
||||
update_parent_on_destroy
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user