闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.8 KiB

  1. # frozen_string_literal: true
  2. class ActivityPub::OutboxesController < Api::BaseController
  3. LIMIT = 20
  4. include SignatureVerification
  5. before_action :set_account
  6. before_action :set_statuses
  7. def show
  8. render json: outbox_presenter, serializer: ActivityPub::OutboxSerializer, adapter: ActivityPub::Adapter, content_type: 'application/activity+json'
  9. end
  10. private
  11. def set_account
  12. @account = Account.find_local!(params[:account_username])
  13. end
  14. def outbox_presenter
  15. if page_requested?
  16. ActivityPub::CollectionPresenter.new(
  17. id: account_outbox_url(@account, page_params),
  18. type: :ordered,
  19. part_of: account_outbox_url(@account),
  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: account_outbox_url(@account, page: true),
  30. last: account_outbox_url(@account, page: true, min_id: 0)
  31. )
  32. end
  33. end
  34. def next_page
  35. account_outbox_url(@account, page: true, max_id: @statuses.last.id) if @statuses.size == LIMIT
  36. end
  37. def prev_page
  38. account_outbox_url(@account, page: true, min_id: @statuses.first.id) unless @statuses.empty?
  39. end
  40. def set_statuses
  41. return unless page_requested?
  42. @statuses = @account.statuses.permitted_for(@account, signed_request_account)
  43. @statuses = params[:min_id].present? ? @statuses.paginate_by_min_id(LIMIT, params[:min_id]).reverse : @statuses.paginate_by_max_id(LIMIT, params[:max_id])
  44. @statuses = cache_collection(@statuses, Status)
  45. end
  46. def page_requested?
  47. params[:page] == 'true'
  48. end
  49. def page_params
  50. { page: true, max_id: params[:max_id], min_id: params[:min_id] }.compact
  51. end
  52. end