A platform for community discussion. Free, open, simple.
FEATURE: allow marking solutions in group messages (#37635)
Régis Hanol39a426505d43
diff --git a/plugins/discourse-solved/app/serializers/concerns/discourse_solved/topic_answer_mixin.rb b/plugins/discourse-solved/app/serializers/concerns/discourse_solved/topic_answer_mixin.rbindex ebff49c1913..3eb1d38bc41 100644--- a/plugins/discourse-solved/app/serializers/concerns/discourse_solved/topic_answer_mixin.rb+++ b/plugins/discourse-solved/app/serializers/concerns/discourse_solved/topic_answer_mixin.rb@@ -16,8 +16,8 @@ module DiscourseSolveddef can_have_answerreturn true if SiteSetting.allow_solved_on_all_topics- return false if object.closed || object.archived- scope.allow_accepted_answers?(object.category_id, object.tags.map(&:name))+ return false if !object.private_message? && (object.closed || object.archived)+ scope.allow_accepted_answers?(object)enddef include_can_have_answer?diff --git a/plugins/discourse-solved/config/locales/server.en.yml b/plugins/discourse-solved/config/locales/server.en.ymlindex c53ac915bf8..3d400ea7d31 100644--- a/plugins/discourse-solved/config/locales/server.en.yml+++ b/plugins/discourse-solved/config/locales/server.en.yml@@ -4,6 +4,7 @@ en:site_settings:solved_enabled: "Enable solved plugin, allow users to select solutions for topics"allow_solved_on_all_topics: "Allow users to select solutions on all topics (when unchecked, solutions can be enabled per category or tag)"+ allow_solved_in_groups: "Allow marking solutions in group messages for these groups."accept_all_solutions_trust_level: "Minimum trust level required to accept solutions on any topic (even when not OP)"accept_all_solutions_allowed_groups: "Groups that are allowed to accept solutions on any topic (even when not OP). Admins and moderators are always allowed."empty_box_on_unsolved: "Display an empty box next to unsolved topics"diff --git a/plugins/discourse-solved/config/settings.yml b/plugins/discourse-solved/config/settings.ymlindex ef73431f5b6..f5679863482 100644--- a/plugins/discourse-solved/config/settings.yml+++ b/plugins/discourse-solved/config/settings.yml@@ -8,6 +8,11 @@ discourse_solved:allow_solved_on_all_topics:default: falseclient: true+ allow_solved_in_groups:+ type: group_list+ default: ""+ allow_any: false+ refresh: trueaccept_all_solutions_trust_level:default: 4client: truediff --git a/plugins/discourse-solved/lib/discourse_solved/before_head_close.rb b/plugins/discourse-solved/lib/discourse_solved/before_head_close.rbindex ec9d0c5bd5f..8752d70d065 100644--- a/plugins/discourse-solved/lib/discourse_solved/before_head_close.rb+++ b/plugins/discourse-solved/lib/discourse_solved/before_head_close.rb@@ -19,8 +19,7 @@ class DiscourseSolved::BeforeHeadClosereturn "" if SiteSetting.solved_add_schema_markup == "never"- allowed =- controller.guardian.allow_accepted_answers?(topic.category_id, topic.tags.pluck(:name))+ allowed = controller.guardian.allow_accepted_answers?(topic)return "" if !allowedfirst_post = topic_view.posts&.firstdiff --git a/plugins/discourse-solved/lib/discourse_solved/guardian_extensions.rb b/plugins/discourse-solved/lib/discourse_solved/guardian_extensions.rbindex ecccdc5766f..375bb39a641 100644--- a/plugins/discourse-solved/lib/discourse_solved/guardian_extensions.rb+++ b/plugins/discourse-solved/lib/discourse_solved/guardian_extensions.rb@@ -2,35 +2,47 @@module DiscourseSolvedmodule GuardianExtensions- def allow_accepted_answers?(category_id, tag_names = [])+ def allow_accepted_answers?(topic)return true if SiteSetting.allow_solved_on_all_topics- if SiteSetting.enable_solved_tags.present? && tag_names.present?- allowed_tags = SiteSetting.enable_solved_tags.split("|")- is_allowed = (tag_names & allowed_tags).present?+ if topic.private_message?+ return(+ SiteSetting.allow_solved_in_groups_map.present? &&+ topic.allowed_groups.exists?(id: SiteSetting.allow_solved_in_groups_map)+ )+ end++ solved_enabled_for_category?(topic.category_id, topic.tags.map(&:name))+ end++ def solved_enabled_for_category?(category_id, tag_names = [])+ return true if SiteSetting.allow_solved_on_all_topics- return true if is_allowed+ if SiteSetting.enable_solved_tags.present? && tag_names.present?+ return true if (tag_names & SiteSetting.enable_solved_tags.split("|")).present?endreturn false if category_id.blank?+if !DiscourseSolved::AcceptedAnswerCache.allowedDiscourseSolved::AcceptedAnswerCache.reset_accepted_answer_cacheend+DiscourseSolved::AcceptedAnswerCache.allowed.include?(category_id)enddef can_accept_answer?(topic, post)return false if !authenticated?- if !topic || topic.private_message? || !post || post.post_number <= 1 || post.whisper?- return false- end- return false if !allow_accepted_answers?(topic.category_id, topic.tags.map(&:name))+ return false if !topic || !post || post.post_number <= 1 || post.whisper?+ return false if !allow_accepted_answers?(topic)return true if is_staff?+if current_user.in_any_groups?(SiteSetting.accept_all_solutions_allowed_groups_map)return trueend- return true if is_category_group_moderator?(topic.category)++ return true if !topic.private_message? && is_category_group_moderator?(topic.category)topic.user_id == current_user.id && !topic.closed && SiteSetting.accept_solutions_topic_authorenddiff --git a/plugins/discourse-solved/plugin.rb b/plugins/discourse-solved/plugin.rbindex f9e028d1eab..cb96650c29b 100644--- a/plugins/discourse-solved/plugin.rb+++ b/plugins/discourse-solved/plugin.rb@@ -300,8 +300,8 @@ after_initialize docategory_id_changes = topic_changes.diff["category_id"].to_atag_changes = topic_changes.diff["tags"].to_a- old_allowed = Guardian.new.allow_accepted_answers?(category_id_changes[0], tag_changes[0])- new_allowed = Guardian.new.allow_accepted_answers?(category_id_changes[1], tag_changes[1])+ old_allowed = Guardian.new.solved_enabled_for_category?(category_id_changes[0], tag_changes[0])+ new_allowed = Guardian.new.solved_enabled_for_category?(category_id_changes[1], tag_changes[1])if old_allowed != new_allowedoptions[:refresh_stream] = truediff --git a/plugins/discourse-solved/spec/lib/guardian_extensions_spec.rb b/plugins/discourse-solved/spec/lib/guardian_extensions_spec.rbindex e0ddc962c18..f2088da4a65 100644--- a/plugins/discourse-solved/spec/lib/guardian_extensions_spec.rb+++ b/plugins/discourse-solved/spec/lib/guardian_extensions_spec.rb@@ -24,9 +24,51 @@ describe DiscourseSolved::GuardianExtensions doexpect(guardian.can_accept_answer?(topic, post)).to eq(false)end- it "returns false for private messages" do- topic.update!(user:, category_id: nil, archetype: Archetype.private_message)- expect(guardian.can_accept_answer?(topic, post)).to eq(false)+ it "returns false for regular private messages" do+ pm = Fabricate(:private_message_topic, user: user)+ pm_post = Fabricate(:post, topic: pm, user: other_user)+ expect(guardian.can_accept_answer?(pm, pm_post)).to eq(false)+ end++ context "with group messages" do+ fab!(:group)+ fab!(:pm_topic) do+ Fabricate(:group_private_message_topic, user: user, recipient_group: group)+ end+ fab!(:pm_op) { Fabricate(:post, topic: pm_topic, user: user) }+ fab!(:pm_post) { Fabricate(:post, topic: pm_topic, user: other_user) }++ before { SiteSetting.allow_solved_on_all_topics = false }++ it "returns false when allow_solved_in_groups is empty" do+ SiteSetting.allow_solved_in_groups = ""+ expect(guardian.can_accept_answer?(pm_topic, pm_post)).to eq(false)+ end++ it "returns false when the group is not in allow_solved_in_groups" do+ other_group = Fabricate(:group)+ SiteSetting.allow_solved_in_groups = other_group.id.to_s+ expect(guardian.can_accept_answer?(pm_topic, pm_post)).to eq(false)+ end++ it "returns true for topic author when the group is in allow_solved_in_groups" do+ SiteSetting.allow_solved_in_groups = group.id.to_s+ SiteSetting.accept_solutions_topic_author = true+ expect(guardian.can_accept_answer?(pm_topic, pm_post)).to eq(true)+ end++ it "returns true for staff when the group is in allow_solved_in_groups" do+ SiteSetting.allow_solved_in_groups = group.id.to_s+ admin = Fabricate(:admin, refresh_auto_groups: true)+ expect(Guardian.new(admin).can_accept_answer?(pm_topic, pm_post)).to eq(true)+ end++ it "returns false for non-author non-staff users" do+ SiteSetting.allow_solved_in_groups = group.id.to_s+ non_author = Fabricate(:user, refresh_auto_groups: true)+ SiteSetting.accept_all_solutions_allowed_groups = Fabricate(:group).id+ expect(Guardian.new(non_author).can_accept_answer?(pm_topic, pm_post)).to eq(false)+ endendit "returns false if accepted answers are not allowed" do