diff --git a/app/controllers/access_controller.rb b/app/controllers/access_controller.rb index 5f19d23e..12cff322 100644 --- a/app/controllers/access_controller.rb +++ b/app/controllers/access_controller.rb @@ -22,12 +22,10 @@ class AccessController < ApplicationController request = AccessRequest.create(user: current_user, map: @map) # what about push notification to map owner? mail = MapMailer.access_request_email(request, @map) - @map.user.notify(mail.subject, mail.body.parts[1].body.to_s) + @map.user.notify(mail.subject, 'access request', request, true, MAILBOXER_CODE_ACCESS_REQUEST) respond_to do |format| - format.json do - head :ok - end + format.json { head :ok } end end @@ -40,14 +38,12 @@ class AccessController < ApplicationController # who we then send an email to user = User.find(user_id) mail = MapMailer.invite_to_edit_email(@map, current_user, User.find(user_id)) - user.notify(mail.subject, mail.body.parts[1].body.to_s) + user.notify(mail.subject, 'invite to edit', @map, true, MAILBOXER_CODE_INVITED_TO_EDIT) end @map.remove_old_collaborators(user_ids) respond_to do |format| - format.json do - head :ok - end + format.json { head :ok } end end diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 10961836..30f44080 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -6,4 +6,16 @@ class ApplicationMailer < ActionMailer::Base def deliver raise NotImplementedError('Please use Mailboxer to send your emails.') end + + class << self + def mail_for_notification(notification) + if notification.notification_code == MAILBOXER_CODE_ACCESS_REQUEST + request = notification.notified_object + MapMailer.access_request_email(request, request.map) + elsif notification.notification_code == MAILBOXER_CODE_INVITED_TO_EDIT + map = notification.notified_object + MapMailer.invite_to_edit_email(map, map.user, 'TODO invited user') + end + end + end end diff --git a/app/views/mailboxer/notification_mailer/new_notification_email.html.erb b/app/views/mailboxer/notification_mailer/new_notification_email.html.erb index ac4af493..d8fda23c 100644 --- a/app/views/mailboxer/notification_mailer/new_notification_email.html.erb +++ b/app/views/mailboxer/notification_mailer/new_notification_email.html.erb @@ -1,9 +1,4 @@ - -
- - - - <%= raw @notification.body %> - - +<% mail = ApplicationMailer.mail_for_notification(@notification) %> +<% @notification.update(body: mail.html_part&.body&.decoded) %> +<%= raw mail.html_part&.body&.decoded %> diff --git a/app/views/mailboxer/notification_mailer/new_notification_email.text.erb b/app/views/mailboxer/notification_mailer/new_notification_email.text.erb index 1c230d08..45fa8ae0 100644 --- a/app/views/mailboxer/notification_mailer/new_notification_email.text.erb +++ b/app/views/mailboxer/notification_mailer/new_notification_email.text.erb @@ -1 +1,2 @@ -<%= @notification.body.html_safe? ? @notification.body : strip_tags(@notification.body) %> +<% mail = ApplicationMailer.mail_for_notification(@notification) %> +<%= mail.text_part&.body&.decoded %> diff --git a/config/initializers/mailboxer.rb b/config/initializers/mailboxer.rb index 9e1efe66..115d80a4 100644 --- a/config/initializers/mailboxer.rb +++ b/config/initializers/mailboxer.rb @@ -1,4 +1,15 @@ # frozen_string_literal: true + +# notification codes to differentiate different types of notifications +# e.g. a notification might have { +# notified_object_type: 'Map', +# notified_object_id: 1, +# notification_code: MAILBOXER_CODE_ACCESS_REQUEST +# }, +# which would imply that this is an access request to Map.find(1) +MAILBOXER_CODE_ACCESS_REQUEST = 'ACCESS_REQUEST' +MAILBOXER_CODE_INVITED_TO_EDIT = 'INVITED_TO_EDIT' + Mailboxer.setup do |config| # Configures if your application uses or not email sending for Notifications and Messages config.uses_emails = true