From 67f404d281b7dbd5867fe5332968f7855ffa4e9c Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 19 Feb 2015 12:40:00 +1100 Subject: [PATCH] FIX: remove notifications on deleted topics from the stream --- app/controllers/notifications_controller.rb | 1 + app/models/notification.rb | 20 +++++++++++++++----- app/models/user.rb | 4 ++-- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 09a1cc6306b..7325fc8fac6 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -28,6 +28,7 @@ class NotificationsController < ApplicationController end notifications = Notification.where(user_id: user.id) + .visible .includes(:topic) .limit(60) .where('created_at < ?', params[:before]) diff --git a/app/models/notification.rb b/app/models/notification.rb index 55ebdcae66b..bd96c36dc98 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -8,7 +8,10 @@ class Notification < ActiveRecord::Base validates_presence_of :notification_type scope :unread, lambda { where(read: false) } - scope :recent, lambda {|n=nil| n ||= 10; order('created_at desc').limit(n) } + scope :recent, lambda {|n=nil| n ||= 10; order('notifications.created_at desc').limit(n) } + scope :visible , lambda { where('notifications.topic_id IS NULL OR notifications.topic_id IN ( + SELECT id FROM topics + WHERE deleted_at IS NULL)') } after_save :refresh_notification_count after_destroy :refresh_notification_count @@ -39,6 +42,7 @@ class Notification < ActiveRecord::Base def self.interesting_after(min_date) result = where("created_at > ?", min_date) .includes(:topic) + .visible .unread .limit(20) .order("CASE WHEN notification_type = #{Notification.types[:replied]} THEN 1 @@ -98,13 +102,19 @@ class Notification < ActiveRecord::Base def self.recent_report(user, count = nil) count ||= 10 - notifications = user.notifications.recent(count).includes(:topic).to_a + notifications = user.notifications + .visible + .recent(count) + .includes(:topic) + .to_a if notifications.present? - notifications += user.notifications - .order('created_at desc') + notifications += user + .notifications + .order('notifications.created_at desc') .where(read: false, notification_type: Notification.types[:private_message]) - .where('id < ?', notifications.last.id) + .joins(:topic) + .where('notifications.id < ?', notifications.last.id) .limit(count) notifications.sort do |x,y| diff --git a/app/models/user.rb b/app/models/user.rb index 20f159e86f3..17d1d883598 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -237,7 +237,7 @@ class User < ActiveRecord::Base end def unread_notifications_by_type - @unread_notifications_by_type ||= notifications.where("id > ? and read = false", seen_notification_id).group(:notification_type).count + @unread_notifications_by_type ||= notifications.visible.where("id > ? and read = false", seen_notification_id).group(:notification_type).count end def reload @@ -248,7 +248,7 @@ class User < ActiveRecord::Base end def unread_private_messages - @unread_pms ||= notifications.where("read = false AND notification_type = ?", Notification.types[:private_message]).count + @unread_pms ||= notifications.visible.where("read = false AND notification_type = ?", Notification.types[:private_message]).count end def unread_notifications