<commit_before># frozen_string_literal: true

class Api::V1::TagsController < Api::BaseController
  before_action -> { doorkeeper_authorize! :follow, :write, :'write:follows' }, except: :show
  before_action :require_user!, except: :show
  before_action :set_or_create_tag

  override_rate_limit_headers :follow, family: :follows

  def show
    render json: @tag, serializer: REST::TagSerializer
  end

  def follow
    TagFollow.create!(tag: @tag, account: current_account, rate_limit: true)
    render json: @tag, serializer: REST::TagSerializer
  end

  def unfollow
    TagFollow.find_by(account: current_account, tag: @tag)&.destroy!
    render json: @tag, serializer: REST::TagSerializer
  end

  private

  def set_or_create_tag
    return not_found unless Tag::HASHTAG_NAME_RE.match?(params[:id])<commit_msg>Make tag following idempotent (#20860)<commit_after># frozen_string_literal: true

class Api::V1::TagsController < Api::BaseController
  before_action -> { doorkeeper_authorize! :follow, :write, :'write:follows' }, except: :show
  before_action :require_user!, except: :show
  before_action :set_or_create_tag

  override_rate_limit_headers :follow, family: :follows

  def show
    render json: @tag, serializer: REST::TagSerializer
  end

  def follow
    TagFollow.first_or_create!(tag: @tag, account: current_account, rate_limit: true)
    render json: @tag, serializer: REST::TagSerializer
  end

  def unfollow
    TagFollow.find_by(account: current_account, tag: @tag)&.destroy!
    render json: @tag, serializer: REST::TagSerializer
  end

  private

  def set_or_create_tag
    return not_found unless Tag::HASHTAG_NAME_RE.match?(params[:id])
{'fraction_non_alphanumeric': 0.10371702637889688, 'fraction_numerical': 0.004196642685851319, 'mean_word_length': 5.444015444015444, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5818795', 'n_tokens_mistral': 551, 'n_tokens_neox': 530, 'n_words': 131}