From b34264b138257226f88587123d631105f20653c0 Mon Sep 17 00:00:00 2001 From: Connor Turland Date: Sat, 11 Feb 2017 04:47:21 +0000 Subject: [PATCH] lots of fixes --- app/helpers/map_mailer_helper.rb | 13 +++++++++++++ app/helpers/topic_mailer_helper.rb | 10 ++++++++++ app/mailers/map_mailer.rb | 13 +------------ app/mailers/topic_mailer.rb | 9 +-------- app/models/mapping.rb | 9 ++++++--- app/services/notification_service.rb | 22 ++++++++++++---------- app/views/notifications/index.html.erb | 17 +++++++++-------- app/views/topic_mailer/connected.html.erb | 2 +- 8 files changed, 53 insertions(+), 42 deletions(-) create mode 100644 app/helpers/map_mailer_helper.rb create mode 100644 app/helpers/topic_mailer_helper.rb diff --git a/app/helpers/map_mailer_helper.rb b/app/helpers/map_mailer_helper.rb new file mode 100644 index 00000000..be95ea04 --- /dev/null +++ b/app/helpers/map_mailer_helper.rb @@ -0,0 +1,13 @@ +module MapMailerHelper + def access_approved_subject(map) + map.name + ' - access approved' + end + + def access_request_subject(map) + map.name + ' - request to edit' + end + + def invite_to_edit_subject(map) + map.name + ' - invited to edit' + end +end \ No newline at end of file diff --git a/app/helpers/topic_mailer_helper.rb b/app/helpers/topic_mailer_helper.rb new file mode 100644 index 00000000..81b87de0 --- /dev/null +++ b/app/helpers/topic_mailer_helper.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true +module TopicMailerHelper + def added_to_map_subject(topic, map) + topic.name + ' was added to map ' + map.name + end + + def connected_subject(topic) + 'new synapse to topic ' + topic.name + end +end \ No newline at end of file diff --git a/app/mailers/map_mailer.rb b/app/mailers/map_mailer.rb index 4e9772bc..6a207b23 100644 --- a/app/mailers/map_mailer.rb +++ b/app/mailers/map_mailer.rb @@ -1,31 +1,20 @@ # frozen_string_literal: true class MapMailer < ApplicationMailer + include MapMailerHelper default from: 'team@metamaps.cc' - def access_approved_subject(map) - map.name + ' - access approved' - end - def access_approved(request) @request = request @map = request.map mail(to: request.user.email, 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 diff --git a/app/mailers/topic_mailer.rb b/app/mailers/topic_mailer.rb index 9175d410..76464148 100644 --- a/app/mailers/topic_mailer.rb +++ b/app/mailers/topic_mailer.rb @@ -1,21 +1,14 @@ # frozen_string_literal: true class TopicMailer < ApplicationMailer + include TopicMailerHelper 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 diff --git a/app/models/mapping.rb b/app/models/mapping.rb index d0b25c23..a1ebdaf5 100644 --- a/app/models/mapping.rb +++ b/app/models/mapping.rb @@ -29,11 +29,8 @@ class Mapping < ApplicationRecord def after_created if mappable_type == 'Topic' - meta = { 'x': xloc, 'y': yloc, 'mapping_id': id } - Events::TopicAddedToMap.publish!(mappable, map, user, meta) ActionCable.server.broadcast 'map_' + map.id.to_s, type: 'topicAdded', topic: mappable.filtered, mapping_id: id elsif mappable_type == 'Synapse' - Events::SynapseAddedToMap.publish!(mappable, map, user, meta) ActionCable.server.broadcast( 'map_' + map.id.to_s, type: 'synapseAdded', @@ -47,6 +44,12 @@ class Mapping < ApplicationRecord def after_created_async FollowService.follow(map, user, 'contributed') + if mappable_type == 'Topic' + meta = { 'x': xloc, 'y': yloc, 'mapping_id': id } + Events::TopicAddedToMap.publish!(mappable, map, user, meta) + elsif mappable_type == 'Synapse' + Events::SynapseAddedToMap.publish!(mappable, map, user, nil) + end end handle_asynchronously :after_created_async diff --git a/app/services/notification_service.rb b/app/services/notification_service.rb index c6997fb7..fa429f2e 100644 --- a/app/services/notification_service.rb +++ b/app/services/notification_service.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true class NotificationService + extend TopicMailerHelper + extend MapMailerHelper # for strip_tags include ActionView::Helpers::SanitizeHelper @@ -13,13 +15,13 @@ class NotificationService def self.get_settings_for_event(entity, event_type, event) case event_type when TOPIC_ADDED_TO_MAP - subject = TopicMailer.added_to_map_subject(entity, event.map) + subject = added_to_map_subject(entity, event.map) template = 'topic_mailer/added_to_map' when TOPIC_CONNECTED_1 - subject = TopicMailer.connected_subject(event.topic1) + subject = connected_subject(event.topic1) template = 'topic_mailer/connected' when TOPIC_CONNECTED_2 - TopicMailer.connected_subject(event.topic2) + subject = connected_subject(event.topic2) template = 'topic_mailer/connected' end @@ -30,19 +32,19 @@ class NotificationService return if follows.length == 0 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: settings.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(settings.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 } end def self.notify_followers(entity, event_type, event, reason_filter = nil, exclude_follows = nil) - follows = entity.follows.joins(:follow_type).where.not(user_id: event.user.id) + follows = entity.follows.where.not(user_id: event.user.id) - if exclude_follows + if !exclude_follows.nil? follows = follows.where.not(id: exclude_follows) end @@ -57,19 +59,19 @@ class NotificationService end def self.access_request(request) - subject = MapMailer.access_request_subject(request.map) + subject = 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, MAP_ACCESS_REQUEST, true, request.user) end def self.access_approved(request) - subject = MapMailer.access_approved_subject(request.map) + subject = 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, MAP_ACCESS_APPROVED, true, request.map.user) end def self.invite_to_edit(user_map) - subject = MapMailer.invite_to_edit_subject(user_map.map) + subject = 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, MAP_INVITE_TO_EDIT, true, user_map.map.user) end diff --git a/app/views/notifications/index.html.erb b/app/views/notifications/index.html.erb index 20505c8c..77de7f65 100644 --- a/app/views/notifications/index.html.erb +++ b/app/views/notifications/index.html.erb @@ -7,7 +7,7 @@

Notifications