2009-01-06 11:49:19 +01:00
|
|
|
class Message < ActiveRecord::Base
|
|
|
|
belongs_to :sender, :class_name => "User", :foreign_key => "sender_id"
|
|
|
|
|
2009-01-15 18:26:37 +01:00
|
|
|
serialize :recipients_ids, Array
|
|
|
|
attr_accessor :sent_to_all, :group_id, :recipients_nicks
|
2009-01-06 11:49:19 +01:00
|
|
|
|
2009-01-15 18:26:37 +01:00
|
|
|
named_scope :pending, :conditions => { :email_state => 0 }
|
|
|
|
named_scope :sent, :conditions => { :email_state => 1 }
|
2009-03-17 18:28:09 +01:00
|
|
|
named_scope :public, :conditions => {:private => false}
|
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
|
|
|
|
|
|
|
|
|
|
|
# clean up the recipients_ids
|
2009-01-06 11:49:19 +01:00
|
|
|
def before_validation_on_create
|
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?
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
2009-01-15 18:26:37 +01:00
|
|
|
def recipients_nicks=(nicks)
|
|
|
|
@recipients_nicks = nicks
|
|
|
|
add_recipients nicks.split(",").collect { |nick| User.find_by_nick(nick) }
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
2009-01-15 18:26:37 +01:00
|
|
|
def recipient=(user)
|
|
|
|
@recipients_nicks = user.nick
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns true if this message is a system message, i.e. was sent automatically by the FoodSoft itself.
|
|
|
|
def system_message?
|
|
|
|
self.sender_id.nil?
|
2009-01-15 18:26:37 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def sender_name
|
2009-09-05 21:08:32 +02:00
|
|
|
system_message? ? 'Foodsoft' : sender.nick rescue "??"
|
2009-01-15 18:26:37 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def recipients
|
|
|
|
User.find(recipients_ids)
|
2013-02-17 22:04:08 +01:00
|
|
|
rescue ActiveRecord::RecordNotFound => error
|
|
|
|
logger.warn "#{Foodsoft.env}: #{error.message}"
|
|
|
|
User.find(recipients_ids.select { |id| User.exists?(id) }.uniq)
|
2009-01-15 18:26:37 +01:00
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
|
|
|
|
# Sends all pending messages that are to be send as emails.
|
|
|
|
def self.send_emails
|
2009-01-15 18:26:37 +01:00
|
|
|
messages = Message.pending
|
2013-02-24 19:15:54 +01:00
|
|
|
# Set all messages not pending, to avoid sending mails twice with different background processes
|
|
|
|
messages.each { |m| m.update_attribute(:email_state, 2) }
|
|
|
|
|
2009-01-15 18:26:37 +01:00
|
|
|
for message in messages
|
|
|
|
for recipient in message.recipients
|
2009-03-06 20:08:11 +01:00
|
|
|
if recipient.settings['messages.sendAsEmail'] == "1" && !recipient.email.blank?
|
2009-03-09 13:02:43 +01:00
|
|
|
begin
|
|
|
|
Mailer.deliver_message(message, recipient)
|
|
|
|
rescue
|
|
|
|
logger.warn "Deliver failed for #{recipient.nick}: #{recipient.email}"
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
end
|
2013-02-24 19:15:54 +01:00
|
|
|
message.update_attribute(:email_state, 1) # Set email state to 'sent'
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-05-07 20:50:39 +02:00
|
|
|
|
2011-05-07 21:55:24 +02:00
|
|
|
|
2011-05-07 20:50:39 +02:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: messages
|
|
|
|
#
|
2011-05-07 21:55:24 +02:00
|
|
|
# id :integer(4) not null, primary key
|
|
|
|
# sender_id :integer(4)
|
2011-05-07 20:50:39 +02:00
|
|
|
# recipients_ids :text
|
|
|
|
# subject :string(255) not null
|
|
|
|
# body :text
|
2011-05-07 21:55:24 +02:00
|
|
|
# email_state :integer(4) default(0), not null
|
|
|
|
# private :boolean(1) default(FALSE)
|
2011-05-07 20:50:39 +02:00
|
|
|
# created_at :datetime
|
|
|
|
#
|
|
|
|
|