<commit_before>module Backend
  module Activities
    extend self

    def activities_older_than(activities_set:, timestamp: nil)
      retrieved_activities = activities_set.below(timestamp || 'inf',
                               count: 20,
                               reversed: true,
                               withscores: true).compact

      retrieved_activities.map do |activity_hash|
        activity = activity_hash[:item]

        h = {
          timestamp: activity_hash[:score],
          user: Pavlov.query(:dead_users_by_ids, user_ids: activity.user.user_id).first,
          action: activity.action,
          created_at: activity.created_at.to_time,
          time_ago: TimeFormatter.as_time_ago(activity.created_at.to_time),
          id: activity.id
        }
        case activity.action
        when "created_comment"
          h[:fact] = Pavlov.query(:'facts/get_dead', id: activity.object.id.to_s)
          h[:comment] = Pavlov.query(:'comments/by_ids', ids: [activity.subject.id.to_s]).first
        when "created_sub_comment"
          h[:fact] = Pavlov.query(:'facts/get_dead', id: activity.object.id.to_s)
          h[:comment] = Pavlov.query(:'comments/by_ids', ids: [activity.subject.parent.id.to_s]).first
          h[:sub_comment] = Backend::SubComments::dead_for(activity.subject)
        when "followed_user"
          subject_user = Pavlov.query(:dead_users_by_ids, user_ids: activity.subject.user_id).first
          h[:followed_user] = subject_user
        end

        h
      end
    end
  end
end<commit_msg>Somewhat smarter fetching of activities<commit_after>module Backend
  module Activities
    extend self

    def activities_older_than(activities_set:, timestamp: nil)
      retrieved_activities = activities_set.below(timestamp || 'inf',
                               count: 20,
                               reversed: true,
                               withscores: true).compact

      retrieved_activities.map do |activity_hash|
        activity = activity_hash[:item]

        h = {
          timestamp: activity_hash[:score],
          action: activity.action,
          created_at: activity.created_at.to_time,
          time_ago: TimeFormatter.as_time_ago(activity.created_at.to_time),
          id: activity.id
        }
        case activity.action
        when "created_comment"
          h[:fact] = Pavlov.query(:'facts/get_dead', id: activity.object_id.to_s)
          h[:comment] = Pavlov.query(:'comments/by_ids', ids: [activity.subject_id.to_s]).first
          h[:user] = h[:comment].created_by
        when "created_sub_comment"
          h[:fact] = Pavlov.query(:'facts/get_dead', id: activity.object_id.to_s)
          h[:comment] = Pavlov.query(:'comments/by_ids', ids: [activity.subject.parent_id.to_s]).first
          h[:sub_comment] = Backend::SubComments::dead_for(activity.subject)
          h[:user] = h[:sub_comment].created_by
        when "followed_user"
          subject_user = Pavlov.query(:dead_users_by_ids, by: :graph_user_id, user_ids: activity.subject_id).first
          h[:followed_user] = subject_user
          h[:user] = Pavlov.query(:dead_users_by_ids, by: :graph_user_id, user_ids: activity.user_id).first
        end

        h
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10872235872235872, 'fraction_numerical': 0.0012285012285012285, 'mean_word_length': 2.3856548856548856, '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': '1999925', 'n_tokens_mistral': 998, 'n_tokens_neox': 970, 'n_words': 165}