ActivityPub: Add basic, read-only support for Outboxes, Notes, and Create/Announce Activities (#2197)
* Clean up collapsible components * Expose user Outboxes and AS2 representations of statuses * Save work thus far. * Fix bad merge. * Save my work * Clean up pagination. * First test working. * Add tests. * Add Forbidden error template. * Revert yarn.lock changes. * Fix code style deviations and use localized instead of hardcoded English text.
This commit is contained in:
@ -15,7 +15,9 @@ class AccountsController < ApplicationController
|
||||
render xml: AtomSerializer.render(AtomSerializer.new.feed(@account, @entries.to_a))
|
||||
end
|
||||
|
||||
format.activitystreams2
|
||||
format.activitystreams2 do
|
||||
headers['Access-Control-Allow-Origin'] = '*'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
27
app/controllers/api/activitypub/activities_controller.rb
Normal file
27
app/controllers/api/activitypub/activities_controller.rb
Normal file
@ -0,0 +1,27 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Api::Activitypub::ActivitiesController < ApiController
|
||||
# before_action :set_follow, only: [:show_follow]
|
||||
before_action :set_status, only: [:show_status]
|
||||
|
||||
respond_to :activitystreams2
|
||||
|
||||
# Show a status in AS2 format, as either an Announce (reblog) or a Create (post) activity.
|
||||
def show_status
|
||||
headers['Access-Control-Allow-Origin'] = '*'
|
||||
|
||||
return forbidden unless @status.permitted?
|
||||
|
||||
if @status.reblog?
|
||||
render :show_status_announce
|
||||
else
|
||||
render :show_status_create
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_status
|
||||
@status = Status.find(params[:id])
|
||||
end
|
||||
end
|
19
app/controllers/api/activitypub/notes_controller.rb
Normal file
19
app/controllers/api/activitypub/notes_controller.rb
Normal file
@ -0,0 +1,19 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Api::Activitypub::NotesController < ApiController
|
||||
before_action :set_status
|
||||
|
||||
respond_to :activitystreams2
|
||||
|
||||
def show
|
||||
headers['Access-Control-Allow-Origin'] = '*'
|
||||
|
||||
forbidden unless @status.permitted?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_status
|
||||
@status = Status.find(params[:id])
|
||||
end
|
||||
end
|
41
app/controllers/api/activitypub/outbox_controller.rb
Normal file
41
app/controllers/api/activitypub/outbox_controller.rb
Normal file
@ -0,0 +1,41 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Api::Activitypub::OutboxController < ApiController
|
||||
before_action :set_account
|
||||
|
||||
respond_to :activitystreams2
|
||||
|
||||
def show
|
||||
headers['Access-Control-Allow-Origin'] = '*'
|
||||
|
||||
@statuses = Status.as_outbox_timeline(@account).paginate_by_max_id(limit_param(DEFAULT_STATUSES_LIMIT), params[:max_id], params[:since_id])
|
||||
@statuses = cache_collection(@statuses)
|
||||
|
||||
set_maps(@statuses)
|
||||
|
||||
# Since the statuses are in reverse chronological order, last is the lowest ID.
|
||||
@next_path = api_activitypub_outbox_url(max_id: @statuses.last.id) if @statuses.size == limit_param(DEFAULT_STATUSES_LIMIT)
|
||||
|
||||
unless @statuses.empty?
|
||||
if @statuses.first.id == 1
|
||||
@prev_path = api_activitypub_outbox_url
|
||||
elsif params[:max_id]
|
||||
@prev_path = api_activitypub_outbox_url(since_id: @statuses.first.id)
|
||||
end
|
||||
end
|
||||
|
||||
@paginated = @next_path || @prev_path
|
||||
|
||||
set_pagination_headers(@next_path, @prev_path)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def cache_collection(raw)
|
||||
super(raw, Status)
|
||||
end
|
||||
|
||||
def set_account
|
||||
@account = Account.find(params[:id])
|
||||
end
|
||||
end
|
@ -62,6 +62,13 @@ class ApplicationController < ActionController::Base
|
||||
end
|
||||
end
|
||||
|
||||
def forbidden
|
||||
respond_to do |format|
|
||||
format.any { head 403 }
|
||||
format.html { render 'errors/403', layout: 'error', status: 403 }
|
||||
end
|
||||
end
|
||||
|
||||
def unprocessable_entity
|
||||
respond_to do |format|
|
||||
format.any { head 422 }
|
||||
|
8
app/helpers/activitystreams2_builder_helper.rb
Normal file
8
app/helpers/activitystreams2_builder_helper.rb
Normal file
@ -0,0 +1,8 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Activitystreams2BuilderHelper
|
||||
# Gets a usable name for an account, using display name or username.
|
||||
def account_name(account)
|
||||
account.display_name.empty? ? account.username : account.display_name
|
||||
end
|
||||
end
|
@ -140,6 +140,10 @@ class Status < ApplicationRecord
|
||||
account.nil? ? filter_timeline_default(query) : filter_timeline_default(filter_timeline(query, account))
|
||||
end
|
||||
|
||||
def as_outbox_timeline(account)
|
||||
where(account: account, visibility: :public)
|
||||
end
|
||||
|
||||
def favourites_map(status_ids, account_id)
|
||||
Favourite.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |f| [f.status_id, true] }.to_h
|
||||
end
|
||||
|
@ -6,3 +6,4 @@ attributes display_name: :name, username: :preferredUsername, note: :summary
|
||||
|
||||
node(:icon) { |account| full_asset_url(account.avatar.url(:original)) }
|
||||
node(:image) { |account| full_asset_url(account.header.url(:original)) }
|
||||
node(:outbox) { |account| api_activitypub_outbox_url(account.id) }
|
||||
|
@ -0,0 +1,3 @@
|
||||
extends 'activitypub/intransient.activitystreams2.rabl'
|
||||
|
||||
node(:type) { 'Announce' }
|
@ -0,0 +1,5 @@
|
||||
extends 'activitypub/intransient.activitystreams2.rabl'
|
||||
|
||||
node(:type) { 'Collection' }
|
||||
node(:items) { [] }
|
||||
node(:totalItems) { 0 }
|
3
app/views/activitypub/types/create.activitystreams2.rabl
Normal file
3
app/views/activitypub/types/create.activitystreams2.rabl
Normal file
@ -0,0 +1,3 @@
|
||||
extends 'activitypub/intransient.activitystreams2.rabl'
|
||||
|
||||
node(:type) { 'Create' }
|
3
app/views/activitypub/types/note.activitystreams2.rabl
Normal file
3
app/views/activitypub/types/note.activitystreams2.rabl
Normal file
@ -0,0 +1,3 @@
|
||||
extends 'activitypub/intransient.activitystreams2.rabl'
|
||||
|
||||
node(:type) { 'Note' }
|
@ -0,0 +1,3 @@
|
||||
extends 'activitypub/types/collection.activitystreams2.rabl'
|
||||
|
||||
node(:type) { 'OrderedCollection' }
|
@ -0,0 +1,4 @@
|
||||
extends 'activitypub/types/ordered_collection.activitystreams2.rabl'
|
||||
|
||||
node(:type) { 'OrderedCollectionPage' }
|
||||
node(:current) { request.original_url }
|
@ -0,0 +1,4 @@
|
||||
object @status
|
||||
|
||||
node(:actor) { |status| TagManager.instance.url_for(status.account) }
|
||||
node(:published) { |status| status.created_at.to_time.xmlschema }
|
@ -0,0 +1,8 @@
|
||||
extends 'activitypub/types/announce.activitystreams2.rabl'
|
||||
extends 'api/activitypub/activities/_show_status.activitystreams2.rabl'
|
||||
|
||||
object @status
|
||||
|
||||
node(:name) { |status| t('activitypub.activity.announce.name', account_name: account_name(status.account)) }
|
||||
node(:url) { |status| TagManager.instance.url_for(status) }
|
||||
node(:object) { |status| api_activitypub_status_url(status.reblog_of_id) }
|
@ -0,0 +1,8 @@
|
||||
extends 'activitypub/types/create.activitystreams2.rabl'
|
||||
extends 'api/activitypub/activities/_show_status.activitystreams2.rabl'
|
||||
|
||||
object @status
|
||||
|
||||
node(:name) { |status| t('activitypub.activity.create.name', account_name: account_name(status.account)) }
|
||||
node(:url) { |status| TagManager.instance.url_for(status) }
|
||||
node(:object) { |status| api_activitypub_note_url(status) }
|
11
app/views/api/activitypub/notes/show.activitystreams2.rabl
Normal file
11
app/views/api/activitypub/notes/show.activitystreams2.rabl
Normal file
@ -0,0 +1,11 @@
|
||||
extends 'activitypub/types/note.activitystreams2.rabl'
|
||||
|
||||
object @status
|
||||
|
||||
attributes :content
|
||||
|
||||
node(:name) { |status| status.content }
|
||||
node(:url) { |status| TagManager.instance.url_for(status) }
|
||||
node(:attributedTo) { |status| TagManager.instance.url_for(status.account) }
|
||||
node(:inReplyTo) { |status| api_activitypub_note_url(status.thread) } if @status.thread
|
||||
node(:published) { |status| status.created_at.to_time.xmlschema }
|
23
app/views/api/activitypub/outbox/show.activitystreams2.rabl
Normal file
23
app/views/api/activitypub/outbox/show.activitystreams2.rabl
Normal file
@ -0,0 +1,23 @@
|
||||
if @paginated
|
||||
extends 'activitypub/types/ordered_collection_page.activitystreams2.rabl'
|
||||
else
|
||||
extends 'activitypub/types/ordered_collection.activitystreams2.rabl'
|
||||
end
|
||||
|
||||
object @account
|
||||
|
||||
node(:items) do
|
||||
@statuses.map { |status| api_activitypub_status_url(status) }
|
||||
end
|
||||
|
||||
node(:totalItems) { @statuses.count }
|
||||
node(:next) { @next_path } if @next_path
|
||||
node(:prev) { @prev_path } if @prev_path
|
||||
|
||||
node(:name) { |account| t('activitypub.outbox.name', account_name: account_name(account)) }
|
||||
node(:summary) { |account| t('activitypub.outbox.summary', account_name: account_name(account)) }
|
||||
node(:updated) do |account|
|
||||
times = @statuses.map { |status| status.updated_at.to_time }
|
||||
times << account.created_at.to_time
|
||||
times.max.xmlschema
|
||||
end
|
5
app/views/errors/403.html.haml
Normal file
5
app/views/errors/403.html.haml
Normal file
@ -0,0 +1,5 @@
|
||||
- content_for :page_title do
|
||||
= t('errors.403')
|
||||
|
||||
- content_for :content do
|
||||
= t('errors.403')
|
Reference in New Issue
Block a user