diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index 45828cca82..71c6a46bec 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 4713d9c863..55ebdcae66 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