foodsoft/plugins/messages/app/models/message.rb
Patrick Gansterer 4e35e2d58e Add handling for message reply via email
If the reply_email_domain configuration is set the messages plugin will
use unique Reply-To addresses for every email. They contain enough
information to reconstruct the message context and a hash to avoid
user forgery and spam.
A mail transfer agent must be configured to call the new rake task
foodsoft:parse_reply_email for incoming mails. The rake task requires
the receipt of the mail in the RECIPIENT variable and the raw message
via standard input. An example invocation would look like:
rake foodsoft:parse_reply_email RECIPIENT=f.1.1.HASH < test.eml
2016-03-04 18:11:27 +01:00

116 lines
3.1 KiB
Ruby

require "base32"
class Message < ActiveRecord::Base
belongs_to :sender, :class_name => "User", :foreign_key => "sender_id"
belongs_to :group, :class_name => "Group", :foreign_key => "group_id"
belongs_to :reply_to_message, :class_name => "Message", :foreign_key => "reply_to"
serialize :recipients_ids, Array
attr_accessor :sent_to_all, :recipient_tokens
scope :pending, -> { where(:email_state => 0) }
scope :sent, -> { where(:email_state => 1) }
scope :pub, -> { where(:private => false) }
scope :threads, -> { where(:reply_to => nil) }
scope :thread, -> (id) { where("id = ? OR reply_to = ?", id, id) }
# Values for the email_state attribute: :none, :pending, :sent, :failed
EMAIL_STATE = {
:pending => 0,
:sent => 1,
:failed => 2
}
validates_presence_of :recipients_ids, :subject, :body
validates_length_of :subject, :in => 1..255
validates_inclusion_of :email_state, :in => EMAIL_STATE.values
before_create :create_salt
before_validation :clean_up_recipient_ids, :on => :create
def self.deliver(message_id)
find(message_id).deliver
end
def clean_up_recipient_ids
add_recipients Group.find(group_id).users unless group_id.blank?
self.recipients_ids = recipients_ids.uniq.reject { |id| id.blank? } unless recipients_ids.nil?
self.recipients_ids = User.all.collect(&:id) if sent_to_all == "1"
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?
super
end
def order_id=(order_id)
@order_id = order_id
add_recipients Order.find(order_id).users_ordered unless order_id.blank?
end
def recipient_tokens=(ids)
@recipient_tokens = ids
add_recipients ids.split(",").collect { |id| User.find(id) }
end
def mail_to=(user_id)
user = User.find(user_id)
add_recipients([user])
end
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
# Returns true if this message is a system message, i.e. was sent automatically by Foodsoft itself.
def system_message?
self.sender_id.nil?
end
def sender_name
system_message? ? I18n.t('layouts.foodsoft') : sender.display rescue "?"
end
def recipients
User.where(id: recipients_ids)
end
def last_reply
Message.where(reply_to: self.id).order(:created_at).last
end
def deliver
for user in recipients
if user.receive_email?
begin
MessagesMailer.foodsoft_message(self, user).deliver
rescue
Rails.logger.warn "Deliver failed for user \##{user.id}: #{user.email}"
end
end
end
update_attribute(:email_state, 1)
end
def is_readable_for?(user)
!private || sender == user || recipients_ids.include?(user.id)
end
private
def create_salt
self.salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp
end
end