A platform for community discussion. Free, open, simple.
FIX: Update tracking state when topic visibility or category changes (#37685)
ducks16158ba62144
diff --git a/app/models/topic.rb b/app/models/topic.rbindex 7995d3ca58d..bc841859162 100644--- a/app/models/topic.rb+++ b/app/models/topic.rb@@ -1110,6 +1110,12 @@ class Topic < ActiveRecord::Base# category is private. this is only done if the category# has actually changed to avoid noise.DB.after_commit { Jobs.enqueue(:update_topic_upload_security, topic_id: self.id) }++ # Notify tracking state of category change so users who lost access+ # have the topic removed from their tracking state+ if SiteSetting.experimental_topic_category_change_notification+ DB.after_commit { TopicTrackingState.publish_category_change(self, old_category) }+ endendCategory.where(id: new_category.id).update_all("topic_count = topic_count + 1")diff --git a/app/models/topic_tracking_state.rb b/app/models/topic_tracking_state.rbindex d4d5b052e09..65a15bade3c 100644--- a/app/models/topic_tracking_state.rb+++ b/app/models/topic_tracking_state.rb@@ -182,6 +182,35 @@ class TopicTrackingStateMessageBus.publish(RECOVER_MESSAGE_BUS_CHANNEL, message.as_json, group_ids: group_ids)end+ # Called when a topic's category changes.+ # If moving to a more restricted category, users who lost access need to+ # have the topic removed from their tracking state.+ def self.publish_category_change(topic, old_category)+ return if !SiteSetting.experimental_topic_category_change_notification+ return unless topic.regular?++ old_restricted = old_category&.read_restricted?+ new_restricted = topic.category&.read_restricted?++ if !old_restricted && new_restricted+ # Moving from public to restricted category+ # First, notify ALL users to remove topic (those who lost access)+ message = { topic_id: topic.id, message_type: DELETE_MESSAGE_TYPE }+ MessageBus.publish(DELETE_MESSAGE_BUS_CHANNEL, message.as_json, group_ids: nil)++ # Then, notify users who CAN see the new category with updated info+ publish_latest(topic)+ elsif old_restricted && !new_restricted+ # Moving from restricted to public category+ # Notify all users of the now-visible topic+ publish_latest(topic)+ elsif old_category&.id != topic.category_id+ # Category changed but restriction level didn't change+ # Just publish the updated category info+ publish_latest(topic)+ end+ end+def self.publish_delete(topic)return unless topic.regular?diff --git a/app/services/topic_status_updater.rb b/app/services/topic_status_updater.rbindex b75598e51f6..6a6ffecff4e 100644--- a/app/services/topic_status_updater.rb+++ b/app/services/topic_status_updater.rb@@ -57,6 +57,17 @@ TopicStatusUpdater =status.enabled? ? :increment! : :decrement!,topic.first_post,)++ # Notify tracking state when topic visibility changes+ # This handles the case where a topic is hidden due to flagging+ # or made visible again after review+ if SiteSetting.experimental_topic_category_change_notification+ if status.enabled?+ TopicTrackingState.publish_recover(topic)+ else+ TopicTrackingState.publish_delete(topic)+ end+ endendif status.visible?diff --git a/config/site_settings.yml b/config/site_settings.ymlindex 65b5306f1ac..69b5c7fc0cd 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -4312,7 +4312,10 @@ experimental:status: "experimental"impact: "feature,staff"learn_more_url: "https://meta.discourse.org/t/-/394971"+ experimental_topic_category_change_notification:+ default: false+ hidden: trueenable_category_type_setup:default: falsehidden: true- client: true\ No newline at end of file+ client: truediff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rbindex b224d4385fe..468c9d549da 100644--- a/spec/models/topic_spec.rb+++ b/spec/models/topic_spec.rb@@ -2121,6 +2121,40 @@ RSpec.describe Topic do}endend++ describe "tracking state notifications" do+ before { SiteSetting.experimental_topic_category_change_notification = true }+ it "publishes category change when moving to a restricted category" do+ restricted_category =+ Fabricate(:category_with_definition, read_restricted: true, user: user)++ messages =+ MessageBus.track_publish { topic.change_category_to_id(restricted_category.id) }++ delete_message = messages.find { |m| m.channel == "/delete" }+ latest_message = messages.find { |m| m.channel == "/latest" }++ expect(delete_message).to be_present+ expect(delete_message.data["topic_id"]).to eq(topic.id)++ expect(latest_message).to be_present+ expect(latest_message.data["topic_id"]).to eq(topic.id)+ end++ it "publishes latest when moving between public categories" do+ another_category =+ Fabricate(:category_with_definition, read_restricted: false, user: user)++ messages =+ MessageBus.track_publish("/latest") do+ topic.change_category_to_id(another_category.id)+ end++ expect(messages.length).to eq(1)+ expect(messages.first.data["topic_id"]).to eq(topic.id)+ expect(messages.first.data["payload"]["category_id"]).to eq(another_category.id)+ end+ endendcontext "when allow_uncategorized_topics is false" dodiff --git a/spec/services/topic_status_updater_spec.rb b/spec/services/topic_status_updater_spec.rbindex 1e579bc3f99..4c47df6acc1 100644--- a/spec/services/topic_status_updater_spec.rb+++ b/spec/services/topic_status_updater_spec.rb@@ -279,4 +279,45 @@ RSpec.describe TopicStatusUpdater doexpect(TopicHotScore.find_by(topic_id: topic.id)).to be_nilendend++ describe "tracking state notifications on visibility change" do+ before { SiteSetting.experimental_topic_category_change_notification = true }++ it "publishes delete when topic becomes invisible" do+ topic = Fabricate(:topic)++ messages =+ MessageBus.track_publish("/delete") do+ TopicStatusUpdater.new(topic, admin).update!("visible", false)+ end++ expect(messages.length).to eq(1)+ expect(messages.first.data["topic_id"]).to eq(topic.id)+ expect(messages.first.data["message_type"]).to eq(TopicTrackingState::DELETE_MESSAGE_TYPE)+ end++ it "publishes recover when topic becomes visible again" do+ topic = Fabricate(:topic, visible: false)++ messages =+ MessageBus.track_publish("/recover") do+ TopicStatusUpdater.new(topic, admin).update!("visible", true)+ end++ expect(messages.length).to eq(1)+ expect(messages.first.data["topic_id"]).to eq(topic.id)+ expect(messages.first.data["message_type"]).to eq(TopicTrackingState::RECOVER_MESSAGE_TYPE)+ end++ it "does not publish for non-regular topics" do+ topic = Fabricate(:private_message_topic)++ messages =+ MessageBus.track_publish("/delete") do+ TopicStatusUpdater.new(topic, admin).update!("visible", false)+ end++ expect(messages.length).to eq(0)+ end+ endend