From 999414fffc4dec51bbaa3c8ee1f4daff0ed94080 Mon Sep 17 00:00:00 2001 From: Connor Turland Date: Tue, 13 Dec 2016 02:42:33 -0500 Subject: [PATCH] make notifications always work --- app/controllers/access_controller.rb | 11 +++----- app/mailers/application_mailer.rb | 7 +++-- app/mailers/map_mailer.rb | 16 +++++++----- app/models/access_request.rb | 11 ++++++-- app/models/map.rb | 4 +++ app/services/notification_service.rb | 26 +++++++++++++++++++ .../new_notification_email.html.erb | 6 +---- .../map_mailer/access_approved_email.html.erb | 8 ++++++ .../map_mailer/access_approved_email.text.erb | 4 +++ .../map_mailer/access_request_email.html.erb | 12 +++++---- .../map_mailer/access_request_email.text.erb | 10 ++++--- .../map_mailer/invite_to_edit_email.html.erb | 12 +++++---- .../map_mailer/invite_to_edit_email.text.erb | 6 +++-- config/initializers/mailboxer.rb | 3 ++- frontend/src/Metamaps/Map/InfoBox.js | 2 +- spec/mailers/previews/map_mailer_preview.rb | 7 ++++- 16 files changed, 103 insertions(+), 42 deletions(-) create mode 100644 app/services/notification_service.rb create mode 100644 app/views/map_mailer/access_approved_email.html.erb create mode 100644 app/views/map_mailer/access_approved_email.text.erb diff --git a/app/controllers/access_controller.rb b/app/controllers/access_controller.rb index 5b6dea6e..a83fd128 100644 --- a/app/controllers/access_controller.rb +++ b/app/controllers/access_controller.rb @@ -20,9 +20,7 @@ class AccessController < ApplicationController # POST maps/:id/access_request def access_request 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, 'access request', request, true, MAILBOXER_CODE_ACCESS_REQUEST) + NotificationService.access_request(request) respond_to do |format| format.json { head :ok } @@ -35,12 +33,9 @@ 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 + # who we then send a notification to user = User.find(user_id) - mail = MapMailer.invite_to_edit_email(@map, current_user, user) - user.notify(mail.subject, 'invite to edit', - UserMap.find_by(user: user, map: @map), - true, MAILBOXER_CODE_INVITED_TO_EDIT) + NotificationService.invite_to_edit(@map, current_user, user) end @map.remove_old_collaborators(user_ids) diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 338f38ee..ebffb2df 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -11,8 +11,11 @@ class ApplicationMailer < ActionMailer::Base 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 + MapMailer.access_request_email(request) + elsif notification.notification_code == MAILBOXER_CODE_ACCESS_APPROVED + request = notification.notified_object + MapMailer.access_approved_email(request) + elsif notification.notification_code == MAILBOXER_CODE_INVITE_TO_EDIT user_map = notification.notified_object MapMailer.invite_to_edit_email(user_map.map, user_map.map.user, user_map.user) end diff --git a/app/mailers/map_mailer.rb b/app/mailers/map_mailer.rb index f6865ecd..bf0cec7b 100644 --- a/app/mailers/map_mailer.rb +++ b/app/mailers/map_mailer.rb @@ -2,17 +2,21 @@ class MapMailer < ApplicationMailer default from: 'team@metamaps.cc' - def access_request_email(request, map) + def access_request_email(request) @request = request - @map = map - subject = @map.name + ' - request to edit' - mail(to: @map.user.email, subject: subject) + @map = request.map + mail(to: @map.user.email, subject: request.requested_text) + end + + def access_approved_email(request) + @request = request + @map = request.map + mail(to: request.user, subject: request.approved_text) end def invite_to_edit_email(map, inviter, invitee) @inviter = inviter @map = map - subject = @map.name + ' - invitation to edit' - mail(to: invitee.email, subject: subject) + mail(to: invitee.email, subject: map.invited_text) end end diff --git a/app/models/access_request.rb b/app/models/access_request.rb index c433f7cc..e5416fff 100644 --- a/app/models/access_request.rb +++ b/app/models/access_request.rb @@ -12,8 +12,7 @@ class AccessRequest < ApplicationRecord end user_map = UserMap.create(user: user, map: map) - mail = MapMailer.invite_to_edit_email(map, map.user, user) - user.notify(mail.subject, 'invite to edit', user_map, true, MAILBOXER_CODE_INVITED_TO_EDIT) + NotificationService.access_approved(self) end def deny @@ -25,4 +24,12 @@ class AccessRequest < ApplicationRecord Mailboxer::Receipt.where(notification: notification).update_all(is_read: true) end end + + def requested_text + self.map.name + ' - request to edit' + end + + def approved_text + self.map.name + ' - access approved' + end end diff --git a/app/models/map.rb b/app/models/map.rb index 36b2d284..5744b856 100644 --- a/app/models/map.rb +++ b/app/models/map.rb @@ -123,4 +123,8 @@ class Map < ApplicationRecord Topic.where(defer_to_map_id: id).update_all(permission: permission) Synapse.where(defer_to_map_id: id).update_all(permission: permission) end + + def invited_text + self.name + ' - invited to edit' + end end diff --git a/app/services/notification_service.rb b/app/services/notification_service.rb new file mode 100644 index 00000000..05a268f4 --- /dev/null +++ b/app/services/notification_service.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true +class NotificationService + + def self.renderer + renderer ||= ApplicationController.renderer.new( + http_host: ENV['MAILER_DEFAULT_URL'], + https: Rails.env.production? ? true : false + ) + end + + def self.access_request(request) + body = renderer.render(template: 'map_mailer/access_request_email', locals: { map: request.map, request: request }, layout: false) + request.map.user.notify(request.requested_text, body, request, false, MAILBOXER_CODE_ACCESS_REQUEST) + end + + def self.access_approved(request) + body = renderer.render(template: 'map_mailer/access_approved_email', locals: { map: request.map }, layout: false) + receipt = request.user.notify(request.approved_text, body, request, false, MAILBOXER_CODE_ACCESS_APPROVED) + end + + def self.invite_to_edit(map, inviter, invited) + user_map = UserMap.find_by(user: invited, map: map) + body = renderer.render(template: 'map_mailer/invite_to_edit_email', locals: { map: map, inviter: inviter }, layout: false) + invited.notify(map.invited_text, body, user_map, false, MAILBOXER_CODE_INVITE_TO_EDIT) + 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 7f680869..ad90419f 100644 --- a/app/views/mailboxer/notification_mailer/new_notification_email.html.erb +++ b/app/views/mailboxer/notification_mailer/new_notification_email.html.erb @@ -1,10 +1,6 @@ -<% mail = ApplicationMailer.mail_for_notification(@notification) %>
- <% if mail %> - <% @notification.update(body: mail.html_part&.body&.decoded) %> - <%= raw mail.html_part&.body&.decoded %> - <% end %> + <%= raw @notification.body %>

