From 1a8729e0d96526f2e55d83c802236eb93ca99e88 Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 30 Jun 2020 14:26:47 -0500 Subject: [PATCH] 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. --- app/models/post.rb | 11 +++++++++-- test/unit/post_test.rb | 8 ++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index e1d40ef91..56278e9c2 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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) diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index ab160680f..3f73b5934 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -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