<commit_before>class StoryFanoutWorker
  include Sidekiq::Worker

  def perform(user_id, story_id)
    followers = $redis.smembers(UserTimeline::USER_FOLLOWERS_PREFIX + user_id.to_s)
    followers += [user_id]
    
    followers.each do |follower_id|
      if $redis.exists(UserTimeline::TIMELINE_CACHE_PREFIX + follower_id.to_s)
        follower = User.find follower_id
        UserTimeline.acquire_lock follower
        
        # TODO "push" story...
        story = Story.find_by_id story_id
        if story
          stories = JSON.parse Entities::Story.represent([story], current_ability: Ability.new(follower), title_language_preference: follower.title_language_preference).to_json
        else
          # Just need to re-aggregate.
          stories = []
        end
        
        timeline = UserTimeline.aggregate(JSON.parse($redis.get(UserTimeline::TIMELINE_CACHE_PREFIX + follower_id.to_s)), stories)
        UserTimeline.update_user_timeline! follower, timeline
          
        UserTimeline.release_lock follower
      end
    end
  end
end
<commit_msg>Start fanning out with the current user.<commit_after>class StoryFanoutWorker
  include Sidekiq::Worker

  def perform(user_id, story_id)
    followers = [user_id] + $redis.smembers(UserTimeline::USER_FOLLOWERS_PREFIX + user_id.to_s)
    
    followers.each do |follower_id|
      if $redis.exists(UserTimeline::TIMELINE_CACHE_PREFIX + follower_id.to_s)
        follower = User.find follower_id
        UserTimeline.acquire_lock follower
        
        # TODO "push" story...
        story = Story.find_by_id story_id
        if story
          stories = JSON.parse Entities::Story.represent([story], current_ability: Ability.new(follower), title_language_preference: follower.title_language_preference).to_json
        else
          # Just need to re-aggregate.
          stories = []
        end
        
        timeline = UserTimeline.aggregate(JSON.parse($redis.get(UserTimeline::TIMELINE_CACHE_PREFIX + follower_id.to_s)), stories)
        UserTimeline.update_user_timeline! follower, timeline
          
        UserTimeline.release_lock follower
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.08063021316033364, 'fraction_numerical': 0.0, 'mean_word_length': 2.9183303085299457, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7569165', 'n_tokens_mistral': 697, 'n_tokens_neox': 659, 'n_words': 121}