public_controller.rb 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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_id(
  18. limit_param(DEFAULT_STATUSES_LIMIT),
  19. params_slice(:max_id, :since_id, :min_id)
  20. )
  21. if truthy_param?(:only_media)
  22. # `SELECT DISTINCT id, updated_at` is too slow, so pluck ids at first, and then select id, updated_at with ids.
  23. status_ids = statuses.joins(:media_attachments).distinct(:id).pluck(:id)
  24. statuses.where(id: status_ids)
  25. else
  26. statuses
  27. end
  28. end
  29. def public_timeline_statuses
  30. Status.as_public_timeline(current_account, truthy_param?(:local))
  31. end
  32. def insert_pagination_headers
  33. set_pagination_headers(next_path, prev_path)
  34. end
  35. def pagination_params(core_params)
  36. params.slice(:local, :limit, :only_media).permit(:local, :limit, :only_media).merge(core_params)
  37. end
  38. def next_path
  39. api_v1_timelines_public_url pagination_params(max_id: pagination_max_id)
  40. end
  41. def prev_path
  42. api_v1_timelines_public_url pagination_params(min_id: pagination_since_id)
  43. end
  44. def pagination_max_id
  45. @statuses.last.id
  46. end
  47. def pagination_since_id
  48. @statuses.first.id
  49. end
  50. end