A platform for community discussion. Free, open, simple.
FEATURE: solved notifications for tracked or watched topics (#38724)
David Battersby7bb9b3b473dd
diff --git a/frontend/discourse/app/components/notifications-tracking.gjs b/frontend/discourse/app/components/notifications-tracking.gjsindex e731bcc523c..7b955a716b4 100644--- a/frontend/discourse/app/components/notifications-tracking.gjs+++ b/frontend/discourse/app/components/notifications-tracking.gjs@@ -10,6 +10,7 @@ import concatClass from "discourse/helpers/concat-class";import icon from "discourse/helpers/d-icon";import lazyHash from "discourse/helpers/lazy-hash";import { allLevels, buttonDetails } from "discourse/lib/notification-levels";+import { applyValueTransformer } from "discourse/lib/transformer";import { i18n } from "discourse-i18n";function constructKey(prefix, level, suffix, key) {@@ -84,11 +85,15 @@ export default class NotificationsTracking extends Component {@actiondescription(level) {- return constructKey(- this.args.prefix,- level.key,- this.args.suffix,- "description"+ return applyValueTransformer(+ "notifications-tracking-description",+ constructKey(+ this.args.prefix,+ level.key,+ this.args.suffix,+ "description"+ ),+ { topic: this.args.topic, level, prefix: this.args.prefix });}diff --git a/frontend/discourse/app/components/topic-notifications-tracking.gjs b/frontend/discourse/app/components/topic-notifications-tracking.gjsindex 6aacd3ccce6..e76a1188697 100644--- a/frontend/discourse/app/components/topic-notifications-tracking.gjs+++ b/frontend/discourse/app/components/topic-notifications-tracking.gjs@@ -20,6 +20,7 @@ export default class TopicNotificationsTracking extends Component {@levels={{topicLevels}}@suffix={{this.suffix}}@contentClass={{@contentClass}}+ @topic={{@topic}}/></template>}diff --git a/frontend/discourse/app/lib/registry/transformers.js b/frontend/discourse/app/lib/registry/transformers.jsindex 2840334b747..b7954ae2adc 100644--- a/frontend/discourse/app/lib/registry/transformers.js+++ b/frontend/discourse/app/lib/registry/transformers.js@@ -72,6 +72,7 @@ export const VALUE_TRANSFORMERS = Object.freeze(["move-to-topic-move-options","navigation-bar-dropdown-icon","navigation-bar-dropdown-mode",+ "notifications-tracking-description","parent-category-row-class","parent-category-row-class-mobile","post-article-class",diff --git a/plugins/discourse-solved/app/services/discourse_solved/accept_answer.rb b/plugins/discourse-solved/app/services/discourse_solved/accept_answer.rbindex 9eae1591396..e414fa29fb2 100644--- a/plugins/discourse-solved/app/services/discourse_solved/accept_answer.rb+++ b/plugins/discourse-solved/app/services/discourse_solved/accept_answer.rb@@ -20,6 +20,11 @@ class DiscourseSolved::AcceptAnswermodel :solved, :mark_as_solvedonly_if(:should_notify_post_author) { step :notify_post_author }only_if(:should_notify_topic_owner) { step :notify_topic_owner }+ model :topic_user_ids, optional: true+ only_if(:has_topic_users?) do+ model :screener+ step :notify_tracking_and_watching_users+ endendend@@ -112,6 +117,48 @@ class DiscourseSolved::AcceptAnswer)end+ def has_topic_users?(topic_user_ids:)+ topic_user_ids.present?+ end++ def fetch_topic_user_ids(post:, topic:, guardian:)+ already_notified_ids = [guardian.user.id, post.user_id, topic.user_id]++ TopicUser+ .where(topic:)+ .where("notification_level >= ?", TopicUser.notification_levels[:tracking])+ .where.not(user_id: already_notified_ids)+ .pluck(:user_id)+ end++ def fetch_screener(guardian:, topic_user_ids:)+ UserCommScreener.new(acting_user_id: guardian.user.id, target_user_ids: topic_user_ids)+ end++ def notify_tracking_and_watching_users(post:, topic:, guardian:, topic_user_ids:, screener:)+ notification_data = {+ message: "solved.topic_solved_notification",+ display_username: guardian.user.username,+ topic_title: topic.title,+ title: "solved.notification.topic_solved_title",+ }.to_json++ records =+ topic_user_ids.filter_map do |user_id|+ next if screener.ignoring_or_muting_actor?(user_id)++ {+ notification_type: Notification.types[:custom],+ user_id: user_id,+ topic_id: topic.id,+ post_number: post.post_number,+ data: notification_data,+ }+ end++ Notification::Action::BulkCreate.call(records:)+ end+def topic_will_auto_close(solved:)solved.topic_timer.present?enddiff --git a/plugins/discourse-solved/app/services/discourse_solved/unaccept_answer.rb b/plugins/discourse-solved/app/services/discourse_solved/unaccept_answer.rbindex 7c0c607bb42..7c3865ed3e7 100644--- a/plugins/discourse-solved/app/services/discourse_solved/unaccept_answer.rb+++ b/plugins/discourse-solved/app/services/discourse_solved/unaccept_answer.rb@@ -18,6 +18,7 @@ class DiscourseSolved::UnacceptAnswertransaction dostep :revoke_solved_creditstep :remove_accepted_answer_notification+ step :remove_topic_solved_notificationsstep :unmark_as_solvedendend@@ -59,6 +60,13 @@ class DiscourseSolved::UnacceptAnswer)&.destroy!end+ def remove_topic_solved_notifications(post:, topic:)+ Notification+ .where(topic:, notification_type: Notification.types[:custom], post_number: post.post_number)+ .where("data::jsonb @> ?", { message: "solved.topic_solved_notification" }.to_json)+ .destroy_all+ end+def unmark_as_solved(topic:)topic.solved.destroy!enddiff --git a/plugins/discourse-solved/assets/javascripts/discourse/initializers/extend-for-solved-button.gjs b/plugins/discourse-solved/assets/javascripts/discourse/initializers/extend-for-solved-button.gjsindex 146bdadd703..b7f44d156d1 100644--- a/plugins/discourse-solved/assets/javascripts/discourse/initializers/extend-for-solved-button.gjs+++ b/plugins/discourse-solved/assets/javascripts/discourse/initializers/extend-for-solved-button.gjs@@ -1,15 +1,39 @@import Component from "@glimmer/component";+import { helperContext } from "discourse/lib/helpers";import { withPluginApi } from "discourse/lib/plugin-api";+import Category from "discourse/models/category";import { i18n } from "discourse-i18n";import SolvedAcceptAnswerButton from "../components/solved-accept-answer-button";import SolvedAcceptedAnswer from "../components/solved-accepted-answer";import SolvedUnacceptAnswerButton from "../components/solved-unaccept-answer-button";import setAcceptedSolution from "../lib/set-accepted-solution";+function topicHasSolvedEnabled(topic) {+ if (!topic) {+ return false;+ }++ const siteSettings = helperContext().siteSettings;++ if (siteSettings.allow_solved_on_all_topics) {+ return true;+ }++ // TODO: change to category.isType("support") once enable_simplified_category_creation is permanent+ const category = Category.findById(topic.category_id);+ if (category?.custom_fields?.enable_accepted_answers === "true") {+ return true;+ }++ const solvedTags = siteSettings.enable_solved_tags.split("|").filter(Boolean);+ return (topic.tags || []).some((t) => solvedTags.includes(t));+}+function initializeWithApi(api) {customizePost(api);customizePostMenu(api);handleMessages(api);+ customizeNotificationDescriptions(api);if (api.addDiscoveryQueryParam) {api.addDiscoveryQueryParam("solved", { replace: true, refreshModel: true });@@ -18,6 +42,27 @@ function initializeWithApi(api) {api.addTrackedTopicProperties("accepted_answer", "has_accepted_answer");}+function customizeNotificationDescriptions(api) {+ api.registerValueTransformer(+ "notifications-tracking-description",+ ({ value, context: { topic, level, prefix } }) => {+ if (prefix !== "topic.notifications" || !topicHasSolvedEnabled(topic)) {+ return value;+ }++ if (level.key === "tracking") {+ return i18n("solved.topic_notifications.tracking.description");+ }++ if (level.key === "watching") {+ return i18n("solved.topic_notifications.watching.description");+ }++ return value;+ }+ );+}+function customizePost(api) {api.addTrackedPostProperties("can_accept_answer",@@ -107,6 +152,10 @@ export default {"notification.solved.accepted_notification","square-check");+ api.replaceIcon(+ "notification.solved.topic_solved_notification",+ "square-check"+ );});withPluginApi((api) => {diff --git a/plugins/discourse-solved/config/locales/client.en.yml b/plugins/discourse-solved/config/locales/client.en.ymlindex 5c24d5de245..c31e83692a5 100644--- a/plugins/discourse-solved/config/locales/client.en.yml+++ b/plugins/discourse-solved/config/locales/client.en.yml@@ -9,6 +9,7 @@ en:alt:solved:accepted_notification: "accepted"+ topic_solved_notification: "solved"solutions: "Solutions"solved:@@ -54,8 +55,15 @@ en:no_answer:title: Has your question been answered?description: "Highlight the answer and help others by using the solution button below the correct reply."+ topic_solved_notification: "<p><span>%{username}</span> %{description}</p>"notification:title: "your post was marked as solution"+ topic_solved_title: "Topic solved"+ topic_notifications:+ tracking:+ description: "You will be notified if someone mentions your @name, replies to you, or when the topic is solved. A count of new replies will be shown for this topic."+ watching:+ description: "You will be notified of every new reply in this topic and when the topic is solved. A count of new replies will be shown for this topic."category_type_support:title: "Support"not_support_type: "Heads up! You've disabled the ability to mark posts as solutions in this category. To turn that back on, you'll find the relevant settings in the Settings tab."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.rbindex 0756e26d8a5..2f4cf084b9e 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@@ -181,6 +181,127 @@ RSpec.describe DiscourseSolved::AcceptAnswer doendend+ context "when users are tracking or watching the topic" do+ fab!(:watching_user, :user)+ fab!(:tracking_user, :user)+ fab!(:muted_user, :user)+ fab!(:acting_user, :admin)++ before do+ TopicUser.change(+ watching_user.id,+ topic.id,+ notification_level: TopicUser.notification_levels[:watching],+ )+ TopicUser.change(+ tracking_user.id,+ topic.id,+ notification_level: TopicUser.notification_levels[:tracking],+ )+ TopicUser.change(+ muted_user.id,+ topic.id,+ notification_level: TopicUser.notification_levels[:muted],+ )+ end++ it "notifies watching users" do+ expect { result }.to change {+ Notification.where(+ notification_type: Notification.types[:custom],+ user: watching_user,+ ).count+ }.by(1)+ end++ it "notifies tracking users" do+ expect { result }.to change {+ Notification.where(+ notification_type: Notification.types[:custom],+ user: tracking_user,+ ).count+ }.by(1)+ end++ it "does not notify muted users" do+ expect { result }.not_to change {+ Notification.where(+ notification_type: Notification.types[:custom],+ user: muted_user,+ ).count+ }+ end++ context "when the acting user is also watching the topic" do+ before do+ TopicUser.change(+ acting_user.id,+ topic.id,+ notification_level: TopicUser.notification_levels[:watching],+ )+ end++ it "does not notify the user who marked the solution" do+ expect { result }.not_to change {+ Notification.where(+ notification_type: Notification.types[:custom],+ user: acting_user,+ ).count+ }+ end+ end++ it "uses the topic_solved_notification message" do+ result+ notification =+ Notification.find_by(+ notification_type: Notification.types[:custom],+ user: watching_user,+ )+ data = JSON.parse(notification.data)+ expect(data["message"]).to eq("solved.topic_solved_notification")+ expect(data["title"]).to eq("solved.notification.topic_solved_title")+ end++ context "when the post author is also watching the topic" do+ before do+ TopicUser.change(+ post.user_id,+ topic.id,+ notification_level: TopicUser.notification_levels[:watching],+ )+ end++ it "does not double-notify the post author" do+ expect { result }.to change {+ Notification.where(+ notification_type: Notification.types[:custom],+ user: post.user,+ topic: topic,+ ).count+ }.by(1)++ notification =+ Notification.find_by(+ notification_type: Notification.types[:custom],+ user: post.user,+ topic: topic,+ )+ expect(JSON.parse(notification.data)["message"]).to eq("solved.accepted_notification")+ end+ end++ it "links to the solution post" do+ result+ notification =+ Notification.find_by(+ notification_type: Notification.types[:custom],+ user: watching_user,+ )+ expect(notification.post_number).to eq(post.post_number)+ end+ end+context "when an accepted_solution webhook is active" dofab!(:web_hook) { Fabricate(:web_hook, active: true) }fab!(:accepted_solution_event_type) { WebHookEventType.find_by(name: "accepted_solution") }diff --git a/plugins/discourse-solved/spec/services/discourse_solved/unaccept_answer_spec.rb b/plugins/discourse-solved/spec/services/discourse_solved/unaccept_answer_spec.rbindex 17418635048..ba74f8afbf0 100644--- a/plugins/discourse-solved/spec/services/discourse_solved/unaccept_answer_spec.rb+++ b/plugins/discourse-solved/spec/services/discourse_solved/unaccept_answer_spec.rb@@ -124,6 +124,30 @@ RSpec.describe DiscourseSolved::UnacceptAnswer doexpect { result }.to change { DiscourseSolved::SolvedTopic.count }.by(-1)end+ context "when tracking/watching users have topic solved notifications" do+ fab!(:watching_user, :user)++ before do+ Notification.create!(+ notification_type: Notification.types[:custom],+ user_id: watching_user.id,+ topic_id: post.topic_id,+ post_number: post.post_number,+ data: { message: "solved.topic_solved_notification" }.to_json,+ )+ end++ it "removes topic solved notifications" do+ expect { result }.to change {+ Notification.where(+ notification_type: Notification.types[:custom],+ user: watching_user,+ topic: post.topic,+ ).count+ }.by(-1)+ end+ end+context "when an unaccepted_solution webhook is active" dofab!(:web_hook) { Fabricate(:web_hook, active: true) }fab!(:unaccepted_solution_event_type) do