2016-02-26 14:42:16 +01:00
|
|
|
require "base32"
|
|
|
|
|
2019-01-13 07:05:54 +01:00
|
|
|
class Message < ApplicationRecord
|
2009-01-06 11:49:19 +01:00
|
|
|
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"
|
2019-10-29 01:19:05 +01:00
|
|
|
has_many :message_recipients, dependent: :destroy
|
|
|
|
has_many :recipients, through: :message_recipients, source: :user
|
2009-01-06 11:49:19 +01:00
|
|
|
|
2017-08-19 17:02:25 +02:00
|
|
|
attr_accessor :send_method, :recipient_tokens, :order_id
|
2015-02-26 00:11:39 +01:00
|
|
|
|
2016-02-18 11:00:03 +01:00
|
|
|
scope :threads, -> { where(:reply_to => nil) }
|
|
|
|
scope :thread, -> (id) { where("id = ? OR reply_to = ?", id, id) }
|
2018-12-20 16:59:37 +01:00
|
|
|
scope :readable_for, -> (user) {
|
|
|
|
joins(:message_recipients).where('private = ? OR message_recipients.user_id = ?', false, user.try(&:id)).distinct
|
|
|
|
}
|
2009-01-15 18:26:37 +01:00
|
|
|
|
2019-10-29 01:19:05 +01:00
|
|
|
validates_presence_of :message_recipients, :subject, :body
|
2009-01-06 11:49:19 +01:00
|
|
|
validates_length_of :subject, :in => 1..255
|
2009-01-15 18:26:37 +01:00
|
|
|
|
2017-08-19 17:02:25 +02:00
|
|
|
after_initialize do
|
2019-10-30 12:02:00 +01:00
|
|
|
@recipients_ids ||= []
|
2017-08-19 17:02:25 +02:00
|
|
|
@send_method ||= 'recipients'
|
|
|
|
end
|
|
|
|
|
2016-02-26 14:42:16 +01:00
|
|
|
before_create :create_salt
|
2019-10-29 01:19:05 +01:00
|
|
|
before_validation :create_message_recipients, on: :create
|
|
|
|
|
|
|
|
def create_message_recipients
|
|
|
|
user_ids = @recipients_ids
|
|
|
|
user_ids += User.undeleted.pluck(:id) if send_method == 'all'
|
|
|
|
user_ids += Group.find(group_id).users.pluck(:id) unless group_id.blank?
|
|
|
|
user_ids += Order.find(order_id).users_ordered.pluck(:id) if send_method == 'order'
|
2009-01-15 18:26:37 +01:00
|
|
|
|
2019-10-29 01:19:05 +01:00
|
|
|
user_ids.uniq.each do |user_id|
|
|
|
|
recipient = MessageRecipient.new message: self, user_id: user_id
|
|
|
|
message_recipients << recipient
|
|
|
|
end
|
2009-01-15 18:26:37 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_recipients(users)
|
2019-10-29 01:19:05 +01:00
|
|
|
@recipients_ids += users
|
2009-01-15 18:26:37 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def group_id=(group_id)
|
2017-08-19 17:02:25 +02:00
|
|
|
group = Group.find(group_id) unless group_id.blank?
|
|
|
|
if group
|
|
|
|
@send_method = 'workgroup' if group.type == 'Workgroup'
|
|
|
|
@send_method = 'ordergroup' if group.type == 'Ordergroup'
|
|
|
|
@send_method = 'messagegroup' if group.type == 'Messagegroup'
|
|
|
|
end
|
2016-02-18 11:23:09 +01:00
|
|
|
super
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
2017-08-19 17:02:25 +02:00
|
|
|
def workgroup_id
|
|
|
|
group_id if send_method == 'workgroup'
|
|
|
|
end
|
|
|
|
|
|
|
|
def ordergroup_id
|
|
|
|
group_id if send_method == 'ordergroup'
|
|
|
|
end
|
|
|
|
|
|
|
|
def messagegroup_id
|
|
|
|
group_id if send_method == 'messagegroup'
|
|
|
|
end
|
|
|
|
|
|
|
|
def workgroup_id=(workgroup_id)
|
|
|
|
self.group_id = workgroup_id if send_method == 'workgroup'
|
|
|
|
end
|
|
|
|
|
|
|
|
def ordergroup_id=(ordergroup_id)
|
|
|
|
self.group_id = ordergroup_id if send_method == 'ordergroup'
|
|
|
|
end
|
|
|
|
|
|
|
|
def messagegroup_id=(messagegroup_id)
|
|
|
|
self.group_id = messagegroup_id if send_method == 'messagegroup'
|
|
|
|
end
|
|
|
|
|
2015-02-19 00:53:57 +01:00
|
|
|
def order_id=(order_id)
|
|
|
|
@order_id = order_id
|
2017-08-19 17:02:25 +02:00
|
|
|
@send_method ||= 'order'
|
2015-02-19 00:53:57 +01:00
|
|
|
end
|
|
|
|
|
2011-05-18 14:47:17 +02:00
|
|
|
def recipient_tokens=(ids)
|
|
|
|
@recipient_tokens = ids
|
2019-10-29 01:19:05 +01:00
|
|
|
@recipients_ids = ids.split(',').map(&:to_i)
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
2011-05-18 14:47:17 +02:00
|
|
|
def mail_to=(user_id)
|
2019-10-29 01:19:05 +01:00
|
|
|
@recipients_ids = [user_id]
|
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
|
|
|
|
|
2019-10-29 01:19:05 +01:00
|
|
|
def recipients_ids
|
|
|
|
@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
|
|
|
|
|
2013-03-24 01:36:50 +01:00
|
|
|
def is_readable_for?(user)
|
2019-10-29 01:19:05 +01:00
|
|
|
!private || sender == user || message_recipients.where(user: user).any?
|
2013-03-24 01:36:50 +01:00
|
|
|
end
|
2011-05-07 20:50:39 +02:00
|
|
|
|
2019-02-11 13:22:54 +01:00
|
|
|
def can_toggle_private?(user)
|
|
|
|
return true if sender == user
|
|
|
|
return false if private?
|
|
|
|
user.role_admin?
|
|
|
|
end
|
|
|
|
|
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
|