Make sense with Metamaps

<%= render partial: 'shared/mailer_unsubscribe_link' %>
diff --git a/app/views/map_mailer/access_approved_email.html.erb b/app/views/map_mailer/access_approved_email.html.erb new file mode 100644 index 00000000..91fd77e0 --- /dev/null +++ b/app/views/map_mailer/access_approved_email.html.erb @@ -0,0 +1,8 @@ +<% map = @map || map %> +<% button_style = "background-color:#4fc059;border-radius:2px;color:white;display:inline-block;font-family:Roboto,Arial,Helvetica,sans-serif;font-size:12px;font-weight:bold;min-height:29px;line-height:29px;min-width:54px;outline:0px;padding:0 8px;text-align:center;text-decoration:none" %> +

<%= map.user.name %> has responded to your access request and invited you to collaboratively edit the following map:

+

<%= link_to map.name, map_url(map), style: "font-size: 18px; text-decoration: none; color: #4fc059;" %>

+<% if map.desc %> +

<%= map.desc %>

+<% end %> +<%= link_to 'Go to Map', map_url(map), style: button_style %> diff --git a/app/views/map_mailer/access_approved_email.text.erb b/app/views/map_mailer/access_approved_email.text.erb new file mode 100644 index 00000000..2a8e54f6 --- /dev/null +++ b/app/views/map_mailer/access_approved_email.text.erb @@ -0,0 +1,4 @@ +<% map = @map || map %> +<%= map.user.name %> has responded to your access request and invited you to collaboratively edit the following map: + +<%= map.name %> [<%= map_url(map) %>] diff --git a/app/views/map_mailer/access_request_email.html.erb b/app/views/map_mailer/access_request_email.html.erb index 74d666bd..759b97eb 100644 --- a/app/views/map_mailer/access_request_email.html.erb +++ b/app/views/map_mailer/access_request_email.html.erb @@ -1,6 +1,8 @@ +<% map = @map || map %> +<% request = @request || request %> <% button_style = "background-color:#4fc059;border-radius:2px;color:white;display:inline-block;font-family:Roboto,Arial,Helvetica,sans-serif;font-size:12px;font-weight:bold;min-height:29px;line-height:29px;min-width:54px;outline:0px;padding:0 8px;text-align:center;text-decoration:none" %> -

<%= @request.user.name %> is requesting access to collaboratively edit the following map:

-

<%= @map.name %>

-

<%= link_to "Allow", approve_access_map_url(id: @map.id, request_id: @request.id), style: "font-size: 18px; text-decoration: none; color: #4fc059;" %> -

<%= link_to "Decline", deny_access_map_url(id: @map.id, request_id: @request.id), style: "font-size: 18px; text-decoration: none; color: #DB5D5D;" %>

-<%= link_to 'Go to Map', map_url(@map), style: button_style %> +

<%= request.user.name %> is requesting access to collaboratively edit the following map:

+

<%= map.name %>

+

<%= link_to "Allow", approve_access_map_url(id: map.id, request_id: request.id), style: "font-size: 18px; text-decoration: none; color: #4fc059;" %> +

<%= link_to "Decline", deny_access_map_url(id: map.id, request_id: request.id), style: "font-size: 18px; text-decoration: none; color: #DB5D5D;" %>

