about summary refs log tree commit diff
path: root/app/controllers/api/v1/timelines/public_controller.rb
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/timelines/public_controller.rb
parent86d5aa73e92c85dfd28356f2e7ce0103094c1536 (diff)
pre-emptively fetch missing remote media when a timeline's api page is read
Diffstat (limited to 'app/controllers/api/v1/timelines/public_controller.rb')
-rw-r--r--app/controllers/api/v1/timelines/public_controller.rb14
1 files changed, 13 insertions, 1 deletions
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