Add REST API for featuring and unfeaturing a hashtag (#34489)
Co-authored-by: Matt Jankowski <matt@jankowski.online> Co-authored-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
@@ -18,7 +18,7 @@ class Api::V1::FeaturedTagsController < Api::BaseController
|
||||
end
|
||||
|
||||
def destroy
|
||||
RemoveFeaturedTagWorker.perform_async(current_account.id, @featured_tag.id)
|
||||
RemoveFeaturedTagService.new.call(current_account, @featured_tag)
|
||||
render_empty
|
||||
end
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Api::V1::TagsController < Api::BaseController
|
||||
before_action -> { doorkeeper_authorize! :follow, :write, :'write:follows' }, except: :show
|
||||
before_action -> { doorkeeper_authorize! :follow, :write, :'write:follows' }, only: [:follow, :unfollow]
|
||||
before_action -> { doorkeeper_authorize! :write, :'write:accounts' }, only: [:feature, :unfeature]
|
||||
before_action :require_user!, except: :show
|
||||
before_action :set_or_create_tag
|
||||
|
||||
@@ -23,6 +24,16 @@ class Api::V1::TagsController < Api::BaseController
|
||||
render json: @tag, serializer: REST::TagSerializer
|
||||
end
|
||||
|
||||
def feature
|
||||
CreateFeaturedTagService.new.call(current_account, @tag)
|
||||
render json: @tag, serializer: REST::TagSerializer
|
||||
end
|
||||
|
||||
def unfeature
|
||||
RemoveFeaturedTagService.new.call(current_account, @tag)
|
||||
render json: @tag, serializer: REST::TagSerializer
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_or_create_tag
|
||||
|
||||
@@ -12,7 +12,7 @@ class Settings::FeaturedTagsController < Settings::BaseController
|
||||
end
|
||||
|
||||
def create
|
||||
@featured_tag = CreateFeaturedTagService.new.call(current_account, featured_tag_params[:name], force: false)
|
||||
@featured_tag = CreateFeaturedTagService.new.call(current_account, featured_tag_params[:name], raise_error: false)
|
||||
|
||||
if @featured_tag.valid?
|
||||
redirect_to settings_featured_tags_path
|
||||
|
||||
@@ -18,17 +18,17 @@ class FeaturedTag < ApplicationRecord
|
||||
belongs_to :account, inverse_of: :featured_tags
|
||||
belongs_to :tag, inverse_of: :featured_tags, optional: true # Set after validation
|
||||
|
||||
validates :name, presence: true, format: { with: Tag::HASHTAG_NAME_RE }, on: :create
|
||||
validates :name, presence: true, on: :create, if: -> { tag_id.nil? }
|
||||
validates :name, format: { with: Tag::HASHTAG_NAME_RE }, on: :create, allow_blank: true
|
||||
validates :tag_id, uniqueness: { scope: :account_id }
|
||||
|
||||
validate :validate_tag_uniqueness, on: :create
|
||||
validate :validate_featured_tags_limit, on: :create
|
||||
|
||||
normalizes :name, with: ->(name) { name.strip.delete_prefix('#') }
|
||||
|
||||
before_create :set_tag
|
||||
before_create :reset_data
|
||||
before_validation :set_tag
|
||||
|
||||
scope :by_name, ->(name) { joins(:tag).where(tag: { name: HashtagNormalizer.new.normalize(name) }) }
|
||||
before_create :reset_data
|
||||
|
||||
LIMIT = 10
|
||||
|
||||
@@ -59,7 +59,11 @@ class FeaturedTag < ApplicationRecord
|
||||
private
|
||||
|
||||
def set_tag
|
||||
self.tag = Tag.find_or_create_by_names(name)&.first
|
||||
if tag.nil?
|
||||
self.tag = Tag.find_or_create_by_names(name)&.first
|
||||
elsif tag&.new_record?
|
||||
tag.save
|
||||
end
|
||||
end
|
||||
|
||||
def reset_data
|
||||
@@ -73,14 +77,6 @@ class FeaturedTag < ApplicationRecord
|
||||
errors.add(:base, I18n.t('featured_tags.errors.limit')) if account.featured_tags.count >= LIMIT
|
||||
end
|
||||
|
||||
def validate_tag_uniqueness
|
||||
errors.add(:name, :taken) if tag_already_featured_for_account?
|
||||
end
|
||||
|
||||
def tag_already_featured_for_account?
|
||||
FeaturedTag.by_name(name).exists?(account_id: account_id)
|
||||
end
|
||||
|
||||
def visible_tagged_account_statuses
|
||||
account.statuses.distributable_visibility.tagged_with(tag)
|
||||
end
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class TagRelationshipsPresenter
|
||||
attr_reader :following_map
|
||||
attr_reader :following_map, :featuring_map
|
||||
|
||||
def initialize(tags, current_account_id = nil, **options)
|
||||
@following_map = if current_account_id.nil?
|
||||
{}
|
||||
else
|
||||
TagFollow.select(:tag_id).where(tag_id: tags.map(&:id), account_id: current_account_id).each_with_object({}) { |f, h| h[f.tag_id] = true }.merge(options[:following_map] || {})
|
||||
end
|
||||
if current_account_id.nil?
|
||||
@following_map = {}
|
||||
@featuring_map = {}
|
||||
else
|
||||
@following_map = TagFollow.select(:tag_id).where(tag_id: tags.map(&:id), account_id: current_account_id).each_with_object({}) { |f, h| h[f.tag_id] = true }.merge(options[:following_map] || {})
|
||||
@featuring_map = FeaturedTag.select(:tag_id).where(tag_id: tags.map(&:id), account_id: current_account_id).each_with_object({}) { |f, h| h[f.tag_id] = true }.merge(options[:featuring_map] || {})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -6,6 +6,7 @@ class REST::TagSerializer < ActiveModel::Serializer
|
||||
attributes :id, :name, :url, :history
|
||||
|
||||
attribute :following, if: :current_user?
|
||||
attribute :featuring, if: :current_user?
|
||||
|
||||
def id
|
||||
object.id.to_s
|
||||
@@ -27,6 +28,14 @@ class REST::TagSerializer < ActiveModel::Serializer
|
||||
end
|
||||
end
|
||||
|
||||
def featuring
|
||||
if instance_options && instance_options[:relationships]
|
||||
instance_options[:relationships].featuring_map[object.id] || false
|
||||
else
|
||||
FeaturedTag.exists?(tag_id: object.id, account_id: current_user.account_id)
|
||||
end
|
||||
end
|
||||
|
||||
def current_user?
|
||||
!current_user.nil?
|
||||
end
|
||||
|
||||
@@ -3,18 +3,24 @@
|
||||
class CreateFeaturedTagService < BaseService
|
||||
include Payloadable
|
||||
|
||||
def call(account, name, force: true)
|
||||
def call(account, name_or_tag, raise_error: true)
|
||||
raise ArgumentError unless account.local?
|
||||
|
||||
@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, 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)
|
||||
@featured_tag = begin
|
||||
if name_or_tag.is_a?(Tag)
|
||||
account.featured_tags.find_or_initialize_by(tag: name_or_tag)
|
||||
else
|
||||
account.featured_tags.find_or_initialize_by(name: name_or_tag)
|
||||
end
|
||||
end
|
||||
|
||||
create_method = raise_error ? :save! : :save
|
||||
|
||||
ActivityPub::AccountRawDistributionWorker.perform_async(build_json(@featured_tag), @account.id) if @featured_tag.new_record? && @featured_tag.public_send(create_method)
|
||||
|
||||
@featured_tag
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -3,11 +3,24 @@
|
||||
class RemoveFeaturedTagService < BaseService
|
||||
include Payloadable
|
||||
|
||||
def call(account, featured_tag)
|
||||
def call(account, featured_tag_or_tag)
|
||||
raise ArgumentError unless account.local?
|
||||
|
||||
@account = account
|
||||
|
||||
featured_tag.destroy!
|
||||
ActivityPub::AccountRawDistributionWorker.perform_async(build_json(featured_tag), account.id) if @account.local?
|
||||
@featured_tag = begin
|
||||
if featured_tag_or_tag.is_a?(FeaturedTag)
|
||||
featured_tag_or_tag
|
||||
elsif featured_tag_or_tag.is_a?(Tag)
|
||||
FeaturedTag.find_by(account: account, tag: featured_tag_or_tag)
|
||||
end
|
||||
end
|
||||
|
||||
return if @featured_tag.nil?
|
||||
|
||||
@featured_tag.destroy!
|
||||
|
||||
ActivityPub::AccountRawDistributionWorker.perform_async(build_json(@featured_tag), account.id) if @account.local?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
Reference in New Issue
Block a user