Fix #5146: Adding an existing favorite to favorite groups leads to an error.

Show "Favgroup already contains post XXX" error when trying to add a
post to a favgroup that already contains that post.
This commit is contained in:
evazion
2022-05-02 14:15:53 -05:00
parent 5e923e266b
commit 93352b318e
6 changed files with 26 additions and 17 deletions

View File

@@ -132,15 +132,15 @@ class FavoriteGroup < ApplicationRecord
Post.joins("JOIN (#{favgroup_posts.to_sql}) favgroup_posts ON favgroup_posts.post_id = posts.id").order("favgroup_posts.favgroup_index ASC")
end
def add!(post)
def add(post)
with_lock do
update!(post_ids: post_ids + [post.id])
update(post_ids: post_ids + [post.id])
end
end
def remove!(post)
def remove(post)
with_lock do
update!(post_ids: post_ids - [post.id])
update(post_ids: post_ids - [post.id])
end
end

View File

@@ -537,12 +537,12 @@ class Post < ApplicationRecord
in "-favgroup", name
favgroup = FavoriteGroup.find_by_name_or_id!(name, CurrentUser.user)
raise User::PrivilegeError unless Pundit.policy!(CurrentUser.user, favgroup).update?
favgroup&.remove!(self)
favgroup&.remove(self)
in "favgroup", name
favgroup = FavoriteGroup.find_by_name_or_id!(name, CurrentUser.user)
raise User::PrivilegeError unless Pundit.policy!(CurrentUser.user, favgroup).update?
favgroup&.add!(self)
favgroup&.add(self)
end
end
@@ -632,7 +632,7 @@ class Post < ApplicationRecord
def remove_from_fav_groups
FavoriteGroup.for_post(id).find_each do |favgroup|
favgroup.remove!(self)
favgroup.remove(self)
end
end
end