tags_controller.rb 928 B

123456789101112131415161718192021222324252627282930
  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. render json: @tag, serializer: REST::TagSerializer
  9. end
  10. def follow
  11. TagFollow.create!(tag: @tag, account: current_account, rate_limit: true)
  12. render json: @tag, serializer: REST::TagSerializer
  13. end
  14. def unfollow
  15. TagFollow.find_by(account: current_account, tag: @tag)&.destroy!
  16. render json: @tag, serializer: REST::TagSerializer
  17. end
  18. private
  19. def set_or_create_tag
  20. return not_found unless /\A(#{Tag::HASHTAG_NAME_RE})\z/.match?(params[:id])
  21. @tag = Tag.find_normalized(params[:id]) || Tag.new(name: Tag.normalize(params[:id]), display_name: params[:id])
  22. end
  23. end