Skip to content

Commit

Permalink
batch save for you
Browse files Browse the repository at this point in the history
  • Loading branch information
jtomchak committed Oct 25, 2023
1 parent 541a041 commit f8de0fe
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 19 deletions.
43 changes: 43 additions & 0 deletions app/lib/for_you_feed_manager.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

# V1 of adding personalize statuses to a user's For You Feed
# Done by indiviual statuses and filtered. :foryou is the
# Mammoth curated list
class ForYouFeedManager
include Singleton
include Redisable

MAX_ITEMS = 1000
MINIMUM_ENGAGMENT_ACTIONS = 2

# Adds to Account's For You Feed
# We zip the statuses using the id for both the score of zadd and the value
# Creating an array of array elements [["111296866514987736", "111296866514987736"]...
def batch_to_feed(account_id, status_ids)
statuses = status_ids.zip(status_ids)

perform_push_to_feed(account_id, statuses)
end

private

def perform_push_to_feed(account_id, statuses)
foryou_key = key(account_id)
redis.zadd(foryou_key, statuses)

# Keep the list from growning infinitely
trim(foryou_key)
end

# Trim a feed to maximum size by removing older items
# @param [Integer] foryou_key
# @return [void]
def trim(foryou_key)
# Remove any items past the MAX_ITEMS'th entry in our feed
redis.zremrangebyrank(foryou_key, 0, -(MAX_ITEMS + 1))
end

def key(account_id)
FeedManager.instance.key('personal', account_id)
end
end
40 changes: 21 additions & 19 deletions app/workers/update_for_you_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,22 @@ def perform(opts)
# If rebuild is true, Zero Out User's for you feed
@personal.reset_feed(@account.id) if opts['rebuild']

push_status!
@statuses = filter_statuses!

foryou_manager.batch_to_feed(@account.id, @statuses)
# Final Step:
# Set user's status to 'idle'
update_user_status('idle').wait
end

private

def push_status!
# Indirect Follow
push_indirect_following_status
# Direct Follows
push_following_status
# Channel Feed
push_channels_status
# Mammoth Curated OG Feed
push_mammoth_curated_status
# Indirect Follow
# Direct Follows
# Channel Feed
# Mammoth Curated OG Feed
def filter_statuses!
[*indirect_following_status, *following_status, *channels_status, *mammoth_curated_status]
end

def update_user_status(status)
Expand All @@ -69,36 +67,36 @@ def mammoth_user(acct)

# TODO: update account.id to user.acct
# Return early if user setting is Zero, meaning 'off' from the iOS perspective
def push_following_status
def following_status
user_setting = @user[:for_you_settings]
return if user_setting[:your_follows].zero?

@personal.statuses_for_direct_follows(@acct)
.filter_map { |s| engagment_threshold(s, user_setting[:your_follows], 'following') }
.each { |s| ForYouFeedWorker.perform_async(s['id'], @account.id, 'personal') }
.pluck('id')
end

# Indirect Follows
def push_indirect_following_status
def indirect_following_status
user_setting = @user[:for_you_settings]
return if user_setting[:friends_of_friends].zero?

@personal.statuses_for_indirect_follows(@account)
.filter_map { |s| engagment_threshold(s, user_setting[:friends_of_friends], 'indirect') }
.each { |s| ForYouFeedWorker.perform_async(s['id'], @account.id, 'personal') }
.pluck('id')
end

# Channels Subscribed
# Include ONLY enabled_channels
def push_channels_status
def channels_status
user_setting = @user[:for_you_settings]
return if user_setting[:from_your_channels].zero?

@personal.statuses_for_enabled_channels(@user).each { |s| ForYouFeedWorker.perform_async(s['id'], @account.id, 'personal') }
@personal.statuses_for_enabled_channels(@user).pluck('id')
end

# Mammoth Curated OG List
def push_mammoth_curated_status
def mammoth_curated_status
user_setting = @user[:for_you_settings]
return if user_setting[:curated_by_mammoth].zero?

Expand All @@ -107,9 +105,9 @@ def push_mammoth_curated_status
origin = Mammoth::StatusOrigin.instance

list_statuses.filter_map { |s| engagment_threshold(s, user_setting[:curated_by_mammoth], 'mammoth') }
.each do |s|
.map do |s|
origin.add_mammoth_pick(s)
ForYouFeedWorker.perform_async(s['id'], @account.id, 'personal')
s['id']
end
end

Expand All @@ -136,4 +134,8 @@ def engagment_metrics(type)
{ 1 => 1, 2 => 2, 3 => 3 }
end
end

def foryou_manager
ForYouFeedManager.instance
end
end

0 comments on commit f8de0fe

Please sign in to comment.