outboxes_controller.rb 2.1 KB

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