<commit_before>module EmailCampaigns
  class HeartsReceived

    # marks hearts as sent and returns params for emails
    def process!
      recent_hearts = Heart.includes(:heartable).where('created_at > ?', 12.hours.ago)

      by_user = recent_hearts.each_with_object({}) do |heart, o|
        o[heart.heartable.author_id] ||= []
        o[heart.heartable.author_id] << heart
      end

      send = {}
      by_user.each do |user_id, hearts|
        last_sent_at = hearts.map(&:sent_at).compact.sort.last

        if last_sent_at.nil? || last_sent_at < 3.hours.ago
          unsent_hearts = hearts.select{|h| h.sent_at.nil? }
          oldest_unsent_heart = unsent_hearts.map(&:created_at).sort.first

          if oldest_unsent_heart.nil? || oldest_unsent_heart < 30.minutes.ago

            # TODO: remove feature flag
            if User.find(user_id).staff?
              send[user_id] = unsent_hearts.map(&:id)
            end

            unsent_hearts.each{|h| h.update! sent_at: Time.now }
          end
        end
      end
      send
    end
  end
end<commit_msg>fix bug in heart campaign<commit_after>module EmailCampaigns
  class HeartsReceived

    # marks hearts as sent and returns params for emails
    def process!
      recent_hearts = Heart.includes(:heartable).where('created_at > ?', 12.hours.ago)

      by_user = recent_hearts.each_with_object({}) do |heart, o|
        o[heart.heartable.author_id] ||= []
        o[heart.heartable.author_id] << heart
      end

      send = {}
      by_user.each do |user_id, hearts|
        last_sent_at = hearts.map(&:sent_at).compact.sort.last

        if last_sent_at.nil? || last_sent_at < 3.hours.ago
          unsent_hearts = hearts.select{|h| h.sent_at.nil? }
          oldest_unsent_heart = unsent_hearts.map(&:created_at).sort.first

          if oldest_unsent_heart.nil? || oldest_unsent_heart < 30.minutes.ago

            # TODO: remove feature flag
            if User.find(user_id).staff?
              send[user_id] = unsent_hearts.map(&:id) if unsent_hearts.any?
            end

            unsent_hearts.each{|h| h.update! sent_at: Time.now }
          end
        end
      end
      send
    end
  end
end
{'fraction_non_alphanumeric': 0.10603290676416818, 'fraction_numerical': 0.004570383912248629, 'mean_word_length': 2.316666666666667, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, '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': '5991629', 'n_tokens_mistral': 746, 'n_tokens_neox': 711, 'n_words': 139}