about summary refs log tree commit diff
path: root/app/services
diff options
context:
space:
mode:
authorMatt Jankowski <mjankowski@thoughtbot.com>2017-05-19 10:21:52 -0400
committerEugen Rochko <eugen@zeonfederated.com>2017-05-19 16:21:52 +0200
commitdb4119f9711e6c2ac640325640f6e61b1e177ed3 (patch)
treecf7a19fe42370e81b99de7e7ac8ac01f260c8243 /app/services
parent4a3db716921b2e290011b1bc33d0f19e2e8f2d55 (diff)
Specs for precompute feed service (#3142)
* Add spec for precompute feed service

* Refactor PrecomputeFeedService

* spec wip
Diffstat (limited to 'app/services')
-rw-r--r--app/services/precompute_feed_service.rb46
1 files changed, 37 insertions, 9 deletions
diff --git a/app/services/precompute_feed_service.rb b/app/services/precompute_feed_service.rb
index 46f0057cb..acda5108a 100644
--- a/app/services/precompute_feed_service.rb
+++ b/app/services/precompute_feed_service.rb
@@ -1,20 +1,48 @@
 # frozen_string_literal: true
 
 class PrecomputeFeedService < BaseService
-  # Fill up a user's home/mentions feed from DB and return a subset
-  # @param [Symbol] type :home or :mentions
-  # @param [Account] account
-  def call(_, account)
+  LIMIT = FeedManager::MAX_ITEMS / 4
+
+  def call(account)
+    @account = account
+    populate_feed
+  end
+
+  private
+
+  attr_reader :account
+
+  def populate_feed
     redis.pipelined do
-      # NOTE: Added `id desc, account_id desc` to `ORDER BY` section to optimize query.
-      Status.as_home_timeline(account).order(account_id: :desc).limit(FeedManager::MAX_ITEMS / 4).each do |status|
-        next if status.direct_visibility? || FeedManager.instance.filter?(:home, status, account.id)
-        redis.zadd(FeedManager.instance.key(:home, account.id), status.id, status.reblog? ? status.reblog_of_id : status.id)
+      statuses.each do |status|
+        process_status(status)
       end
     end
   end
 
-  private
+  def process_status(status)
+    add_status_to_feed(status) unless skip_status?(status)
+  end
+
+  def skip_status?(status)
+    status.direct_visibility? || status_filtered?(status)
+  end
+
+  def add_status_to_feed(status)
+    redis.zadd(account_home_key, status.id, status.reblog? ? status.reblog_of_id : status.id)
+  end
+
+  def status_filtered?(status)
+    FeedManager.instance.filter?(:home, status, account.id)
+  end
+
+  def account_home_key
+    FeedManager.instance.key(:home, account.id)
+  end
+
+  def statuses
+    Status.as_home_timeline(account).order(account_id: :desc).limit(LIMIT)
+  end
 
   def redis
     Redis.current