Change featured tag updates to add/remove activity (#19409)

* Change featured tag updates to add/remove activity

* Fix to check for the existence of feature tag

* Rename service and worker

* Merge AddHashtagSerializer with AddSerializer

* Undo removal of sidekiq_options
This commit is contained in:
Takeshi Umeda 2022-10-23 01:30:55 +09:00 committed by GitHub
parent 73a48318a1
commit 74ead7d106
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 117 additions and 15 deletions

View File

@ -13,14 +13,12 @@ class Api::V1::FeaturedTagsController < Api::BaseController
end end
def create def create
@featured_tag = current_account.featured_tags.create!(featured_tag_params) featured_tag = CreateFeaturedTagService.new.call(current_account, featured_tag_params[:name])
ActivityPub::UpdateDistributionWorker.perform_in(3.minutes, current_account.id) render json: featured_tag, serializer: REST::FeaturedTagSerializer
render json: @featured_tag, serializer: REST::FeaturedTagSerializer
end end
def destroy def destroy
@featured_tag.destroy! RemoveFeaturedTagWorker.perform_async(current_account.id, @featured_tag.id)
ActivityPub::UpdateDistributionWorker.perform_in(3.minutes, current_account.id)
render_empty render_empty
end end

View File

@ -10,10 +10,8 @@ class Settings::FeaturedTagsController < Settings::BaseController
end end
def create def create
@featured_tag = current_account.featured_tags.new(featured_tag_params) if !featured_tag_exists?
CreateFeaturedTagService.new.call(current_account, featured_tag_params[:name])
if @featured_tag.save
ActivityPub::UpdateDistributionWorker.perform_in(3.minutes, current_account.id)
redirect_to settings_featured_tags_path redirect_to settings_featured_tags_path
else else
set_featured_tags set_featured_tags
@ -24,13 +22,16 @@ class Settings::FeaturedTagsController < Settings::BaseController
end end
def destroy def destroy
@featured_tag.destroy! RemoveFeaturedTagWorker.perform_async(current_account.id, @featured_tag.id)
ActivityPub::UpdateDistributionWorker.perform_in(3.minutes, current_account.id)
redirect_to settings_featured_tags_path redirect_to settings_featured_tags_path
end end
private private
def featured_tag_exists?
current_account.featured_tags.by_name(featured_tag_params[:name]).exists?
end
def set_featured_tag def set_featured_tag
@featured_tag = current_account.featured_tags.find(params[:id]) @featured_tag = current_account.featured_tags.find(params[:id])
end end

View File

@ -30,6 +30,10 @@ class FeaturedTag < ApplicationRecord
LIMIT = 10 LIMIT = 10
def sign?
true
end
def name def name
tag_id.present? ? tag.name : @name tag_id.present? ? tag.name : @name
end end

View File

@ -1,10 +1,29 @@
# frozen_string_literal: true # frozen_string_literal: true
class ActivityPub::AddSerializer < ActivityPub::Serializer class ActivityPub::AddSerializer < ActivityPub::Serializer
class UriSerializer < ActiveModel::Serializer
include RoutingHelper
def serializable_hash(*_args)
ActivityPub::TagManager.instance.uri_for(object)
end
end
def self.serializer_for(model, options)
case model.class.name
when 'Status'
UriSerializer
when 'FeaturedTag'
ActivityPub::HashtagSerializer
else
super
end
end
include RoutingHelper include RoutingHelper
attributes :type, :actor, :target attributes :type, :actor, :target
attribute :proper_object, key: :object has_one :proper_object, key: :object
def type def type
'Add' 'Add'
@ -15,7 +34,7 @@ class ActivityPub::AddSerializer < ActivityPub::Serializer
end end
def proper_object def proper_object
ActivityPub::TagManager.instance.uri_for(object) object
end end
def target def target

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
class ActivityPub::HashtagSerializer < ActivityPub::Serializer class ActivityPub::HashtagSerializer < ActivityPub::Serializer
context_extensions :hashtag
include RoutingHelper include RoutingHelper
attributes :type, :href, :name attributes :type, :href, :name

View File

@ -1,10 +1,29 @@
# frozen_string_literal: true # frozen_string_literal: true
class ActivityPub::RemoveSerializer < ActivityPub::Serializer class ActivityPub::RemoveSerializer < ActivityPub::Serializer
class UriSerializer < ActiveModel::Serializer
include RoutingHelper
def serializable_hash(*_args)
ActivityPub::TagManager.instance.uri_for(object)
end
end
def self.serializer_for(model, options)
case model.class.name
when 'Status'
UriSerializer
when 'FeaturedTag'
ActivityPub::HashtagSerializer
else
super
end
end
include RoutingHelper include RoutingHelper
attributes :type, :actor, :target attributes :type, :actor, :target
attribute :proper_object, key: :object has_one :proper_object, key: :object
def type def type
'Remove' 'Remove'
@ -15,7 +34,7 @@ class ActivityPub::RemoveSerializer < ActivityPub::Serializer
end end
def proper_object def proper_object
ActivityPub::TagManager.instance.uri_for(object) object
end end
def target def target

View File

@ -0,0 +1,21 @@
# frozen_string_literal: true
class CreateFeaturedTagService < BaseService
include Payloadable
def call(account, name)
@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)
end
private
def build_json(featured_tag)
Oj.dump(serialize_payload(featured_tag, ActivityPub::AddSerializer, signer: @account))
end
end

View File

@ -0,0 +1,18 @@
# frozen_string_literal: true
class RemoveFeaturedTagService < BaseService
include Payloadable
def call(account, featured_tag)
@account = account
featured_tag.destroy!
ActivityPub::AccountRawDistributionWorker.perform_async(build_json(featured_tag), account.id) if @account.local?
end
private
def build_json(featured_tag)
Oj.dump(serialize_payload(featured_tag, ActivityPub::RemoveSerializer, signer: @account))
end
end

View File

@ -0,0 +1,9 @@
# frozen_string_literal: true
class ActivityPub::AccountRawDistributionWorker < ActivityPub::RawDistributionWorker
protected
def inboxes
@inboxes ||= AccountReachFinder.new(@account).inboxes
end
end

View File

@ -0,0 +1,11 @@
# frozen_string_literal: true
class RemoveFeaturedTagWorker
include Sidekiq::Worker
def perform(account_id, featured_tag_id)
RemoveFeaturedTagService.new.call(Account.find(account_id), FeaturedTag.find(featured_tag_id))
rescue ActiveRecord::RecordNotFound
true
end
end