Skip to content

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

FEATURE: disable link notification user preference (#35352)

Régis Hanolc5b32aff76db
diff --git a/AI-AGENTS.md b/AI-AGENTS.md
index bda2eafdd1a..c4bff4ecc5a 100644
--- a/AI-AGENTS.md
+++ b/AI-AGENTS.md
@@ -35,6 +35,13 @@ Discourse is large with long history. Understand context before changes.
- Don't write obvious tests
- Ruby: use `fab!()` over `let()`, system tests for UI (`spec/system`), use page objects for system spec finders (`spec/system/page_objects`)
+### Page Objects (System Specs)
+- Located in `spec/system/page_objects/pages/`, inherit from `PageObjects::Pages::Base`
+- NEVER store `find()` results - causes stale element references after re-renders
+- Use `has_x?` / `has_no_x?` patterns for state checks (finds fresh each time)
+- Action methods find+interact atomically, return `self` for chaining
+- Don't assert immediate UI feedback after clicks (tests browser, not app logic)
+
### Commands
```bash
# Ruby tests
diff --git a/app/assets/javascripts/discourse/app/controllers/preferences/notifications.js b/app/assets/javascripts/discourse/app/controllers/preferences/notifications.js
index 17b9ac64c86..a783924d832 100644
--- a/app/assets/javascripts/discourse/app/controllers/preferences/notifications.js
+++ b/app/assets/javascripts/discourse/app/controllers/preferences/notifications.js
@@ -10,13 +10,14 @@ export default class NotificationsController extends Controller {
super.init(...arguments);
this.saveAttrNames = [
+ "allow_private_messages",
+ "auto_track_topics_after_msecs",
+ "enable_allowed_pm_users",
+ "like_notification_frequency",
"muted_usernames",
"new_topic_duration_minutes",
- "auto_track_topics_after_msecs",
"notification_level_when_replying",
- "like_notification_frequency",
- "allow_private_messages",
- "enable_allowed_pm_users",
+ "notify_on_linked_posts",
"user_notification_schedule",
];
diff --git a/app/assets/javascripts/discourse/app/models/user.js b/app/assets/javascripts/discourse/app/models/user.js
index a88c051e2aa..19ce65c324c 100644
--- a/app/assets/javascripts/discourse/app/models/user.js
+++ b/app/assets/javascripts/discourse/app/models/user.js
@@ -70,30 +70,30 @@ export function extendTextSizeCookie() {
const isForever = (dt) => moment().diff(dt, "years") < -100;
let userFields = [
+ "allowed_pm_usernames",
"bio_raw",
- "website",
- "location",
- "name",
- "title",
- "locale",
+ "card_background_upload_url",
"custom_fields",
- "user_fields",
- "muted_usernames",
+ "date_of_birth",
+ "flair_group_id",
"ignored_usernames",
- "allowed_pm_usernames",
- "profile_background_upload_url",
- "card_background_upload_url",
+ "locale",
+ "location",
"muted_tags",
- "tracked_tags",
- "watched_tags",
- "watching_first_post_tags",
- "date_of_birth",
+ "muted_usernames",
+ "name",
"primary_group_id",
- "flair_group_id",
- "user_notification_schedule",
+ "profile_background_upload_url",
"sidebar_category_ids",
"sidebar_tag_names",
"status",
+ "title",
+ "tracked_tags",
+ "user_fields",
+ "user_notification_schedule",
+ "watched_tags",
+ "watching_first_post_tags",
+ "website",
];
export function addSaveableUserField(fieldName) {
@@ -101,47 +101,48 @@ export function addSaveableUserField(fieldName) {
}
let userOptionFields = [
- "mailing_list_mode",
- "mailing_list_mode_frequency",
- "external_links_in_new_tab",
+ "allow_private_messages",
+ "auto_track_topics_after_msecs",
+ "automatically_unpin_topics",
+ "bookmark_auto_delete_preference",
+ "color_scheme_id",
+ "composition_mode",
+ "dark_scheme_id",
+ "default_calendar",
+ "digest_after_minutes",
+ "dynamic_favicon",
"email_digests",
"email_in_reply_to",
- "email_messages_level",
"email_level",
+ "email_messages_level",
"email_previous_replies",
- "color_scheme_id",
- "dark_scheme_id",
- "interface_color_mode",
- "dynamic_favicon",
- "enable_quoting",
- "enable_smart_lists",
+ "enable_allowed_pm_users",
"enable_defer",
"enable_markdown_monospace_font",
- "automatically_unpin_topics",
- "digest_after_minutes",
+ "enable_quoting",
+ "enable_smart_lists",
+ "external_links_in_new_tab",
+ "hide_presence",
+ "hide_profile",
+ "homepage_id",
+ "include_tl0_in_digests",
+ "interface_color_mode",
+ "like_notification_frequency",
+ "mailing_list_mode",
+ "mailing_list_mode_frequency",
"new_topic_duration_minutes",
- "auto_track_topics_after_msecs",
"notification_level_when_replying",
- "like_notification_frequency",
- "include_tl0_in_digests",
- "theme_ids",
- "allow_private_messages",
- "enable_allowed_pm_users",
- "homepage_id",
- "hide_profile",
- "hide_presence",
- "text_size",
- "title_count_mode",
- "timezone",
- "skip_new_user_tips",
+ "notify_on_linked_posts",
"seen_popups",
- "default_calendar",
- "bookmark_auto_delete_preference",
"sidebar_link_to_filtered_list",
"sidebar_show_count_of_new_items",
- "watched_precedence_over_muted",
+ "skip_new_user_tips",
+ "text_size",
+ "theme_ids",
+ "timezone",
+ "title_count_mode",
"topics_unread_when_closed",
- "composition_mode",
+ "watched_precedence_over_muted",
];
export function addSaveableUserOptionField(fieldName) {
diff --git a/app/assets/javascripts/discourse/app/templates/preferences/notifications.gjs b/app/assets/javascripts/discourse/app/templates/preferences/notifications.gjs
index 0681b807d7a..1daeed3ac68 100644
--- a/app/assets/javascripts/discourse/app/templates/preferences/notifications.gjs
+++ b/app/assets/javascripts/discourse/app/templates/preferences/notifications.gjs
@@ -2,6 +2,7 @@ import { fn } from "@ember/helper";
import RouteTemplate from "ember-route-template";
import DesktopNotificationConfig from "discourse/components/desktop-notification-config";
import PluginOutlet from "discourse/components/plugin-outlet";
+import PreferenceCheckbox from "discourse/components/preference-checkbox";
import SaveControls from "discourse/components/save-controls";
import UserNotificationSchedule from "discourse/components/user-notification-schedule";
import lazyHash from "discourse/helpers/lazy-hash";
@@ -27,6 +28,13 @@ export default RouteTemplate(
}}
/>
</div>
+
+ <PreferenceCheckbox
+ @labelKey="user.notify_on_linked_posts"
+ @checked={{@controller.model.user_option.notify_on_linked_posts}}
+ data-setting-name="user-notify-on-linked-posts"
+ class="pref-notify-on-linked-posts"
+ />
</div>
{{#unless @controller.capabilities.isAppWebview}}
diff --git a/app/models/user_option.rb b/app/models/user_option.rb
index 9e1c57f5540..cd6d9c70749 100644
--- a/app/models/user_option.rb
+++ b/app/models/user_option.rb
@@ -292,6 +292,7 @@ end
# mailing_list_mode_frequency :integer default(1), not null
# new_topic_duration_minutes :integer
# notification_level_when_replying :integer
+# notify_on_linked_posts :boolean default(TRUE), not null
# oldest_search_log_date :datetime
# seen_popups :integer is an Array
# sidebar_link_to_filtered_list :boolean default(FALSE), not null
diff --git a/app/serializers/current_user_option_serializer.rb b/app/serializers/current_user_option_serializer.rb
index c23e3a3cc94..741a2085c63 100644
--- a/app/serializers/current_user_option_serializer.rb
+++ b/app/serializers/current_user_option_serializer.rb
@@ -18,6 +18,7 @@ class CurrentUserOptionSerializer < ApplicationSerializer
:skip_new_user_tips,
:default_calendar,
:bookmark_auto_delete_preference,
+ :notify_on_linked_posts,
:seen_popups,
:should_be_redirected_to_top,
:redirected_to_top,
diff --git a/app/serializers/user_option_serializer.rb b/app/serializers/user_option_serializer.rb
index c36a50807e6..62a5d7a3fa0 100644
--- a/app/serializers/user_option_serializer.rb
+++ b/app/serializers/user_option_serializer.rb
@@ -23,6 +23,7 @@ class UserOptionSerializer < ApplicationSerializer
:email_previous_replies,
:email_in_reply_to,
:like_notification_frequency,
+ :notify_on_linked_posts,
:include_tl0_in_digests,
:theme_ids,
:theme_key_seq,
diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rb
index ee8d13527b5..af7a79e5204 100644
--- a/app/services/post_alerter.rb
+++ b/app/services/post_alerter.rb
@@ -539,6 +539,8 @@ class PostAlerter
return
end
+ return if type == Notification.types[:linked] && !user.user_option.notify_on_linked_posts
+
return if !Guardian.new(user).can_receive_post_notifications?(post)
return if user.staged? && topic.category&.mailinglist_mirror?
diff --git a/app/services/user_updater.rb b/app/services/user_updater.rb
index dcb62f65808..f9187c7c5c0 100644
--- a/app/services/user_updater.rb
+++ b/app/services/user_updater.rb
@@ -39,6 +39,7 @@ class UserUpdater
email_previous_replies
email_in_reply_to
like_notification_frequency
+ notify_on_linked_posts
include_tl0_in_digests
theme_ids
allow_private_messages
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index a56e68d61ea..525d64bed00 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -2090,6 +2090,7 @@ en:
first_time_and_daily: "First time a post is liked and daily"
first_time: "First time a post is liked"
never: "Never"
+ notify_on_linked_posts: "Notify when someone shares a link to one of my posts"
email_previous_replies:
title: "Include previous replies at the bottom of emails"
unless_emailed: "unless previously sent"
diff --git a/db/migrate/20251013091641_add_notify_on_linked_posts_to_user_options.rb b/db/migrate/20251013091641_add_notify_on_linked_posts_to_user_options.rb
new file mode 100644
index 00000000000..f776cd30804
--- /dev/null
+++ b/db/migrate/20251013091641_add_notify_on_linked_posts_to_user_options.rb
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+class AddNotifyOnLinkedPostsToUserOptions < ActiveRecord::Migration[7.1]
+ def change
+ add_column :user_options, :notify_on_linked_posts, :boolean, default: true, null: false
+ end
+end
diff --git a/spec/requests/api/schemas/json/user_get_response.json b/spec/requests/api/schemas/json/user_get_response.json
index 9fe3b095338..8bbf5af43c2 100644
--- a/spec/requests/api/schemas/json/user_get_response.json
+++ b/spec/requests/api/schemas/json/user_get_response.json
@@ -747,6 +747,9 @@
"like_notification_frequency": {
"type": "integer"
},
+ "notify_on_linked_posts": {
+ "type": "boolean"
+ },
"include_tl0_in_digests": {
"type": "boolean"
},
@@ -849,6 +852,7 @@
"email_previous_replies",
"email_in_reply_to",
"like_notification_frequency",
+ "notify_on_linked_posts",
"include_tl0_in_digests",
"theme_ids",
"theme_key_seq",
diff --git a/spec/services/post_alerter_spec.rb b/spec/services/post_alerter_spec.rb
index beb279402c7..c07ceaa1282 100644
--- a/spec/services/post_alerter_spec.rb
+++ b/spec/services/post_alerter_spec.rb
@@ -733,6 +733,24 @@ RSpec.describe PostAlerter do
staged_user.notifications.where(notification_type: Notification.types[:linked]).count,
).to eq(0)
end
+
+ it "does not notify when user has disabled linked post notifications" do
+ user.user_option.update!(notify_on_linked_posts: false)
+ linking_post
+
+ expect(user.notifications.where(notification_type: Notification.types[:linked]).count).to eq(
+ 0,
+ )
+ end
+
+ it "still notifies when user has enabled linked post notifications (default)" do
+ expect(user.user_option.notify_on_linked_posts).to eq(true)
+ linking_post
+
+ expect(user.notifications.where(notification_type: Notification.types[:linked]).count).to eq(
+ 1,
+ )
+ end
end
context "with @here" do
diff --git a/spec/system/page_objects/pages/user_preferences_notifications.rb b/spec/system/page_objects/pages/user_preferences_notifications.rb
new file mode 100644
index 00000000000..083015cf279
--- /dev/null
+++ b/spec/system/page_objects/pages/user_preferences_notifications.rb
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+module PageObjects
+ module Pages
+ class UserPreferencesNotifications < PageObjects::Pages::Base
+ def visit(user)
+ page.visit("/u/#{user.username}/preferences/notifications")
+ self
+ end
+
+ def has_notify_on_linked_posts_enabled?
+ has_css?(".pref-notify-on-linked-posts input[type='checkbox']:checked")
+ end
+
+ def has_notify_on_linked_posts_disabled?
+ has_css?(".pref-notify-on-linked-posts input[type='checkbox']:not(:checked)")
+ end
+
+ def toggle_notify_on_linked_posts
+ find(".pref-notify-on-linked-posts input[type='checkbox']").click
+ self
+ end
+
+ def save_changes
+ click_button "Save Changes"
+ expect(page).to have_css(".saved")
+ self
+ end
+ end
+ end
+end
diff --git a/spec/system/user_page/user_preferences_notifications_spec.rb b/spec/system/user_page/user_preferences_notifications_spec.rb
new file mode 100644
index 00000000000..17e8721d313
--- /dev/null
+++ b/spec/system/user_page/user_preferences_notifications_spec.rb
@@ -0,0 +1,35 @@
+# frozen_string_literal: true
+
+describe "User preferences | Notifications", type: :system do
+ fab!(:user)
+ let(:user_preferences_notifications_page) { PageObjects::Pages::UserPreferencesNotifications.new }
+
+ before { sign_in(user) }
+
+ describe "notify_on_linked_posts preference" do
+ it "correctly updates the user_option when toggling the checkbox" do
+ user_preferences_notifications_page.visit(user)
+
+ expect(user_preferences_notifications_page).to have_notify_on_linked_posts_enabled
+ expect(user.user_option.notify_on_linked_posts).to eq(true)
+
+ user_preferences_notifications_page.toggle_notify_on_linked_posts
+ user_preferences_notifications_page.save_changes
+
+ expect(user.user_option.reload.notify_on_linked_posts).to eq(false)
+
+ page.refresh
+
+ expect(user_preferences_notifications_page).to have_notify_on_linked_posts_disabled
+
+ user_preferences_notifications_page.toggle_notify_on_linked_posts
+ user_preferences_notifications_page.save_changes
+
+ expect(user.user_option.reload.notify_on_linked_posts).to eq(true)
+
+ page.refresh
+
+ expect(user_preferences_notifications_page).to have_notify_on_linked_posts_enabled
+ end
+ end
+end