From de4f51bb5c9948f4a6dd7990b2550b6c13368442 Mon Sep 17 00:00:00 2001 From: Connor Turland Date: Wed, 8 Mar 2017 15:45:40 -0500 Subject: [PATCH] fix permissions and don't send if has map open --- app/models/user.rb | 12 ++++++++++++ app/services/notification_service.rb | 11 +++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 2c7ddf13..bb22f972 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -132,6 +132,18 @@ class User < ApplicationRecord stars.where(map_id: map.id).exists? end + def has_map_open(map) + latestEvent = Event.where(map: map, user: self) + .where(kind: ['user_present_on_map', 'user_not_present_on_map']) + .order(:created_at) + .last + latestEvent && latestEvent.kind == 'user_present_on_map' + end + + def has_map_with_synapse_open(synapse) + synapse.maps.any?{|map| has_map_open(map)} + end + def settings self[:settings] = UserPreference.new if self[:settings].nil? if not self[:settings].respond_to?(:follow_topic_on_created) diff --git a/app/services/notification_service.rb b/app/services/notification_service.rb index 737dbb4d..c587e9ed 100644 --- a/app/services/notification_service.rb +++ b/app/services/notification_service.rb @@ -34,10 +34,13 @@ class NotificationService # 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) follows.each{|follow| - if entity.class == Map - next unless MapPolicy.new(follow.user, entity).show? - elsif entity.class == Topic - next unless TopicPolicy.new(follow.user, entity).show? + case event_type + when TOPIC_ADDED_TO_MAP + next unless TopicPolicy.new(follow.user, entity).show? && MapPolicy.new(follow.user, event.map).show? + next if follow.user.has_map_open(event.map) + when TOPIC_CONNECTED_1, TOPIC_CONNECTED_2 + next unless SynapsePolicy.new(follow.user, event).show? + next if follow.user.has_map_with_synapse_open(event) end # 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, event.user)