<commit_before>#   Copyright (c) 2010-2011, Diaspora Inc.  This file is
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

class Contact < ActiveRecord::Base
  belongs_to :user

  belongs_to :person
  validates :person, :presence => true

  has_many :aspect_memberships
  has_many :aspects, :through => :aspect_memberships

  has_many :share_visibilities, :source => :shareable, :source_type => 'Post'
  has_many :posts, :through => :share_visibilities, :source => :shareable, :source_type => 'Post'

  validate :not_contact_for_self,
           :not_blocked_user

  validates_presence_of :user
  validates_uniqueness_of :person_id, :scope => :user_id

  before_destroy :destroy_notifications,
                 :repopulate_cache!

  # contact.sharing is true when contact.person is sharing with contact.user
  scope :sharing, lambda {
    where(:sharing => true)
  }

  # contact.receiving is true when contact.user is sharing with contact.person
  scope :receiving, lambda {
    where(:receiving => true)
  }

  scope :only_sharing, lambda {
    sharing.where(:receiving => false)
  }

  def destroy_notifications
    Notification.where(:target_type => "Person",
                       :target_id => person_id,
                       :recipient_id => user_id,
                       :type => "Notifications::StartedSharing").delete_all
  end

  def repopulate_cache!
    if RedisCache.configured?
      cache = RedisCache.new(self.user)
      cache.repopulate!
    end
  end

  def dispatch_request
    request = self.generate_request
    Postzord::Dispatcher.build(self.user, request).post
    request
  end<commit_msg>Only repopulate cache on contact destroy if user exists<commit_after>#   Copyright (c) 2010-2011, Diaspora Inc.  This file is
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

class Contact < ActiveRecord::Base
  belongs_to :user

  belongs_to :person
  validates :person, :presence => true

  has_many :aspect_memberships
  has_many :aspects, :through => :aspect_memberships

  has_many :share_visibilities, :source => :shareable, :source_type => 'Post'
  has_many :posts, :through => :share_visibilities, :source => :shareable, :source_type => 'Post'

  validate :not_contact_for_self,
           :not_blocked_user

  validates_presence_of :user
  validates_uniqueness_of :person_id, :scope => :user_id

  before_destroy :destroy_notifications,
                 :repopulate_cache!

  # contact.sharing is true when contact.person is sharing with contact.user
  scope :sharing, lambda {
    where(:sharing => true)
  }

  # contact.receiving is true when contact.user is sharing with contact.person
  scope :receiving, lambda {
    where(:receiving => true)
  }

  scope :only_sharing, lambda {
    sharing.where(:receiving => false)
  }

  def destroy_notifications
    Notification.where(:target_type => "Person",
                       :target_id => person_id,
                       :recipient_id => user_id,
                       :type => "Notifications::StartedSharing").delete_all
  end

  def repopulate_cache!
    if RedisCache.configured? && self.user.present?
      cache = RedisCache.new(self.user)
      cache.repopulate!
    end
  end

  def dispatch_request
    request = self.generate_request
    Postzord::Dispatcher.build(self.user, request).post
    request
  end
{'fraction_non_alphanumeric': 0.09178885630498533, 'fraction_numerical': 0.005278592375366569, 'mean_word_length': 3.2797992471769133, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, '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': '6678912', 'n_tokens_mistral': 1088, 'n_tokens_neox': 1019, 'n_words': 264}