+<%= link_to 'Go to Map', map_url(map), style: button_style %> diff --git a/app/views/map_mailer/access_request_email.text.erb b/app/views/map_mailer/access_request_email.text.erb index ef302a8b..c99aa6e6 100644 --- a/app/views/map_mailer/access_request_email.text.erb +++ b/app/views/map_mailer/access_request_email.text.erb @@ -1,8 +1,10 @@ -<%= @request.user.name %> has requested to collaboratively edit the following map: +<% map = @map || map %> +<% request = @request || request %> +<%= request.user.name %> has requested to collaboratively edit the following map: -<%= @map.name %> [<%= map_url(@map) %>] +<%= map.name %> [<%= map_url(map) %>] -Allow [<%= approve_access_map_url(id: @map.id, request_id: @request.id) %>] -Decline [<%= deny_access_map_url(id: @map.id, request_id: @request.id) %>] +Allow [<%= approve_access_map_url(id: map.id, request_id: request.id) %>] +Decline [<%= deny_access_map_url(id: map.id, request_id: request.id) %>] diff --git a/app/views/map_mailer/invite_to_edit_email.html.erb b/app/views/map_mailer/invite_to_edit_email.html.erb index aba7cfd4..f08cc377 100644 --- a/app/views/map_mailer/invite_to_edit_email.html.erb +++ b/app/views/map_mailer/invite_to_edit_email.html.erb @@ -1,7 +1,9 @@ +<% map = @map || map %> +<% inviter = @inviter || inviter %> <% button_style = "background-color:#4fc059;border-radius:2px;color:white;display:inline-block;font-family:Roboto,Arial,Helvetica,sans-serif;font-size:12px;font-weight:bold;min-height:29px;line-height:29px;min-width:54px;outline:0px;padding:0 8px;text-align:center;text-decoration:none" %> -

<%= @inviter.name %> has invited you to collaboratively edit the following map:

-

<%= link_to @map.name, map_url(@map), style: "font-size: 18px; text-decoration: none; color: #4fc059;" %>

-<% if @map.desc %> -

<%= @map.desc %>

+

<%= inviter.name %> has invited you to collaboratively edit the following map:

+

<%= link_to map.name, map_url(map), style: "font-size: 18px; text-decoration: none; color: #4fc059;" %>

+<% if map.desc %> +

<%= map.desc %>

<% end %> -<%= link_to 'Go to Map', map_url(@map), style: button_style %> +<%= link_to 'Go to Map', map_url(map), style: button_style %> diff --git a/app/views/map_mailer/invite_to_edit_email.text.erb b/app/views/map_mailer/invite_to_edit_email.text.erb index 4e822842..b58cced9 100644 --- a/app/views/map_mailer/invite_to_edit_email.text.erb +++ b/app/views/map_mailer/invite_to_edit_email.text.erb @@ -1,3 +1,5 @@ -<%= @inviter.name %> has invited you to collaboratively edit the following map: +<% map = @map || map %> +<% inviter = @inviter || inviter %> +<%= inviter.name %> has invited you to collaboratively edit the following map: -<%= @map.name %> [<%= map_url(@map) %>] +<%= map.name %> [<%= map_url(map) %>] diff --git a/config/initializers/mailboxer.rb b/config/initializers/mailboxer.rb index b937df92..49824f50 100644 --- a/config/initializers/mailboxer.rb +++ b/config/initializers/mailboxer.rb @@ -8,7 +8,8 @@ # }, # 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_CODE_ACCESS_APPROVED = 'ACCESS_APPROVED' +MAILBOXER_CODE_INVITE_TO_EDIT = 'INVITE_TO_EDIT' Mailboxer.setup do |config| # Configures if your application uses or not email sending for Notifications and Messages diff --git a/frontend/src/Metamaps/Map/InfoBox.js b/frontend/src/Metamaps/Map/InfoBox.js index 1b06daf5..5fced292 100644 --- a/frontend/src/Metamaps/Map/InfoBox.js +++ b/frontend/src/Metamaps/Map/InfoBox.js @@ -264,7 +264,7 @@ const InfoBox = { var mapperIds = DataModel.Collaborators.models.map(function(mapper) { return mapper.id }) $.post('/maps/' + Active.Map.id + '/access', { access: mapperIds }) var name = DataModel.Collaborators.get(newCollaboratorId).get('name') - GlobalUI.notifyUser(name + ' will be notified by email') + GlobalUI.notifyUser(name + ' will be notified') self.updateNumbers() } diff --git a/spec/mailers/previews/map_mailer_preview.rb b/spec/mailers/previews/map_mailer_preview.rb index 17ea7671..61e33eb8 100644 --- a/spec/mailers/previews/map_mailer_preview.rb +++ b/spec/mailers/previews/map_mailer_preview.rb @@ -7,6 +7,11 @@ class MapMailerPreview < ActionMailer::Preview def access_request_email request = AccessRequest.first - MapMailer.access_request_email(request, request.map) + MapMailer.access_request_email(request) + end + + def access_approved_email + request = AccessRequest.first + MapMailer.access_approved_email(request) end end