about summary refs log tree commit diff
path: root/app/controllers/api/v1
diff options
context:
space:
mode:
authormultiple creatures <dev@multiple-creature.party>2019-09-01 22:48:22 -0500
committermultiple creatures <dev@multiple-creature.party>2019-09-01 22:48:22 -0500
commit2d37586daeb38995933da10c88721d4cc25005e8 (patch)
treef9f8789c3139d354830eeda5f48f56dc901d84fd /app/controllers/api/v1
parent86d5aa73e92c85dfd28356f2e7ce0103094c1536 (diff)
pre-emptively fetch missing remote media when a timeline's api page is read
Diffstat (limited to 'app/controllers/api/v1')
-rw-r--r--app/controllers/api/v1/timelines/direct_controller.rb6
-rw-r--r--app/controllers/api/v1/timelines/home_controller.rb9
-rw-r--r--app/controllers/api/v1/timelines/list_controller.rb9
-rw-r--r--app/controllers/api/v1/timelines/public_controller.rb14
-rw-r--r--app/controllers/api/v1/timelines/tag_controller.rb10
5 files changed, 44 insertions, 4 deletions
diff --git a/app/controllers/api/v1/timelines/direct_controller.rb b/app/controllers/api/v1/timelines/direct_controller.rb
index d8a76d153..61c12ebf7 100644
--- a/app/controllers/api/v1/timelines/direct_controller.rb
+++ b/app/controllers/api/v1/timelines/direct_controller.rb
@@ -9,6 +9,7 @@ class Api::V1::Timelines::DirectController < Api::BaseController
 
   def show
     @statuses = load_statuses
+    preload_media
     render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
   end
 
@@ -60,4 +61,9 @@ class Api::V1::Timelines::DirectController < Api::BaseController
   def pagination_since_id
     @statuses.first.id
   end
+
+  def preload_media
+    fetch_ids = @statuses.flat_map { |s| s.media_attachments.select { |m| m.needs_redownload? }.pluck(:id) }.uniq
+    fetch_ids.each { |m| FetchMediaWorker.perform_async(m) }
+  end
 end
diff --git a/app/controllers/api/v1/timelines/home_controller.rb b/app/controllers/api/v1/timelines/home_controller.rb
index fcd0757f1..3e8de92bb 100644
--- a/app/controllers/api/v1/timelines/home_controller.rb
+++ b/app/controllers/api/v1/timelines/home_controller.rb
@@ -27,7 +27,9 @@ class Api::V1::Timelines::HomeController < Api::BaseController
   end
 
   def home_statuses
-    account_home_feed.get(
+    home_feed = account_home_feed
+    preload_media(home_feed.get(DEFAULT_STATUSES_LIMIT * 2, params[:max_id], params[:since_id], params[:min_id]))
+    home_feed.get(
       limit_param(DEFAULT_STATUSES_LIMIT),
       params[:max_id],
       params[:since_id],
@@ -66,4 +68,9 @@ class Api::V1::Timelines::HomeController < Api::BaseController
   def regeneration_in_progress?
     Redis.current.exists("account:#{current_account.id}:regeneration")
   end
+
+  def preload_media(statuses)
+    fetch_ids = statuses.flat_map { |s| s.media_attachments.select { |m| m.needs_redownload? }.pluck(:id) }.uniq
+    fetch_ids.each { |m| FetchMediaWorker.perform_async(m) }
+  end
 end
diff --git a/app/controllers/api/v1/timelines/list_controller.rb b/app/controllers/api/v1/timelines/list_controller.rb
index a15eae468..59a1eb798 100644
--- a/app/controllers/api/v1/timelines/list_controller.rb
+++ b/app/controllers/api/v1/timelines/list_controller.rb
@@ -29,7 +29,9 @@ class Api::V1::Timelines::ListController < Api::BaseController
   end
 
   def list_statuses
-    list_feed.get(
+    feed = list_feed
+    preload_media(feed.get(DEFAULT_STATUSES_LIMIT * 2, params[:max_id], params[:since_id], params[:min_id]))
+    feed.get(
       limit_param(DEFAULT_STATUSES_LIMIT),
       params[:max_id],
       params[:since_id],
@@ -64,4 +66,9 @@ class Api::V1::Timelines::ListController < Api::BaseController
   def pagination_since_id
     @statuses.first.id
   end
+
+  def preload_media(statuses)
+    fetch_ids = statuses.flat_map { |s| s.media_attachments.select { |m| m.needs_redownload? }.pluck(:id) }.uniq
+    fetch_ids.each { |m| FetchMediaWorker.perform_async(m) }
+  end
 end
diff --git a/app/controllers/api/v1/timelines/public_controller.rb b/app/controllers/api/v1/timelines/public_controller.rb
index 9c3f6c0a8..9d226ba5a 100644
--- a/app/controllers/api/v1/timelines/public_controller.rb
+++ b/app/controllers/api/v1/timelines/public_controller.rb
@@ -22,7 +22,13 @@ class Api::V1::Timelines::PublicController < Api::BaseController
   end
 
   def public_statuses
-    statuses = public_timeline_statuses.paginate_by_id(
+    ptl = public_timeline_statuses
+    ptl_preload = ptl.paginate_by_id(
+      limit_param(DEFAULT_STATUSES_LIMIT * 2),
+      params_slice(:max_id, :since_id, :min_id)
+    )
+    preload_media(ptl_preload)
+    statuses = ptl.paginate_by_id(
       limit_param(DEFAULT_STATUSES_LIMIT),
       params_slice(:max_id, :since_id, :min_id)
     )
@@ -63,4 +69,10 @@ class Api::V1::Timelines::PublicController < Api::BaseController
   def pagination_since_id
     @statuses.first.id
   end
+
+  def preload_media(statuses)
+    status_ids = statuses.joins(:media_attachments).distinct(:id).select(:id).reorder(nil)
+    fetch_ids = MediaAttachment.where(status_id: status_ids, file_updated_at: nil).reject { |m| m.blocked? }.pluck(:id)
+    fetch_ids.each { |m| FetchMediaWorker.perform_async(m) }
+  end
 end
diff --git a/app/controllers/api/v1/timelines/tag_controller.rb b/app/controllers/api/v1/timelines/tag_controller.rb
index a0a10d349..4641d6941 100644
--- a/app/controllers/api/v1/timelines/tag_controller.rb
+++ b/app/controllers/api/v1/timelines/tag_controller.rb
@@ -51,7 +51,9 @@ class Api::V1::Timelines::TagController < Api::BaseController
   end
 
   def bookmark_results
-    @_results ||= account_bookmarks.paginate_by_max_id(
+    bookmarks = account_bookmarks
+    preload_media(bookmarks)
+    @_results ||= bookmarks.paginate_by_max_id(
       limit_param(DEFAULT_STATUSES_LIMIT),
       params[:max_id],
       params[:since_id]
@@ -85,4 +87,10 @@ class Api::V1::Timelines::TagController < Api::BaseController
   def pagination_since_id
     @statuses.first.id
   end
+
+  def preload_media(statuses)
+    status_ids = statuses.joins(:media_attachments).distinct(:id).select(:id).reorder(nil)
+    fetch_ids = MediaAttachment.where(status_id: status_ids, file_updated_at: nil).pluck(:id)
+    fetch_ids.each { |m| FetchMediaWorker.perform_async(m) }
+  end
 end