tags_controller.rb 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. # frozen_string_literal: true
  2. class Api::V1::TagsController < Api::BaseController
  3. before_action -> { doorkeeper_authorize! :follow, :write, :'write:follows' }, except: :show
  4. before_action :require_user!, except: :show
  5. before_action :set_or_create_tag
  6. override_rate_limit_headers :follow, family: :follows
  7. def show
  8. cache_if_unauthenticated!
  9. render json: @tag, serializer: REST::TagSerializer
  10. end
  11. def follow
  12. TagFollow.create_with(rate_limit: true).find_or_create_by!(tag: @tag, account: current_account)
  13. render json: @tag, serializer: REST::TagSerializer
  14. end
  15. def unfollow
  16. TagFollow.find_by(account: current_account, tag: @tag)&.destroy!
  17. TagUnmergeWorker.perform_async(@tag.id, current_account.id)
  18. render json: @tag, serializer: REST::TagSerializer
  19. end
  20. private
  21. def set_or_create_tag
  22. return not_found unless Tag::HASHTAG_NAME_RE.match?(params[:id])
  23. @tag = Tag.find_normalized(params[:id]) || Tag.new(name: Tag.normalize(params[:id]), display_name: params[:id])
  24. end
  25. end