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.

66 lines
1.6 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::Timelines::PublicController < Api::BaseController
  3. after_action :insert_pagination_headers, unless: -> { @statuses.empty? }
  4. respond_to :json
  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_statuses
  11. cached_public_statuses
  12. end
  13. def cached_public_statuses
  14. cache_collection public_statuses, Status
  15. end
  16. def public_statuses
  17. statuses = public_timeline_statuses.paginate_by_max_id(
  18. limit_param(DEFAULT_STATUSES_LIMIT),
  19. params[:max_id],
  20. params[:since_id]
  21. )
  22. if truthy_param?(:only_media)
  23. # `SELECT DISTINCT id, updated_at` is too slow, so pluck ids at first, and then select id, updated_at with ids.
  24. status_ids = statuses.joins(:media_attachments).distinct(:id).pluck(:id)
  25. statuses.where(id: status_ids)
  26. else
  27. statuses
  28. end
  29. end
  30. def public_timeline_statuses
  31. Status.as_public_timeline(current_account, truthy_param?(:local))
  32. end
  33. def insert_pagination_headers
  34. set_pagination_headers(next_path, prev_path)
  35. end
  36. def pagination_params(core_params)
  37. params.permit(:local, :limit, :only_media).merge(core_params)
  38. end
  39. def next_path
  40. api_v1_timelines_public_url pagination_params(max_id: pagination_max_id)
  41. end
  42. def prev_path
  43. api_v1_timelines_public_url pagination_params(since_id: pagination_since_id)
  44. end
  45. def pagination_max_id
  46. @statuses.last.id
  47. end
  48. def pagination_since_id
  49. @statuses.first.id
  50. end
  51. end