about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
authorEvan Minto <evan.minto@gmail.com>2017-04-25 06:06:06 -0700
committerEugen Rochko <eugen@zeonfederated.com>2017-04-25 15:06:06 +0200
commit122d59ac41a0c637b19357c2b7422002ffa0381c (patch)
tree150a8793fcc7d51793a24b2b1df900025799dc73 /app
parent8b5179d006a07cf759e751e9d883bfe472cee868 (diff)
Change ActivityPub paging to match spec. Clean up ActivityPub outbox changes. (#2410)
* Change ActivityPub paging to match spec. Clean up ActivityPub outbox changes.

* Fix code style and test failures for OutboxController.

* Attempt to fix CI errors.
Diffstat (limited to 'app')
-rw-r--r--app/controllers/accounts_controller.rb4
-rw-r--r--app/controllers/api/activitypub/activities_controller.rb2
-rw-r--r--app/controllers/api/activitypub/notes_controller.rb2
-rw-r--r--app/controllers/api/activitypub/outbox_controller.rb58
-rw-r--r--app/helpers/activitystreams2_builder_helper.rb2
-rw-r--r--app/views/activitypub/types/collection.activitystreams2.rabl2
-rw-r--r--app/views/activitypub/types/ordered_collection_page.activitystreams2.rabl1
-rw-r--r--app/views/api/activitypub/outbox/show.activitystreams2.rabl21
-rw-r--r--app/views/api/activitypub/outbox/show_page.activitystreams2.rabl16
9 files changed, 66 insertions, 42 deletions
diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb
index d79ed142a..8eda96336 100644
--- a/app/controllers/accounts_controller.rb
+++ b/app/controllers/accounts_controller.rb
@@ -15,9 +15,7 @@ class AccountsController < ApplicationController
         render xml: AtomSerializer.render(AtomSerializer.new.feed(@account, @entries.to_a))
       end
 
-      format.activitystreams2 do
-        headers['Access-Control-Allow-Origin'] = '*'
-      end
+      format.activitystreams2
     end
   end
 
diff --git a/app/controllers/api/activitypub/activities_controller.rb b/app/controllers/api/activitypub/activities_controller.rb
index 03f27c7f6..ba03738fc 100644
--- a/app/controllers/api/activitypub/activities_controller.rb
+++ b/app/controllers/api/activitypub/activities_controller.rb
@@ -8,8 +8,6 @@ class Api::Activitypub::ActivitiesController < ApiController
 
   # 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?
diff --git a/app/controllers/api/activitypub/notes_controller.rb b/app/controllers/api/activitypub/notes_controller.rb
index 722961ec6..6489243dc 100644
--- a/app/controllers/api/activitypub/notes_controller.rb
+++ b/app/controllers/api/activitypub/notes_controller.rb
@@ -6,8 +6,6 @@ class Api::Activitypub::NotesController < ApiController
   respond_to :activitystreams2
 
   def show
-    headers['Access-Control-Allow-Origin'] = '*'
-
     forbidden unless @status.permitted?
   end
 
diff --git a/app/controllers/api/activitypub/outbox_controller.rb b/app/controllers/api/activitypub/outbox_controller.rb
index 05d779910..7b6cbdd38 100644
--- a/app/controllers/api/activitypub/outbox_controller.rb
+++ b/app/controllers/api/activitypub/outbox_controller.rb
@@ -6,30 +6,47 @@ class Api::Activitypub::OutboxController < ApiController
   respond_to :activitystreams2
 
   def show
-    headers['Access-Control-Allow-Origin'] = '*'
+    if params[:max_id] || params[:since_id]
+      show_outbox_page
+    else
+      show_base_outbox
+    end
+  end
+
+  private
 
-    @statuses = Status.as_outbox_timeline(@account).paginate_by_max_id(limit_param(DEFAULT_STATUSES_LIMIT), params[:max_id], params[:since_id])
+  def show_base_outbox
+    @statuses = Status.as_outbox_timeline(@account)
     @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)
+    set_first_last_page(@statuses)
 
-    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
+    render :show
+  end
 
-    @paginated = @next_path || @prev_path
+  def show_outbox_page
+    all_statuses = Status.as_outbox_timeline(@account)
+    @statuses = all_statuses.paginate_by_max_id(limit_param(DEFAULT_STATUSES_LIMIT), params[:max_id], params[:since_id])
 
-    set_pagination_headers(@next_path, @prev_path)
-  end
+    all_statuses = cache_collection(all_statuses)
+    @statuses = cache_collection(@statuses)
 
