|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +class ActivityPub::ContextsController < ActivityPub::BaseController |
| 4 | + vary_by -> { 'Signature' if authorized_fetch_mode? } |
| 5 | + |
| 6 | + before_action :require_account_signature!, if: :authorized_fetch_mode? |
| 7 | + before_action :set_conversation |
| 8 | + before_action :set_items |
| 9 | + |
| 10 | + DESCENDANTS_LIMIT = 60 |
| 11 | + |
| 12 | + def show |
| 13 | + expires_in 3.minutes, public: public_fetch_mode? |
| 14 | + render_with_cache json: context_presenter, serializer: ActivityPub::ContextSerializer, adapter: ActivityPub::Adapter, content_type: 'application/activity+json' |
| 15 | + end |
| 16 | + |
| 17 | + def items |
| 18 | + expires_in 3.minutes, public: public_fetch_mode? |
| 19 | + render_with_cache json: items_collection_presenter, serializer: ActivityPub::CollectionSerializer, adapter: ActivityPub::Adapter, content_type: 'application/activity+json' |
| 20 | + end |
| 21 | + |
| 22 | + private |
| 23 | + |
| 24 | + def account_required? |
| 25 | + false |
| 26 | + end |
| 27 | + |
| 28 | + def set_conversation |
| 29 | + @conversation = Conversation.local.find(params[:id]) |
| 30 | + end |
| 31 | + |
| 32 | + def set_items |
| 33 | + @items = @conversation.statuses.distributable_visibility.paginate_by_min_id(DESCENDANTS_LIMIT, params[:min_id]) |
| 34 | + end |
| 35 | + |
| 36 | + def context_presenter |
| 37 | + first_page = ActivityPub::CollectionPresenter.new( |
| 38 | + id: items_context_url(@conversation, page_params), |
| 39 | + type: :unordered, |
| 40 | + part_of: items_context_url(@conversation), |
| 41 | + next: next_page, |
| 42 | + items: @items.map { |status| status.local? ? ActivityPub::TagManager.instance.uri_for(status) : status.uri } |
| 43 | + ) |
| 44 | + |
| 45 | + ActivityPub::ContextPresenter.from_conversation(@conversation).tap do |presenter| |
| 46 | + presenter.first = first_page |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + def items_collection_presenter |
| 51 | + page = ActivityPub::CollectionPresenter.new( |
| 52 | + id: items_context_url(@conversation, page_params), |
| 53 | + type: :unordered, |
| 54 | + part_of: items_context_url(@conversation), |
| 55 | + next: next_page, |
| 56 | + items: @items.map { |status| status.local? ? ActivityPub::TagManager.instance.uri_for(status) : status.uri } |
| 57 | + ) |
| 58 | + |
| 59 | + return page if page_requested? |
| 60 | + |
| 61 | + ActivityPub::CollectionPresenter.new( |
| 62 | + id: items_context_url(@conversation), |
| 63 | + type: :unordered, |
| 64 | + first: page |
| 65 | + ) |
| 66 | + end |
| 67 | + |
| 68 | + def page_requested? |
| 69 | + truthy_param?(:page) |
| 70 | + end |
| 71 | + |
| 72 | + def next_page |
| 73 | + return nil if @items.size < DESCENDANTS_LIMIT |
| 74 | + |
| 75 | + items_context_url(@conversation, page: true, min_id: @items.last.id) |
| 76 | + end |
| 77 | + |
| 78 | + def page_params |
| 79 | + params.permit(:page, :min_id) |
| 80 | + end |
| 81 | +end |
0 commit comments