9ece6666bf
4-byte utf8 chars like emoji are passed around in ruby fine, but when they are put into mysql queries, strings get truncated at the first mb4 character. to prevent truncation, strip out mb4 characters in most user-controlled fields like comments, story descriptions and titles, and messages. to properly support utf8mb4, mysql server 5.5 is needed, the table encodings need to be changed to utf8mb4, and the mysql2 gem needs to be upgraded once it supports utf8mb4: https://github.com/brianmario/mysql2/issues/249
103 lines
2.4 KiB
Ruby
103 lines
2.4 KiB
Ruby
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
|
|
validates_presence_of :author
|
|
|
|
attr_accessor :recipient_username
|
|
|
|
attr_accessible :recipient_username, :subject, :body
|
|
|
|
validates_length_of :subject, :in => 1..150
|
|
validates_length_of :body, :maximum => (64 * 1024)
|
|
|
|
before_create :assign_short_id
|
|
after_create :deliver_reply_notifications
|
|
after_save :check_for_both_deleted
|
|
after_save :update_unread_counts
|
|
|
|
def assign_short_id
|
|
10.times do |try|
|
|
if try == 10
|
|
raise "too many hash collisions"
|
|
end
|
|
|
|
self.short_id = Utils.random_str(6)
|
|
|
|
if !Message.find_by_short_id(self.short_id)
|
|
break
|
|
end
|
|
end
|
|
end
|
|
|
|
def check_for_both_deleted
|
|
if self.deleted_by_author && self.deleted_by_recipient
|
|
self.destroy
|
|
end
|
|
end
|
|
|
|
def update_unread_counts
|
|
self.recipient.update_unread_message_count!
|
|
end
|
|
|
|
def deliver_reply_notifications
|
|
begin
|
|
if self.recipient.email_messages?
|
|
EmailMessage.notify(self, self.recipient).deliver
|
|
end
|
|
|
|
if self.recipient.pushover_messages? &&
|
|
self.recipient.pushover_user_key.present?
|
|
Pushover.push(self.recipient.pushover_user_key,
|
|
self.recipient.pushover_device, {
|
|
:title => "Lobsters message from #{self.author.username}: " <<
|
|
"#{self.subject}",
|
|
:message => self.plaintext_body,
|
|
:url => self.url,
|
|
:url_title => "Reply to #{self.author.username}",
|
|
})
|
|
end
|
|
rescue
|
|
end
|
|
end
|
|
|
|
def recipient_username=(username)
|
|
self.recipient_user_id = nil
|
|
|
|
if u = User.find_by_username(username)
|
|
self.recipient_user_id = u.id
|
|
@recipient_username = username
|
|
else
|
|
errors.add(:recipient_username, "is not a valid user")
|
|
end
|
|
end
|
|
|
|
# TODO: remove remove_mb4 hack
|
|
def body=(b)
|
|
self[:body] = b.to_s.remove_mb4
|
|
end
|
|
|
|
# TODO: remove remove_mb4 hack
|
|
def subject=(s)
|
|
self[:subject] = s.to_s.remove_mb4
|
|
end
|
|
|
|
def linkified_body
|
|
Markdowner.to_html(self.body)
|
|
end
|
|
|
|
def plaintext_body
|
|
# TODO: linkify then strip tags and convert entities back
|
|
self.body.to_s
|
|
end
|
|
|
|
def url
|
|
Rails.application.routes.url_helpers.root_url + "messages/#{self.short_id}"
|
|
end
|
|
end
|