1 # == Schema Information
5 # id :bigint(8) not null, primary key
6 # from_user_id :bigint(8) not null
7 # title :string not null
9 # sent_on :datetime not null
10 # message_read :boolean default(FALSE), not null
11 # to_user_id :bigint(8) 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 # muted :boolean default(FALSE), not null
19 # messages_from_user_id_idx (from_user_id)
20 # messages_to_user_id_idx (to_user_id)
24 # messages_from_user_id_fkey (from_user_id => users.id)
25 # messages_to_user_id_fkey (to_user_id => users.id)
28 class Message < ApplicationRecord
29 belongs_to :sender, :class_name => "User", :foreign_key => :from_user_id
30 belongs_to :recipient, :class_name => "User", :foreign_key => :to_user_id
32 validates :title, :presence => true, :utf8 => true, :length => 1..255
33 validates :body, :sent_on, :presence => true
34 validates :title, :body, :characters => true
36 scope :muted, -> { where(:muted => true) }
38 before_create :set_muted
40 def self.from_mail(mail, from, to)
43 body = mail.text_part.decoded
45 body = HTMLEntities.new.decode(Sanitize.clean(mail.html_part.decoded))
47 elsif mail.text? && mail.sub_type == "html"
48 body = HTMLEntities.new.decode(Sanitize.clean(mail.decoded))
56 :sent_on => mail.date.new_offset(0),
57 :title => mail.subject.sub(/\[OpenStreetMap\] */, ""),
59 :body_format => "text"
64 RichText.new(self[:body_format], self[:body])
67 def notification_token
68 sha256 = Digest::SHA256.new
69 sha256 << Rails.application.key_generator.generate_key("openstreetmap/message")
71 Base64.urlsafe_encode64(sha256.digest)[0, 8]
79 update(:muted => false)
85 self.muted ||= UserMute.active?(:owner => recipient, :subject => sender)