about summary refs log tree commit diff
path: root/app/services/precompute_feed_service.rb
blob: 0fb33db2352d2937ea220c9cd10daedf0de47b87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class PrecomputeFeedService < BaseService
  MAX_FEED_SIZE = 800

  # Fill up a user's home/mentions feed from DB and return it
  # @param [Symbol] type :home or :mentions
  # @param [Account] account
  # @return [Array]
  def call(type, account)
    statuses = send(type.to_s, account).order('created_at desc').limit(MAX_FEED_SIZE)
    statuses.each { |status| push(type, account.id, status) }
    statuses
  end

  private

  def push(type, receiver_id, status)
    redis.zadd(key(type, receiver_id), status.created_at.to_i, status.id)
  end

  def home(account)
    Status.where(account: [account] + account.following).with_includes.with_counters
  end

  def mentions(account)
    Status.where(id: Mention.where(account: account).pluck(:status_id)).with_includes.with_counters
  end

  def key(type, id)
    "feed:#{type}:#{id}"
  end

  def redis
    $redis
  end
end