]> git.openstreetmap.org Git - rails.git/blob - app/models/message.rb
add browse symbols of openstreetmap-carto 2.30.0
[rails.git] / app / models / message.rb
1 require "validators"
2
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
6
7   validates :title, :presence => true, :utf8 => true, :length => 1..255
8   validates :body, :sent_on, :sender, :recipient, :presence => true
9
10   def self.from_mail(mail, from, to)
11     if mail.multipart?
12       if mail.text_part
13         body = mail.text_part.decoded
14       elsif mail.html_part
15         body = HTMLEntities.new.decode(Sanitize.clean(mail.html_part.decoded))
16       end
17     elsif mail.text? && mail.sub_type == "html"
18       body = HTMLEntities.new.decode(Sanitize.clean(mail.decoded))
19     else
20       body = mail.decoded
21     end
22
23     Message.new(
24       :sender => from,
25       :recipient => to,
26       :sent_on => mail.date.new_offset(0),
27       :title => mail.subject.sub(/\[OpenStreetMap\] */, ""),
28       :body => body,
29       :body_format => "text"
30     )
31   end
32
33   def body
34     RichText.new(self[:body_format], self[:body])
35   end
36
37   def digest
38     md5 = Digest::MD5.new
39     md5 << from_user_id.to_s
40     md5 << to_user_id.to_s
41     md5 << sent_on.xmlschema
42     md5 << title
43     md5 << body
44     md5.hexdigest
45   end
46 end