<commit_before>class Conversation < ActiveRecord::Base

  after_save :update_read_statuses, :save_person_conversations, :send_email_to_participants

  belongs_to :listing

  has_many :person_conversations
  has_many :participants, :through => :person_conversations, :source => :person
  
  has_many :messages
  
  attr_accessor :request_protocol, :request_host
  
  validates_length_of :title, :within => 2..100
  validates_numericality_of :listing_id, :only_integer => true, :allow_nil => true 

  # Includes the title of the conversation in the url
  def to_param
    "#{id}_#{title.to_s.gsub(/\W/, '_').downcase}"
  end
  
  # Creates a new message in the conversation
  def message_attributes=(attributes)
    messages.build(attributes)
  end
  
  # Sets the participants of the conversation
  def conversation_participants=(conversation_participants)
    Person.find(conversation_participants).each do |participant|
      is_read = (participant.id == messages.first.sender.id) ? 1 : 0
      person_conversations.build(:person_id => participant.id,
                                 :is_read => is_read,
                                 :last_sent_at => created_at)
    end
  end 
  
  # Send email notification to message receivers and returns the receivers
  def send_email_to_participants
    if RAILS_ENV == "production"
      recipients(last_message.sender).each do |recipient|
        if recipient.settings.email_when_new_comment == 1
          url = "#{request_protocol}#{request_host}#{person_inbox_path(recipient, this)}"
          UserMailer.deliver_notification_of_new_message(recipient, last_message, url)
        end  
      end
    end
  end
  
  # Updates read statuses for each participant so that the sender of the newest
  # message has read the conversation and others haven't
  def update_read_statuses
    person_conversations.each do |p_c|
      if p_c.person_id == last_message.sender.id
        p_c.update_attribute(:last_sent_at, last_message.created_at)
      else
        p_c.update_attributes({ :is_read => 0, :last_received_at => last_message.created_at })
      end  
    end<commit_msg>Ja vielä säätöä mailien lähetyksessä.<commit_after>class Conversation < ActiveRecord::Base

  after_save :update_read_statuses, :save_person_conversations, :send_email_to_participants

  belongs_to :listing

  has_many :person_conversations
  has_many :participants, :through => :person_conversations, :source => :person
  
  has_many :messages
  
  attr_accessor :request_protocol, :request_host
  
  validates_length_of :title, :within => 2..100
  validates_numericality_of :listing_id, :only_integer => true, :allow_nil => true 

  # Includes the title of the conversation in the url
  def to_param
    "#{id}_#{title.to_s.gsub(/\W/, '_').downcase}"
  end
  
  # Creates a new message in the conversation
  def message_attributes=(attributes)
    messages.build(attributes)
  end
  
  # Sets the participants of the conversation
  def conversation_participants=(conversation_participants)
    Person.find(conversation_participants).each do |participant|
      is_read = (participant.id == messages.first.sender.id) ? 1 : 0
      person_conversations.build(:person_id => participant.id,
                                 :is_read => is_read,
                                 :last_sent_at => created_at)
    end
  end 
  
  # Send email notification to message receivers and returns the receivers
  def send_email_to_participants
    if RAILS_ENV == "production"
      recipients(last_message.sender).each do |recipient|
        if recipient.settings.email_when_new_comment == 1
          url = "#{request_protocol}#{request_host}#{person_inbox_path(recipient, self)}"
          UserMailer.deliver_notification_of_new_message(recipient, last_message, url)
        end  
      end
    end
  end
  
  # Updates read statuses for each participant so that the sender of the newest
  # message has read the conversation and others haven't
  def update_read_statuses
    person_conversations.each do |p_c|
      if p_c.person_id == last_message.sender.id
        p_c.update_attribute(:last_sent_at, last_message.created_at)
      else
        p_c.update_attributes({ :is_read => 0, :last_received_at => last_message.created_at })
      end  
    end
{'fraction_non_alphanumeric': 0.08090994371482177, 'fraction_numerical': 0.00375234521575985, 'mean_word_length': 3.461297071129707, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6581476', 'n_tokens_mistral': 1352, 'n_tokens_neox': 1296, 'n_words': 305}