diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 828f6551..32e8f77e 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -19,10 +19,8 @@ class ApplicationMailer < ActionMailer::Base when MAILBOXER_CODE_INVITE_TO_EDIT user_map = notification.notified_object MapMailer.invite_to_edit(user_map) - when MAILBOXER_CODE_MAP_MESSAGE - when MAILBOXER_CODE_MAP_STARRED - when MAILBOXER_CODE_TOPIC_ADDED_TO_MAP - when MAILBOXER_CODE_TOPIC_CONNECTED + when TOPIC_ADDED_TO_MAP + when TOPIC_CONNECTED end end end diff --git a/app/mailers/map_mailer.rb b/app/mailers/map_mailer.rb index 581d6223..f6f36d95 100644 --- a/app/mailers/map_mailer.rb +++ b/app/mailers/map_mailer.rb @@ -2,21 +2,38 @@ class MapMailer < ApplicationMailer default from: 'team@metamaps.cc' - def access_request(request) - @request = request - @map = request.map - mail(to: @map.user.email, subject: NotificationService.get_subject_for_event(@map, 'access_request', request)) +# when 'map_message' # disabled , but event is a Message +# entity.name + ' - received a chat message' +# when 'map_starred' # event is a Star +# entity.name + ' was starred by ' + event.user.name + + def access_approved_subject(map) + map.name + ' - access approved' end def access_approved(request) @request = request @map = request.map - mail(to: request.user, subject: NotificationService.get_subject_for_event(@map, 'access_approved', request)) + mail(to: request.user, subject: access_approved_subject(@map)) + end + + def access_request_subject(map) + map.name + ' - request to edit' + end + + def access_request(request) + @request = request + @map = request.map + mail(to: @map.user.email, subject: access_request_subject(@map)) + end + + def invite_to_edit_subject(map) + map.name + ' - invited to edit' end def invite_to_edit(user_map) @inviter = user_map.map.user @map = user_map.map - mail(to: user_map.user.email, subject: NotificationService.get_subject_for_event(@map, 'invite_to_edit', user_map)) + mail(to: user_map.user.email, subject: invite_to_edit_subject(@map)) end end diff --git a/app/models/events/topic_added_to_map.rb b/app/models/events/topic_added_to_map.rb index 5fc31d81..c9259ca8 100644 --- a/app/models/events/topic_added_to_map.rb +++ b/app/models/events/topic_added_to_map.rb @@ -13,8 +13,8 @@ module Events def notify_users! # in the future, notify followers of both the topic, and the map - NotificationService.notify_followers(eventable, 'topic_added_to_map', self) - # NotificationService.notify_followers(map, 'topic_added_to_map', self) + NotificationService.notify_followers(eventable, TOPIC_ADDED_TO_MAP, self) + # NotificationService.notify_followers(map, MAP_RECEIVED_TOPIC, self) end handle_asynchronously :notify_users! end diff --git a/app/models/synapse.rb b/app/models/synapse.rb index 1df3d183..5fe95fe4 100644 --- a/app/models/synapse.rb +++ b/app/models/synapse.rb @@ -76,8 +76,8 @@ class Synapse < ApplicationRecord end def after_created_async - follow_ids = NotificationService.notify_followers(topic1, 'topic_connected', self) - NotificationService.notify_followers(topic2, 'topic_connected', self, nil, follow_ids) + follow_ids = NotificationService.notify_followers(topic1, TOPIC_CONNECTED, self) + NotificationService.notify_followers(topic2, TOPIC_CONNECTED, self, nil, follow_ids) end handle_asynchronously :after_created_async diff --git a/app/services/notification_service.rb b/app/services/notification_service.rb index d0dcc76c..763c8a97 100644 --- a/app/services/notification_service.rb +++ b/app/services/notification_service.rb @@ -10,86 +10,24 @@ class NotificationService ) end - def self.get_template_for_event_type(event_type) - 'map_mailer/' + event_type - end - - def self.get_mailboxer_code_for_event_type(event_type) - case event_type - when 'access_approved' - MAILBOXER_CODE_ACCESS_APPROVED - when 'access_request' - MAILBOXER_CODE_ACCESS_REQUEST - when 'invite_to_edit' - MAILBOXER_CODE_INVITE_TO_EDIT - - when 'map_activity' - MAILBOXER_CODE_MAP_ACTIVITY - when 'map_collaborator_added' - MAILBOXER_CODE_MAP_COLLABORATOR_ADDED - when 'map_updated' - MAILBOXER_CODE_MAP_UPDATED - when 'map_message' - MAILBOXER_CODE_MAP_MESSAGE - when 'map_starred' - MAILBOXER_CODE_MAP_STARRED - - when 'topic_added_to_map' - MAILBOXER_CODE_TOPIC_ADDED_TO_MAP - when 'topic_connected' - MAILBOXER_CODE_TOPIC_CONNECTED - when 'topic_deleted' - MAILBOXER_CODE_TOPIC_DELETED - when 'topic_disconnected' - MAILBOXER_CODE_TOPIC_DISCONNECTED - when 'topic_updated' - MAILBOXER_CODE_TOPIC_UPDATED - end - end - def self.get_subject_for_event(entity, event_type, event) case event_type - when 'access_approved' # event is an AccessRequest - entity.name + ' - access approved' - when 'access_request' # event is an AccessRequest - entity.name + ' - request to edit' - when 'invite_to_edit' # event is a UserMap - entity.name + ' - invited to edit' - - when 'map_activity' #disabled - 'placeholder' - when 'map_collaborator_added' #disabled - 'placeholder' - when 'map_updated' #disabled - 'placeholder' - when 'map_message' # disabled , but event is a Message - entity.name + ' - received a chat message' - when 'map_starred' # event is a Star - entity.name + ' was starred by ' + event.user.name - - when 'topic_added_to_map' # event is an Event::TopicAddedToMap + when TOPIC_ADDED_TO_MAP # event is an Event::TopicAddedToMap entity.name + ' was added to map ' + event.map.name - when 'topic_connected' # event is a Synapse + when TOPIC_CONNECTED # event is a Synapse 'new synapse to topic ' + entity.name - when 'topic_deleted' #disabled - 'placeholder' - when 'topic_disconnected' #disabled - 'placeholder' - when 'topic_updated' #disabled - 'placeholder' end end def self.send_for_follows(follows, entity, event_type, event) return if follows.length == 0 - template = get_template_for_event_type(event_type) - mailboxer_code = get_mailboxer_code_for_event_type(event_type) + template = 'map_mailer/' + event_type.downcase subject = get_subject_for_event(entity, event_type, event) # we'll prbly want to put the body into the actual loop so we can pass the current user in as a local body = renderer.render(template: template, locals: { entity: entity, event: event }, layout: false) follows.each{|follow| # this handles email and in-app notifications, in the future, include push - receipt = follow.user.notify(subject, body, event, false, mailboxer_code, (follow.user.emails_allowed && follow.email), event.user) + follow.user.notify(subject, body, event, false, event_type, (follow.user.emails_allowed && follow.email), event.user) # push could be handled with Actioncable to send transient notifications to the UI # the receipt from the notify call could be used to link to the full notification } @@ -113,30 +51,21 @@ class NotificationService end def self.access_request(request) - event_type = 'access_request' - template = get_template_for_event_type(event_type) - mailboxer_code = get_mailboxer_code_for_event_type(event_type) - subject = get_subject_for_event(request.map, event_type, request) - body = renderer.render(template: template, locals: { map: request.map, request: request }, layout: false) - request.map.user.notify(subject, body, request, false, mailboxer_code, true, request.user) + subject = MapMailer.access_request_subject(request.map) + body = renderer.render(template: 'map_mailer/access_request', locals: { map: request.map, request: request }, layout: false) + request.map.user.notify(subject, body, request, false, MAILBOXER_CODE_ACCESS_REQUEST, true, request.user) end def self.access_approved(request) - event_type = 'access_approved' - template = get_template_for_event_type(event_type) - mailboxer_code = get_mailboxer_code_for_event_type(event_type) - subject = get_subject_for_event(request.map, event_type, request) - body = renderer.render(template: template, locals: { map: request.map }, layout: false) - request.user.notify(subject, body, request, false, mailboxer_code, true, request.map.user) + subject = MapMailer.access_approved_subject(request.map) + body = renderer.render(template: 'map_mailer/access_approved', locals: { map: request.map }, layout: false) + request.user.notify(subject, body, request, false, MAILBOXER_CODE_ACCESS_APPROVED, true, request.map.user) end def self.invite_to_edit(user_map) - event_type = 'invite_to_edit' - template = get_template_for_event_type(event_type) - mailboxer_code = get_mailboxer_code_for_event_type(event_type) - subject = get_subject_for_event(user_map.map, event_type, user_map) - body = renderer.render(template: template, locals: { map: user_map.map, inviter: user_map.map.user }, layout: false) - user_map.user.notify(subject, body, user_map, false, mailboxer_code, true, user_map.map.user) + subject = MapMailer.invite_to_edit_subject(user_map.map) + body = renderer.render(template: 'map_mailer/invite_to_edit', locals: { map: user_map.map, inviter: user_map.map.user }, layout: false) + user_map.user.notify(subject, body, user_map, false, MAILBOXER_CODE_INVITE_TO_EDIT, true, user_map.map.user) end # note: this is a global function, probably called from the rails console with some html body diff --git a/app/views/map_mailer/map_collaborator_added.html.erb b/app/views/map_mailer/map_collaborator_added.html.erb deleted file mode 100644 index e69de29b..00000000 diff --git a/app/views/map_mailer/map_collaborator_added.text.erb b/app/views/map_mailer/map_collaborator_added.text.erb deleted file mode 100644 index e69de29b..00000000 diff --git a/app/views/map_mailer/map_message.html.erb b/app/views/map_mailer/map_message.html.erb deleted file mode 100644 index 621f39f8..00000000 --- a/app/views/map_mailer/map_message.html.erb +++ /dev/null @@ -1,5 +0,0 @@ -<% 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" %> -

