From b1d2aba0f30eadccdfe93f735c35a557c411a2df Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 26 May 2014 13:26:28 -0400 Subject: [PATCH] Clean up notifications if a user can't see a topic --- app/controllers/topics_controller.rb | 8 ++++++++ app/models/notification.rb | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index 45828cca829..71c6a46bec8 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -61,6 +61,14 @@ class TopicsController < ApplicationController perform_show_response canonical_url absolute_without_cdn(@topic_view.canonical_path) + rescue Discourse::InvalidAccess => ex + + if current_user + # If the user can't see the topic, clean up notifications for it. + Notification.remove_for(current_user.id, params[:topic_id]) + end + + raise ex end def wordpress diff --git a/app/models/notification.rb b/app/models/notification.rb index 4713d9c863c..55ebdcae66b 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -65,6 +65,12 @@ class Notification < ActiveRecord::Base result end + # Clean up any notifications the user can no longer see. For example, if a topic was previously + # public then turns private. + def self.remove_for(user_id, topic_id) + Notification.where(user_id: user_id, topic_id: topic_id).delete_all + end + # Be wary of calling this frequently. O(n) JSON parsing can suck. def data_hash @data_hash ||= begin