Skip to content

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

FIX: Do not notify new sites or plugins enabled of old upcoming changes (#42045)

Martin Brennan4de06a006b49
diff --git a/.skills/discourse-upcoming-changes-authoring/SKILL.md b/.skills/discourse-upcoming-changes-authoring/SKILL.md
index 572029eab11..7fa98bfd242 100644
--- a/.skills/discourse-upcoming-changes-authoring/SKILL.md
+++ b/.skills/discourse-upcoming-changes-authoring/SKILL.md
@@ -51,6 +51,7 @@ All services use `Service::Base`. They're organized under `app/services/upcoming
| `NotifyPromotions` | Iterates all changes and calls `NotifyPromotion` for each |
| `NotifyPromotion` | Handles one promotion — checks policies, merges notifications, fires events |
| `NotificationDataMerger` | Consolidates multiple change notifications into one to avoid spam (used by both the weekly availability job and `NotifyPromotion`) |
+| `BackfillNotifiedEvents` | Marks the changes that already exist as notified about, at site creation and on plugin enable — see [Back-Catalogue Notification Suppression](#back-catalogue-notification-suppression) |
**`SiteSetting::UpsertGroups`** — Manages group assignments for settings (upserts `SiteSettingGroup`, refreshes caches, notifies clients).
@@ -300,9 +301,22 @@ When multiple changes need notifications, `NotificationDataMerger` consolidates
For "change available" notifications specifically, merging also happens across the *batch* processed by the weekly `NotifyAdminsOfAvailableUpcomingChanges` job — for admins without an existing unread notification, the job builds a single new notification per admin that lists every newly-available change in that run, rather than emitting one notification per change.
-### New Site Notification Suppression
+### Back-Catalogue Notification Suppression
-Notifications for "change available" and "promoted" are skipped on new sites (determined by `Migration::Helpers.new_site?` in `lib/migration/helpers.rb` — a site is "new" if its first schema migration was less than 1 hour ago). Both the weekly `NotifyAdminsOfAvailableUpcomingChanges` job and the `NotifyPromotion` service guard on `UpcomingChanges.should_notify_admins?`. This prevents freshly provisioned sites from being flooded with notifications for every existing upcoming change on their first run. The tracking/detection steps still execute — only the notification delivery is suppressed.
+Admins should never be notified about changes that pre-date their ability to act on them. Two mechanisms cover this.
+
+**The one-hour window.** `UpcomingChanges.should_notify_admins?` is false while `Migration::Helpers.new_site?` is true (a site is "new" if its first schema migration was less than 1 hour ago). Both the weekly `NotifyAdminsOfAvailableUpcomingChanges` job and the `NotifyPromotion` service guard on it. This only covers the first hour, so it is a backstop, not the main mechanism — the weekly job runs weekly, and `NotifyPromotion` fires as soon as the hour elapses.
+
+**Backfilled events.** `UpcomingChanges::Action::BackfillNotifiedEvents` writes `added` plus the appropriate notified event (`admins_notified_available_change` at `promote_upcoming_changes_on_status - 1`, `admins_notified_automatic_promotion` at/above the promote status) so both notification paths permanently treat those changes as handled. It is called from:
+
+- `db/fixtures/995_upcoming_changes.rb`, on new sites (skipped in development so notifications remain testable locally). This covers every *installed* plugin's changes too, enabled or not — plugin settings are registered regardless of enabled state, so they are already in `SiteSetting.upcoming_change_site_settings`.
+- The `:site_setting_changed` handler in `config/initializers/015-track-upcoming-change-toggle.rb`, when a plugin's `enabled_site_setting` is turned on. Until that moment `ConditionalDisplay` is the only thing holding the plugin's notifications back, so enabling it would otherwise release the whole back-catalogue at once.
+
+Changes below the notification threshold at backfill time are deliberately left alone, so they notify normally when they later reach it.
+
+**Promotion still happens.** The backfill only suppresses *notifications*. `NotifyPromotion` keys its "already handled?" policy (`promotion_not_already_handled`) on the `automatically_promoted` event, and separately skips only the notification steps when an `admins_notified_automatic_promotion` event exists. So a backfilled change still gets its `automatically_promoted` event and still fires `DiscourseEvent(:upcoming_change_enabled)`. That matters because the event is what applies a change's real side effects, and because the `automatically_promoted` record is read elsewhere as the "when did this turn on" anchor — see `BrowserPageviewEvent#beacon_cutover_date` and `SiteSetting::Action::RemoveAndReplaceUncategorizedToggled::SNAPSHOT_EVENT_TYPES`.
+
+When adding a policy to `NotifyPromotion`, be clear about which of the two questions it answers. A policy aborts the whole service, so anything about *notification* belongs in the `notify_admins?` guard instead — putting it in a policy would silently stop the change from promoting.
### Group-Based Access
diff --git a/app/jobs/scheduled/check_upcoming_changes.rb b/app/jobs/scheduled/check_upcoming_changes.rb
index 7aff9598f7d..8307edf6350 100644
--- a/app/jobs/scheduled/check_upcoming_changes.rb
+++ b/app/jobs/scheduled/check_upcoming_changes.rb
@@ -77,7 +77,7 @@ module Jobs
:info
when :should_not_be_displayed
:debug
- when :already_notified_about_promotion
+ when :already_promoted
:debug
when :already_manually_toggled
:debug
diff --git a/app/models/upcoming_change_event.rb b/app/models/upcoming_change_event.rb
index 0247f939696..d4537b3c0a2 100644
--- a/app/models/upcoming_change_event.rb
+++ b/app/models/upcoming_change_event.rb
@@ -19,6 +19,14 @@ class UpcomingChangeEvent < ActiveRecord::Base
}
default_scope { order(:created_at) }
+ scope :not_backfilled, -> { where("event_data->>'backfilled' IS DISTINCT FROM 'true'") }
+
+ def self.change_names_with_event(event_type)
+ UpcomingChangeEvent
+ .where(upcoming_change_name: SiteSetting.upcoming_change_site_settings, event_type:)
+ .pluck(:upcoming_change_name)
+ .map(&:to_sym)
+ end
end
# == Schema Information
diff --git a/app/serializers/current_user_serializer.rb b/app/serializers/current_user_serializer.rb
index 51376754334..b0e94c2d814 100644
--- a/app/serializers/current_user_serializer.rb
+++ b/app/serializers/current_user_serializer.rb
@@ -201,7 +201,7 @@ class CurrentUserSerializer < BasicUserSerializer
last_visited = object.custom_fields["last_visited_upcoming_changes_at"]
return false if last_visited.blank? && object.created_at < Discourse.site_creation_date + 1.hour
cutoff = last_visited.present? ? Time.zone.parse(last_visited) : object.created_at
- UpcomingChangeEvent.added.where("created_at > ?", cutoff).exists?
+ UpcomingChangeEvent.added.not_backfilled.where("created_at > ?", cutoff).exists?
end
def can_post_anonymously
diff --git a/app/services/upcoming_changes/action/backfill_notified_events.rb b/app/services/upcoming_changes/action/backfill_notified_events.rb
new file mode 100644
index 00000000000..85cab80dae5
--- /dev/null
+++ b/app/services/upcoming_changes/action/backfill_notified_events.rb
@@ -0,0 +1,74 @@
+# frozen_string_literal: true
+
+# Marks upcoming changes as "already notified about", so admins are never told
+# about a back-catalogue of changes that pre-dates them being able to act on it.
+#
+# Called at the two points where a site gains a set of upcoming changes it was
+# never in a position to hear about:
+#
+# * Site creation (db/fixtures/995_upcoming_changes.rb)
+# * A plugin being enabled (config/initializers/015-track-upcoming-change-toggle.rb),
+# since a disabled plugin's settings are still registered, and therefore its
+# changes are only held back by ConditionalDisplay, not by the audit trail.
+#
+# Only changes that are *notifiable right now* are marked. A change still sitting
+# below the notification threshold is left alone, so it notifies normally when it
+# later reaches that threshold -- the site genuinely pre-dates that milestone.
+#
+# Deliberately does not write `automatically_promoted` or fire
+# :upcoming_change_enabled. Promotion still has to happen for real (see
+# UpcomingChanges::NotifyPromotion) -- it is only the notification we suppress.
+class UpcomingChanges::Action::BackfillNotifiedEvents < Service::ActionBase
+ option :upcoming_change_names, default: -> { SiteSetting.upcoming_change_site_settings }
+
+ def call
+ return [] if upcoming_change_names.blank?
+
+ UpcomingChangeEvent.insert_all(
+ event_records_to_insert,
+ unique_by: :idx_upcoming_change_events_unique_once_off,
+ )
+
+ upcoming_change_names
+ end
+
+ private
+
+ def event_records_to_insert
+ now = Time.zone.now
+
+ upcoming_change_names.flat_map do |change_name|
+ event_types_for(change_name).map do |event_type|
+ {
+ event_type: UpcomingChangeEvent.event_types[event_type],
+ event_data: {
+ backfilled: true,
+ },
+ upcoming_change_name: change_name,
+ created_at: now,
+ updated_at: now,
+ }
+ end
+ end
+ end
+
+ def event_types_for(change_name)
+ event_types = [:added]
+
+ if UpcomingChanges.meets_or_exceeds_status?(change_name, promote_status)
+ event_types << :admins_notified_automatic_promotion
+ elsif UpcomingChanges.meets_or_exceeds_status?(change_name, available_status)
+ event_types << :admins_notified_available_change
+ end
+
+ event_types
+ end
+
+ def promote_status
+ @promote_status ||= SiteSetting.promote_upcoming_changes_on_status.to_sym
+ end
+
+ def available_status
+ @available_status ||= UpcomingChanges.previous_status(promote_status)
+ end
+end
diff --git a/app/services/upcoming_changes/notify_promotion.rb b/app/services/upcoming_changes/notify_promotion.rb
index 13b26c2757a..d5cb81392e9 100644
--- a/app/services/upcoming_changes/notify_promotion.rb
+++ b/app/services/upcoming_changes/notify_promotion.rb
@@ -13,6 +13,14 @@
# both via a staff action log and a Notification in the UI.
# We don't need to notify admins if they have manually opted in
# or out of the change, since that overrides the automatic promotion.
+#
+# Note that "has this promotion been handled?" and "have admins been notified?"
+# are deliberately separate questions. The former is keyed on the
+# automatically_promoted event and gates the whole service, since promoting twice
+# would re-fire :upcoming_change_enabled every time the job runs. The latter is
+# keyed on the admins_notified_automatic_promotion event and only skips the
+# notification, so a change that was pre-marked as notified (see
+# UpcomingChanges::Action::BackfillNotifiedEvents) still promotes for real.
class UpcomingChanges::NotifyPromotion
include Service::Base
@@ -20,6 +28,7 @@ class UpcomingChanges::NotifyPromotion
attribute :setting_name, :symbol
attribute :admin_user_ids, :array
attribute :changes_already_notified_about_promotion, :array, default: []
+ attribute :changes_already_promoted, :array, default: []
validates :setting_name, presence: true
validates :admin_user_ids, presence: true
@@ -28,17 +37,20 @@ class UpcomingChanges::NotifyPromotion
policy :setting_is_available
policy :change_should_be_displayed
policy :meets_or_exceeds_status
- policy :change_has_not_already_been_notified_about_promotion
+ policy :promotion_not_already_handled
policy :admin_has_not_manually_toggled
policy :should_notify_admins
try do
transaction do
step :log_promotion
- model :existing_notifications, optional: true
- model :bulk_notification_new_records
- step :notify_admins
- step :create_event
+ only_if(:notify_admins?) do
+ model :existing_notifications, optional: true
+ model :bulk_notification_new_records, optional: true
+ step :notify_admins
+ step :create_notified_event
+ end
+ step :create_automatically_promoted_event
end
step :trigger_discourse_event
@@ -61,8 +73,8 @@ class UpcomingChanges::NotifyPromotion
)
end
- def change_has_not_already_been_notified_about_promotion(params:)
- !params.changes_already_notified_about_promotion.include?(params.setting_name)
+ def promotion_not_already_handled(params:)
+ !params.changes_already_promoted.include?(params.setting_name)
end
def admin_has_not_manually_toggled(params:)
@@ -89,6 +101,10 @@ class UpcomingChanges::NotifyPromotion
)
end
+ def notify_admins?(params:)
+ !params.changes_already_notified_about_promotion.include?(params.setting_name)
+ end
+
def fetch_existing_notifications(params:)
Notification.where(
notification_type: Notification.types[:upcoming_change_automatically_promoted],
@@ -124,13 +140,15 @@ class UpcomingChanges::NotifyPromotion
end
end
- def create_event(params:)
+ def create_notified_event(params:)
UpcomingChangeEvent.create!(
event_type: :admins_notified_automatic_promotion,
upcoming_change_name: params.setting_name,
acting_user: Discourse.system_user,
)
+ end
+ def create_automatically_promoted_event(params:)
UpcomingChangeEvent.find_or_create_by(
event_type: :automatically_promoted,
upcoming_change_name: params.setting_name,
diff --git a/app/services/upcoming_changes/notify_promotions.rb b/app/services/upcoming_changes/notify_promotions.rb
index 71b072398c7..dfd5410e27e 100644
--- a/app/services/upcoming_changes/notify_promotions.rb
+++ b/app/services/upcoming_changes/notify_promotions.rb
@@ -6,26 +6,29 @@ class UpcomingChanges::NotifyPromotions
include Service::Base
model :changes_already_notified_about_promotion, optional: true
+ model :changes_already_promoted, optional: true
model :admin_user_ids
model :change_notification_statuses
private
def fetch_changes_already_notified_about_promotion
- UpcomingChangeEvent
- .where(
- upcoming_change_name: SiteSetting.upcoming_change_site_settings,
- event_type: :admins_notified_automatic_promotion,
- )
- .pluck(:upcoming_change_name)
- .map(&:to_sym)
+ UpcomingChangeEvent.change_names_with_event(:admins_notified_automatic_promotion)
+ end
+
+ def fetch_changes_already_promoted
+ UpcomingChangeEvent.change_names_with_event(:automatically_promoted)
end
def fetch_admin_user_ids
User.human_users.admins.pluck(:id)
end
- def fetch_change_notification_statuses(changes_already_notified_about_promotion:, admin_user_ids:)
+ def fetch_change_notification_statuses(
+ changes_already_notified_about_promotion:,
+ changes_already_promoted:,
+ admin_user_ids:
+ )
SiteSetting.upcoming_change_site_settings.index_with do |setting_name|
status_hash = {}
@@ -35,6 +38,7 @@ class UpcomingChanges::NotifyPromotions
params: {
setting_name: setting_name.to_sym,
changes_already_notified_about_promotion:,
+ changes_already_promoted:,
admin_user_ids:,
},
guardian: Discourse.system_user.guardian,
@@ -65,11 +69,9 @@ class UpcomingChanges::NotifyPromotions
status_hash[:error_key] = :does_not_meet_or_exceed_promotion_status
end
- on_failed_policy(:change_has_not_already_been_notified_about_promotion) do |policy|
- status_hash[
- :error
- ] = "Setting #{setting_name} has already notified admins about promotion"
- status_hash[:error_key] = :already_notified_about_promotion
+ on_failed_policy(:promotion_not_already_handled) do |policy|
+ status_hash[:error] = "Setting #{setting_name} has already been promoted"
+ status_hash[:error_key] = :already_promoted
end
on_failed_policy(:admin_has_not_manually_toggled) do |policy|
diff --git a/config/initializers/015-track-upcoming-change-toggle.rb b/config/initializers/015-track-upcoming-change-toggle.rb
index e22f3be24f1..5d9f32b6c25 100644
--- a/config/initializers/015-track-upcoming-change-toggle.rb
+++ b/config/initializers/015-track-upcoming-change-toggle.rb
@@ -33,3 +33,32 @@ DiscourseEvent.on(:upcoming_change_disabled) do |setting_name|
SiteSetting::Action::RemoveAndReplaceUncategorizedToggled.call(enabled: false)
end
end
+
+# A plugin's settings are registered whether or not it is enabled, so its upcoming
+# changes have been accumulating in the audit trail all along, only
+# ConditionalDisplay was holding their notifications back. Enabling the plugin would
+# otherwise release that whole back-catalogue at once, which is noise. The admin just
+# opted into the plugin, they did not have anything change out from under them, so they
+# don't need to have a flood of notifications.
+#
+# Treat it the same way as a new site and mark those changes as already notified
+# about. Promotion itself still happens, so :upcoming_change_enabled fires as usual.
+DiscourseEvent.on(:site_setting_changed) do |name, _old_value, new_value|
+ next if !new_value
+
+ plugin_name = SiteSetting.plugins[name]
+ next if plugin_name.blank?
+
+ plugin = Discourse.plugins_by_name[plugin_name]
+ next if plugin&.enabled_site_setting&.to_sym != name.to_sym
+
+ all_plugin_upcoming_changes =
+ SiteSetting.upcoming_change_site_settings.select do |change_name|
+ SiteSetting.plugins[change_name] == plugin_name
+ end
+ next if all_plugin_upcoming_changes.blank?
+
+ UpcomingChanges::Action::BackfillNotifiedEvents.call(
+ upcoming_change_names: all_plugin_upcoming_changes,
+ )
+end
diff --git a/db/fixtures/995_upcoming_changes.rb b/db/fixtures/995_upcoming_changes.rb
new file mode 100644
index 00000000000..7c9b8cee09b
--- /dev/null
+++ b/db/fixtures/995_upcoming_changes.rb
@@ -0,0 +1,11 @@
+# frozen_string_literal: true
+
+# A brand new site has no back-catalogue to be told about, every upcoming change
+# that exists right now pre-dates it. Marking them as notified here makes that
+# permanent, rather than relying on the one hour Migration::Helpers.new_site?
+# window, which the weekly and 20 minute notification jobs both outlive.
+#
+# This intentionally covers every installed plugin's changes, enabled or not, since
+# their settings are registered regardless. A plugin installed *after* the site was
+# created is handled on enable instead, in 015-track-upcoming-change-toggle.rb.
+UpcomingChanges::Action::BackfillNotifiedEvents.call if Migration::Helpers.new_site?
diff --git a/spec/initializers/track_upcoming_change_toggle_spec.rb b/spec/initializers/track_upcoming_change_toggle_spec.rb
new file mode 100644
index 00000000000..054893ca90a
--- /dev/null
+++ b/spec/initializers/track_upcoming_change_toggle_spec.rb
@@ -0,0 +1,85 @@
+# frozen_string_literal: true
+
+RSpec.describe "Upcoming change toggles" do
+ describe "when a plugin is enabled" do
+ let(:plugin) do
+ Plugin::Instance.new.tap do |instance|
+ instance.metadata = Plugin::Metadata.new.tap { |m| m.name = "my-plugin" }
+ instance.enabled_site_setting(:enable_upload_debug_mode)
+ end
+ end
+
+ before do
+ SiteSetting.promote_upcoming_changes_on_status = :beta
+
+ mock_upcoming_change_metadata(
+ {
+ show_user_menu_avatars: {
+ impact: "feature,all_members",
+ status: :stable,
+ impact_type: "feature",
+ impact_role: "all_members",
+ },
+ enable_experimental_admin_ui_grouped_filters: {
+ impact: "feature,admins",
+ status: :stable,
+ impact_type: "feature",
+ impact_role: "admins",
+ },
+ },
+ )
+
+ Discourse.plugins_by_name["my-plugin"] = plugin
+ SiteSetting.plugins[:enable_upload_debug_mode] = "my-plugin"
+ SiteSetting.plugins[:show_user_menu_avatars] = "my-plugin"
+
+ UpcomingChangeEvent.delete_all
+ end
+
+ after do
+ Discourse.plugins_by_name.delete("my-plugin")
+ SiteSetting.plugins.delete(:enable_upload_debug_mode)
+ SiteSetting.plugins.delete(:show_user_menu_avatars)
+ end
+
+ def trigger_enable
+ DiscourseEvent.trigger(:site_setting_changed, :enable_upload_debug_mode, false, true)
+ end
+
+ it "marks the plugin's upcoming changes as already notified about" do
+ trigger_enable
+
+ expect(
+ UpcomingChangeEvent.where(upcoming_change_name: :show_user_menu_avatars).pluck(:event_type),
+ ).to contain_exactly("added", "admins_notified_automatic_promotion")
+ end
+
+ it "leaves changes owned by core and other plugins alone" do
+ trigger_enable
+
+ expect(
+ UpcomingChangeEvent.exists?(
+ upcoming_change_name: :enable_experimental_admin_ui_grouped_filters,
+ ),
+ ).to eq(false)
+ end
+
+ it "does nothing when the plugin is disabled" do
+ expect {
+ DiscourseEvent.trigger(:site_setting_changed, :enable_upload_debug_mode, true, false)
+ }.not_to change { UpcomingChangeEvent.count }
+ end
+
+ it "does nothing for a setting that is not a plugin's enabled setting" do
+ expect {
+ DiscourseEvent.trigger(:site_setting_changed, :show_user_menu_avatars, false, true)
+ }.not_to change { UpcomingChangeEvent.count }
+ end
+
+ it "is a no-op when the plugin is re-enabled" do
+ trigger_enable
+
+ expect { trigger_enable }.not_to change { UpcomingChangeEvent.count }
+ end
+ end
+end
diff --git a/spec/jobs/scheduled/notify_admins_of_available_upcoming_changes_spec.rb b/spec/jobs/scheduled/notify_admins_of_available_upcoming_changes_spec.rb
index 115c9ac98b7..fa9790f4719 100644
--- a/spec/jobs/scheduled/notify_admins_of_available_upcoming_changes_spec.rb
+++ b/spec/jobs/scheduled/notify_admins_of_available_upcoming_changes_spec.rb
@@ -184,6 +184,72 @@ RSpec.describe Jobs::NotifyAdminsOfAvailableUpcomingChanges do
end
end
+ context "when the changes were backfilled as already notified about" do
+ before do
+ SiteSetting.promote_upcoming_changes_on_status = :stable
+ UpcomingChangeEvent.delete_all
+ UpcomingChanges::Action::BackfillNotifiedEvents.call(
+ upcoming_change_names: %i[
+ test_upcoming_change
+ test_upcoming_change_b
+ test_upcoming_change_c
+ ],
+ )
+ end
+
+ it "sends no notifications, even once the site is no longer new" do
+ expect { result }.not_to change { Notification.count }
+ end
+
+ context "when a backfilled change later reaches the available status" do
+ before do
+ mock_upcoming_change_metadata(
+ {
+ test_upcoming_change_d: {
+ impact: "feature,all_members",
+ status: :experimental,
+ impact_type: "feature",
+ impact_role: "all_members",
+ },
+ },
+ )
+ UpcomingChanges::Action::BackfillNotifiedEvents.call(
+ upcoming_change_names: [:test_upcoming_change_d],
+ )
+
+ mock_upcoming_change_metadata(
+ {
+ test_upcoming_change_d: {
+ impact: "feature,all_members",
+ status: :beta,
+ impact_type: "feature",
+ impact_role: "all_members",
+ },
+ },
+ )
+ UpcomingChangeEvent.create!(
+ event_type: :status_changed,
+ upcoming_change_name: :test_upcoming_change_d,
+ event_data: {
+ previous_value: :experimental,
+ new_value: :beta,
+ },
+ )
+ end
+
+ it "notifies admins about it" do
+ result
+
+ expect(
+ Notification
+ .where(notification_type: Notification.types[:upcoming_change_available])
+ .where("data::text LIKE ?", "%test_upcoming_change_d%")
+ .count,
+ ).to eq(2)
+ end
+ end
+ end
+
context "when an upcoming change has an old added event" do
before { new_change_log.update!(created_at: 2.weeks.ago) }
diff --git a/spec/models/upcoming_change_event_spec.rb b/spec/models/upcoming_change_event_spec.rb
new file mode 100644
index 00000000000..a2b8bccf19b
--- /dev/null
+++ b/spec/models/upcoming_change_event_spec.rb
@@ -0,0 +1,30 @@
+# frozen_string_literal: true
+
+RSpec.describe UpcomingChangeEvent do
+ describe ".not_backfilled" do
+ it "excludes backfilled events" do
+ event_without_data =
+ described_class.create!(event_type: :added, upcoming_change_name: "event_without_data")
+ explicitly_not_backfilled_event =
+ described_class.create!(
+ event_type: :added,
+ event_data: {
+ backfilled: false,
+ },
+ upcoming_change_name: "explicitly_not_backfilled_event",
+ )
+ described_class.create!(
+ event_type: :added,
+ event_data: {
+ backfilled: true,
+ },
+ upcoming_change_name: "backfilled_event",
+ )
+
+ expect(described_class.not_backfilled).to contain_exactly(
+ event_without_data,
+ explicitly_not_backfilled_event,
+ )
+ end
+ end
+end
diff --git a/spec/serializers/current_user_serializer_spec.rb b/spec/serializers/current_user_serializer_spec.rb
index 7aacf994b47..e7ded57ad1c 100644
--- a/spec/serializers/current_user_serializer_spec.rb
+++ b/spec/serializers/current_user_serializer_spec.rb
@@ -541,6 +541,21 @@ RSpec.describe CurrentUserSerializer do
expect(serialize_for(admin)[:has_new_upcoming_changes]).to eq(true)
end
+ it "ignores backfilled `added` events" do
+ Discourse.stubs(:site_creation_date).returns(2.years.ago)
+ admin = Fabricate(:admin)
+ UpcomingChangeEvent.create!(
+ event_type: :added,
+ event_data: {
+ backfilled: true,
+ },
+ upcoming_change_name: "backfilled_setting",
+ created_at: 1.minute.from_now,
+ )
+
+ expect(serialize_for(admin)[:has_new_upcoming_changes]).to eq(false)
+ end
+
it "honors last_visited_upcoming_changes_at when set" do
Discourse.stubs(:site_creation_date).returns(2.years.ago)
admin = Fabricate(:admin)
diff --git a/spec/services/upcoming_changes/action/backfill_notified_events_spec.rb b/spec/services/upcoming_changes/action/backfill_notified_events_spec.rb
new file mode 100644
index 00000000000..22a4f653a8e
--- /dev/null
+++ b/spec/services/upcoming_changes/action/backfill_notified_events_spec.rb
@@ -0,0 +1,133 @@
+# frozen_string_literal: true
+
+RSpec.describe UpcomingChanges::Action::BackfillNotifiedEvents do
+ let(:change_names) do
+ %i[enable_upload_debug_mode show_user_menu_avatars enable_experimental_admin_ui_grouped_filters]
+ end
+
+ before do
+ SiteSetting.promote_upcoming_changes_on_status = :beta
+
+ mock_upcoming_change_metadata(
+ {
+ # Below the "available" threshold (alpha), so not notifiable yet.
+ enable_upload_debug_mode: {
+ impact: "other,developers",
+ status: :experimental,
+ impact_type: "other",
+ impact_role: "developers",
+ },
+ # At the "available" threshold.
+ show_user_menu_avatars: {
+ impact: "feature,all_members",
+ status: :alpha,
+ impact_type: "feature",
+ impact_role: "all_members",
+ },
+ # At/above the promotion threshold.
+ enable_experimental_admin_ui_grouped_filters: {
+ impact: "feature,admins",
+ status: :stable,
+ impact_type: "feature",
+ impact_role: "admins",
+ },
+ },
+ )
+
+ scoped_events.delete_all
+ end
+
+ def scoped_events
+ UpcomingChangeEvent.where(upcoming_change_name: change_names)
+ end
+
+ def event_types_for(change_name)
+ scoped_events.where(upcoming_change_name: change_name).pluck(:event_type).map(&:to_sym)
+ end
+
+ describe ".call" do
+ subject(:result) { described_class.call(upcoming_change_names: change_names) }
+
+ it "records every change as added" do
+ result
+
+ expect(scoped_events.where(event_type: :added).pluck(:upcoming_change_name)).to match_array(
+ change_names.map(&:to_s),
+ )
+ end
+
+ it "does not mark a change below the available threshold as notified about" do
+ result
+
+ expect(event_types_for(:enable_upload_debug_mode)).to contain_exactly(:added)
+ end
+
+ it "marks a change at the available threshold as notified about availability" do
+ result
+
+ expect(event_types_for(:show_user_menu_avatars)).to contain_exactly(
+ :added,
+ :admins_notified_available_change,
+ )
+ end
+
+ it "marks a change at the promotion threshold as notified about promotion" do
+ result
+
+ expect(event_types_for(:enable_experimental_admin_ui_grouped_filters)).to contain_exactly(
+ :added,
+ :admins_notified_automatic_promotion,
+ )
+ end
+
+ it "does not promote anything itself" do
+ result
+
+ expect(scoped_events.where(event_type: :automatically_promoted)).to be_empty
+ end
+
+ it "returns the changes it backfilled" do
+ expect(result).to match_array(change_names)
+ end
+
+ it "is idempotent" do
+ described_class.call(change_names: change_names)
+
+ expect { result }.not_to change { scoped_events.count }
+ end
+
+ it "does not duplicate events that already exist" do
+ UpcomingChangeEvent.create!(event_type: :added, upcoming_change_name: :show_user_menu_avatars)
+
+ result
+
+ expect(event_types_for(:show_user_menu_avatars)).to contain_exactly(
+ :added,
+ :admins_notified_available_change,
+ )
+ end
+
+ context "when no change names are given" do
+ subject(:result) { described_class.call(upcoming_change_names: []) }
+
+ it "does nothing" do
+ expect { result }.not_to change { UpcomingChangeEvent.count }
+ end
+ end
+
+ context "when change names are not passed" do
+ subject(:result) { described_class.call }
+
+ it "backfills every upcoming change on the site" do
+ result
+
+ expect(
+ UpcomingChangeEvent.where(
+ event_type: :added,
+ upcoming_change_name: SiteSetting.upcoming_change_site_settings,
+ ).count,
+ ).to eq(SiteSetting.upcoming_change_site_settings.count)
+ end
+ end
+ end
+end
diff --git a/spec/services/upcoming_changes/notify_promotion_spec.rb b/spec/services/upcoming_changes/notify_promotion_spec.rb
index 57192322c5e..d946c3ce25c 100644
--- a/spec/services/upcoming_changes/notify_promotion_spec.rb
+++ b/spec/services/upcoming_changes/notify_promotion_spec.rb
@@ -13,6 +13,7 @@ RSpec.describe UpcomingChanges::NotifyPromotion do
setting_name:,
admin_user_ids:,
changes_already_notified_about_promotion:,
+ changes_already_promoted:,
},
guardian: Discourse.system_user.guardian,
)
@@ -24,6 +25,7 @@ RSpec.describe UpcomingChanges::NotifyPromotion do
let(:setting_name) { :enable_upload_debug_mode }
let(:admin_user_ids) { [admin.id, admin_2.id] }
let(:changes_already_notified_about_promotion) { [] }
+ let(:changes_already_promoted) { [] }
let(:setting_status) { :stable }
before do
@@ -103,10 +105,50 @@ RSpec.describe UpcomingChanges::NotifyPromotion do
end
end
- context "when change has already been notified about promotion" do
+ context "when the change has already been promoted" do
+ let(:changes_already_promoted) { [:enable_upload_debug_mode] }
+
+ it { is_expected.to fail_a_policy(:promotion_not_already_handled) }
+ end
+
+ context "when the change has already been notified about, but not yet promoted" do
let(:changes_already_notified_about_promotion) { [:enable_upload_debug_mode] }
- it { is_expected.to fail_a_policy(:change_has_not_already_been_notified_about_promotion) }
+ it { is_expected.to run_successfully }
+
+ it "does not notify admins" do
+ expect { result }.not_to change {
+ Notification.where(
+ notification_type: Notification.types[:upcoming_change_automatically_promoted],
+ ).count
+ }
+ end
+
+ it "does not record that admins were notified" do
+ expect { result }.not_to change {
+ UpcomingChangeEvent.where(
+ event_type: :admins_notified_automatic_promotion,
+ upcoming_change_name: :enable_upload_debug_mode,
+ ).count
+ }
+ end
+
+ it "still promotes the change for real" do
+ events = DiscourseEvent.track_events { result }
+
+ expect(
+ events.select do |e|
+ e[:event_name] == :upcoming_change_enabled &&
+ e[:params].first == :enable_upload_debug_mode
+ end,
+ ).to be_present
+ expect(
+ UpcomingChangeEvent.exists?(
+ event_type: :automatically_promoted,
+ upcoming_change_name: :enable_upload_debug_mode,
+ ),
+ ).to eq(true)
+ end
end
context "when admin has manually opted out" do
diff --git a/spec/services/upcoming_changes/notify_promotions_spec.rb b/spec/services/upcoming_changes/notify_promotions_spec.rb
index d9d7041e006..8a020959c07 100644
--- a/spec/services/upcoming_changes/notify_promotions_spec.rb
+++ b/spec/services/upcoming_changes/notify_promotions_spec.rb
@@ -210,16 +210,16 @@ RSpec.describe UpcomingChanges::NotifyPromotions do
end
end
- context "when settings are already notified about promotion" do
+ context "when settings have already been promoted" do
before do
UpcomingChangeEvent.create!(
- event_type: :admins_notified_automatic_promotion,
+ event_type: :automatically_promoted,
upcoming_change_name: :enable_upload_debug_mode,
acting_user: Discourse.system_user,
)
end
- it "does not notify admins again for the already-notified setting" do
+ it "does not notify admins again for the already-promoted setting" do
expect { result }.not_to change {
Notification
.where(notification_type: Notification.types[:upcoming_change_automatically_promoted])
@@ -228,7 +228,7 @@ RSpec.describe UpcomingChanges::NotifyPromotions do
}
end
- it "does not trigger event for the already-notified setting" do
+ it "does not trigger event for the already-promoted setting" do
events = DiscourseEvent.track_events { result }
expect(
events.select do |e|
@@ -241,10 +241,46 @@ RSpec.describe UpcomingChanges::NotifyPromotions do
it "returns the correct error and error key" do
expect(result[:change_notification_statuses][:enable_upload_debug_mode]).to match(
success: false,
- error: "Setting enable_upload_debug_mode has already notified admins about promotion",
- error_key: :already_notified_about_promotion,
+ error: "Setting enable_upload_debug_mode has already been promoted",
+ error_key: :already_promoted,
+ )
+ end
+ end
+
+ context "when settings are marked as already notified about, but not promoted" do
+ before do
+ UpcomingChangeEvent.create!(
+ event_type: :admins_notified_automatic_promotion,
+ upcoming_change_name: :enable_upload_debug_mode,
+ acting_user: Discourse.system_user,
)
end
+
+ it "does not notify admins" do
+ expect { result }.not_to change {
+ Notification
+ .where(notification_type: Notification.types[:upcoming_change_automatically_promoted])
+ .where("data::text LIKE ?", "%enable_upload_debug_mode%")
+ .count
+ }
+ end
+
+ it "still promotes the change, so its side effects run" do
+ events = DiscourseEvent.track_events { result }
+
+ expect(
+ events.select do |e|
+ e[:event_name] == :upcoming_change_enabled &&
+ e[:params].first == :enable_upload_debug_mode
+ end,
+ ).to be_present
+ expect(
+ UpcomingChangeEvent.exists?(
+ event_type: :automatically_promoted,
+ upcoming_change_name: :enable_upload_debug_mode,
+ ),
+ ).to eq(true)
+ end
end
context "when a change should not be displayed on this site" do