<commit_before>require 'gmail_api_client'
require 'twitter_client'
require 'alchemyapi'
require 'module_text'

class User < ActiveRecord::Base
  has_many :supporters
  has_many :daily_reports
  has_many :check_ins, through: :daily_reports
  has_many :tweets, through: :daily_reports
  has_many :gmails, through: :daily_reports
  has_many :triggers
  has_many :trigger_histories
  has_many :tokens

  phony_normalize :phone, :default_country_code => 'US' # takes care of the many different ways to input a phone number
  validates :phone, :phony_plausible => true
  validates :gmail, :phone, :first_name, :last_name, presence: true
  validates :gmail, uniqueness: true
  has_secure_password

  include Texter


  def set_token(token)
    self.tokens << token
    self.set_history_id
  end

  def twitter_token
    self.tokens.where('type=?', "TwitterToken").first
  end

  def gmail_token
    self.tokens.where('type=?', "GmailToken").first
  end

  def active? # activity on GMAIL
    if self.last_history_number != find_last_history_id
      update_active_time
      return true
    end
    return false
  end

  def update_active_time
    self.last_active = Time.now
    self.set_history_id
    self.save
  end

  def find_last_history_id
    client = GmailAPI.new(self.gmail_token)
    if self.last_history_number.nil?
      self.last_history_number = client.get_start_history_id
    else
      return client.get_last_history_id(self.last_history_number)
    end
  end

  def text_supporters(message)
    self.supporters.each do |supporter|
      supporter.text(message, supporter.phone)
    end
  end

  def happy_messages?(number_of_days, data_type)
    return true if daily_reports.empty?
    reports = get_last_reports(number_of_days)
    avg_sentiment = reports.map{|report| report.data_average(data_type)}.inject(:+) / reports.count.to_f
    return avg_sentiment > -0.6
  end

  def check_email_sentiment(trigger)<commit_msg>added empty check for daily reports<commit_after>require 'gmail_api_client'
require 'twitter_client'
require 'alchemyapi'
require 'module_text'

class User < ActiveRecord::Base
  has_many :supporters
  has_many :daily_reports
  has_many :check_ins, through: :daily_reports
  has_many :tweets, through: :daily_reports
  has_many :gmails, through: :daily_reports
  has_many :triggers
  has_many :trigger_histories
  has_many :tokens

  phony_normalize :phone, :default_country_code => 'US' # takes care of the many different ways to input a phone number
  validates :phone, :phony_plausible => true
  validates :gmail, :phone, :first_name, :last_name, presence: true
  validates :gmail, uniqueness: true
  has_secure_password

  include Texter


  def set_token(token)
    self.tokens << token
    self.set_history_id
  end

  def twitter_token
    self.tokens.where('type=?', "TwitterToken").first
  end

  def gmail_token
    self.tokens.where('type=?', "GmailToken").first
  end

  def active? # activity on GMAIL
    if self.last_history_number != find_last_history_id
      update_active_time
      return true
    end
    return false
  end

  def update_active_time
    self.last_active = Time.now
    self.set_history_id
    self.save
  end

  def find_last_history_id
    client = GmailAPI.new(self.gmail_token)
    if self.last_history_number.nil?
      self.last_history_number = client.get_start_history_id
    else
      return client.get_last_history_id(self.last_history_number)
    end
  end

  def text_supporters(message)
    self.supporters.each do |supporter|
      supporter.text(message, supporter.phone)
    end
  end

  def happy_messages?(number_of_days, data_type)
    return true if (daily_reports.empty? || daily_reports.send(data_type).empty?)
    reports = get_last_reports(number_of_days)
    avg_sentiment = reports.map{|report| report.data_average(data_type)}.inject(:+) / reports.count.to_f
    return avg_sentiment > -0.6
  end

  def check_email_sentiment(trigger)
{'fraction_non_alphanumeric': 0.08011190233977619, 'fraction_numerical': 0.001017293997965412, 'mean_word_length': 4.134464751958225, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '6372455', 'n_tokens_mistral': 1350, 'n_tokens_neox': 1284, 'n_words': 297}