evendis / mandrill-rails

Webhook processing and event decoration to make using Mandrill with Rails just that much easier
MIT License
288 stars 36 forks source link

before_filter :authenticate_user!, only: [:show] #9

Closed ACPK closed 10 years ago

ACPK commented 10 years ago

If a user visits, http://mysite.com/inbox, they will get redirected to the signup page. Is that causing the webhook to break as I can't get it working?

class InboxController < ApplicationController before_filter :authenticate_user!, only: [:show] include Mandrill::Rails::WebHookProcessor def handle_inbound(event_payload)

ACPK commented 10 years ago

I temporarily commented out the line, and now get the following message in my logs:

(0.7ms) BEGIN 2013-12-03 01:53:05.420179+00:00 app web.1 - - Ticket Load (1.0ms) SELECT "tickets".* FROM "tickets" WHERE "tickets"."id" = 0 LIMIT 1 2013-12-03 01:53:05.421232+00:00 app web.1 - - (0.7ms) ROLLBACK 2013-12-03 01:53:05.421795+00:00 app web.1 - - Completed 500 Internal Server Error in 6ms 2013-12-03 01:53:05.424868+00:00 app web.1 - - 2013-12-03 01:53:05.424868+00:00 app web.1 - - NoMethodError (undefined method contact' for nil:NilClass): 2013-12-03 01:53:05.424868+00:00 app web.1 - - app/models/ticket_reply.rb:20:increate_from_to' 2013-12-03 01:53:05.424868+00:00 app web.1 - - app/controllers/inbox_controller.rb:22:in `handle_inbound'

tardate commented 10 years ago

@ACPK .. seems to indicate there's something unsafe being done within the application-specific handle_inbound processing. That's not something that Mandrill::Rails knows how to deal with - and it explicitly lets it 'blow up' so you know there's a problem.

If you can paste the code that is failing, may be able to comment further if it has something to do with extracting details from the Mandrill event payload

ACPK commented 10 years ago

@tardate Thank you for the help Paul!

Here's a copy of my app/models/ticket_reply.rb file

class TicketReply < ActiveRecord::Base attr_accessible :ticket_id,:email, :from_email, :from_name, :headers, :html, :sender, :subject, :tags, :text, :to, :notification_status, :message belongs_to :ticket

before_save :create_message before_save :create_from_to

def create_message h = self.html.split('>').first if h.scan("
").empty? f = h else f = h.split('
').first[15..-1] end

self.message = f

end

def create_from_to if self.from_email == self.ticket.contact.email fm = self.ticket.business.callred_email fn = self.ticket.business.name else fm = self.ticket.user.callred_email fn = self.ticket.user.name end

if self.to == self.ticket.business.callred_email
  tm = self.ticket.business.email
  tn = self.ticket.business.name
else
  tm = self.ticket.user.email
  tn = self.ticket.user.name
end

self.from_mail = fm
self.from_name = fn
self.to_mail = tm
self.to_name = tn

end

end

ACPK commented 10 years ago

@tardate An AWESOME dev from Thoughtbot said that event_payload is returning the wrong value.

When he had me hardcode :ticket_id => 1 on the 17th line in the inbox controller, the system is able to accept the incoming email and make the reply.

Hence, the event_payload is returning the wrong ticket_id so any advice on that would be greatly appreciated.

Git of my inbox_controller: https://gist.github.com/ACPK/80d86e73ff1ed7447f16 Gist of my ticket_reply: https://gist.github.com/ACPK/a01d9cc609ad58689b1b

tardate commented 10 years ago

@ACPK thanks for posting the gists. Nothing like code to show what the issue is.

Actually, I'd say it's not that event_payload is returning the wrong value, but the couple of lines in the InboxController that attempt to extract the @ticket_id from the email are failing, and so a TicketReply is created with a nil ticket_id. This then causes TicketReply#create_from_to to fail because it assumes a valid ticket has been assigned [where the NoMethodError (undefined method contact' for nil:NilClass) exception comes from]

A couple of suggestions:

tardate commented 10 years ago

@ACPK I'm going to close the issue because it is looking like a problem in the application code itself, not mandrill-rails. But feel free to continue to discuss here or off-list.