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 after_initialize :set_defaults
14 def self.from_mail(mail, from, to)
17 body = mail.text_part.decoded
19 body = HTMLEntities.new.decode(Sanitize.clean(mail.html_part.decoded))
21 elsif mail.text? and mail.sub_type == "html"
22 body = HTMLEntities.new.decode(Sanitize.clean(mail.decoded))
27 message = Message.new(
30 :sent_on => mail.date.new_offset(0),
31 :title => mail.subject.sub(/\[OpenStreetMap\] */, ""),
33 :body_format => "text"
38 RichText.new(read_attribute(:body_format), read_attribute(:body))
43 md5 << from_user_id.to_s
44 md5 << to_user_id.to_s
45 md5 << sent_on.xmlschema
54 self.body_format = "markdown" unless self.attribute_present?(:body_format)