make notifications always work

This commit is contained in:
Connor Turland 2016-12-13 02:42:33 -05:00
parent 87cb620d1b
commit 999414fffc
16 changed files with 103 additions and 42 deletions

View file

@ -20,9 +20,7 @@ class AccessController < ApplicationController
# POST maps/:id/access_request # POST maps/:id/access_request
def access_request def access_request
request = AccessRequest.create(user: current_user, map: @map) request = AccessRequest.create(user: current_user, map: @map)
# what about push notification to map owner? NotificationService.access_request(request)
mail = MapMailer.access_request_email(request, @map)
@map.user.notify(mail.subject, 'access request', request, true, MAILBOXER_CODE_ACCESS_REQUEST)
respond_to do |format| respond_to do |format|
format.json { head :ok } format.json { head :ok }
@ -35,12 +33,9 @@ class AccessController < ApplicationController
@map.add_new_collaborators(user_ids).each do |user_id| @map.add_new_collaborators(user_ids).each do |user_id|
# add_new_collaborators returns array of added users, # 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) user = User.find(user_id)
mail = MapMailer.invite_to_edit_email(@map, current_user, user) NotificationService.invite_to_edit(@map, current_user, user)
user.notify(mail.subject, 'invite to edit',
UserMap.find_by(user: user, map: @map),
true, MAILBOXER_CODE_INVITED_TO_EDIT)
end end
@map.remove_old_collaborators(user_ids) @map.remove_old_collaborators(user_ids)

View file

@ -11,8 +11,11 @@ class ApplicationMailer < ActionMailer::Base
def mail_for_notification(notification) def mail_for_notification(notification)
if notification.notification_code == MAILBOXER_CODE_ACCESS_REQUEST if notification.notification_code == MAILBOXER_CODE_ACCESS_REQUEST
request = notification.notified_object request = notification.notified_object
MapMailer.access_request_email(request, request.map) MapMailer.access_request_email(request)
elsif notification.notification_code == MAILBOXER_CODE_INVITED_TO_EDIT 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 user_map = notification.notified_object
MapMailer.invite_to_edit_email(user_map.map, user_map.map.user, user_map.user) MapMailer.invite_to_edit_email(user_map.map, user_map.map.user, user_map.user)
end end

View file

@ -2,17 +2,21 @@
class MapMailer < ApplicationMailer class MapMailer < ApplicationMailer
default from: 'team@metamaps.cc' default from: 'team@metamaps.cc'
def access_request_email(request, map) def access_request_email(request)
@request = request @request = request
@map = map @map = request.map
subject = @map.name + ' - request to edit' mail(to: @map.user.email, subject: request.requested_text)
mail(to: @map.user.email, subject: subject) end
def access_approved_email(request)
@request = request
@map = request.map
mail(to: request.user, subject: request.approved_text)
end end
def invite_to_edit_email(map, inviter, invitee) def invite_to_edit_email(map, inviter, invitee)
@inviter = inviter @inviter = inviter
@map = map @map = map
subject = @map.name + ' - invitation to edit' mail(to: invitee.email, subject: map.invited_text)
mail(to: invitee.email, subject: subject)
end end
end end

View file

@ -12,8 +12,7 @@ class AccessRequest < ApplicationRecord
end end
user_map = UserMap.create(user: user, map: map) user_map = UserMap.create(user: user, map: map)
mail = MapMailer.invite_to_edit_email(map, map.user, user) NotificationService.access_approved(self)
user.notify(mail.subject, 'invite to edit', user_map, true, MAILBOXER_CODE_INVITED_TO_EDIT)
end end
def deny def deny
@ -25,4 +24,12 @@ class AccessRequest < ApplicationRecord
Mailboxer::Receipt.where(notification: notification).update_all(is_read: true) Mailboxer::Receipt.where(notification: notification).update_all(is_read: true)
end end
end end
def requested_text
self.map.name + ' - request to edit'
end
def approved_text
self.map.name + ' - access approved'
end
end end

View file

@ -123,4 +123,8 @@ class Map < ApplicationRecord
Topic.where(defer_to_map_id: id).update_all(permission: permission) Topic.where(defer_to_map_id: id).update_all(permission: permission)
Synapse.where(defer_to_map_id: id).update_all(permission: permission) Synapse.where(defer_to_map_id: id).update_all(permission: permission)
end end
def invited_text
self.name + ' - invited to edit'
end
end end

View file

@ -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

View file

