Fix #4462: "You have already favorited this post" on upload.

Make adding the fav:self metatag ignore all errors, including when the
post was already favorited before editing the post.
This commit is contained in:
evazion
2020-06-30 14:26:47 -05:00
parent 048bc7faf5
commit 1a8729e0d9
2 changed files with 17 additions and 2 deletions

View File

@@ -625,10 +625,10 @@ class Post < ApplicationRecord
add_pool!(pool) if pool
when /^fav:(.+)$/i
add_favorite!(CurrentUser.user)
add_favorite(CurrentUser.user)
when /^-fav:(.+)$/i
remove_favorite!(CurrentUser.user)
remove_favorite(CurrentUser.user)
when /^(up|down)vote:(.+)$/i
vote!($1)
@@ -792,6 +792,13 @@ class Post < ApplicationRecord
rescue PostVote::Error
end
def remove_favorite(user)
remove_favorite!(user)
true
rescue Favorite::Error
false
end
# users who favorited this post, ordered by users who favorited it first
def favorited_users
favorited_user_ids = fav_string.scan(/\d+/).map(&:to_i)

View File

@@ -954,6 +954,14 @@ class PostTest < ActiveSupport::TestCase
@post.update(tag_string: "aaa -fav:self")
assert_equal("", @post.fav_string)
end
should "not fail when the fav: metatag is used twice" do
@post.update(tag_string: "aaa fav:self fav:me")
assert_equal("fav:#{@user.id}", @post.fav_string)
@post.update(tag_string: "aaa -fav:self -fav:me")
assert_equal("", @post.fav_string)
end
end
context "for a child" do