2012-07-04 03:48:01 +02:00
|
|
|
class Message < ActiveRecord::Base
|
|
|
|
belongs_to :recipient,
|
|
|
|
:class_name => "User",
|
|
|
|
:foreign_key => "recipient_user_id"
|
|
|
|
belongs_to :author,
|
|
|
|
:class_name => "User",
|
|
|
|
:foreign_key => "author_user_id"
|
|
|
|
|
|
|
|
validates_presence_of :recipient
|
|
|
|
|
|
|
|
attr_accessor :recipient_username
|
|
|
|
|
|
|
|
validates_length_of :subject, :in => 1..150
|
|
|
|
validates_length_of :body, :maximum => (64 * 1024)
|
2013-01-23 07:32:45 +01:00
|
|
|
|
2015-04-03 19:57:12 +02:00
|
|
|
scope :unread, -> { where(:has_been_read => false,
|
|
|
|
:deleted_by_recipient => false) }
|
|
|
|
|
2013-07-20 05:05:00 +02:00
|
|
|
before_validation :assign_short_id,
|
|
|
|
:on => :create
|
2014-01-09 03:40:03 +01:00
|
|
|
after_create :deliver_email_notifications
|
2012-07-04 03:48:01 +02:00
|
|
|
after_save :update_unread_counts
|
2013-01-24 21:06:10 +01:00
|
|
|
after_save :check_for_both_deleted
|
2012-07-04 03:48:01 +02:00
|
|
|
|
2013-01-23 07:32:45 +01:00
|
|
|
def assign_short_id
|
|
|
|
self.short_id = ShortId.new(self.class).generate
|
2012-07-04 03:48:01 +02:00
|
|
|
end
|
|
|
|
|
2015-10-15 17:01:42 +02:00
|
|
|
def author_username
|
|
|
|
if self.author
|
|
|
|
self.author.username
|
|
|
|
else
|
|
|
|
"System"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-04 03:48:01 +02:00
|
|
|
def check_for_both_deleted
|
2013-01-24 21:06:10 +01:00
|
|
|
if self.deleted_by_author? && self.deleted_by_recipient?
|
2012-07-04 03:48:01 +02:00
|
|
|
self.destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_unread_counts
|
|
|
|
self.recipient.update_unread_message_count!
|
|
|
|
end
|
2013-01-23 07:32:45 +01:00
|
|
|
|
2014-01-09 03:40:03 +01:00
|
|
|
def deliver_email_notifications
|
2015-04-03 19:57:12 +02:00
|
|
|
return if Rails.env.development?
|
|
|
|
|
2014-01-09 03:40:03 +01:00
|
|
|
if self.recipient.email_messages?
|
|
|
|
begin
|
2012-07-04 03:48:01 +02:00
|
|
|
EmailMessage.notify(self, self.recipient).deliver
|
2014-01-09 03:40:03 +01:00
|
|
|
rescue => e
|
|
|
|
Rails.logger.error "error e-mailing #{self.recipient.email}: #{e}"
|
2012-07-04 03:48:01 +02:00
|
|
|
end
|
2014-01-09 03:40:03 +01:00
|
|
|
end
|
2012-07-04 03:48:01 +02:00
|
|
|
|
2014-01-21 08:05:27 +01:00
|
|
|
if self.recipient.pushover_messages?
|
|
|
|
self.recipient.pushover!({
|
|
|
|
:title => "#{Rails.application.name} message from " <<
|
2015-10-15 17:01:42 +02:00
|
|
|
"#{self.author_username}: #{self.subject}",
|
2014-01-21 08:05:27 +01:00
|
|
|
:message => self.plaintext_body,
|
|
|
|
:url => self.url,
|
2015-10-15 17:01:42 +02:00
|
|
|
:url_title => (self.author ? "Reply to #{self.author_username}" :
|
|
|
|
"View message"),
|
2014-01-21 08:05:27 +01:00
|
|
|
})
|
2012-07-04 03:48:01 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def recipient_username=(username)
|
|
|
|
self.recipient_user_id = nil
|
|
|
|
|
2013-12-24 04:20:06 +01:00
|
|
|
if u = User.where(:username => username).first
|
2012-07-04 03:48:01 +02:00
|
|
|
self.recipient_user_id = u.id
|
|
|
|
@recipient_username = username
|
|
|
|
else
|
|
|
|
errors.add(:recipient_username, "is not a valid user")
|
|
|
|
end
|
|
|
|
end
|
2012-11-08 04:58:10 +01:00
|
|
|
|
2012-07-04 03:48:01 +02:00
|
|
|
def linkified_body
|
2012-07-07 00:52:32 +02:00
|
|
|
Markdowner.to_html(self.body)
|
2012-07-04 03:48:01 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def plaintext_body
|
|
|
|
# TODO: linkify then strip tags and convert entities back
|
|
|
|
self.body.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def url
|
2015-01-03 00:02:55 +01:00
|
|
|
Rails.application.root_url + "messages/#{self.short_id}"
|
2012-07-04 03:48:01 +02:00
|
|
|
end
|
|
|
|
end
|