2016-02-26 14:42:16 +01:00
|
|
|
require "base32"
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
class Message < ActiveRecord::Base
|
|
|
|
belongs_to :sender, :class_name => "User", :foreign_key => "sender_id"
|
2015-02-26 00:11:39 +01:00
|
|
|
belongs_to :group, :class_name => "Group", :foreign_key => "group_id"
|
|
|
|
belongs_to :reply_to_message, :class_name => "Message", :foreign_key => "reply_to"
|
2009-01-06 11:49:19 +01:00
|
|
|
|
2009-01-15 18:26:37 +01:00
|
|
|
serialize :recipients_ids, Array
|
2015-02-26 00:11:39 +01:00
|
|
|
attr_accessor :sent_to_all, :recipient_tokens
|
|
|
|
|
2014-02-20 15:04:53 +01:00
|
|
|
scope :pending, -> { where(:email_state => 0) }
|
|
|
|
scope :sent, -> { where(:email_state => 1) }
|
2014-11-21 14:37:56 +01:00
|
|
|
scope :pub, -> { where(:private => false) }
|
2016-02-18 11:00:03 +01:00
|
|
|
scope :threads, -> { where(:reply_to => nil) }
|
|
|
|
scope :thread, -> (id) { where("id = ? OR reply_to = ?", id, id) }
|
2009-01-15 18:26:37 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Values for the email_state attribute: :none, :pending, :sent, :failed
|
|
|
|
EMAIL_STATE = {
|
2009-01-15 18:26:37 +01:00
|
|
|
:pending => 0,
|
|
|
|
:sent => 1,
|
|
|
|
:failed => 2
|
2009-01-06 11:49:19 +01:00
|
|
|
}
|
|
|
|
|
2009-01-15 18:26:37 +01:00
|
|
|
validates_presence_of :recipients_ids, :subject, :body
|
2009-01-06 11:49:19 +01:00
|
|
|
validates_length_of :subject, :in => 1..255
|
|
|
|
validates_inclusion_of :email_state, :in => EMAIL_STATE.values
|
2009-01-15 18:26:37 +01:00
|
|
|
|
2016-02-26 14:42:16 +01:00
|
|
|
before_create :create_salt
|
2011-05-11 11:17:02 +02:00
|
|
|
before_validation :clean_up_recipient_ids, :on => :create
|
2009-01-15 18:26:37 +01:00
|
|
|
|
2012-08-24 14:33:45 +02:00
|
|
|
def self.deliver(message_id)
|
|
|
|
find(message_id).deliver
|
|
|
|
end
|
|
|
|
|
2011-05-11 11:17:02 +02:00
|
|
|
def clean_up_recipient_ids
|
2015-02-26 00:11:39 +01:00
|
|
|
add_recipients Group.find(group_id).users unless group_id.blank?
|
2009-01-15 18:26:37 +01:00
|
|
|
self.recipients_ids = recipients_ids.uniq.reject { |id| id.blank? } unless recipients_ids.nil?
|
2009-03-06 19:38:06 +01:00
|
|
|
self.recipients_ids = User.all.collect(&:id) if sent_to_all == "1"
|
2009-01-15 18:26:37 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_recipients(users)
|
|
|
|
self.recipients_ids = [] if recipients_ids.blank?
|
|
|
|
self.recipients_ids += users.collect(&:id) unless users.blank?
|
|
|
|
end
|
|
|
|
|
|
|
|
def group_id=(group_id)
|
|
|
|
@group_id = group_id
|
|
|
|
add_recipients Group.find(group_id).users unless group_id.blank?
|
2016-02-18 11:23:09 +01:00
|
|
|
super
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
2015-02-19 00:53:57 +01:00
|
|
|
def order_id=(order_id)
|
|
|
|
@order_id = order_id
|
2015-04-10 18:30:12 +02:00
|
|
|
add_recipients Order.find(order_id).users_ordered unless order_id.blank?
|
2015-02-19 00:53:57 +01:00
|
|
|
end
|
|
|
|
|
2011-05-18 14:47:17 +02:00
|
|
|
def recipient_tokens=(ids)
|
|
|
|
@recipient_tokens = ids
|
|
|
|
add_recipients ids.split(",").collect { |id| User.find(id) }
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
2011-05-18 14:47:17 +02:00
|
|
|
def mail_to=(user_id)
|
|
|
|
user = User.find(user_id)
|
2012-08-17 19:08:02 +02:00
|
|
|
add_recipients([user])
|
2011-05-18 14:47:17 +02:00
|
|
|
end
|
|
|
|
|
2016-02-26 14:42:16 +01:00
|
|
|
def mail_hash_for_user(user)
|
|
|
|
digest = Digest::SHA1.new
|
|
|
|
digest.update self.id.to_s
|
|
|
|
digest.update ":"
|
|
|
|
digest.update salt
|
|
|
|
digest.update ":"
|
|
|
|
digest.update user.id.to_s
|
|
|
|
Base32.encode digest.digest
|
|
|
|
end
|
|
|
|
|
2014-06-18 13:28:19 +02:00
|
|
|
# Returns true if this message is a system message, i.e. was sent automatically by Foodsoft itself.
|
2016-02-26 14:42:16 +01:00
|
|
|
def system_message?
|
2009-01-06 11:49:19 +01:00
|
|
|
self.sender_id.nil?
|
2009-01-15 18:26:37 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def sender_name
|
2013-09-22 14:38:56 +02:00
|
|
|
system_message? ? I18n.t('layouts.foodsoft') : sender.display rescue "?"
|
2009-01-15 18:26:37 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def recipients
|
2015-06-07 19:05:54 +02:00
|
|
|
User.where(id: recipients_ids)
|
2009-01-15 18:26:37 +01:00
|
|
|
end
|
2015-06-07 19:05:54 +02:00
|
|
|
|
2016-02-18 11:00:03 +01:00
|
|
|
def last_reply
|
|
|
|
Message.where(reply_to: self.id).order(:created_at).last
|
|
|
|
end
|
|
|
|
|
2012-08-17 19:08:02 +02:00
|
|
|
def deliver
|
2012-08-24 11:11:40 +02:00
|
|
|
for user in recipients
|
|
|
|
if user.receive_email?
|
2012-08-17 19:08:02 +02:00
|
|
|
begin
|
2014-03-07 09:51:24 +01:00
|
|
|
MessagesMailer.foodsoft_message(self, user).deliver
|
2012-08-24 11:11:40 +02:00
|
|
|
rescue
|
2013-09-22 14:38:56 +02:00
|
|
|
Rails.logger.warn "Deliver failed for user \##{user.id}: #{user.email}"
|
2012-08-17 19:08:02 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
update_attribute(:email_state, 1)
|
|
|
|
end
|
2013-03-24 01:36:50 +01:00
|
|
|
|
|
|
|
def is_readable_for?(user)
|
|
|
|
!private || sender == user || recipients_ids.include?(user.id)
|
|
|
|
end
|
2011-05-07 20:50:39 +02:00
|
|
|
|
2016-02-26 14:42:16 +01:00
|
|
|
private
|
2011-05-07 21:55:24 +02:00
|
|
|
|
2016-02-26 14:42:16 +01:00
|
|
|
def create_salt
|
|
|
|
self.salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp
|
|
|
|
end
|
|
|
|
end
|