followed_tags_controller.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # frozen_string_literal: true
  2. class Api::V1::FollowedTagsController < Api::BaseController
  3. TAGS_LIMIT = 100
  4. before_action -> { doorkeeper_authorize! :follow, :read, :'read:follows' }, except: :show
  5. before_action :require_user!
  6. before_action :set_results
  7. after_action :insert_pagination_headers, only: :show
  8. def index
  9. render json: @results.map(&:tag), each_serializer: REST::TagSerializer, relationships: TagRelationshipsPresenter.new(@results.map(&:tag), current_user&.account_id)
  10. end
  11. private
  12. def set_results
  13. @results = TagFollow.where(account: current_account).joins(:tag).eager_load(:tag).to_a_paginated_by_id(
  14. limit_param(TAGS_LIMIT),
  15. params_slice(:max_id, :since_id, :min_id)
  16. )
  17. end
  18. def insert_pagination_headers
  19. set_pagination_headers(next_path, prev_path)
  20. end
  21. def next_path
  22. api_v1_followed_tags_url pagination_params(max_id: pagination_max_id) if records_continue?
  23. end
  24. def prev_path
  25. api_v1_followed_tags_url pagination_params(since_id: pagination_since_id) unless @results.empty?
  26. end
  27. def pagination_max_id
  28. @results.last.id
  29. end
  30. def pagination_since_id
  31. @results.first.id
  32. end
  33. def records_continue?
  34. @results.size == limit_param(TAG_LIMIT)
  35. end
  36. def pagination_params(core_params)
  37. params.slice(:limit).permit(:limit).merge(core_params)
  38. end
  39. end