diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 32e8f77e..112b28ab 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -10,17 +10,24 @@ class ApplicationMailer < ActionMailer::Base class << self def mail_for_notification(notification) case notification.notification_code - when MAILBOXER_CODE_ACCESS_REQUEST + when MAP_ACCESS_REQUEST request = notification.notified_object MapMailer.access_request(request) - when MAILBOXER_CODE_ACCESS_APPROVED + when MAP_ACCESS_APPROVED request = notification.notified_object MapMailer.access_approved(request) - when MAILBOXER_CODE_INVITE_TO_EDIT + when MAP_INVITE_TO_EDIT user_map = notification.notified_object MapMailer.invite_to_edit(user_map) when TOPIC_ADDED_TO_MAP - when TOPIC_CONNECTED + event = notification.notified_object + TopicMailer.added_to_map(event, notification.recipients[0]) + when TOPIC_CONNECTED_1 + synapse = notification.notified_object + TopicMailer.connected(synapse, synapse.topic1, notification.recipients[0]) + when TOPIC_CONNECTED_2 + synapse = notification.notified_object + TopicMailer.connected(synapse, synapse.topic2, notification.recipients[0]) end end end diff --git a/app/mailers/map_mailer.rb b/app/mailers/map_mailer.rb index f6f36d95..4e9772bc 100644 --- a/app/mailers/map_mailer.rb +++ b/app/mailers/map_mailer.rb @@ -2,11 +2,6 @@ class MapMailer < ApplicationMailer default from: 'team@metamaps.cc' -# 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 @@ -14,7 +9,7 @@ class MapMailer < ApplicationMailer def access_approved(request) @request = request @map = request.map - mail(to: request.user, subject: access_approved_subject(@map)) + mail(to: request.user.email, subject: access_approved_subject(@map)) end def access_request_subject(map) diff --git a/app/mailers/topic_mailer.rb b/app/mailers/topic_mailer.rb new file mode 100644 index 00000000..9175d410 --- /dev/null +++ b/app/mailers/topic_mailer.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true +class TopicMailer < ApplicationMailer + default from: 'team@metamaps.cc' + + def added_to_map_subject(topic, map) + topic.name + ' was added to map ' + map.name + end + + def added_to_map(event, user) + @entity = event.eventable + @event = event + mail(to: user.email, subject: added_to_map_subject(@entity, event.map)) + end + + def connected_subject(topic) + 'new synapse to topic ' + topic.name + end + + def connected(synapse, topic, user) + @entity = topic + @event = synapse + mail(to: user.email, subject: connected_subject(topic)) + end +end diff --git a/app/models/synapse.rb b/app/models/synapse.rb index 5fe95fe4..e8378f0e 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_1, self) + NotificationService.notify_followers(topic2, TOPIC_CONNECTED_2, 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 763c8a97..c6997fb7 100644 --- a/app/services/notification_service.rb +++ b/app/services/notification_service.rb @@ -10,24 +10,30 @@ class NotificationService ) end - def self.get_subject_for_event(entity, event_type, event) + def self.get_settings_for_event(entity, event_type, event) case event_type - 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 - 'new synapse to topic ' + entity.name + when TOPIC_ADDED_TO_MAP + subject = TopicMailer.added_to_map_subject(entity, event.map) + template = 'topic_mailer/added_to_map' + when TOPIC_CONNECTED_1 + subject = TopicMailer.connected_subject(event.topic1) + template = 'topic_mailer/connected' + when TOPIC_CONNECTED_2 + TopicMailer.connected_subject(event.topic2) + template = 'topic_mailer/connected' end + + {template: template, subject: subject} end def self.send_for_follows(follows, entity, event_type, event) return if follows.length == 0 - template = 'map_mailer/' + event_type.downcase - subject = get_subject_for_event(entity, event_type, event) + settings = get_settings_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) + body = renderer.render(template: settings.template, locals: { entity: entity, event: event }, layout: false) follows.each{|follow| # this handles email and in-app notifications, in the future, include push - follow.user.notify(subject, body, event, false, event_type, (follow.user.emails_allowed && follow.email), event.user) + follow.user.notify(settings.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 } @@ -53,19 +59,19 @@ class NotificationService def self.access_request(request) 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) + request.map.user.notify(subject, body, request, false, MAP_ACCESS_REQUEST, true, request.user) end def self.access_approved(request) 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) + request.user.notify(subject, body, request, false, MAP_ACCESS_APPROVED, true, request.map.user) end def self.invite_to_edit(user_map) 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) + user_map.user.notify(subject, body, user_map, false, MAP_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 @@ -73,39 +79,9 @@ class NotificationService users = opts[:users] || User.all obj = opts[:obj] || nil sanitize_text = opts[:sanitize_text] || false - notification_code = opts[:notification_code] || MAILBOXER_CODE_MESSAGE_FROM_DEVS + notification_code = opts[:notification_code] || MESSAGE_FROM_DEVS send_mail = opts[:send_mail] || true sender = opts[:sender] || User.find_by(email: 'ishanshapiro@gmail.com') Mailboxer::Notification.notify_all(users, subject, body, obj, sanitize_text, notification_code, send_mail, sender) end - - def self.text_for_notification(notification) - case notification.notification_code - when MAILBOXER_CODE_ACCESS_APPROVED - map = notification.notified_object.map - 'granted your request to edit map ' + map.name + '' - when MAILBOXER_CODE_ACCESS_REQUEST - map = notification.notified_object.map - 'wants permission to map with you on ' + map.name + '
<%= event.user.name %> - added topic <%= entity.name %> + added topic <%= topic.name %> to map <%= event.map.name %>
-<%= link_to 'Go to Topic', topic_url(entity), style: button_style %> +<%= link_to 'Go to Topic', topic_url(topic), style: button_style %> <%= link_to 'Go to Map', map_url(event.map), style: button_style %> \ No newline at end of file diff --git a/app/views/topic_mailer/added_to_map.text.erb b/app/views/topic_mailer/added_to_map.text.erb new file mode 100644 index 00000000..183d1e8e --- /dev/null +++ b/app/views/topic_mailer/added_to_map.text.erb @@ -0,0 +1,6 @@ +<% topic = @entity || entity %> +<% event = @event || event %> +<%= event.user.name %> added topic <%= topic.name %> to map <%= event.map.name %> + +topic_url(topic) +map_url(event.map) \ No newline at end of file diff --git a/app/views/map_mailer/topic_connected.html.erb b/app/views/topic_mailer/connected.html.erb similarity index 58% rename from app/views/map_mailer/topic_connected.html.erb rename to app/views/topic_mailer/connected.html.erb index d190d878..bada609a 100644 --- a/app/views/map_mailer/topic_connected.html.erb +++ b/app/views/topic_mailer/connected.html.erb @@ -1,13 +1,13 @@ -<% 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" %> - -<% topic1 = entity %> -<% topic2 = (entity.id == event.topic1_id ? event.topic2 : event.topic1) %> +<% topic1 = @entity || entity + synapse = @event || event + 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" + topic2 = (topic1.id == synapse.topic1_id ? synapse.topic2 : synapse.topic1) %>- <%= event.user.name %> + <%= synapse.user.name %> connected topic <%= topic1.name %> to topic <%= topic2.name %> - <% if event.desc %> + <% if synapse.desc.length > 0 %> with the description "<%= event.desc %>". <% end %>
diff --git a/app/views/topic_mailer/connected.text.erb b/app/views/topic_mailer/connected.text.erb new file mode 100644 index 00000000..e173ae4d --- /dev/null +++ b/app/views/topic_mailer/connected.text.erb @@ -0,0 +1,8 @@ +<% topic1 = @entity || entity + synapse = @event || event + topic2 = (topic1.id == synapse.topic1_id ? synapse.topic2 : synapse.topic1) %> + +<%= synapse.user.name %> connected topic <%= topic1.name %> to topic <%= topic2.name %> +<%= synapse.desc.length > 0 ? ' with the description' + event.desc : '' %> + +<%= topic_url(topic1) %> \ No newline at end of file diff --git a/config/initializers/mailboxer.rb b/config/initializers/mailboxer.rb index 4a4bd287..baa5ea9d 100644 --- a/config/initializers/mailboxer.rb +++ b/config/initializers/mailboxer.rb @@ -7,12 +7,12 @@ # notification_code: MAILBOXER_CODE_ACCESS_REQUEST # }, # which would imply that this is an access request to Map.find(1) -MAILBOXER_CODE_MESSAGE_FROM_DEVS = 'MESSAGE_FROM_DEVS' +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' +MAP_ACCESS_APPROVED = 'ACCESS_APPROVED' +MAP_ACCESS_REQUEST = 'ACCESS_REQUEST' +MAP_INVITE_TO_EDIT = 'INVITE_TO_EDIT' # these ones are new # this one's a catch all for occurences on the map @@ -30,8 +30,9 @@ MAP_ACTIVITY = 'MAP_ACTIVITY' # MAP_STARRED # MAP_COLLABORATOR_REMOVED -TOPIC_ADDED_TO_MAP = 'TOPIC_ADDED_TO_MAP' -TOPIC_CONNECTED = 'TOPIC_CONNECTED' +TOPIC_ADDED_TO_MAP = 'TOPIC_ADDED_TO_MAP' +TOPIC_CONNECTED_1 = 'TOPIC_CONNECTED_1' +TOPIC_CONNECTED_2 = 'TOPIC_CONNECTED_2' # TOPIC_DELETED # TOPIC_DISCONNECTED # TOPIC_UPDATED