outboxes_controller.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # frozen_string_literal: true
  2. class ActivityPub::OutboxesController < ActivityPub::BaseController
  3. LIMIT = 20
  4. include SignatureVerification
  5. include AccountOwnedConcern
  6. before_action :require_signature!, if: :authorized_fetch_mode?
  7. before_action :set_statuses
  8. before_action :set_cache_headers
  9. def show
  10. expires_in(page_requested? ? 0 : 3.minutes, public: public_fetch_mode? && !(signed_request_account.present? && page_requested?))
  11. render json: outbox_presenter, serializer: ActivityPub::OutboxSerializer, adapter: ActivityPub::Adapter, content_type: 'application/activity+json'
  12. end
  13. private
  14. def outbox_presenter
  15. if page_requested?
  16. ActivityPub::CollectionPresenter.new(
  17. id: outbox_url(page_params),
  18. type: :ordered,
  19. part_of: outbox_url,
  20. prev: prev_page,
  21. next: next_page,
  22. items: @statuses
  23. )
  24. else
  25. ActivityPub::CollectionPresenter.new(
  26. id: account_outbox_url(@account),
  27. type: :ordered,
  28. size: @account.statuses_count,
  29. first: outbox_url(page: true),
  30. last: outbox_url(page: true, min_id: 0)
  31. )
  32. end
  33. end
  34. def outbox_url(**kwargs)
  35. if params[:account_username].present?
  36. account_outbox_url(@account, **kwargs)
  37. else
  38. instance_actor_outbox_url(**kwargs)
  39. end
  40. end
  41. def next_page
  42. account_outbox_url(@account, page: true, max_id: @statuses.last.id) if @statuses.size == LIMIT
  43. end
  44. def prev_page
  45. account_outbox_url(@account, page: true, min_id: @statuses.first.id) unless @statuses.empty?
  46. end
  47. def set_statuses
  48. return unless page_requested?
  49. @statuses = cache_collection_paginated_by_id(
  50. @account.statuses.permitted_for(@account, signed_request_account),
  51. Status,
  52. LIMIT,
  53. params_slice(:max_id, :min_id, :since_id)
  54. )
  55. end
  56. def page_requested?
  57. truthy_param?(:page)
  58. end
  59. def page_params
  60. { page: true, max_id: params[:max_id], min_id: params[:min_id] }.compact
  61. end
  62. def set_account
  63. @account = params[:account_username].present? ? Account.find_local!(username_param) : Account.representative
  64. end
  65. end