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

@@ -55,6 +55,8 @@ class FavoriteGroupsController < ApplicationController
def add_post
@favorite_group = authorize FavoriteGroup.find(params[:id])
@post = Post.find(params[:post_id])
@favorite_group.add!(@post)
@favorite_group.add(@post)
respond_with(@favorite_group)
end
end

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

View File

@@ -1,2 +1,7 @@
Danbooru.notice("Added post to favorite group <%= escape_javascript(@favorite_group.pretty_name) %>");
<% if @favorite_group.errors.any? %>
Danbooru.notice("<%= j @favorite_group.errors.full_messages.join("; ") %>");
<% else %>
Danbooru.notice("Added post to favorite group <%= j @favorite_group.pretty_name %>");
<% end %>
$("#add-to-favgroup-dialog").dialog("close");