Skip to content

A platform for community discussion. Free, open, simple.

FIX: keep retired settings in feature feeds (#44634)

David Battersby9c1520fbcdfb
diff --git a/app/services/upcoming_changes/action/track_status_changes.rb b/app/services/upcoming_changes/action/track_status_changes.rb
index 9ee93dd1575..4ceabaf3d57 100644
--- a/app/services/upcoming_changes/action/track_status_changes.rb
+++ b/app/services/upcoming_changes/action/track_status_changes.rb
@@ -57,6 +57,8 @@ class UpcomingChanges::Action::TrackStatusChanges < Service::ActionBase
end
end
+ sync_permanent_display_states
+
UpcomingChanges.clear_caches!
DiscourseUpdates.clear_latest_new_feature_created_at_cache
@@ -85,4 +87,35 @@ class UpcomingChanges::Action::TrackStatusChanges < Service::ActionBase
def status_changed?(previous_status, current_status)
previous_status&.to_sym != current_status
end
+
+ # Once a permanent change's setting is removed its conditional display rule is
+ # gone too, so we keep the latest result on its permanent status event for the
+ # new features feed to respect.
+ def sync_permanent_display_states
+ permanent_change_names =
+ SiteSetting.upcoming_change_site_settings.select do |change_name|
+ UpcomingChanges.change_status(change_name) == :permanent
+ end
+ return if permanent_change_names.empty?
+
+ latest_status_events =
+ UpcomingChangeEvent
+ .status_changed
+ .where(upcoming_change_name: permanent_change_names)
+ .group_by(&:upcoming_change_name)
+ .transform_values(&:last)
+
+ permanent_change_names.each do |change_name|
+ event = latest_status_events[change_name.to_s]
+ next if event&.event_data&.dig("new_value") != "permanent"
+
+ was_conditionally_displayed = UpcomingChanges::ConditionalDisplay.should_display?(change_name)
+ next if event.event_data["was_conditionally_displayed"] == was_conditionally_displayed
+
+ event.update!(
+ event_data:
+ event.event_data.merge("was_conditionally_displayed" => was_conditionally_displayed),
+ )
+ end
+ end
end
diff --git a/lib/discourse_updates.rb b/lib/discourse_updates.rb
index c9c48d215a6..0187244c8b3 100644
--- a/lib/discourse_updates.rb
+++ b/lib/discourse_updates.rb
@@ -187,18 +187,23 @@ module DiscourseUpdates
end
def merge_new_features_with_upcoming_changes(new_features)
- # Any new features that have an upcoming_change_setting_name that is not
- # in the permanent upcoming changes list are excluded, since sites can
- # have different versions of Discourse deployed and may not have the
- # upcoming change or the same status.
+ # Only show feed items for upcoming changes that are permanent on this site,
+ # or that were permanent and displayed here before their setting was removed.
permanent_upcoming_change_names =
UpcomingChanges.permanent_upcoming_changes.map { |uc| uc[:setting].to_s }
new_features.reject! do |feature|
- if feature[:upcoming_change_setting_name].present?
- !permanent_upcoming_change_names.include?(feature[:upcoming_change_setting_name])
- else
- false
- end
+ setting_name = feature[:upcoming_change_setting_name]
+ next false if setting_name.blank?
+
+ # Changes removed before display state was tracked have no flag, so only
+ # an explicit false hides them.
+ status = UpcomingChanges.current_statuses[setting_name] || {}
+ retired =
+ !SiteSetting.has_setting?(setting_name) && status[:status] == "permanent" &&
+ status[:was_conditionally_displayed] != false
+ next false if retired
+
+ !permanent_upcoming_change_names.include?(setting_name)
end
return new_features if permanent_upcoming_change_names.blank?
diff --git a/lib/upcoming_changes.rb b/lib/upcoming_changes.rb
index 37cc2c79e9a..dc6b7dde258 100644
--- a/lib/upcoming_changes.rb
+++ b/lib/upcoming_changes.rb
@@ -491,7 +491,7 @@ module UpcomingChanges
GROUP BY upcoming_change_name
ORDER BY MAX(created_at) DESC
)
- SELECT latest_status_changes.upcoming_change_name, latest_status_changes.created_at, upcoming_change_events.event_data->>'new_value' as new_value
+ SELECT latest_status_changes.upcoming_change_name, latest_status_changes.created_at, upcoming_change_events.event_data->>'new_value' as new_value, (upcoming_change_events.event_data->>'was_conditionally_displayed')::boolean as was_conditionally_displayed
FROM latest_status_changes
INNER JOIN upcoming_change_events ON upcoming_change_events.upcoming_change_name = latest_status_changes.upcoming_change_name AND upcoming_change_events.created_at = latest_status_changes.created_at
ORDER BY latest_status_changes.created_at DESC
@@ -501,6 +501,7 @@ module UpcomingChanges
statuses[result.upcoming_change_name] = {
status: result.new_value,
changed_at: result.created_at,
+ was_conditionally_displayed: result.was_conditionally_displayed,
}
end
end
diff --git a/spec/lib/discourse_updates_spec.rb b/spec/lib/discourse_updates_spec.rb
index 02bc148fe95..be4a5004819 100644
--- a/spec/lib/discourse_updates_spec.rb
+++ b/spec/lib/discourse_updates_spec.rb
@@ -580,7 +580,7 @@ RSpec.describe DiscourseUpdates do
new_features = [feed_feature.deep_dup]
result = described_class.merge_new_features_with_upcoming_changes(new_features)
expect(result.map { |r| r[:upcoming_change_setting_name] }).not_to include(
- :enable_upload_debug_mode,
+ "enable_upload_debug_mode",
)
end
end
@@ -592,7 +592,91 @@ RSpec.describe DiscourseUpdates do
new_features = [feed_feature.deep_dup]
result = described_class.merge_new_features_with_upcoming_changes(new_features)
expect(result.map { |r| r[:upcoming_change_setting_name] }).not_to include(
- :enable_upload_debug_mode,
+ "enable_upload_debug_mode",
+ )
+ end
+ end
+
+ def record_status_change(new_value, was_conditionally_displayed: nil)
+ UpcomingChangeEvent.create!(
+ event_type: :status_changed,
+ upcoming_change_name: upcoming_change_setting_name,
+ event_data: {
+ "previous_value" => "stable",
+ "new_value" => new_value,
+ "was_conditionally_displayed" => was_conditionally_displayed,
+ }.compact,
+ )
+ end
+
+ context "when the upcoming change setting has been removed" do
+ let(:upcoming_change_setting_name) { "enable_removed_feature" }
+
+ it "keeps the feed item when the change was permanent and displayed on this site" do
+ record_status_change("permanent", was_conditionally_displayed: true)
+
+ result = described_class.merge_new_features_with_upcoming_changes([feed_feature.deep_dup])
+ expect(result.map { |r| r[:upcoming_change_setting_name] }).to include(
+ "enable_removed_feature",
+ )
+ end
+
+ it "keeps the feed item when the permanent status predates display tracking" do
+ record_status_change("permanent")
+
+ result = described_class.merge_new_features_with_upcoming_changes([feed_feature.deep_dup])
+ expect(result.map { |r| r[:upcoming_change_setting_name] }).to include(
+ "enable_removed_feature",
+ )
+ end
+
+ it "excludes the feed item when the change was hidden on this site" do
+ record_status_change("permanent", was_conditionally_displayed: false)
+
+ result = described_class.merge_new_features_with_upcoming_changes([feed_feature.deep_dup])
+ expect(result.map { |r| r[:upcoming_change_setting_name] }).not_to include(
+ "enable_removed_feature",
+ )
+ end
+
+ it "keeps the feed item when no upcoming changes are currently permanent" do
+ stub_permanent_upcoming_changes!([])
+ record_status_change("permanent", was_conditionally_displayed: true)
+
+ result = described_class.merge_new_features_with_upcoming_changes([feed_feature.deep_dup])
+ expect(result.map { |r| r[:upcoming_change_setting_name] }).to include(
+ "enable_removed_feature",
+ )
+ end
+
+ it "excludes the feed item when the change was not permanent on this site" do
+ record_status_change("beta")
+
+ result = described_class.merge_new_features_with_upcoming_changes([feed_feature.deep_dup])
+ expect(result.map { |r| r[:upcoming_change_setting_name] }).not_to include(
+ "enable_removed_feature",
+ )
+ end
+
+ it "excludes the feed item when this site never recorded the change" do
+ result = described_class.merge_new_features_with_upcoming_changes([feed_feature.deep_dup])
+ expect(result.map { |r| r[:upcoming_change_setting_name] }).not_to include(
+ "enable_removed_feature",
+ )
+ end
+ end
+
+ context "when the upcoming change is permanent but hidden on this site" do
+ # Conditionally hidden changes are left out of the permanent list even
+ # though the setting still exists.
+ before { stub_permanent_upcoming_changes!([]) }
+
+ it "excludes the feed item even though this site recorded it as permanent" do
+ record_status_change("permanent")
+
+ result = described_class.merge_new_features_with_upcoming_changes([feed_feature.deep_dup])
+ expect(result.map { |r| r[:upcoming_change_setting_name] }).not_to include(
+ "enable_upload_debug_mode",
)
end
end
diff --git a/spec/lib/upcoming_changes_spec.rb b/spec/lib/upcoming_changes_spec.rb
index edb81f65fed..6647d0343b8 100644
--- a/spec/lib/upcoming_changes_spec.rb
+++ b/spec/lib/upcoming_changes_spec.rb
@@ -1051,10 +1051,29 @@ RSpec.describe UpcomingChanges do
result = described_class.current_statuses
expect(result["timeline_status_setting"]).to eq(
- { status: "stable", changed_at: latest_event.created_at },
+ { status: "stable", changed_at: latest_event.created_at, was_conditionally_displayed: nil },
)
end
+ it "includes the display state recorded on the latest status_changed event" do
+ UpcomingChangeEvent.create!(
+ event_type: :status_changed,
+ upcoming_change_name: "timeline_status_setting",
+ event_data: {
+ "previous_value" => "stable",
+ "new_value" => "permanent",
+ "was_conditionally_displayed" => false,
+ },
+ )
+
+ expect(
+ described_class.current_statuses.dig(
+ "timeline_status_setting",
+ :was_conditionally_displayed,
+ ),
+ ).to eq(false)
+ end
+
it "caches the result so the SQL runs only once until the cache key is deleted" do
UpcomingChangeEvent.create!(
event_type: :status_changed,
diff --git a/spec/services/upcoming_changes/action/track_status_changes_spec.rb b/spec/services/upcoming_changes/action/track_status_changes_spec.rb
index 9131644cb6f..72da74c82fb 100644
--- a/spec/services/upcoming_changes/action/track_status_changes_spec.rb
+++ b/spec/services/upcoming_changes/action/track_status_changes_spec.rb
@@ -209,6 +209,50 @@ RSpec.describe UpcomingChanges::Action::TrackStatusChanges do
end
end
+ context "when a change is permanent" do
+ let(:enable_upload_debug_mode_status) { :permanent }
+
+ def latest_permanent_event
+ scoped_events.where(
+ event_type: :status_changed,
+ upcoming_change_name: :enable_upload_debug_mode,
+ ).last
+ end
+
+ it "records that the change is displayed on the new status event" do
+ result
+ expect(latest_permanent_event.event_data["was_conditionally_displayed"]).to eq(true)
+ end
+
+ it "records that the change is hidden when conditional display hides it" do
+ UpcomingChanges::ConditionalDisplay.stubs(:should_display?).returns(false)
+
+ result
+ expect(latest_permanent_event.event_data["was_conditionally_displayed"]).to eq(false)
+ end
+
+ it "updates an existing permanent event when the display state changes" do
+ event =
+ UpcomingChangeEvent.create!(
+ event_type: :status_changed,
+ upcoming_change_name: :enable_upload_debug_mode,
+ event_data: {
+ "previous_value" => "stable",
+ "new_value" => "permanent",
+ },
+ )
+ UpcomingChanges::ConditionalDisplay.stubs(:should_display?).returns(false)
+
+ expect { result }.not_to change {
+ scoped_events.where(
+ event_type: :status_changed,
+ upcoming_change_name: :enable_upload_debug_mode,
+ ).count
+ }
+ expect(event.reload.event_data["was_conditionally_displayed"]).to eq(false)
+ end
+ end
+
context "when status has not changed" do
let(:show_user_menu_avatars_status) { :beta }