]> git.openstreetmap.org Git - rails.git/blob - app/models/message.rb
Use relative translations for changeset comments
[rails.git] / app / models / message.rb
1 # == Schema Information
2 #
3 # Table name: messages
4 #
5 #  id                :integer          not null, primary key
6 #  from_user_id      :integer          not null
7 #  title             :string           not null
8 #  body              :text             not null
9 #  sent_on           :datetime         not null
10 #  message_read      :boolean          default(FALSE), not null
11 #  to_user_id        :integer          not null
12 #  to_user_visible   :boolean          default(TRUE), not null
13 #  from_user_visible :boolean          default(TRUE), not null
14 #  body_format       :enum             default("markdown"), not null
15 #
16 # Indexes
17 #
18 #  messages_from_user_id_idx  (from_user_id)
19 #  messages_to_user_id_idx    (to_user_id)
20 #
21 # Foreign Keys
22 #
23 #  messages_from_user_id_fkey  (from_user_id => users.id)
24 #  messages_to_user_id_fkey    (to_user_id => users.id)
25 #
26
27 require "validators"
28
29 class Message < ActiveRecord::Base
30   belongs_to :sender, :class_name => "User", :foreign_key => :from_user_id
31   belongs_to :recipient, :class_name => "User", :foreign_key => :to_user_id
32
33   validates :title, :presence => true, :utf8 => true, :length => 1..255
34   validates :body, :sent_on, :sender, :recipient, :presence => true
35   validates :title, :body, :characters => true
36
37   def self.from_mail(mail, from, to)
38     if mail.multipart?
39       if mail.text_part
40         body = mail.text_part.decoded
41       elsif mail.html_part
42         body = HTMLEntities.new.decode(Sanitize.clean(mail.html_part.decoded))
43       end
44     elsif mail.text? && mail.sub_type == "html"
45       body = HTMLEntities.new.decode(Sanitize.clean(mail.decoded))
46     else
47       body = mail.decoded
48     end
49
50     Message.new(
51       :sender => from,
52       :recipient => to,
53       :sent_on => mail.date.new_offset(0),
54       :title => mail.subject.sub(/\[OpenStreetMap\] */, ""),
55       :body => body,
56       :body_format => "text"
57     )
58   end
59
60   def body
61     RichText.new(self[:body_format], self[:body])
62   end
63
64   def digest
65     md5 = Digest::MD5.new
66     md5 << from_user_id.to_s
67     md5 << to_user_id.to_s
68     md5 << sent_on.xmlschema
69     md5 << title
70     md5 << body
71     md5.hexdigest
72   end
73 end