Improve the interface of FoodsoftMailReceiver

This commit is contained in:
Patrick Gansterer 2017-10-02 16:16:02 +02:00
parent e9bae618ed
commit e017a1196e
3 changed files with 24 additions and 19 deletions

View file

@ -4,11 +4,14 @@ class BounceMailReceiver
/bounce\+(?<local>.*)=(?<domain>[^=]+)/ /bounce\+(?<local>.*)=(?<domain>[^=]+)/
end end
def received(match, data) def initialize(match)
address = "#{match[:local]}@#{match[:domain]}" @address = "#{match[:local]}@#{match[:domain]}"
end
def received(data)
mail = Mail.new data mail = Mail.new data
subject = mail.subject || 'Unknown bounce error' subject = mail.subject || 'Unknown bounce error'
MailDeliveryStatus.create email: address, MailDeliveryStatus.create email: @address,
message: subject, message: subject,
attachment_mime: 'message/rfc822', attachment_mime: 'message/rfc822',
attachment_data: data attachment_data: data

View file

@ -17,7 +17,7 @@ class FoodsoftMailReceiver < MidiSmtpServer::Smtpd
@@registered_classes.each do |klass| @@registered_classes.each do |klass|
klass_m = klass.regexp.match(m[:address]) klass_m = klass.regexp.match(m[:address])
return klass.new.received klass_m, data if klass_m return klass.new(klass_m).received(data) if klass_m
end end
raise "invalid format for recipient" raise "invalid format for recipient"

View file

@ -6,16 +6,18 @@ class MessagesMailReceiver
/(?<message_id>\d+)\.(?<user_id>\d+)\.(?<hash>\w+)/ /(?<message_id>\d+)\.(?<user_id>\d+)\.(?<hash>\w+)/
end end
def received(match, data) def initialize(match)
original_message = Message.find_by_id(match[:message_id]) @message = Message.find_by_id(match[:message_id])
user = User.find_by_id(match[:user_id]) @user = User.find_by_id(match[:user_id])
raise "Message could not be found" if original_message.nil? raise "Message could not be found" if @message.nil?
raise "User could not be found" if user.nil? raise "User could not be found" if @user.nil?
hash = original_message.mail_hash_for_user user hash = @message.mail_hash_for_user(@user)
raise "Hash does not match expectations" unless hash.casecmp(match[:hash]) == 0 raise "Hash does not match expectations" unless hash.casecmp(match[:hash]) == 0
end
def received(data)
mail = Mail.new data mail = Mail.new data
mail_part = nil mail_part = nil
@ -41,18 +43,18 @@ class MessagesMailReceiver
body = EmailReplyTrimmer.trim(body) body = EmailReplyTrimmer.trim(body)
message = user.send_messages.new body: body, message = @user.send_messages.new body: body,
group: original_message.group, group: @message.group,
private: original_message.private, private: @message.private,
received_email: received_email, received_email: data,
subject: mail.subject.gsub("[#{FoodsoftConfig[:name]}] ", "") subject: mail.subject.gsub("[#{FoodsoftConfig[:name]}] ", "")
if original_message.reply_to if @message.reply_to
message.reply_to_message = original_message.reply_to_message message.reply_to_message = @message.reply_to_message
else else
message.reply_to_message = original_message message.reply_to_message = @message
end end
message.add_recipients original_message.recipients message.add_recipients @message.recipients
message.add_recipients [original_message.sender] message.add_recipients [@message.sender]
message.save! message.save!
Resque.enqueue(MessageNotifier, FoodsoftConfig.scope, "message_deliver", message.id) Resque.enqueue(MessageNotifier, FoodsoftConfig.scope, "message_deliver", message.id)