app/controllers/ahoy/messages_controller.rb in ahoy_email-0.1.2 vs app/controllers/ahoy/messages_controller.rb in ahoy_email-0.1.3
- old
+ new
@@ -5,20 +5,23 @@
def open
if @message and !@message.opened_at
@message.opened_at = Time.now
@message.save!
end
+ publish :open
send_data Base64.decode64("R0lGODlhAQABAPAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="), type: "image/gif", disposition: "inline"
end
def click
if @message and !@message.clicked_at
@message.clicked_at = Time.now
+ @message.opened_at ||= @message.clicked_at
@message.save!
end
url = params[:url]
signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha1"), AhoyEmail.secret_token, url)
+ publish :click, url: params[:url]
if secure_compare(params[:signature], signature)
redirect_to url
else
redirect_to main_app.root_url
end
@@ -26,9 +29,19 @@
protected
def set_message
@message = AhoyEmail.message_model.where(token: params[:id]).first
+ end
+
+ def publish(name, event = {})
+ AhoyEmail.subscribers.each do |subscriber|
+ if subscriber.respond_to?(name)
+ event[:message] = @message
+ event[:controller] = self
+ subscriber.send name, event
+ end
+ end
end
# from https://github.com/rails/rails/blob/master/activesupport/lib/active_support/message_verifier.rb
# constant-time comparison algorithm to prevent timing attacks
def secure_compare(a, b)