Skip to content

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

FEATURE: add setting to opt out of solved notifications (#38730)

David Battersby938439198048
diff --git a/plugins/discourse-solved/app/services/discourse_solved/accept_answer.rb b/plugins/discourse-solved/app/services/discourse_solved/accept_answer.rb
index e414fa29fb2..20ad1ec55db 100644
--- a/plugins/discourse-solved/app/services/discourse_solved/accept_answer.rb
+++ b/plugins/discourse-solved/app/services/discourse_solved/accept_answer.rb
@@ -75,6 +75,7 @@ class DiscourseSolved::AcceptAnswer
def should_notify_post_author(post:, guardian:)
return if guardian.user.id == post.user_id || !User.exists?(post.user_id)
+ return if !UserOption.exists?(user_id: post.user_id, notify_on_solved: true)
screener = UserCommScreener.new(acting_user_id: guardian.user.id, target_user_ids: post.user_id)
!screener.ignoring_or_muting_actor?(post.user_id)
end
@@ -97,6 +98,7 @@ class DiscourseSolved::AcceptAnswer
def should_notify_topic_owner(topic:, guardian:)
return if !topic.category&.notify_on_staff_accept_solved?
return if guardian.user.id == topic.user_id || !User.exists?(topic.user_id)
+ return if !UserOption.exists?(user_id: topic.user_id, notify_on_solved: true)
screener =
UserCommScreener.new(acting_user_id: guardian.user.id, target_user_ids: topic.user_id)
!screener.ignoring_or_muting_actor?(topic.user_id)
@@ -128,6 +130,8 @@ class DiscourseSolved::AcceptAnswer
.where(topic:)
.where("notification_level >= ?", TopicUser.notification_levels[:tracking])
.where.not(user_id: already_notified_ids)
+ .joins(user: :user_option)
+ .where(user_options: { notify_on_solved: true })
.pluck(:user_id)
end
diff --git a/plugins/discourse-solved/assets/javascripts/discourse/connectors/user-preferences-notifications/solved-notifications.gjs b/plugins/discourse-solved/assets/javascripts/discourse/connectors/user-preferences-notifications/solved-notifications.gjs
new file mode 100644
index 00000000000..c6f9a9e588a
--- /dev/null
+++ b/plugins/discourse-solved/assets/javascripts/discourse/connectors/user-preferences-notifications/solved-notifications.gjs
@@ -0,0 +1,33 @@
+import Component from "@glimmer/component";
+import PreferenceCheckbox from "discourse/components/preference-checkbox";
+import { i18n } from "discourse-i18n";
+
+export default class SolvedNotifications extends Component {
+ static shouldRender(_args, { siteSettings, site }) {
+ if (!siteSettings.solved_enabled) {
+ return false;
+ }
+
+ if (siteSettings.allow_solved_on_all_topics) {
+ return true;
+ }
+
+ const solvedCategories = site.categories?.some(
+ (c) => c.custom_fields?.enable_accepted_answers === "true"
+ );
+
+ return solvedCategories || siteSettings.enable_solved_tags?.length > 0;
+ }
+
+ <template>
+ <div class="control-group solved-notifications">
+ <label class="control-label">{{i18n "solved.title"}}</label>
+ <PreferenceCheckbox
+ @labelKey="solved.notify_on_solved"
+ @checked={{@outletArgs.model.user_option.notify_on_solved}}
+ data-setting-name="user-notify-on-solved"
+ class="pref-notify-on-solved"
+ />
+ </div>
+ </template>
+}
diff --git a/plugins/discourse-solved/assets/javascripts/discourse/initializers/solved-user-options.js b/plugins/discourse-solved/assets/javascripts/discourse/initializers/solved-user-options.js
new file mode 100644
index 00000000000..59ee70fd1ce
--- /dev/null
+++ b/plugins/discourse-solved/assets/javascripts/discourse/initializers/solved-user-options.js
@@ -0,0 +1,18 @@
+import { withPluginApi } from "discourse/lib/plugin-api";
+
+export default {
+ name: "solved-user-options",
+
+ initialize(container) {
+ const siteSettings = container.lookup("service:site-settings");
+ if (!siteSettings.solved_enabled) {
+ return;
+ }
+
+ withPluginApi((api) => {
+ api.addSaveableUserOption("notify_on_solved", {
+ page: "notifications",
+ });
+ });
+ },
+};
diff --git a/plugins/discourse-solved/config/locales/client.en.yml b/plugins/discourse-solved/config/locales/client.en.yml
index c31e83692a5..8c5977f3576 100644
--- a/plugins/discourse-solved/config/locales/client.en.yml
+++ b/plugins/discourse-solved/config/locales/client.en.yml
@@ -14,6 +14,7 @@ en:
solved:
title: "Solved"
+ notify_on_solved: "Notify when a topic that I am Tracking or Watching is solved. Only applies to topics in categories that allow solutions."
allow_accepted_answers: "Allow topic owner and staff to mark a reply as the solution"
solved_topics_auto_close_hours: "Auto close topic (n) hours after the last reply once the topic has been marked as solved."
notify_on_staff_accept_solved: "Notify topic creator when staff marks a solution"
diff --git a/plugins/discourse-solved/db/migrate/20260319120000_add_notify_on_solved_to_user_options.rb b/plugins/discourse-solved/db/migrate/20260319120000_add_notify_on_solved_to_user_options.rb
new file mode 100644
index 00000000000..7fff123850f
--- /dev/null
+++ b/plugins/discourse-solved/db/migrate/20260319120000_add_notify_on_solved_to_user_options.rb
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+class AddNotifyOnSolvedToUserOptions < ActiveRecord::Migration[7.2]
+ def change
+ add_column :user_options, :notify_on_solved, :boolean, default: true, null: false
+ end
+end
diff --git a/plugins/discourse-solved/plugin.rb b/plugins/discourse-solved/plugin.rb
index c2f9008d192..b0d0e43292d 100644
--- a/plugins/discourse-solved/plugin.rb
+++ b/plugins/discourse-solved/plugin.rb
@@ -42,6 +42,9 @@ require_relative "lib/discourse_solved/engine"
after_initialize do
SeedFu.fixture_paths << Rails.root.join("plugins", "discourse-solved", "db", "fixtures").to_s
+ UserUpdater::OPTION_ATTR.push(:notify_on_solved)
+ add_to_serializer(:user_option, :notify_on_solved) { object.notify_on_solved }
+
reloadable_patch do
register_category_type(DiscourseSolved::Categories::Types::Support)
::Guardian.prepend(DiscourseSolved::GuardianExtensions)
diff --git a/plugins/discourse-solved/spec/services/discourse_solved/accept_answer_spec.rb b/plugins/discourse-solved/spec/services/discourse_solved/accept_answer_spec.rb
index 2f4cf084b9e..8237a87cf82 100644
--- a/plugins/discourse-solved/spec/services/discourse_solved/accept_answer_spec.rb
+++ b/plugins/discourse-solved/spec/services/discourse_solved/accept_answer_spec.rb
@@ -121,6 +121,21 @@ RSpec.describe DiscourseSolved::AcceptAnswer do
end
end
+ context "when the post author has opted out of solved notifications" do
+ fab!(:acting_user, :admin)
+
+ before { post.user.user_option.update!(notify_on_solved: false) }
+
+ it "does not notify the post author" do
+ expect { result }.not_to change {
+ Notification.where(
+ notification_type: Notification.types[:custom],
+ user: post.user,
+ ).count
+ }
+ end
+ end
+
context "when the acting user is the post author" do
let(:guardian) { post.user.guardian }
@@ -163,6 +178,21 @@ RSpec.describe DiscourseSolved::AcceptAnswer do
}
end
end
+
+ context "when the topic owner has opted out of solved notifications" do
+ fab!(:acting_user, :admin)
+
+ before { topic.user.user_option.update!(notify_on_solved: false) }
+
+ it "does not notify the topic owner" do
+ expect { result }.not_to change {
+ Notification.where(
+ notification_type: Notification.types[:custom],
+ user: topic.user,
+ ).count
+ }
+ end
+ end
end
context "when notify_on_staff_accept_solved is disabled" do
@@ -300,6 +330,19 @@ RSpec.describe DiscourseSolved::AcceptAnswer do
)
expect(notification.post_number).to eq(post.post_number)
end
+
+ context "when a watching user has opted out of solved notifications" do
+ before { watching_user.user_option.update!(notify_on_solved: false) }
+
+ it "does not notify the watching user" do
+ expect { result }.not_to change {
+ Notification.where(
+ notification_type: Notification.types[:custom],
+ user: watching_user,
+ ).count
+ }
+ end
+ end
end
context "when an accepted_solution webhook is active" do