outboxes_controller.rb 2.1 KB

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