You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.6 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::Timelines::TagController < Api::BaseController
  3. before_action :load_tag
  4. after_action :insert_pagination_headers, unless: -> { @statuses.empty? }
  5. def show
  6. @statuses = load_statuses
  7. render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
  8. end
  9. private
  10. def load_tag
  11. @tag = Tag.find_normalized(params[:id])
  12. end
  13. def load_statuses
  14. cached_tagged_statuses
  15. end
  16. def cached_tagged_statuses
  17. cache_collection tagged_statuses, Status
  18. end
  19. def tagged_statuses
  20. if @tag.nil?
  21. []
  22. else
  23. statuses = tag_timeline_statuses.paginate_by_id(
  24. limit_param(DEFAULT_STATUSES_LIMIT),
  25. params_slice(:max_id, :since_id, :min_id)
  26. )
  27. if truthy_param?(:only_media)
  28. statuses.joins(:media_attachments)
  29. else
  30. statuses
  31. end
  32. end
  33. end
  34. def tag_timeline_statuses
  35. HashtagQueryService.new.call(@tag, params.slice(:any, :all, :none), current_account, truthy_param?(:local))
  36. end
  37. def insert_pagination_headers
  38. set_pagination_headers(next_path, prev_path)
  39. end
  40. def pagination_params(core_params)
  41. params.slice(:local, :limit, :only_media).permit(:local, :limit, :only_media).merge(core_params)
  42. end
  43. def next_path
  44. api_v1_timelines_tag_url params[:id], pagination_params(max_id: pagination_max_id)
  45. end
  46. def prev_path
  47. api_v1_timelines_tag_url params[:id], pagination_params(min_id: pagination_since_id)
  48. end
  49. def pagination_max_id
  50. @statuses.last.id
  51. end
  52. def pagination_since_id
  53. @statuses.first.id
  54. end
  55. end