3 class Message < ActiveRecord::Base
 
   4   belongs_to :sender, :class_name => "User", :foreign_key => :from_user_id
 
   5   belongs_to :recipient, :class_name => "User", :foreign_key => :to_user_id
 
   7   validates_presence_of :title, :body, :sent_on, :sender, :recipient
 
   8   validates_length_of :title, :within => 1..255
 
   9   validates_inclusion_of :message_read, :in => [true, false]
 
  10   validates_as_utf8 :title
 
  12   def self.from_mail(mail, from, to)
 
  15         body = mail.text_part.decoded
 
  17         body = HTMLEntities.new.decode(Sanitize.clean(mail.html_part.decoded))
 
  19     elsif mail.text? && mail.sub_type == "html"
 
  20       body = HTMLEntities.new.decode(Sanitize.clean(mail.decoded))
 
  25     message = Message.new(
 
  28       :sent_on => mail.date.new_offset(0),
 
  29       :title => mail.subject.sub(/\[OpenStreetMap\] */, ""),
 
  31       :body_format => "text"
 
  36     RichText.new(read_attribute(:body_format), read_attribute(:body))
 
  41     md5 << from_user_id.to_s
 
  42     md5 << to_user_id.to_s
 
  43     md5 << sent_on.xmlschema