outboxes_controller.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # frozen_string_literal: true
  2. class ActivityPub::OutboxesController < ActivityPub::BaseController
  3. LIMIT = 20
  4. include SignatureVerification
  5. include AccountOwnedConcern
  6. before_action :require_account_signature!, if: :authorized_fetch_mode?
  7. before_action :set_statuses
  8. before_action :set_cache_headers
  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. def set_cache_headers
  70. response.headers['Vary'] = 'Signature' if authorized_fetch_mode? || page_requested?
  71. end
  72. end