0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-14 13:58:53 +08:00
discourse/plugins/discourse-solved/lib/discourse_solved/guardian_extensions.rb
Kris c894e2f722
FEATURE: make shared issues optional per-category (#40501)
This makes the shared issue feature of support categories optional
per-category, so admins can turn it off selectively.

This is accomplished by adding a category level setting: `Enable shared
issues`

<img width="1404" height="564" alt="image"
src="https://github.com/user-attachments/assets/e4997382-b5fb-4839-af9d-6baf57ec3201"
/>


When enabled this exposes the Site Setting to edit the button's text in
the section below.

Enabled: 
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/39293f0d-63a3-421c-8c2b-2ee967faee56"
/>


Disabled:
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/6bc92173-0cd4-40cf-89b2-56d74469e752"
/>
2026-06-04 11:58:28 -04:00

92 lines
3.1 KiB
Ruby
Vendored

# frozen_string_literal: true
module DiscourseSolved
module GuardianExtensions
def allow_accepted_answers?(topic)
return true if SiteSetting.allow_solved_on_all_topics
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
if SiteSetting.enable_solved_tags.present? && tag_names.present?
return true if (tag_names & SiteSetting.enable_solved_tags.split("|")).present?
end
return false if category_id.blank?
if !DiscourseSolved::AcceptedAnswerCache.allowed
DiscourseSolved::AcceptedAnswerCache.reset_accepted_answer_cache
end
DiscourseSolved::AcceptedAnswerCache.allowed.include?(category_id)
end
def can_accept_answer?(topic, post)
return false if !authenticated?
return false if !topic || !post || post.post_number <= 1 || post.whisper?
return false if !allow_accepted_answers?(topic)
return false if !can_see_post?(post)
return true if is_staff?
if current_user.in_any_groups?(SiteSetting.accept_all_solutions_allowed_groups_map)
return true
end
return true if !topic.private_message? && is_category_group_moderator?(topic.category)
topic.user_id == current_user.id && !topic.closed && SiteSetting.accept_solutions_topic_author
end
def can_unaccept_answer?(topic, post)
can_accept_answer?(topic, post) ||
(is_staff? && topic&.topic_answers&.exists?(answer_post_id: post.id))
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?
return false if topic.solved.present? && !SiteSetting.solved_allow_multiple_solutions
return false unless topic_in_support_category?(topic)
return false unless shared_issues_enabled_for_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)
return false unless shared_issues_enabled_for_category?(topic)
unless UpcomingChanges.enabled_for_user?(:enable_solved_shared_issues, current_user)
return false
end
true
end
def shared_issues_enabled_for_category?(topic)
topic.category&.shared_issues_enabled?
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)
end
end
end