-  private
+    set_maps(@statuses)
+
+    set_first_last_page(all_statuses)
+
+    @next_page_url = api_activitypub_outbox_url(pagination_params(max_id: @statuses.last.id))    unless @statuses.empty?
+    @prev_page_url = api_activitypub_outbox_url(pagination_params(since_id: @statuses.first.id)) unless @statuses.empty?
+
+    @paginated = @next_page_url || @prev_page_url
+    @part_of_url = api_activitypub_outbox_url
+
+    set_pagination_headers(@next_page_url, @prev_page_url)
+
+    render :show_page
+  end
 
   def cache_collection(raw)
     super(raw, Status)
@@ -38,4 +55,15 @@ class Api::Activitypub::OutboxController < ApiController
   def set_account
     @account = Account.find(params[:id])
   end
+
+  def set_first_last_page(statuses) # rubocop:disable Style/AccessorMethodName
+    return if statuses.empty?
+
+    @first_page_url = api_activitypub_outbox_url(max_id: statuses.first.id + 1)
+    @last_page_url = api_activitypub_outbox_url(since_id: statuses.last.id - 1)
+  end
+
+  def pagination_params(core_params)
+    params.permit(:local, :limit).merge(core_params)
+  end
 end
diff --git a/app/helpers/activitystreams2_builder_helper.rb b/app/helpers/activitystreams2_builder_helper.rb
index eeada56f2..717b470f0 100644
--- a/app/helpers/activitystreams2_builder_helper.rb
+++ b/app/helpers/activitystreams2_builder_helper.rb
@@ -3,6 +3,6 @@
 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
+    account.display_name.presence || account.username
   end
 end
diff --git a/app/views/activitypub/types/collection.activitystreams2.rabl b/app/views/activitypub/types/collection.activitystreams2.rabl
index 9e7e14e2b..d3f8ddcac 100644
--- a/app/views/activitypub/types/collection.activitystreams2.rabl
+++ b/app/views/activitypub/types/collection.activitystreams2.rabl
@@ -1,5 +1,3 @@
 extends 'activitypub/intransient.activitystreams2.rabl'
 
 node(:type)       { 'Collection' }
-node(:items)      { [] }
-node(:totalItems) { 0 }
diff --git a/app/views/activitypub/types/ordered_collection_page.activitystreams2.rabl b/app/views/activitypub/types/ordered_collection_page.activitystreams2.rabl
index f498fe8e5..c821fa928 100644
--- a/app/views/activitypub/types/ordered_collection_page.activitystreams2.rabl
+++ b/app/views/activitypub/types/ordered_collection_page.activitystreams2.rabl
@@ -1,4 +1,3 @@
 extends 'activitypub/types/ordered_collection.activitystreams2.rabl'
 
 node(:type)     { 'OrderedCollectionPage' }
-node(:current)  { request.original_url }
diff --git a/app/views/api/activitypub/outbox/show.activitystreams2.rabl b/app/views/api/activitypub/outbox/show.activitystreams2.rabl
index a498f74bc..273b15e82 100644
--- a/app/views/api/activitypub/outbox/show.activitystreams2.rabl
+++ b/app/views/api/activitypub/outbox/show.activitystreams2.rabl
@@ -1,23 +1,12 @@
-if @paginated
-  extends 'activitypub/types/ordered_collection_page.activitystreams2.rabl'
-else
-  extends 'activitypub/types/ordered_collection.activitystreams2.rabl'
-end
+extends 'activitypub/types/ordered_collection.activitystreams2.rabl'
 
 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(:current)    { @first_page_url } if @first_page_url
+node(:first)      { @first_page_url } if @first_page_url
+node(:last)       { @last_page_url } if @last_page_url
 
 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
+node(:updated)    { |account| (@statuses.empty? ? account.created_at.to_time : @statuses.first.updated_at.to_time).xmlschema }
diff --git a/app/views/api/activitypub/outbox/show_page.activitystreams2.rabl b/app/views/api/activitypub/outbox/show_page.activitystreams2.rabl
new file mode 100644
index 000000000..b6433ccf2
--- /dev/null
+++ b/app/views/api/activitypub/outbox/show_page.activitystreams2.rabl
@@ -0,0 +1,16 @@
+extends 'activitypub/types/ordered_collection_page.activitystreams2.rabl'
+
+object @account
+
+node(:items) do
+  @statuses.map { |status| api_activitypub_status_url(status) }
+end
+
+node(:next)       { @next_page_url } if @next_page_url
+node(:prev)       { @prev_page_url } if @prev_page_url
+node(:current)    { @first_page_url } if @first_page_url
+node(:first)      { @first_page_url } if @first_page_url
+node(:last)       { @last_page_url } if @last_page_url
+node(:partOf)     { @part_of_url } if @part_of_url
+
+node(:updated)    { |account| (@statuses.empty? ? account.created_at.to_time : @statuses.first.updated_at.to_time).xmlschema }