A platform for community discussion. Free, open, simple.
FEATURE: add solved me too button (#39652)
David Battersby9a3f8569d8df
diff --git a/plugins/discourse-solved/app/controllers/discourse_solved/shared_issue_controller.rb b/plugins/discourse-solved/app/controllers/discourse_solved/shared_issue_controller.rbnew file mode 100644index 00000000000..32812554888--- /dev/null+++ b/plugins/discourse-solved/app/controllers/discourse_solved/shared_issue_controller.rb@@ -0,0 +1,37 @@+# frozen_string_literal: true++class DiscourseSolved::SharedIssueController < ::ApplicationController+ requires_plugin DiscourseSolved::PLUGIN_NAME+ requires_login++ before_action :rate_limit_shared_issue++ def create+ DiscourseSolved::SharedIssue::Toggle.call(service_params) do+ on_success do |topic:, existing_shared_issue:|+ render json: {+ count: DiscourseSolved::SharedIssue.count_for(topic),+ user_created_shared_issue: existing_shared_issue.blank?,+ }+ end+ on_model_not_found(:topic) { raise Discourse::NotFound }+ on_failed_policy(:can_create_shared_issue) { raise Discourse::InvalidAccess }+ on_failed_contract do |contract|+ render json: failed_json.merge(errors: contract.errors.full_messages), status: :bad_request+ end+ on_failure { render json: failed_json, status: :unprocessable_entity }+ end+ end++ private++ def rate_limit_shared_issue+ return if current_user.staff? || params[:topic_id].blank?+ RateLimiter.new(+ nil,+ "shared-issue-#{current_user.id}-#{params[:topic_id]}",+ 5,+ 1.hour,+ ).performed!+ end+enddiff --git a/plugins/discourse-solved/app/models/discourse_solved/shared_issue.rb b/plugins/discourse-solved/app/models/discourse_solved/shared_issue.rbnew file mode 100644index 00000000000..6186758c20d--- /dev/null+++ b/plugins/discourse-solved/app/models/discourse_solved/shared_issue.rb@@ -0,0 +1,33 @@+# frozen_string_literal: true++module DiscourseSolved+ class SharedIssue < ActiveRecord::Base+ self.table_name = "discourse_solved_shared_issues"++ belongs_to :user+ belongs_to :topic, -> { with_deleted }++ validates :topic_id, presence: true, uniqueness: { scope: :user_id }+ validates :user_id, presence: true++ def self.count_for(topic)+ where(topic: topic).count + 1 # topic author is implicitly counted+ end+ end+end++# == Schema Information+#+# Table name: discourse_solved_shared_issues+#+# id :bigint not null, primary key+# created_at :datetime not null+# updated_at :datetime not null+# topic_id :integer not null+# user_id :integer not null+#+# Indexes+#+# index_discourse_solved_shared_issues_on_topic_id_and_user_id (topic_id,user_id) UNIQUE+# index_discourse_solved_shared_issues_on_user_id_and_topic_id (user_id,topic_id)+#diff --git a/plugins/discourse-solved/app/services/discourse_solved/shared_issue/toggle.rb b/plugins/discourse-solved/app/services/discourse_solved/shared_issue/toggle.rbnew file mode 100644index 00000000000..fcf688dd876--- /dev/null+++ b/plugins/discourse-solved/app/services/discourse_solved/shared_issue/toggle.rb@@ -0,0 +1,85 @@+# frozen_string_literal: true++class DiscourseSolved::SharedIssue::Toggle+ include Service::Base++ params do+ attribute :topic_id, :integer++ validates :topic_id, presence: true+ end++ model :topic+ policy :can_create_shared_issue++ lock(:topic) do+ model :existing_shared_issue, optional: true++ transaction do+ only_if(:existing_shared_issue_present?) { step :withdraw_shared_issue }+ only_if(:existing_shared_issue_absent?) do+ model :shared_issue, :create_shared_issue+ only_if(:notification_level_below_tracking?) { step :start_tracking_topic }+ end+ end+ end++ step :publish_shared_issue_change++ private++ def fetch_topic(params:)+ Topic.find_by(id: params.topic_id)+ end++ def can_create_shared_issue(guardian:, topic:)+ guardian.can_create_shared_issue?(topic)+ end++ def fetch_existing_shared_issue(topic:, guardian:)+ DiscourseSolved::SharedIssue.find_by(topic:, user: guardian.user)+ end++ def existing_shared_issue_present?(existing_shared_issue:)+ existing_shared_issue.present?+ end++ def existing_shared_issue_absent?(existing_shared_issue:)+ existing_shared_issue.blank?+ end++ def withdraw_shared_issue(existing_shared_issue:)+ existing_shared_issue.destroy+ end++ def create_shared_issue(topic:, guardian:)+ DiscourseSolved::SharedIssue.create(topic:, user: guardian.user)+ end++ def notification_level_below_tracking?(topic:, guardian:)+ current_level =+ TopicUser.get(topic, guardian.user)&.notification_level ||+ TopicUser.notification_levels[:regular]+ current_level < TopicUser.notification_levels[:tracking]+ end++ def start_tracking_topic(topic:, guardian:)+ TopicUser.change(+ guardian.user.id,+ topic.id,+ notification_level: TopicUser.notification_levels[:tracking],+ )+ end++ def publish_shared_issue_change(topic:, existing_shared_issue:)+ MessageBus.publish(+ "/topic/#{topic.id}",+ {+ type: :shared_issue,+ count: DiscourseSolved::SharedIssue.count_for(topic),+ user_created_shared_issue: existing_shared_issue.blank?,+ },+ topic.secure_audience_publish_messages,+ )+ end+enddiff --git a/plugins/discourse-solved/assets/javascripts/discourse/components/solved-shared-issue-button.gjs b/plugins/discourse-solved/assets/javascripts/discourse/components/solved-shared-issue-button.gjsnew file mode 100644index 00000000000..dba79ffb5f2--- /dev/null+++ b/plugins/discourse-solved/assets/javascripts/discourse/components/solved-shared-issue-button.gjs@@ -0,0 +1,97 @@+import Component from "@glimmer/component";+import { tracked } from "@glimmer/tracking";+import { action } from "@ember/object";+import { service } from "@ember/service";+import { ajax } from "discourse/lib/ajax";+import { popupAjaxError } from "discourse/lib/ajax-error";+import DButton from "discourse/ui-kit/d-button";+import dConcatClass from "discourse/ui-kit/helpers/d-concat-class";+import { i18n } from "discourse-i18n";++export default class SolvedSharedIssueButton extends Component {+ @service currentUser;+ @service router;++ @tracked saving = false;++ get show() {+ return (+ this.args.post.topic.shared_issue_visible &&+ !this.args.post.topic.accepted_answer+ );+ }++ get count() {+ return this.args.post.topic.shared_issue_count ?? 1;+ }++ get hasSharedIssue() {+ return this.args.post.topic.user_created_shared_issue;+ }++ get isTopicAuthor() {+ return this.currentUser && this.currentUser.id === this.args.post.user_id;+ }++ get disabled() {+ return this.saving || this.isTopicAuthor;+ }++ get label() {+ return i18n("solved.shared_issue.label", { count: this.count });+ }++ @action+ async toggle() {+ if (this.isTopicAuthor) {+ return;+ }++ if (!this.currentUser) {+ this.router.transitionTo("login");+ return;+ }++ const topic = this.args.post.topic;+ this.saving = true;++ try {+ const result = await ajax("/solution/shared_issue", {+ type: "POST",+ data: { topic_id: topic.id },+ });++ topic.set("shared_issue_count", result.count);+ topic.set("user_created_shared_issue", result.user_created_shared_issue);+ } catch (e) {+ popupAjaxError(e);+ } finally {+ this.saving = false;+ }+ }++ <template>+ {{#if this.show}}+ <div class="solved-shared-issue-row">+ <DButton+ class={{dConcatClass+ "btn-default"+ "post-action-menu__solved-shared-issue"+ (if this.hasSharedIssue "has-shared-issue")+ (if this.isTopicAuthor "disabled")+ }}+ ...attributes+ @action={{this.toggle}}+ @disabled={{this.disabled}}+ @icon="hand"+ @translatedLabel={{this.label}}+ @title={{if+ this.isTopicAuthor+ "solved.shared_issue.author_title"+ "solved.shared_issue.title"+ }}+ />+ </div>+ {{/if}}+ </template>+}diff --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 b7f44d156d1..2ed2c526346 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@@ -5,6 +5,7 @@ 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 SolvedSharedIssueButton from "../components/solved-shared-issue-button";import SolvedUnacceptAnswerButton from "../components/solved-unaccept-answer-button";import setAcceptedSolution from "../lib/set-accepted-solution";@@ -39,7 +40,13 @@ function initializeWithApi(api) {api.addDiscoveryQueryParam("solved", { replace: true, refreshModel: true });}- api.addTrackedTopicProperties("accepted_answer", "has_accepted_answer");+ api.addTrackedTopicProperties(+ "accepted_answer",+ "has_accepted_answer",+ "shared_issue_count",+ "user_created_shared_issue",+ "shared_issue_visible"+ );}function customizeNotificationDescriptions(api) {@@ -127,6 +134,17 @@ function customizePostMenu(api) {);});++ api.renderAfterWrapperOutlet(+ "post-content-cooked-html",+ class extends Component {+ static shouldRender(args) {+ return args.post?.post_number === 1;+ }++ <template><SolvedSharedIssueButton @post={{@post}} /></template>+ }+ );}function handleMessages(api) {@@ -140,6 +158,17 @@ function handleMessages(api) {api.registerCustomPostMessageCallback("accepted_solution", callback);api.registerCustomPostMessageCallback("unaccepted_solution", callback);++ api.registerCustomPostMessageCallback(+ "shared_issue",+ (controller, message) => {+ const topic = controller.model;+ if (!topic) {+ return;+ }+ topic.set("shared_issue_count", message.count);+ }+ );}export default {diff --git a/plugins/discourse-solved/assets/stylesheets/solutions.scss b/plugins/discourse-solved/assets/stylesheets/solutions.scssindex 48004b45734..ee8f9d58d62 100644--- a/plugins/discourse-solved/assets/stylesheets/solutions.scss+++ b/plugins/discourse-solved/assets/stylesheets/solutions.scss@@ -240,3 +240,13 @@ $solved-color: var(--success);display: none;}}++.solved-shared-issue-row {+ padding-left: var(--topic-body-width-padding);+}++.solved-shared-issue-row .post-action-menu__solved-shared-issue {+ &.has-shared-issue {+ --d-button-default-icon-color: var(--tertiary);+ }+}diff --git a/plugins/discourse-solved/config/locales/client.en.yml b/plugins/discourse-solved/config/locales/client.en.ymlindex 168c338783f..cc5d0132749 100644--- a/plugins/discourse-solved/config/locales/client.en.yml+++ b/plugins/discourse-solved/config/locales/client.en.yml@@ -59,6 +59,13 @@ en:confirm_solved_removal_confirm: "Confirm"confirm_solved_removal_cancel: "Cancel"+ shared_issue:+ label:+ one: "Me too (%{count})"+ other: "Me too (%{count})"+ title: "I am also experiencing this issue"+ author_title: "You started this topic"+no_answer:title: Has your question been answered?description: "Highlight the answer and help others by using the solution button below the correct reply."diff --git a/plugins/discourse-solved/config/locales/server.en.yml b/plugins/discourse-solved/config/locales/server.en.ymlindex 10b468d58ec..93e6f57c63d 100644--- a/plugins/discourse-solved/config/locales/server.en.yml+++ b/plugins/discourse-solved/config/locales/server.en.yml@@ -39,6 +39,7 @@ en:enable_solved_tags: "Tags that will allow users to select solutions."prioritize_solved_topics_in_search: "Prioritize solved topics in search results."enable_support_category_type_setup: "Enables the Support category type option during category creation setup. Note that 'enable simplified category creation' must be enabled for this to take effect."+ enable_solved_shared_issues: "Adds a “Me too” button to unsolved topics in support categories, letting members signal they’re experiencing the same issue and opt into notifications about the topic's solution. Note: You must enable the “Enable support type category setup” upcoming change to use this feature."show_who_marked_solved: "Show who marked the topic as solved. This is indicated in the topic's first post, and the answer post in a tooltip."keywords:diff --git a/plugins/discourse-solved/config/routes.rb b/plugins/discourse-solved/config/routes.rbindex 201333c398d..45f79939fd6 100644--- a/plugins/discourse-solved/config/routes.rb+++ b/plugins/discourse-solved/config/routes.rb@@ -4,6 +4,8 @@ DiscourseSolved::Engine.routes.draw dopost "/accept" => "answer#accept"post "/unaccept" => "answer#unaccept"+ post "/shared_issue" => "shared_issue#create"+get "/by_user" => "solved_topics#by_user"enddiff --git a/plugins/discourse-solved/config/settings.yml b/plugins/discourse-solved/config/settings.ymlindex 8e50cd447b3..d8b3d468db2 100644--- a/plugins/discourse-solved/config/settings.yml+++ b/plugins/discourse-solved/config/settings.yml@@ -66,4 +66,12 @@ discourse_solved:status: "stable"impact: "feature,staff"learn_more_url: "https://meta.discourse.org/t/-/398338"+ enable_solved_shared_issues:+ default: false+ hidden: true+ client: true+ upcoming_change:+ status: "experimental"+ impact: "feature,all_members"+ learn_more_url: "https://meta.discourse.org/t/-/402498"diff --git a/plugins/discourse-solved/db/migrate/20260429122035_create_discourse_solved_shared_issues.rb b/plugins/discourse-solved/db/migrate/20260429122035_create_discourse_solved_shared_issues.rbnew file mode 100644index 00000000000..f55c5e5b6cd--- /dev/null+++ b/plugins/discourse-solved/db/migrate/20260429122035_create_discourse_solved_shared_issues.rb@@ -0,0 +1,13 @@+# frozen_string_literal: true+class CreateDiscourseSolvedSharedIssues < ActiveRecord::Migration[8.0]+ def change+ create_table :discourse_solved_shared_issues do |t|+ t.integer :topic_id, null: false+ t.integer :user_id, null: false+ t.timestamps+ end++ add_index :discourse_solved_shared_issues, %i[topic_id user_id], unique: true+ add_index :discourse_solved_shared_issues, %i[user_id topic_id]+ end+enddiff --git a/plugins/discourse-solved/lib/discourse_solved/guardian_extensions.rb b/plugins/discourse-solved/lib/discourse_solved/guardian_extensions.rbindex a58d6940475..e7574e9c1e0 100644--- a/plugins/discourse-solved/lib/discourse_solved/guardian_extensions.rb+++ b/plugins/discourse-solved/lib/discourse_solved/guardian_extensions.rb@@ -51,5 +51,34 @@ module DiscourseSolveddef can_unaccept_answer?(topic, post)can_accept_answer?(topic, post) || (is_staff? && topic&.solved.present?)end++ def can_create_shared_issue?(topic)+ return false if topic.blank? || !authenticated?+ return false if topic.user_id == current_user.id+ return false if topic.private_message? || topic.solved.present?+ return false unless topic_in_support_category?(topic)+ return false unless current_user.upcoming_change_enabled?(:enable_solved_shared_issues)+ can_see_topic?(topic)+ end++ def shared_issue_visible?(topic)+ return false if topic.blank?+ return false if topic.private_message?+ return false unless topic_in_support_category?(topic)+ unless UpcomingChanges.enabled_for_user?(:enable_solved_shared_issues, current_user)+ return false+ end+ true+ end++ def topic_in_support_category?(topic)+ return false if topic.category_id.blank?++ if !DiscourseSolved::AcceptedAnswerCache.allowed+ DiscourseSolved::AcceptedAnswerCache.reset_accepted_answer_cache+ end++ DiscourseSolved::AcceptedAnswerCache.allowed.include?(topic.category_id)+ endendenddiff --git a/plugins/discourse-solved/lib/discourse_solved/topic_extension.rb b/plugins/discourse-solved/lib/discourse_solved/topic_extension.rbindex e857cd46aee..31c7f610aa7 100644--- a/plugins/discourse-solved/lib/discourse_solved/topic_extension.rb+++ b/plugins/discourse-solved/lib/discourse_solved/topic_extension.rb@@ -3,7 +3,13 @@module DiscourseSolved::TopicExtensionextend ActiveSupport::Concern- prepended { has_one :solved, class_name: "DiscourseSolved::SolvedTopic", dependent: :destroy }+ prepended do+ has_one :solved, class_name: "DiscourseSolved::SolvedTopic", dependent: :destroy+ has_many :shared_issues,+ class_name: "DiscourseSolved::SharedIssue",+ foreign_key: :topic_id,+ dependent: :delete_all+ enddef solved_auto_close_hourshours = category&.solved_auto_close_hours || 0diff --git a/plugins/discourse-solved/lib/discourse_solved/user_extension.rb b/plugins/discourse-solved/lib/discourse_solved/user_extension.rbnew file mode 100644index 00000000000..70a260bbdd9--- /dev/null+++ b/plugins/discourse-solved/lib/discourse_solved/user_extension.rb@@ -0,0 +1,11 @@+# frozen_string_literal: true++module DiscourseSolved::UserExtension+ extend ActiveSupport::Concern++ prepended do+ has_many :discourse_solved_shared_issues,+ class_name: "DiscourseSolved::SharedIssue",+ dependent: :delete_all+ end+enddiff --git a/plugins/discourse-solved/plugin.rb b/plugins/discourse-solved/plugin.rbindex 1c85b95e13a..6130cc249a4 100644--- a/plugins/discourse-solved/plugin.rb+++ b/plugins/discourse-solved/plugin.rb@@ -50,6 +50,7 @@ after_initialize do::WebHook.prepend(DiscourseSolved::WebHookExtension)::TopicViewSerializer.prepend(DiscourseSolved::TopicViewSerializerExtension)::Topic.prepend(DiscourseSolved::TopicExtension)+ ::User.prepend(DiscourseSolved::UserExtension)::Category.prepend(DiscourseSolved::CategoryExtension)::PostSerializer.prepend(DiscourseSolved::PostSerializerExtension)::PostMover.prepend(DiscourseSolved::PostMoverExtension)@@ -231,6 +232,23 @@ after_initialize doadd_to_serializer(:post, :accepted_answer) { topic&.solved&.answer_post_id == object.id }add_to_serializer(:post, :topic_accepted_answer) { topic&.solved&.present? }+ add_to_serializer(+ :topic_view,+ :shared_issue_count,+ include_condition: -> { scope.shared_issue_visible?(object.topic) },+ ) { DiscourseSolved::SharedIssue.count_for(object.topic) }+ add_to_serializer(+ :topic_view,+ :user_created_shared_issue,+ include_condition: -> { scope.shared_issue_visible?(object.topic) && scope.user.present? },+ ) { DiscourseSolved::SharedIssue.exists?(topic_id: object.topic.id, user_id: scope.user.id) }+ add_to_serializer(:topic_view, :can_create_shared_issue) do+ scope.can_create_shared_issue?(object.topic)+ end+ add_to_serializer(:topic_view, :shared_issue_visible) do+ scope.shared_issue_visible?(object.topic)+ end+on(:post_destroyed) do |post|DiscourseSolved::UnacceptAnswer.call(params: {diff --git a/plugins/discourse-solved/public/images/upcoming_changes/enable_solved_shared_issues.png b/plugins/discourse-solved/public/images/upcoming_changes/enable_solved_shared_issues.pngnew file mode 100644index 00000000000..81c2e84a711Binary files /dev/null and b/plugins/discourse-solved/public/images/upcoming_changes/enable_solved_shared_issues.png differdiff --git a/plugins/discourse-solved/spec/fabricators/shared_issue_fabricator.rb b/plugins/discourse-solved/spec/fabricators/shared_issue_fabricator.rbnew file mode 100644index 00000000000..c4ad2b704f2--- /dev/null+++ b/plugins/discourse-solved/spec/fabricators/shared_issue_fabricator.rb@@ -0,0 +1,5 @@+# frozen_string_literal: true+Fabricator(:shared_issue, from: DiscourseSolved::SharedIssue) do+ topic+ user+enddiff --git a/plugins/discourse-solved/spec/models/discourse_solved/shared_issue_spec.rb b/plugins/discourse-solved/spec/models/discourse_solved/shared_issue_spec.rbnew file mode 100644index 00000000000..c9c09c6e955--- /dev/null+++ b/plugins/discourse-solved/spec/models/discourse_solved/shared_issue_spec.rb@@ -0,0 +1,33 @@+# frozen_string_literal: true++RSpec.describe DiscourseSolved::SharedIssue, type: :model do+ fab!(:topic)+ fab!(:user)++ it "validates presence" do+ expect(described_class.new).not_to be_valid+ end++ it "is unique per topic and user" do+ described_class.create!(topic:, user:)+ duplicate = described_class.new(topic:, user:)+ expect(duplicate).not_to be_valid+ expect(duplicate.errors[:topic_id]).to include("has already been taken")+ end++ it "allows different users to flag the same topic" do+ described_class.create!(topic:, user:)+ other_user = Fabricate(:user)+ expect(described_class.new(topic:, user: other_user)).to be_valid+ end++ it "is deleted when its user is destroyed" do+ Fabricate(:shared_issue, topic:, user:)+ expect { user.destroy! }.to change { described_class.where(user_id: user.id).count }.to(0)+ end++ it "is deleted when its topic is destroyed" do+ Fabricate(:shared_issue, topic:, user:)+ expect { topic.destroy! }.to change { described_class.where(topic_id: topic.id).count }.to(0)+ end+enddiff --git a/plugins/discourse-solved/spec/requests/discourse_solved/shared_issue_controller_spec.rb b/plugins/discourse-solved/spec/requests/discourse_solved/shared_issue_controller_spec.rbnew file mode 100644index 00000000000..6e2b674b063--- /dev/null+++ b/plugins/discourse-solved/spec/requests/discourse_solved/shared_issue_controller_spec.rb@@ -0,0 +1,65 @@+# frozen_string_literal: true++RSpec.describe DiscourseSolved::SharedIssueController do+ fab!(:author, :user)+ fab!(:acting_user, :user)+ fab!(:category) do+ Fabricate(:category).tap do |c|+ c.upsert_custom_fields(DiscourseSolved::ENABLE_ACCEPTED_ANSWERS_CUSTOM_FIELD => "true")+ end+ end+ fab!(:topic) { Fabricate(:topic, category:, user: author) }+ fab!(:post_1, :post) { Fabricate(:post, topic:) }++ before do+ SiteSetting.solved_enabled = true+ SiteSetting.enable_solved_shared_issues = true+ DiscourseSolved::AcceptedAnswerCache.reset_accepted_answer_cache+ end++ describe "POST /solution/shared_issue" do+ it "requires login" do+ post "/solution/shared_issue.json", params: { topic_id: topic.id }+ expect(response.status).to eq(403)+ end++ context "when logged in" do+ before { sign_in(acting_user) }++ it "creates a shared issue record and returns updated counts" do+ expect { post "/solution/shared_issue.json", params: { topic_id: topic.id } }.to change {+ DiscourseSolved::SharedIssue.count+ }.by(1)+ expect(response.status).to eq(200)+ body = response.parsed_body+ expect(body["count"]).to eq(2)+ expect(body["user_created_shared_issue"]).to eq(true)+ end++ it "toggles the shared issue off when called twice" do+ post "/solution/shared_issue.json", params: { topic_id: topic.id }+ expect { post "/solution/shared_issue.json", params: { topic_id: topic.id } }.to change {+ DiscourseSolved::SharedIssue.count+ }.by(-1)+ expect(response.parsed_body["user_created_shared_issue"]).to eq(false)+ end++ it "rejects when the policy fails" do+ SiteSetting.enable_solved_shared_issues = false+ post "/solution/shared_issue.json", params: { topic_id: topic.id }+ expect(response.status).to eq(403)+ end++ it "rejects when the topic is not in a support category" do+ other_topic = Fabricate(:topic, user: author)+ post "/solution/shared_issue.json", params: { topic_id: other_topic.id }+ expect(response.status).to eq(403)+ end++ it "returns 404 for unknown topic" do+ post "/solution/shared_issue.json", params: { topic_id: -1 }+ expect(response.status).to eq(404)+ end+ end+ end+enddiff --git a/plugins/discourse-solved/spec/services/discourse_solved/shared_issue/toggle_spec.rb b/plugins/discourse-solved/spec/services/discourse_solved/shared_issue/toggle_spec.rbnew file mode 100644index 00000000000..6da2450330d--- /dev/null+++ b/plugins/discourse-solved/spec/services/discourse_solved/shared_issue/toggle_spec.rb@@ -0,0 +1,181 @@+# frozen_string_literal: true++RSpec.describe DiscourseSolved::SharedIssue::Toggle do+ describe described_class::Contract, type: :model do+ it { is_expected.to validate_presence_of(:topic_id) }+ end++ describe ".call" do+ subject(:result) { described_class.call(params:, **dependencies) }++ fab!(:author, :user)+ fab!(:acting_user, :user)+ fab!(:category) do+ Fabricate(:category).tap do |c|+ c.upsert_custom_fields(DiscourseSolved::ENABLE_ACCEPTED_ANSWERS_CUSTOM_FIELD => "true")+ end+ end+ fab!(:topic) { Fabricate(:topic_with_op, category:, user: author) }++ let(:params) { { topic_id: topic.id } }+ let(:dependencies) { { guardian: acting_user.guardian } }++ before do+ SiteSetting.solved_enabled = true+ SiteSetting.enable_solved_shared_issues = true+ DiscourseSolved::AcceptedAnswerCache.reset_accepted_answer_cache+ end++ context "when contract is invalid" do+ let(:params) { {} }++ it { is_expected.to fail_a_contract }+ end++ context "when topic is not found" do+ let(:params) { { topic_id: -1 } }++ it { is_expected.to fail_to_find_a_model(:topic) }+ end++ context "when user cannot create a shared issue for the topic" do+ context "when the feature flag is disabled" do+ before { SiteSetting.enable_solved_shared_issues = false }++ it { is_expected.to fail_a_policy(:can_create_shared_issue) }+ end++ context "when the topic is already solved" do+ fab!(:answer_post) { Fabricate(:post, topic:) }++ before { Fabricate(:solved_topic, topic:, answer_post:, accepter: author) }++ it { is_expected.to fail_a_policy(:can_create_shared_issue) }+ end++ context "when the acting user is the topic author" do+ fab!(:acting_user) { author }++ it { is_expected.to fail_a_policy(:can_create_shared_issue) }+ end++ context "when the topic is a private message" do+ fab!(:topic) { Fabricate(:private_message_topic, user: author) }++ it { is_expected.to fail_a_policy(:can_create_shared_issue) }+ end++ context "when the topic is not in a support category" do+ fab!(:other_category, :category)+ fab!(:topic) { Fabricate(:topic_with_op, category: other_category, user: author) }++ it { is_expected.to fail_a_policy(:can_create_shared_issue) }+ end++ context "when allow_solved_on_all_topics is enabled but the category is not a support category" do+ fab!(:other_category, :category)+ fab!(:topic) { Fabricate(:topic_with_op, category: other_category, user: author) }++ before { SiteSetting.allow_solved_on_all_topics = true }++ it { is_expected.to fail_a_policy(:can_create_shared_issue) }+ end++ context "when the guardian is anonymous" do+ let(:dependencies) { { guardian: Guardian.new } }++ it { is_expected.to fail_a_policy(:can_create_shared_issue) }+ end+ end++ context "when no shared issue has been recorded yet" do+ let(:messages) { MessageBus.track_publish("/topic/#{topic.id}") { result } }++ it { is_expected.to run_successfully }++ it "creates a shared issue record" do+ expect { result }.to change {+ DiscourseSolved::SharedIssue.where(topic:, user: acting_user).count+ }.by(1)+ end++ it "raises the notification level to tracking" do+ expect { result }.to change { TopicUser.get(topic, acting_user)&.notification_level }.to(+ TopicUser.notification_levels[:tracking],+ )+ end++ context "when the user is already watching the topic" do+ before do+ TopicUser.change(+ acting_user.id,+ topic.id,+ notification_level: TopicUser.notification_levels[:watching],+ )+ end++ it "does not lower the notification level" do+ expect { result }.not_to change { TopicUser.get(topic, acting_user).notification_level }+ end+ end++ context "when the user is already tracking the topic" do+ before do+ TopicUser.change(+ acting_user.id,+ topic.id,+ notification_level: TopicUser.notification_levels[:tracking],+ )+ end++ it "does not change the notification level" do+ expect { result }.not_to change { TopicUser.get(topic, acting_user).notification_level }+ end+ end++ it "publishes a shared issue message indicating the user created a shared issue" do+ expect(messages).to include(+ an_object_having_attributes(+ data: a_hash_including(type: :shared_issue, count: 2, user_created_shared_issue: true),+ ),+ )+ end+ end++ context "when the user has already recorded a shared issue" do+ let(:messages) { MessageBus.track_publish("/topic/#{topic.id}") { result } }++ before { Fabricate(:shared_issue, topic:, user: acting_user) }++ it { is_expected.to run_successfully }++ it "removes the shared issue record" do+ expect { result }.to change {+ DiscourseSolved::SharedIssue.where(topic:, user: acting_user).count+ }.by(-1)+ end++ context "when the user is already tracking the topic" do+ before do+ TopicUser.change(+ acting_user.id,+ topic.id,+ notification_level: TopicUser.notification_levels[:tracking],+ )+ end++ it "does not change the notification level" do+ expect { result }.not_to change { TopicUser.get(topic, acting_user).notification_level }+ end+ end++ it "publishes a shared issue message indicating the user withdrew their shared issue" do+ expect(messages).to include(+ an_object_having_attributes(+ data: a_hash_including(type: :shared_issue, count: 1, user_created_shared_issue: false),+ ),+ )+ end+ end+ end+enddiff --git a/plugins/discourse-solved/spec/system/page_objects/components/post_shared_issue_button.rb b/plugins/discourse-solved/spec/system/page_objects/components/post_shared_issue_button.rbnew file mode 100644index 00000000000..5bbcf7b8855--- /dev/null+++ b/plugins/discourse-solved/spec/system/page_objects/components/post_shared_issue_button.rb@@ -0,0 +1,48 @@+# frozen_string_literal: true++module PageObjects+ module Components+ class PostSharedIssueButton < PageObjects::Components::Base+ SELECTOR = ".post-action-menu__solved-shared-issue"++ def initialize(post)+ @post = post+ end++ def click+ within_post { find(SELECTOR).click }+ self+ end++ def has_shared_issue_button?+ within_post { has_css?(SELECTOR) }+ end++ def has_no_shared_issue_button?+ within_post { has_no_css?(SELECTOR) }+ end++ def has_active?+ within_post { has_css?("#{SELECTOR}.has-shared-issue") }+ end++ def has_read_only?+ within_post { has_css?("#{SELECTOR}.disabled") }+ end++ def has_count?(count)+ within_post { has_css?(SELECTOR, text: "Me too (#{count})") }+ end++ private++ def within_post+ within(post_selector) { yield }+ end++ def post_selector+ "#post_#{@post.post_number}"+ end+ end+ end+enddiff --git a/plugins/discourse-solved/spec/system/shared_issue_spec.rb b/plugins/discourse-solved/spec/system/shared_issue_spec.rbnew file mode 100644index 00000000000..4a2b9c3223b--- /dev/null+++ b/plugins/discourse-solved/spec/system/shared_issue_spec.rb@@ -0,0 +1,113 @@+# frozen_string_literal: true++describe "Solved | Shared issue button" do+ fab!(:author, :user)+ fab!(:member, :user)+ fab!(:support_category) do+ Fabricate(:category).tap do |c|+ c.upsert_custom_fields(DiscourseSolved::ENABLE_ACCEPTED_ANSWERS_CUSTOM_FIELD => "true")+ end+ end+ fab!(:topic) do+ Fabricate(+ :post,+ user: author,+ topic: Fabricate(:topic, category: support_category, user: author),+ ).topic+ end+ fab!(:op) { topic.first_post }++ let(:topic_page) { PageObjects::Pages::Topic.new }+ let(:shared_issue_button) { PageObjects::Components::PostSharedIssueButton.new(op) }++ before do+ SiteSetting.solved_enabled = true+ SiteSetting.enable_solved_shared_issues = true+ DiscourseSolved::AcceptedAnswerCache.reset_accepted_answer_cache+ end++ it "lets a member toggle the shared issue on and off" do+ sign_in(member)+ topic_page.visit_topic(topic)++ expect(shared_issue_button).to have_shared_issue_button+ expect(shared_issue_button).to have_count(1)++ shared_issue_button.click+ expect(shared_issue_button).to have_count(2)+ expect(shared_issue_button).to have_active++ expect(TopicUser.get(topic, member).notification_level).to eq(+ TopicUser.notification_levels[:tracking],+ )++ shared_issue_button.click+ expect(shared_issue_button).to have_count(1)+ expect(shared_issue_button).to have_no_css(".has-shared-issue")+ end++ it "hides the button once the topic is solved" do+ answer_post = Fabricate(:post, topic:)+ Fabricate(:solved_topic, topic:, answer_post:, accepter: author)++ sign_in(member)+ topic_page.visit_topic(topic)++ expect(shared_issue_button).to have_no_shared_issue_button+ end++ it "re-shows the button after a solution is accepted then unaccepted" do+ answer_post = Fabricate(:post, topic:)++ sign_in(author)+ topic_page.visit_topic(topic)++ expect(shared_issue_button).to have_shared_issue_button++ within("#post_#{answer_post.post_number}") do+ find(".post-action-menu__solved-unaccepted").click+ end+ expect(page).to have_css(".accepted-answer")+ expect(shared_issue_button).to have_no_shared_issue_button++ within("#post_#{answer_post.post_number}") { find(".post-action-menu__solved-accepted").click }+ expect(page).to have_no_css(".accepted-answer")+ expect(shared_issue_button).to have_shared_issue_button+ end++ it "hides the button when the upcoming change is disabled" do+ SiteSetting.enable_solved_shared_issues = false++ sign_in(member)+ topic_page.visit_topic(topic)++ expect(shared_issue_button).to have_no_shared_issue_button+ end++ it "prompts anonymous visitors to log in" do+ topic_page.visit_topic(topic)++ expect(shared_issue_button).to have_shared_issue_button+ shared_issue_button.click++ expect(page).to have_current_path(%r{/login})+ end++ it "hides the button on topics outside a support category" do+ other_topic = Fabricate(:post, user: author).topic++ sign_in(member)+ topic_page.visit_topic(other_topic)++ expect(shared_issue_button).to have_no_shared_issue_button+ end++ it "shows the button as read-only for the topic author" do+ sign_in(author)+ topic_page.visit_topic(topic)++ expect(shared_issue_button).to have_shared_issue_button+ expect(shared_issue_button).to have_read_only+ expect(shared_issue_button).to have_count(1)+ end+enddiff --git a/themes/horizon/scss/buttons.scss b/themes/horizon/scss/buttons.scssindex 5b0447a331f..465d4cf512f 100644--- a/themes/horizon/scss/buttons.scss+++ b/themes/horizon/scss/buttons.scss@@ -113,3 +113,10 @@box-shadow: none;}}++// Solved+.btn-default.post-action-menu__solved-shared-issue:not(.has-shared-issue) {+ .d-icon {+ color: var(--icon-color);+ }+}