From 9debcdde39fb479af345ca3ebbfd0198b1d5395a Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Thu, 8 Dec 2016 14:39:41 -0500 Subject: [PATCH] Integrate rails mailers with mailboxer --- app/assets/stylesheets/notifications.scss.erb | 10 ++++++++-- app/controllers/access_controller.rb | 13 ++++--------- app/helpers/application_helper.rb | 11 +---------- app/mailers/application_mailer.rb | 12 ++++++++++++ app/views/layouts/_mobilemenu.html.erb | 4 ++-- app/views/layouts/_upperelements.html.erb | 6 +++--- .../new_notification_email.html.erb | 11 +++-------- .../new_notification_email.text.erb | 3 ++- app/views/notifications/show.html.erb | 12 ++++++++---- config/initializers/mailboxer.rb | 11 +++++++++++ frontend/src/components/NotificationIcon.js | 2 +- 11 files changed, 55 insertions(+), 40 deletions(-) diff --git a/app/assets/stylesheets/notifications.scss.erb b/app/assets/stylesheets/notifications.scss.erb index a866af04..d7942135 100644 --- a/app/assets/stylesheets/notifications.scss.erb +++ b/app/assets/stylesheets/notifications.scss.erb @@ -30,7 +30,7 @@ $unread_notifications_dot_size: 8px; margin-top: 1em + $menu_bar_height; font-family: 'din-regular', Sans-Serif; - & > .title { + & > .notification-title { border-bottom: 1px solid #eee; padding-bottom: 0.25em; margin-bottom: 0.5em; @@ -42,7 +42,7 @@ $unread_notifications_dot_size: 8px; } - .notification { + .notificationsPage .notification { .notification-subject { width: 25%; } @@ -68,4 +68,10 @@ $unread_notifications_dot_size: 8px; } } } + + .notificationPage .notification-body { + p, div { + margin: 1em auto; + } + } } diff --git a/app/controllers/access_controller.rb b/app/controllers/access_controller.rb index 5f19d23e..a287ca1b 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 @@ -38,16 +36,13 @@ class AccessController < ApplicationController @map.add_new_collaborators(user_ids).each do |user_id| # add_new_collaborators returns array of added users, # 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', UserMap.find_by(user_id: user_id, map: @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/helpers/application_helper.rb b/app/helpers/application_helper.rb index cc121cbe..45a5d565 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -38,18 +38,9 @@ module ApplicationHelper "#{request.base_url}/join" + (current_user ? "?code=#{current_user.code}" : '') end - def user_has_unread_notifications? - return @user_has_unread_notifications unless @user_has_unread_notifications.nil? - return (@user_has_unread_notifications = false) if current_user.nil? - current_user.mailboxer_notification_receipts.each do |receipt| - return (@user_has_unread_notifications = true) if receipt.is_read == false - end - @user_has_unread_notifications = false - end - def user_unread_notification_count return 0 if current_user.nil? - current_user.mailboxer_notification_receipts.reduce(0) do |total, receipt| + @user_unread_notification_count ||= current_user.mailboxer_notification_receipts.reduce(0) do |total, receipt| receipt.is_read ? total : total + 1 end end diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 10961836..338f38ee 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 + user_map = notification.notified_object + MapMailer.invite_to_edit_email(user_map.map, user_map.map.user, user_map.user) + end + end + end end diff --git a/app/views/layouts/_mobilemenu.html.erb b/app/views/layouts/_mobilemenu.html.erb index c557f516..5ef3a66d 100644 --- a/app/views/layouts/_mobilemenu.html.erb +++ b/app/views/layouts/_mobilemenu.html.erb @@ -3,7 +3,7 @@ <%= yield(:mobile_title) %> @@ -55,7 +55,7 @@
  • <%= link_to "Notifications", notifications_path %> - <% if user_has_unread_notifications? %> + <% if user_unread_notification_count > 0 %>
    <% end %>
  • diff --git a/app/views/layouts/_upperelements.html.erb b/app/views/layouts/_upperelements.html.erb index 515bfcd4..1f499615 100644 --- a/app/views/layouts/_upperelements.html.erb +++ b/app/views/layouts/_upperelements.html.erb @@ -76,11 +76,11 @@ <% if current_user.present? %> - <%= link_to notifications_path, class: "notificationsIcon upperRightEl upperRightIcon #{user_has_unread_notifications? ? 'unread' : 'read'}" do %> + <%= link_to notifications_path, class: "notificationsIcon upperRightEl upperRightIcon #{user_unread_notification_count > 0 ? 'unread' : 'read'}" do %>
    - Notifications + Notifications (<%= user_unread_notification_count %> unread)
    - <% if user_has_unread_notifications? %> + <% if user_unread_notification_count > 0 %>
    <% 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/app/views/notifications/show.html.erb b/app/views/notifications/show.html.erb index 1d61ccc3..1c555954 100644 --- a/app/views/notifications/show.html.erb +++ b/app/views/notifications/show.html.erb @@ -3,12 +3,16 @@
    -

    <%= @notification.subject %>

    - <%= raw @notification.body %> -
    - <%= link_to 'Back', notifications_path %> +

    <%= @notification.subject %>

    +
    + <%= raw @notification.body %>
    + +
    + <%= link_to 'Back', notifications_path %> +
    + <%= render partial: 'shared/back_to_mapping' %>
    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 diff --git a/frontend/src/components/NotificationIcon.js b/frontend/src/components/NotificationIcon.js index b886f557..98782a75 100644 --- a/frontend/src/components/NotificationIcon.js +++ b/frontend/src/components/NotificationIcon.js @@ -20,7 +20,7 @@ class NotificationIcon extends Component { return (
    - Notifications + Notifications ({this.props.unreadNotificationsCount} unread)
    {this.props.unreadNotificationsCount === 0 ? null : (