From 8e9fde55045cb9455282b4837ef47d11bc0cdfc2 Mon Sep 17 00:00:00 2001 From: Connor Turland Date: Fri, 10 Feb 2017 01:11:21 +0000 Subject: [PATCH] dont send duplicates --- app/models/message.rb | 2 +- app/models/star.rb | 4 ++-- app/models/synapse.rb | 4 ++-- app/services/notification_service.rb | 17 ++++++++++------- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/app/models/message.rb b/app/models/message.rb index 5e435968..203e8adb 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -6,7 +6,7 @@ class Message < ApplicationRecord delegate :name, to: :user, prefix: true after_create :after_created - after_create :after_created_async + #after_create :after_created_async def user_image diff --git a/app/models/star.rb b/app/models/star.rb index 75e3761c..8a45a0c3 100644 --- a/app/models/star.rb +++ b/app/models/star.rb @@ -4,8 +4,8 @@ class Star < ActiveRecord::Base belongs_to :map validates :map, uniqueness: { scope: :user, message: 'You have already starred this map' } - after_create :after_created_async - before_destroy :before_destroyed + #after_create :after_created_async + #before_destroy :before_destroyed protected diff --git a/app/models/synapse.rb b/app/models/synapse.rb index 75cb9759..1df3d183 100644 --- a/app/models/synapse.rb +++ b/app/models/synapse.rb @@ -76,8 +76,8 @@ class Synapse < ApplicationRecord end def after_created_async - NotificationService.notify_followers(topic1, 'topic_connected', self) - NotificationService.notify_followers(topic2, 'topic_connected', self) + 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 9644d23b..0549f7a9 100644 --- a/app/services/notification_service.rb +++ b/app/services/notification_service.rb @@ -62,7 +62,7 @@ class NotificationService 'placeholder' when 'map_updated' #disabled 'placeholder' - when 'map_message' # event is a Message + 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 @@ -88,8 +88,6 @@ 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: template, locals: { entity: entity, event: event }, layout: false) follows.each{|follow| - # don't send if they've indicated disinterest in this event_type - return unless follow.follow_type.all || follow.follow_type.read_attribute(event_type) # 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) # push could be handled with Actioncable to send transient notifications to the UI @@ -97,8 +95,14 @@ class NotificationService } end - def self.notify_followers(entity, event_type, event, reason_filter = nil) - follows = entity.follows + def self.notify_followers(entity, event_type, event, reason_filter = nil, exclude_follows = nil) + follows = entity.follows.joins(:follow_type).where("follow_types.all = ? OR follow_types.#{event_type} = ?", true, true) + + follows = follows.where.not(user_id: event.user.id) + + if exclude_follows + follows = follows.where.not(id: exclude_follows) + end if reason_filter.class == String && FollowReason::REASONS.include?(reason_filter) follows = follows.joins(:follow_reason).where('follow_reasons.' + reason_filter => true) @@ -106,9 +110,8 @@ class NotificationService # TODO: throw an error here if all the reasons aren't valid follows = follows.joins(:follow_reason).where(reason_filter.map{|r| "follow_reasons.#{r} = 't'"}.join(' OR ')) end - # filter out the person who took the action - follows = follows.to_a.delete_if{|f| f.user.id == event.user.id} send_for_follows(follows, entity, event_type, event) + return follows.map(&:id) end