about summary refs log tree commit diff
path: root/app/controllers/activitypub/outboxes_controller.rb
blob: 1da8b5913fef1956b7647dc90314d86f6b3964d0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# frozen_string_literal: true

class ActivityPub::OutboxesController < Api::BaseController
  LIMIT = 20

  include SignatureVerification

  before_action :set_account
  before_action :set_statuses
  before_action :set_cache_headers

  def show
    unless page_requested?
      skip_session!
      expires_in 1.minute, public: true
    end

    render json: outbox_presenter, serializer: ActivityPub::OutboxSerializer, adapter: ActivityPub::Adapter, content_type: 'application/activity+json'
  end

  private

  def set_account
    @account = Account.find_local!(params[:account_username])
  end

  def outbox_presenter
    if page_requested?
      ActivityPub::CollectionPresenter.new(
        id: account_outbox_url(@account, page_params),
        type: :ordered,
        part_of: account_outbox_url(@account),
        prev: prev_page,
        next: next_page,
        items: @statuses
      )
    else
      ActivityPub::CollectionPresenter.new(
        id: account_outbox_url(@account),
        type: :ordered,
        size: @account.statuses_count,
        first: account_outbox_url(@account, page: true),
        last: account_outbox_url(@account, page: true, min_id: 0)
      )
    end
  end

  def next_page
    account_outbox_url(@account, page: true, max_id: @statuses.last.id) if @statuses.size == LIMIT
  end

  def prev_page
    account_outbox_url(@account, page: true, min_id: @statuses.first.id) unless @statuses.empty?
  end

  def set_statuses
    return unless page_requested?
    account_owner = current_account && current_account.id == @account.id
    outbox_hidden = @account&.user && @account.user.hides_public_outbox?
    local_follower = current_account && current_account.following?(@account)

    if account_owner || !@account.hidden? || (outbox_hidden && local_follower)
      @statuses = @account.statuses.permitted_for(@account, signed_request_account)
    else
      @statuses = Status.none
    end
    @statuses = params[:min_id].present? ? @statuses.paginate_by_min_id(LIMIT, params[:min_id]).reverse : @statuses.paginate_by_max_id(LIMIT, params[:max_id])
    @statuses = cache_collection(@statuses, Status)
  end

  def page_requested?
    params[:page] == 'true'
  end

  def page_params
    { page: true, max_id: params[:max_id], min_id: params[:min_id] }.compact
  end
end