refactor
This commit is contained in:
parent
5f97261641
commit
ce843c7ba0
15 changed files with 115 additions and 70 deletions
|
@ -10,17 +10,24 @@ class ApplicationMailer < ActionMailer::Base
|
||||||
class << self
|
class << self
|
||||||
def mail_for_notification(notification)
|
def mail_for_notification(notification)
|
||||||
case notification.notification_code
|
case notification.notification_code
|
||||||
when MAILBOXER_CODE_ACCESS_REQUEST
|
when MAP_ACCESS_REQUEST
|
||||||
request = notification.notified_object
|
request = notification.notified_object
|
||||||
MapMailer.access_request(request)
|
MapMailer.access_request(request)
|
||||||
when MAILBOXER_CODE_ACCESS_APPROVED
|
when MAP_ACCESS_APPROVED
|
||||||
request = notification.notified_object
|
request = notification.notified_object
|
||||||
MapMailer.access_approved(request)
|
MapMailer.access_approved(request)
|
||||||
when MAILBOXER_CODE_INVITE_TO_EDIT
|
when MAP_INVITE_TO_EDIT
|
||||||
user_map = notification.notified_object
|
user_map = notification.notified_object
|
||||||
MapMailer.invite_to_edit(user_map)
|
MapMailer.invite_to_edit(user_map)
|
||||||
when TOPIC_ADDED_TO_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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,11 +2,6 @@
|
||||||
class MapMailer < ApplicationMailer
|
class MapMailer < ApplicationMailer
|
||||||
default from: 'team@metamaps.cc'
|
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)
|
def access_approved_subject(map)
|
||||||
map.name + ' - access approved'
|
map.name + ' - access approved'
|
||||||
end
|
end
|
||||||
|
@ -14,7 +9,7 @@ class MapMailer < ApplicationMailer
|
||||||
def access_approved(request)
|
def access_approved(request)
|
||||||
@request = request
|
@request = request
|
||||||
@map = request.map
|
@map = request.map
|
||||||
mail(to: request.user, subject: access_approved_subject(@map))
|
mail(to: request.user.email, subject: access_approved_subject(@map))
|
||||||
end
|
end
|
||||||
|
|
||||||
def access_request_subject(map)
|
def access_request_subject(map)
|
||||||
|
|
24
app/mailers/topic_mailer.rb
Normal file
24
app/mailers/topic_mailer.rb
Normal file
|
@ -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
|
|
@ -76,8 +76,8 @@ class Synapse < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def after_created_async
|
def after_created_async
|
||||||
follow_ids = NotificationService.notify_followers(topic1, TOPIC_CONNECTED, self)
|
follow_ids = NotificationService.notify_followers(topic1, TOPIC_CONNECTED_1, self)
|
||||||
NotificationService.notify_followers(topic2, TOPIC_CONNECTED, self, nil, follow_ids)
|
NotificationService.notify_followers(topic2, TOPIC_CONNECTED_2, self, nil, follow_ids)
|
||||||
end
|
end
|
||||||
handle_asynchronously :after_created_async
|
handle_asynchronously :after_created_async
|
||||||
|
|
||||||
|
|
|
@ -10,24 +10,30 @@ class NotificationService
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.get_subject_for_event(entity, event_type, event)
|
def self.get_settings_for_event(entity, event_type, event)
|
||||||
case event_type
|
case event_type
|
||||||
when TOPIC_ADDED_TO_MAP # event is an Event::TopicAddedToMap
|
when TOPIC_ADDED_TO_MAP
|
||||||
entity.name + ' was added to map ' + event.map.name
|
subject = TopicMailer.added_to_map_subject(entity, event.map)
|
||||||
when TOPIC_CONNECTED # event is a Synapse
|
template = 'topic_mailer/added_to_map'
|
||||||
'new synapse to topic ' + entity.name
|
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
|
end
|
||||||
|
|
||||||
|
{template: template, subject: subject}
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.send_for_follows(follows, entity, event_type, event)
|
def self.send_for_follows(follows, entity, event_type, event)
|
||||||
return if follows.length == 0
|
return if follows.length == 0
|
||||||
template = 'map_mailer/' + event_type.downcase
|
settings = get_settings_for_event(entity, event_type, event)
|
||||||
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
|
# 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|
|
follows.each{|follow|
|
||||||
# this handles email and in-app notifications, in the future, include push
|
# 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
|
# 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
|
# 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)
|
def self.access_request(request)
|
||||||
subject = MapMailer.access_request_subject(request.map)
|
subject = MapMailer.access_request_subject(request.map)
|
||||||
body = renderer.render(template: 'map_mailer/access_request', locals: { map: request.map, request: request }, layout: false)
|
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
|
end
|
||||||
|
|
||||||
def self.access_approved(request)
|
def self.access_approved(request)
|
||||||
subject = MapMailer.access_approved_subject(request.map)
|
subject = MapMailer.access_approved_subject(request.map)
|
||||||
body = renderer.render(template: 'map_mailer/access_approved', locals: { map: request.map }, layout: false)
|
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
|
end
|
||||||
|
|
||||||
def self.invite_to_edit(user_map)
|
def self.invite_to_edit(user_map)
|
||||||
subject = MapMailer.invite_to_edit_subject(user_map.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)
|
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
|
end
|
||||||
|
|
||||||
# note: this is a global function, probably called from the rails console with some html body
|
# 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
|
users = opts[:users] || User.all
|
||||||
obj = opts[:obj] || nil
|
obj = opts[:obj] || nil
|
||||||
sanitize_text = opts[:sanitize_text] || false
|
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
|
send_mail = opts[:send_mail] || true
|
||||||
sender = opts[:sender] || User.find_by(email: 'ishanshapiro@gmail.com')
|
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)
|
Mailboxer::Notification.notify_all(users, subject, body, obj, sanitize_text, notification_code, send_mail, sender)
|
||||||
end
|
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 <span class="in-bold">' + map.name + '</span>'
|
|
||||||
when MAILBOXER_CODE_ACCESS_REQUEST
|
|
||||||
map = notification.notified_object.map
|
|
||||||
'wants permission to map with you on <span class="in-bold">' + map.name + '</span> <div class="action">Offer a response</div>'
|
|
||||||
when MAILBOXER_CODE_INVITE_TO_EDIT
|
|
||||||
map = notification.notified_object.map
|
|
||||||
'gave you edit access to map <span class="in-bold">' + map.name + '</span>'
|
|
||||||
when MAILBOXER_CODE_MAP_MESSAGE
|
|
||||||
map = notification.notified_object.resource
|
|
||||||
'commented on map <span class="in-bold">' + map.name + '</span>'
|
|
||||||
when MAILBOXER_CODE_MAP_STARRED
|
|
||||||
map = notification.notified_object.map
|
|
||||||
'starred map <span class="in-bold">' + map.name + '</span>'
|
|
||||||
when MAILBOXER_CODE_TOPIC_ADDED_TO_MAP
|
|
||||||
topic = notification.notified_object.eventable
|
|
||||||
map = notification.notified_object.map
|
|
||||||
'added topic <span class="in-bold">' + topic.name + '</span> to map <span class="in-bold">' + map.name + '</span>'
|
|
||||||
when MAILBOXER_CODE_TOPIC_CONNECTED
|
|
||||||
topic1 = notification.notified_object.topic1
|
|
||||||
topic2 = notification.notified_object.topic2
|
|
||||||
'connected <span class="in-bold">' + topic1.name + '</span> to <span class="in-bold">' + topic2.name + '</span>'
|
|
||||||
when MAILBOXER_CODE_MESSAGE_FROM_DEVS
|
|
||||||
notification.subject
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -18,7 +18,33 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="notification-body">
|
<div class="notification-body">
|
||||||
<div class="in-bold"><%= notification.sender.name %></div>
|
<div class="in-bold"><%= notification.sender.name %></div>
|
||||||
<%= raw NotificationService.text_for_notification(notification) %>
|
<%=
|
||||||
|
case notification.notification_code
|
||||||
|
when MAP_ACCESS_APPROVED
|
||||||
|
map = notification.notified_object.map
|
||||||
|
'granted your request to edit map <span class="in-bold">' + map.name + '</span>'
|
||||||
|
when MAP_ACCESS_REQUEST
|
||||||
|
map = notification.notified_object.map
|
||||||
|
'wants permission to map with you on <span class="in-bold">' + map.name + '</span> <div class="action">Offer a response</div>'
|
||||||
|
when MAP_INVITE_TO_EDIT
|
||||||
|
map = notification.notified_object.map
|
||||||
|
'gave you edit access to map <span class="in-bold">' + map.name + '</span>'
|
||||||
|
when TOPIC_ADDED_TO_MAP
|
||||||
|
topic = notification.notified_object.eventable
|
||||||
|
map = notification.notified_object.map
|
||||||
|
'added topic <span class="in-bold">' + topic.name + '</span> to map <span class="in-bold">' + map.name + '</span>'
|
||||||
|
when TOPIC_CONNECTED_1
|
||||||
|
topic1 = notification.notified_object.topic1
|
||||||
|
topic2 = notification.notified_object.topic2
|
||||||
|
'connected <span class="in-bold">' + topic1.name + '</span> to <span class="in-bold">' + topic2.name + '</span>'
|
||||||
|
when TOPIC_CONNECTED_2
|
||||||
|
topic1 = notification.notified_object.topic1
|
||||||
|
topic2 = notification.notified_object.topic2
|
||||||
|
'connected <span class="in-bold">' + topic2.name + '</span> to <span class="in-bold">' + topic1.name + '</span>'
|
||||||
|
when MESSAGE_FROM_DEVS
|
||||||
|
notification.subject
|
||||||
|
end
|
||||||
|
%>
|
||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
<div class="notification-read-unread">
|
<div class="notification-read-unread">
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
|
<% topic = @entity || entity %>
|
||||||
|
<% event = @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" %>
|
<% 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>
|
<p>
|
||||||
<span style="font-weight: bold;"><%= event.user.name %></span>
|
<span style="font-weight: bold;"><%= event.user.name %></span>
|
||||||
added topic <span style="font-weight: bold"><%= entity.name %></span>
|
added topic <span style="font-weight: bold"><%= topic.name %></span>
|
||||||
to map <span style="font-weight: bold"><%= event.map.name %></span>
|
to map <span style="font-weight: bold"><%= event.map.name %></span>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<%= 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 %>
|
<%= link_to 'Go to Map', map_url(event.map), style: button_style %>
|
6
app/views/topic_mailer/added_to_map.text.erb
Normal file
6
app/views/topic_mailer/added_to_map.text.erb
Normal file
|
@ -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)
|
|
@ -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 || entity
|
||||||
|
synapse = @event || event
|
||||||
<% topic1 = entity %>
|
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 = (entity.id == event.topic1_id ? event.topic2 : event.topic1) %>
|
topic2 = (topic1.id == synapse.topic1_id ? synapse.topic2 : synapse.topic1) %>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<span style="font-weight: bold;"><%= event.user.name %></span>
|
<span style="font-weight: bold;"><%= synapse.user.name %></span>
|
||||||
connected topic <span style="font-weight: bold"><%= topic1.name %></span>
|
connected topic <span style="font-weight: bold"><%= topic1.name %></span>
|
||||||
to topic <span style="font-weight: bold"><%= topic2.name %></span>
|
to topic <span style="font-weight: bold"><%= topic2.name %></span>
|
||||||
<% if event.desc %>
|
<% if synapse.desc.length > 0 %>
|
||||||
with the description "<%= event.desc %>".
|
with the description "<%= event.desc %>".
|
||||||
<% end %>
|
<% end %>
|
||||||
</p>
|
</p>
|
8
app/views/topic_mailer/connected.text.erb
Normal file
8
app/views/topic_mailer/connected.text.erb
Normal file
|
@ -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) %>
|
|
@ -7,12 +7,12 @@
|
||||||
# notification_code: MAILBOXER_CODE_ACCESS_REQUEST
|
# notification_code: MAILBOXER_CODE_ACCESS_REQUEST
|
||||||
# },
|
# },
|
||||||
# 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_MESSAGE_FROM_DEVS = 'MESSAGE_FROM_DEVS'
|
MESSAGE_FROM_DEVS = 'MESSAGE_FROM_DEVS'
|
||||||
|
|
||||||
# these ones are old and can't change without a migration
|
# these ones are old and can't change without a migration
|
||||||
MAILBOXER_CODE_ACCESS_APPROVED = 'ACCESS_APPROVED'
|
MAP_ACCESS_APPROVED = 'ACCESS_APPROVED'
|
||||||
MAILBOXER_CODE_ACCESS_REQUEST = 'ACCESS_REQUEST'
|
MAP_ACCESS_REQUEST = 'ACCESS_REQUEST'
|
||||||
MAILBOXER_CODE_INVITE_TO_EDIT = 'INVITE_TO_EDIT'
|
MAP_INVITE_TO_EDIT = 'INVITE_TO_EDIT'
|
||||||
|
|
||||||
# these ones are new
|
# these ones are new
|
||||||
# this one's a catch all for occurences on the map
|
# this one's a catch all for occurences on the map
|
||||||
|
@ -30,8 +30,9 @@ MAP_ACTIVITY = 'MAP_ACTIVITY'
|
||||||
# MAP_STARRED
|
# MAP_STARRED
|
||||||
# MAP_COLLABORATOR_REMOVED
|
# MAP_COLLABORATOR_REMOVED
|
||||||
|
|
||||||
TOPIC_ADDED_TO_MAP = 'TOPIC_ADDED_TO_MAP'
|
TOPIC_ADDED_TO_MAP = 'TOPIC_ADDED_TO_MAP'
|
||||||
TOPIC_CONNECTED = 'TOPIC_CONNECTED'
|
TOPIC_CONNECTED_1 = 'TOPIC_CONNECTED_1'
|
||||||
|
TOPIC_CONNECTED_2 = 'TOPIC_CONNECTED_2'
|
||||||
# TOPIC_DELETED
|
# TOPIC_DELETED
|
||||||
# TOPIC_DISCONNECTED
|
# TOPIC_DISCONNECTED
|
||||||
# TOPIC_UPDATED
|
# TOPIC_UPDATED
|
||||||
|
|
Loading…
Add table
Reference in a new issue