@ -1,10 +1,6 @@
<!DOCTYPE html> <!DOCTYPE html>
<% mail = ApplicationMailer.mail_for_notification(@notification) %>
<div style="padding: 16px; background: white; text-align: left;"> <div style="padding: 16px; background: white; text-align: left;">
<% if mail %> <%= raw @notification.body %>
<% @notification.update(body: mail.html_part&.body&.decoded) %>
<%= raw mail.html_part&.body&.decoded %>
<% end %>
<p style="font-size: 12px;">Make sense with Metamaps</p> <p style="font-size: 12px;">Make sense with Metamaps</p>
<%= render partial: 'shared/mailer_unsubscribe_link' %> <%= render partial: 'shared/mailer_unsubscribe_link' %>
</div> </div>

View file

@ -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" %>
<p><span style="font-weight: bold;"><%= map.user.name %></span> has responded to your access request and invited you to <span style="font-weight: bold">collaboratively edit</span> the following map:</p>
<p><%= link_to map.name, map_url(map), style: "font-size: 18px; text-decoration: none; color: #4fc059;" %></p>
<% if map.desc %>
<p style="font-size: 12px;"><%= map.desc %></p>
<% end %>
<%= link_to 'Go to Map', map_url(map), style: button_style %>

View file

@ -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) %>]

View file

@ -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" %> <% 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" %>
<p><span style="font-weight: bold;"><%= @request.user.name %></span> is requesting access to <span style="font-weight: bold">collaboratively edit</span> the following map:</p> <p><span style="font-weight: bold;"><%= request.user.name %></span> is requesting access to <span style="font-weight: bold">collaboratively edit</span> the following map:</p>
<p><%= @map.name %></p> <p><%= map.name %></p>
<p><%= link_to "Allow", approve_access_map_url(id: @map.id, request_id: @request.id), style: "font-size: 18px; text-decoration: none; color: #4fc059;" %> <p><%= link_to "Allow", approve_access_map_url(id: map.id, request_id: request.id), style: "font-size: 18px; text-decoration: none; color: #4fc059;" %>
<p><%= link_to "Decline", deny_access_map_url(id: @map.id, request_id: @request.id), style: "font-size: 18px; text-decoration: none; color: #DB5D5D;" %></p> <p><%= link_to "Decline", deny_access_map_url(id: map.id, request_id: request.id), style: "font-size: 18px; text-decoration: none; color: #DB5D5D;" %></p>
<%= link_to 'Go to Map', map_url(@map), style: button_style %> <%= link_to 'Go to Map', map_url(map), style: button_style %>

View file

@ -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) %>] Allow [<%= approve_access_map_url(id: map.id, request_id: request.id) %>]
Decline [<%= deny_access_map_url(id: @map.id, request_id: @request.id) %>] Decline [<%= deny_access_map_url(id: map.id, request_id: request.id) %>]

View file

@ -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" %> <% 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" %>
<p><span style="font-weight: bold;"><%= @inviter.name %></span> has invited you to <span style="font-weight: bold">collaboratively edit</span> the following map:</p> <p><span style="font-weight: bold;"><%= inviter.name %></span> has invited you to <span style="font-weight: bold">collaboratively edit</span> the following map:</p>
<p><%= link_to @map.name, map_url(@map), style: "font-size: 18px; text-decoration: none; color: #4fc059;" %></p> <p><%= link_to map.name, map_url(map), style: "font-size: 18px; text-decoration: none; color: #4fc059;" %></p>
<% if @map.desc %> <% if map.desc %>
<p style="font-size: 12px;"><%= @map.desc %></p> <p style="font-size: 12px;"><%= map.desc %></p>
<% end %> <% end %>
<%= link_to 'Go to Map', map_url(@map), style: button_style %> <%= link_to 'Go to Map', map_url(map), style: button_style %>

View file

@ -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) %>]

View file

@ -8,7 +8,8 @@
# }, # },
# which would imply that this is an access request to Map.find(1) # which would imply that this is an access request to Map.find(1)
MAILBOXER_CODE_ACCESS_REQUEST = 'ACCESS_REQUEST' 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| Mailboxer.setup do |config|
# Configures if your application uses or not email sending for Notifications and Messages # Configures if your application uses or not email sending for Notifications and Messages

View file

@ -264,7 +264,7 @@ const InfoBox = {
var mapperIds = DataModel.Collaborators.models.map(function(mapper) { return mapper.id }) var mapperIds = DataModel.Collaborators.models.map(function(mapper) { return mapper.id })
$.post('/maps/' + Active.Map.id + '/access', { access: mapperIds }) $.post('/maps/' + Active.Map.id + '/access', { access: mapperIds })
var name = DataModel.Collaborators.get(newCollaboratorId).get('name') var name = DataModel.Collaborators.get(newCollaboratorId).get('name')
GlobalUI.notifyUser(name + ' will be notified by email') GlobalUI.notifyUser(name + ' will be notified')
self.updateNumbers() self.updateNumbers()
} }

View file

@ -7,6 +7,11 @@ class MapMailerPreview < ActionMailer::Preview
def access_request_email def access_request_email
request = AccessRequest.first 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
end end