<commit_before>require 'ohm/contrib'
require 'pavlov'

class GraphUser < OurOhm;end # needed because of removed const_missing from ohm

require_relative 'activity/subject'
require_relative 'activity/followers'
require_relative 'activity/listener'
require_relative 'activity/create_listeners'
require_relative '../interactors/interactors/send_mail_for_activity'

class Activity < OurOhm
  include Ohm::Timestamping
  reference :user, GraphUser

  generic_reference :subject
  generic_reference :object

  attribute :action
  index     :action

  include Pavlov::Helpers

  alias :old_set_user :user= unless method_defined?(:old_set_user)
  def user=(new_user)
    old_set_user new_user.graph_user
  end

  after :create, :process_activity
  def process_activity
    Resque.enqueue(ProcessActivity, self.id)
  end

  after :create, :send_mail_for_activity
  def send_mail_for_activity
    interactor :send_mail_for_activity, self
  end


  def self.for(search_for)
    res = find(subject_id: search_for.id, subject_class: search_for.class) |
          find(object_id: search_for.id, object_class: search_for.class)

    res |= find(user_id: search_for.id) if search_for.class == GraphUser

    res
  end

  def to_hash_without_time
    h = { user: user,
          action: action.to_sym,
          subject: subject, }
    h[:object] = object if object
    h
  end

  def still_valid?
    user_valid? and subject_valid? and object_valid?
  end

  def valid_for_show?
    still_valid? and
      valid_for_activity?(subject) and
      valid_for_activity?(object)
  end

  def timestamp
    Ohm::Model::TimestampedSet.current_time(DateTime.parse(created_at))
  end

  def add_to_list_with_score list
    list.add(self,timestamp)
    self.key[:containing_sorted_sets].sadd list.key.to_s
  end

  def remove_from_list list
    list.delete self
    self.key[:containing_sorted_sets].srem list.key.to_s
  end

  before :delete, :remove_from_containing_sorted_sets

  def remove_from_containing_sorted_sets
    self.key[:containing_sorted_sets].smembers.each do |list|
      Nest.new(list).zrem self.id
    end
    self.key[:containing_sorted_sets].del
  end

  #Hack to make sure the mail interactor gets executed with authorization errors.
  def pavlov_options
    {current_user: true}
  end

  private

  def user_valid?
    user or not user_id
  end

  def subject_valid?
    subject or not subject_id
  end

  def object_valid?
    object or not object_id
  end
<commit_msg>made pavlov invocation cleaner<commit_after>require 'ohm/contrib'
require 'pavlov'

class GraphUser < OurOhm;end # needed because of removed const_missing from ohm

require_relative 'activity/subject'
require_relative 'activity/followers'
require_relative 'activity/listener'
require_relative 'activity/create_listeners'
require_relative '../interactors/interactors/send_mail_for_activity'

class Activity < OurOhm
  include Ohm::Timestamping
  reference :user, GraphUser

  generic_reference :subject
  generic_reference :object

  attribute :action
  index     :action

  alias :old_set_user :user= unless method_defined?(:old_set_user)
  def user=(new_user)
    old_set_user new_user.graph_user
  end

  after :create, :process_activity
  def process_activity
    Resque.enqueue(ProcessActivity, self.id)
  end

  after :create, :send_mail_for_activity
  def send_mail_for_activity
    Pavlov.interactor :send_mail_for_activity,
                        self, {current_user: true}
  end

  end


  def self.for(search_for)
    res = find(subject_id: search_for.id, subject_class: search_for.class) |
          find(object_id: search_for.id, object_class: search_for.class)

    res |= find(user_id: search_for.id) if search_for.class == GraphUser

    res
  end

  def to_hash_without_time
    h = { user: user,
          action: action.to_sym,
          subject: subject, }
    h[:object] = object if object
    h
  end

  def still_valid?
    user_valid? and subject_valid? and object_valid?
  end

  def valid_for_show?
    still_valid? and
      valid_for_activity?(subject) and
      valid_for_activity?(object)
  end

  def timestamp
    Ohm::Model::TimestampedSet.current_time(DateTime.parse(created_at))
  end

  def add_to_list_with_score list
    list.add(self,timestamp)
    self.key[:containing_sorted_sets].sadd list.key.to_s
  end

  def remove_from_list list
    list.delete self
    self.key[:containing_sorted_sets].srem list.key.to_s
  end

  before :delete, :remove_from_containing_sorted_sets

  def remove_from_containing_sorted_sets
    self.key[:containing_sorted_sets].smembers.each do |list|
      Nest.new(list).zrem self.id
    end
    self.key[:containing_sorted_sets].del
  end

  private

  def user_valid?
    user or not user_id
  end

  def subject_valid?
    subject or not subject_id
  end

  def object_valid?
    object or not object_id
  end

{'fraction_non_alphanumeric': 0.07259228706949887, 'fraction_numerical': 0.0, 'mean_word_length': 3.854854854854855, 'pattern_counts': {'":': 0, '<': 7, '<?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': '3373840', 'n_tokens_mistral': 1634, 'n_tokens_neox': 1572, 'n_words': 367}