<%= event.user.name %> added a chat message to map <%= entity.name %>

-

<%= event.message %>

- -<%= link_to 'Go to Map', map_url(entity), style: button_style %> \ No newline at end of file diff --git a/app/views/map_mailer/map_message.text.erb b/app/views/map_mailer/map_message.text.erb deleted file mode 100644 index e69de29b..00000000 diff --git a/app/views/map_mailer/map_starred.html.erb b/app/views/map_mailer/map_starred.html.erb deleted file mode 100644 index ac051536..00000000 --- a/app/views/map_mailer/map_starred.html.erb +++ /dev/null @@ -1,3 +0,0 @@ -<% 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" %> -

<%= event.user.name %> starred map <%= entity.name %>

-<%= link_to 'Go to Map', map_url(entity), style: button_style %> \ No newline at end of file diff --git a/app/views/map_mailer/map_starred.text.erb b/app/views/map_mailer/map_starred.text.erb deleted file mode 100644 index e69de29b..00000000 diff --git a/app/views/map_mailer/map_updated.html.erb b/app/views/map_mailer/map_updated.html.erb deleted file mode 100644 index e69de29b..00000000 diff --git a/app/views/map_mailer/map_updated.text.erb b/app/views/map_mailer/map_updated.text.erb deleted file mode 100644 index e69de29b..00000000 diff --git a/app/views/map_mailer/topic_deleted.html.erb b/app/views/map_mailer/topic_deleted.html.erb deleted file mode 100644 index e69de29b..00000000 diff --git a/app/views/map_mailer/topic_deleted.text.erb b/app/views/map_mailer/topic_deleted.text.erb deleted file mode 100644 index e69de29b..00000000 diff --git a/app/views/map_mailer/topic_disconnected.html.erb b/app/views/map_mailer/topic_disconnected.html.erb deleted file mode 100644 index e69de29b..00000000 diff --git a/app/views/map_mailer/topic_disconnected.text.erb b/app/views/map_mailer/topic_disconnected.text.erb deleted file mode 100644 index e69de29b..00000000 diff --git a/app/views/map_mailer/topic_updated.html.erb b/app/views/map_mailer/topic_updated.html.erb deleted file mode 100644 index e69de29b..00000000 diff --git a/app/views/map_mailer/topic_updated.text.erb b/app/views/map_mailer/topic_updated.text.erb deleted file mode 100644 index e69de29b..00000000 diff --git a/config/initializers/mailboxer.rb b/config/initializers/mailboxer.rb index 8409132a..4a4bd287 100644 --- a/config/initializers/mailboxer.rb +++ b/config/initializers/mailboxer.rb @@ -10,27 +10,32 @@ MAILBOXER_CODE_MESSAGE_FROM_DEVS = 'MESSAGE_FROM_DEVS' # these ones are old and can't change without a migration -MAILBOXER_CODE_ACCESS_APPROVED = 'ACCESS_APPROVED' -MAILBOXER_CODE_ACCESS_REQUEST = 'ACCESS_REQUEST' -MAILBOXER_CODE_INVITE_TO_EDIT = 'INVITE_TO_EDIT' +MAILBOXER_CODE_ACCESS_APPROVED = 'ACCESS_APPROVED' +MAILBOXER_CODE_ACCESS_REQUEST = 'ACCESS_REQUEST' +MAILBOXER_CODE_INVITE_TO_EDIT = 'INVITE_TO_EDIT' # these ones are new -# this one's a catch all for edits ON the map -MAILBOXER_CODE_MAP_ACTIVITY = 'MAP_ACTIVITY' -# MAILBOXER_CODE_MAP_ADDED_TO_MAP = 'MAP_ADDED_TO_MAP' -MAILBOXER_CODE_MAP_COLLABORATOR_ADDED = 'MAP_COLLABORATOR_ADDED' -# BOTHER WITH MAP_COLLABORATOR_REMOVED ? -MAILBOXER_CODE_MAP_UPDATED = 'MAP_UPDATED' # this refers to the map object itself: name, desc, permission, etc -# ADD MAP_FORKED -MAILBOXER_CODE_MAP_MESSAGE = 'MAP_MESSAGE' -MAILBOXER_CODE_MAP_STARRED = 'MAP_STARRED' +# this one's a catch all for occurences on the map +MAP_ACTIVITY = 'MAP_ACTIVITY' +# MAP_RECEIVED_TOPIC +# MAP_LOST_TOPIC +# MAP_TOPIC_MOVED +# MAP_RECEIVED_SYNAPSE +# MAP_LOST_SYNAPSE +# MAP_COLLABORATOR_ADDED +# MAP_UPDATED +# ADD_MAP_FORKED +# MAP_ADDED_TO_MAP +# MAP_MESSAGE +# MAP_STARRED +# MAP_COLLABORATOR_REMOVED -MAILBOXER_CODE_TOPIC_ADDED_TO_MAP = 'TOPIC_ADDED_TO_MAP' -MAILBOXER_CODE_TOPIC_CONNECTED = 'TOPIC_CONNECTED' -MAILBOXER_CODE_TOPIC_DELETED = 'TOPIC_DELETED' -MAILBOXER_CODE_TOPIC_DISCONNECTED = 'TOPIC_DISCONNECTED' -MAILBOXER_CODE_TOPIC_UPDATED = 'TOPIC_UPDATED' -# BOTHER WITH TOPIC_REMOVED_FROM_MAP ? +TOPIC_ADDED_TO_MAP = 'TOPIC_ADDED_TO_MAP' +TOPIC_CONNECTED = 'TOPIC_CONNECTED' +# TOPIC_DELETED +# TOPIC_DISCONNECTED +# TOPIC_UPDATED +# TOPIC_REMOVED_FROM_MAP Mailboxer.setup do |config| # Configures if your application uses or not email sending for Notifications and Messages