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.

75 lines
1.9 KiB

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