From 45d3b324883c1e72ad5f9820d3c23f7f779f28db Mon Sep 17 00:00:00 2001 From: Yamagishi Kazutoshi Date: Sun, 23 Oct 2022 06:14:58 +0900 Subject: [PATCH] Fix `Settings::FeaturedTagsController` (#19418) Regression from #19409 --- app/controllers/settings/featured_tags_controller.rb | 9 +++------ app/services/create_featured_tag_service.rb | 10 +++++++--- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/app/controllers/settings/featured_tags_controller.rb b/app/controllers/settings/featured_tags_controller.rb index cc6ec08438..b3db04a426 100644 --- a/app/controllers/settings/featured_tags_controller.rb +++ b/app/controllers/settings/featured_tags_controller.rb @@ -10,8 +10,9 @@ class Settings::FeaturedTagsController < Settings::BaseController end def create - if !featured_tag_exists? - CreateFeaturedTagService.new.call(current_account, featured_tag_params[:name]) + @featured_tag = CreateFeaturedTagService.new.call(current_account, featured_tag_params[:name], force: false) + + if @featured_tag.valid? redirect_to settings_featured_tags_path else set_featured_tags @@ -28,10 +29,6 @@ class Settings::FeaturedTagsController < Settings::BaseController private - def featured_tag_exists? - current_account.featured_tags.by_name(featured_tag_params[:name]).exists? - end - def set_featured_tag @featured_tag = current_account.featured_tags.find(params[:id]) end diff --git a/app/services/create_featured_tag_service.rb b/app/services/create_featured_tag_service.rb index c99d16113b..3cc59156db 100644 --- a/app/services/create_featured_tag_service.rb +++ b/app/services/create_featured_tag_service.rb @@ -3,14 +3,18 @@ class CreateFeaturedTagService < BaseService include Payloadable - def call(account, name) + def call(account, name, force: true) @account = account FeaturedTag.create!(account: account, name: name).tap do |featured_tag| ActivityPub::AccountRawDistributionWorker.perform_async(build_json(featured_tag), account.id) if @account.local? end - rescue ActiveRecord::RecordNotUnique - FeaturedTag.by_name(name).find_by!(account: account) + rescue ActiveRecord::RecordNotUnique, ActiveRecord::RecordInvalid => e + if force && e.is_a(ActiveRecord::RecordNotUnique) + FeaturedTag.by_name(name).find_by!(account: account) + else + account.featured_tags.new(name: name) + end end private