FEATURE: add new custom status filters (#286)

* FEATURE: add new custom status filters

Those can be used in /filter route.

Depends on https://github.com/discourse/discourse/pull/26770

Internal ref. /t/127278

* DEV: pin plugin for Discourse 3.3.0.beta2-dev
This commit is contained in:
Régis Hanol 2024-04-26 16:17:50 +02:00 committed by GitHub
parent a18ce6d712
commit 2c96c5b67c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 268 additions and 242 deletions

View File

@ -1,3 +1,4 @@
< 3.3.0.beta2-dev: a18ce6d712fafed286bcc99543dd173110c6dfb8
< 3.3.0.beta1-dev: 526a44644a7b3f0c2a3ba4fc16e72f364e9fce6d < 3.3.0.beta1-dev: 526a44644a7b3f0c2a3ba4fc16e72f364e9fce6d
< 3.2.0.beta2-dev: 9fbf43e2f077e86f0a1ff769af6036d4e78bfff1 < 3.2.0.beta2-dev: 9fbf43e2f077e86f0a1ff769af6036d4e78bfff1
3.1.999: b5d487d6a5bfe2571d936eec5911d02a5f3fcc32 3.1.999: b5d487d6a5bfe2571d936eec5911d02a5f3fcc32

181
plugin.rb
View File

@ -9,11 +9,9 @@
enabled_site_setting :solved_enabled enabled_site_setting :solved_enabled
if respond_to?(:register_svg_icon) register_svg_icon "far fa-check-square"
register_svg_icon "far fa-check-square" register_svg_icon "check-square"
register_svg_icon "check-square" register_svg_icon "far fa-square"
register_svg_icon "far fa-square"
end
register_asset "stylesheets/solutions.scss" register_asset "stylesheets/solutions.scss"
register_asset "stylesheets/mobile/solutions.scss", :mobile register_asset "stylesheets/mobile/solutions.scss", :mobile
@ -59,16 +57,13 @@ after_initialize do
p2.custom_fields.delete(IS_ACCEPTED_ANSWER_CUSTOM_FIELD) p2.custom_fields.delete(IS_ACCEPTED_ANSWER_CUSTOM_FIELD)
p2.save! p2.save!
if defined?(UserAction::SOLVED)
UserAction.where(action_type: UserAction::SOLVED, target_post_id: p2.id).destroy_all UserAction.where(action_type: UserAction::SOLVED, target_post_id: p2.id).destroy_all
end end
end end
end
post.custom_fields[IS_ACCEPTED_ANSWER_CUSTOM_FIELD] = "true" post.custom_fields[IS_ACCEPTED_ANSWER_CUSTOM_FIELD] = "true"
topic.custom_fields[ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD] = post.id topic.custom_fields[ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD] = post.id
if defined?(UserAction::SOLVED)
UserAction.log_action!( UserAction.log_action!(
action_type: UserAction::SOLVED, action_type: UserAction::SOLVED,
user_id: post.user_id, user_id: post.user_id,
@ -76,7 +71,6 @@ after_initialize do
target_post_id: post.id, target_post_id: post.id,
target_topic_id: post.topic_id, target_topic_id: post.topic_id,
) )
end
notification_data = { notification_data = {
message: "solved.accepted_notification", message: "solved.accepted_notification",
@ -156,9 +150,7 @@ after_initialize do
post.save! post.save!
# TODO remove_action! does not allow for this type of interface # TODO remove_action! does not allow for this type of interface
if defined?(UserAction::SOLVED)
UserAction.where(action_type: UserAction::SOLVED, target_post_id: post.id).destroy_all UserAction.where(action_type: UserAction::SOLVED, target_post_id: post.id).destroy_all
end
# yank notification # yank notification
notification = notification =
@ -191,12 +183,10 @@ after_initialize do
::TopicViewSerializer.prepend(DiscourseSolved::TopicViewSerializerExtension) ::TopicViewSerializer.prepend(DiscourseSolved::TopicViewSerializerExtension)
::Category.prepend(DiscourseSolved::CategoryExtension) ::Category.prepend(DiscourseSolved::CategoryExtension)
::PostSerializer.prepend(DiscourseSolved::PostSerializerExtension) ::PostSerializer.prepend(DiscourseSolved::PostSerializerExtension)
::UserSummary.prepend(DiscourseSolved::UserSummaryExtension) if defined?(::UserAction::SOLVED) ::UserSummary.prepend(DiscourseSolved::UserSummaryExtension)
if respond_to?(:register_topic_list_preload_user_ids)
::Topic.attr_accessor(:accepted_answer_user_id) ::Topic.attr_accessor(:accepted_answer_user_id)
::TopicPostersSummary.alias_method(:old_user_ids, :user_ids) ::TopicPostersSummary.alias_method(:old_user_ids, :user_ids)
::TopicPostersSummary.prepend(DiscourseSolved::TopicPostersSummaryExtension) ::TopicPostersSummary.prepend(DiscourseSolved::TopicPostersSummaryExtension)
end
[ [
::TopicListItemSerializer, ::TopicListItemSerializer,
::SearchTopicListItemSerializer, ::SearchTopicListItemSerializer,
@ -207,7 +197,7 @@ after_initialize do
end end
# we got to do a one time upgrade # we got to do a one time upgrade
if !::DiscourseSolved.skip_db? && defined?(UserAction::SOLVED) if !::DiscourseSolved.skip_db?
unless Discourse.redis.get("solved_already_upgraded") unless Discourse.redis.get("solved_already_upgraded")
unless UserAction.where(action_type: UserAction::SOLVED).exists? unless UserAction.where(action_type: UserAction::SOLVED).exists?
Rails.logger.info("Upgrading storage for solved") Rails.logger.info("Upgrading storage for solved")
@ -272,7 +262,6 @@ after_initialize do
DiscourseSolved::BeforeHeadClose.new(controller).html DiscourseSolved::BeforeHeadClose.new(controller).html
end end
if Report.respond_to?(:add_report)
Report.add_report("accepted_solutions") do |report| Report.add_report("accepted_solutions") do |report|
report.data = [] report.data = []
@ -307,17 +296,16 @@ after_initialize do
.where("topic_custom_fields.created_at <= ?", report.start_date) .where("topic_custom_fields.created_at <= ?", report.start_date)
.count .count
end end
end
if respond_to?(:register_modifier) register_modifier(:search_rank_sort_priorities) do |priorities, _search|
register_modifier(:search_rank_sort_priorities) do |priorities, search|
if SiteSetting.prioritize_solved_topics_in_search if SiteSetting.prioritize_solved_topics_in_search
condition = <<~SQL condition = <<~SQL
EXISTS EXISTS (
( SELECT 1
SELECT 1 FROM topic_custom_fields f FROM topic_custom_fields
WHERE topics.id = f.topic_id WHERE topic_id = topics.id
AND f.name = '#{::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD}' AND name = '#{::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD}'
AND value IS NOT NULL
) )
SQL SQL
@ -326,11 +314,8 @@ after_initialize do
priorities priorities
end end
end end
end
if defined?(::UserAction::SOLVED)
add_to_serializer(:user_summary, :solved_count) { object.solved_count } add_to_serializer(:user_summary, :solved_count) { object.solved_count }
end
add_to_serializer(:post, :can_accept_answer) do add_to_serializer(:post, :can_accept_answer) do
scope.can_accept_answer?(topic, object) && object.post_number > 1 && !accepted_answer scope.can_accept_answer?(topic, object) && object.post_number > 1 && !accepted_answer
end end
@ -344,108 +329,92 @@ after_initialize do
topic&.custom_fields&.[](::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD).present? topic&.custom_fields&.[](::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD).present?
end end
#TODO Remove when plugin is 1.0 solved_callback = ->(scope) do
if Search.respond_to? :advanced_filter sql = <<~SQL
Search.advanced_filter(/status:solved/) do |posts| topics.id IN (
posts.where( SELECT topic_id
"topics.id IN ( FROM topic_custom_fields
SELECT tc.topic_id WHERE name = '#{::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD}'
FROM topic_custom_fields tc AND value IS NOT NULL
WHERE tc.name = '#{::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD}' AND
tc.value IS NOT NULL
)",
) )
SQL
scope.where(sql)
end end
Search.advanced_filter(/status:unsolved/) do |posts| unsolved_callback = ->(scope) do
if SiteSetting.allow_solved_on_all_topics scope = scope.where <<~SQL
posts.where( topics.id NOT IN (
"topics.id NOT IN ( SELECT topic_id
SELECT tc.topic_id FROM topic_custom_fields
FROM topic_custom_fields tc WHERE name = '#{::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD}'
WHERE tc.name = '#{::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD}' AND AND value IS NOT NULL
tc.value IS NOT NULL
)",
) )
else SQL
if !SiteSetting.allow_solved_on_all_topics
tag_ids = Tag.where(name: SiteSetting.enable_solved_tags.split("|")).pluck(:id) tag_ids = Tag.where(name: SiteSetting.enable_solved_tags.split("|")).pluck(:id)
posts.where( scope = scope.where <<~SQL, tag_ids
"topics.id NOT IN ( topics.id IN (
SELECT tc.topic_id SELECT t.id
FROM topic_custom_fields tc FROM topics t
WHERE tc.name = '#{::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD}' AND JOIN category_custom_fields cc
tc.value IS NOT NULL ON t.category_id = cc.category_id
) AND (topics.id IN ( AND cc.name = '#{::DiscourseSolved::ENABLE_ACCEPTED_ANSWERS_CUSTOM_FIELD}'
SELECT top.id AND cc.value = 'true'
FROM topics top
INNER JOIN category_custom_fields cc
ON top.category_id = cc.category_id
WHERE cc.name = '#{::DiscourseSolved::ENABLE_ACCEPTED_ANSWERS_CUSTOM_FIELD}' AND
cc.value = 'true'
) OR topics.id IN (
SELECT top.id
FROM topics top
INNER JOIN topic_tags tt
ON top.id = tt.topic_id
WHERE tt.tag_id IN (?)
))",
tag_ids,
) )
end OR
end topics.id IN (
SELECT topic_id
FROM topic_tags
WHERE tag_id IN (?)
)
SQL
end end
if Discourse.has_needed_version?(Discourse::VERSION::STRING, "1.8.0.beta6") scope
end
register_custom_filter_by_status("solved", &solved_callback)
register_custom_filter_by_status("unsolved", &unsolved_callback)
register_search_advanced_filter(/status:solved/, &solved_callback)
register_search_advanced_filter(/status:unsolved/, &unsolved_callback)
TopicQuery.add_custom_filter(:solved) do |results, topic_query| TopicQuery.add_custom_filter(:solved) do |results, topic_query|
if topic_query.options[:solved] == "yes" if topic_query.options[:solved] == "yes"
results = solved_callback.call(results)
results.where(
"topics.id IN (
SELECT tc.topic_id
FROM topic_custom_fields tc
WHERE tc.name = '#{::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD}' AND
tc.value IS NOT NULL
)",
)
elsif topic_query.options[:solved] == "no" elsif topic_query.options[:solved] == "no"
results = unsolved_callback.call(results)
results.where( else
"topics.id NOT IN (
SELECT tc.topic_id
FROM topic_custom_fields tc
WHERE tc.name = '#{::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD}' AND
tc.value IS NOT NULL
)",
)
end
results results
end end
end end
if TopicList.respond_to? :preloaded_custom_fields
TopicList.preloaded_custom_fields << ::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD TopicList.preloaded_custom_fields << ::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD
end
if Site.respond_to? :preloaded_category_custom_fields
Site.preloaded_category_custom_fields << ::DiscourseSolved::ENABLE_ACCEPTED_ANSWERS_CUSTOM_FIELD Site.preloaded_category_custom_fields << ::DiscourseSolved::ENABLE_ACCEPTED_ANSWERS_CUSTOM_FIELD
end
if Search.respond_to? :preloaded_topic_custom_fields
Search.preloaded_topic_custom_fields << ::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD Search.preloaded_topic_custom_fields << ::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD
end
if CategoryList.respond_to?(:preloaded_topic_custom_fields)
CategoryList.preloaded_topic_custom_fields << ::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD CategoryList.preloaded_topic_custom_fields << ::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD
end
on(:filter_auto_bump_topics) { |_category, filters| filters.push(->(r) { r.where(<<~SQL) }) } on(:filter_auto_bump_topics) do |_category, filters|
NOT EXISTS( filters.push(
SELECT 1 FROM topic_custom_fields ->(r) do
sql = <<~SQL
NOT EXISTS (
SELECT 1
FROM topic_custom_fields
WHERE topic_id = topics.id WHERE topic_id = topics.id
AND name = '#{::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD}' AND name = '#{::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD}'
AND value IS NOT NULL AND value IS NOT NULL
) )
SQL SQL
r.where(sql)
end,
)
end
on(:before_post_publish_changes) do |post_changes, topic_changes, options| on(:before_post_publish_changes) do |post_changes, topic_changes, options|
category_id_changes = topic_changes.diff["category_id"].to_a category_id_changes = topic_changes.diff["category_id"].to_a
tag_changes = topic_changes.diff["tags"].to_a tag_changes = topic_changes.diff["tags"].to_a
@ -521,7 +490,7 @@ after_initialize do
AND di.period_type = :period_type AND di.period_type = :period_type
AND di.solutions <> x.solutions AND di.solutions <> x.solutions
" "
add_directory_column("solutions", query: query) if respond_to?(:add_directory_column) add_directory_column("solutions", query: query)
add_to_class(:composer_messages_finder, :check_topic_is_solved) do add_to_class(:composer_messages_finder, :check_topic_is_solved) do
return if !SiteSetting.solved_enabled || SiteSetting.disable_solved_education_message return if !SiteSetting.solved_enabled || SiteSetting.disable_solved_education_message
@ -538,13 +507,10 @@ after_initialize do
} }
end end
if defined?(UserAction::SOLVED)
add_to_serializer(:user_card, :accepted_answers) do add_to_serializer(:user_card, :accepted_answers) do
UserAction.where(user_id: object.id).where(action_type: UserAction::SOLVED).count UserAction.where(user_id: object.id).where(action_type: UserAction::SOLVED).count
end end
end
if respond_to?(:register_topic_list_preload_user_ids)
register_topic_list_preload_user_ids do |topics, user_ids, topic_list| register_topic_list_preload_user_ids do |topics, user_ids, topic_list|
answer_post_ids = answer_post_ids =
TopicCustomField TopicCustomField
@ -555,10 +521,8 @@ after_initialize do
topics.each { |topic| topic.accepted_answer_user_id = answer_user_ids[topic.id] } topics.each { |topic| topic.accepted_answer_user_id = answer_user_ids[topic.id] }
user_ids.concat(answer_user_ids.values) user_ids.concat(answer_user_ids.values)
end end
end
if defined?(DiscourseAutomation) if defined?(DiscourseAutomation)
if respond_to?(:add_triggerable_to_scriptable)
on(:accepted_solution) do |post| on(:accepted_solution) do |post|
# testing directly automation is prone to issues # testing directly automation is prone to issues
# we prefer to abstract logic in service object and test this # we prefer to abstract logic in service object and test this
@ -621,5 +585,4 @@ after_initialize do
required: true required: true
end end
end end
end
end end

View File

@ -9,6 +9,68 @@ RSpec.describe "Managing Posts solved status" do
before { SiteSetting.allow_solved_on_all_topics = true } before { SiteSetting.allow_solved_on_all_topics = true }
describe "customer filters" do
before do
SiteSetting.allow_solved_on_all_topics = false
SiteSetting.enable_solved_tags = solvable_tag.name
end
fab!(:solvable_category) do
category = Fabricate(:category)
CategoryCustomField.create(
category_id: category.id,
name: ::DiscourseSolved::ENABLE_ACCEPTED_ANSWERS_CUSTOM_FIELD,
value: "true",
)
category
end
fab!(:solvable_tag) { Fabricate(:tag) }
fab!(:solved_in_category) do
Fabricate(
:custom_topic,
category: solvable_category,
custom_topic_name: ::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD,
value: "42",
)
end
fab!(:solved_in_tag) do
Fabricate(
:custom_topic,
tags: [solvable_tag],
custom_topic_name: ::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD,
value: "42",
)
end
fab!(:unsolved_in_category) { Fabricate(:topic, category: solvable_category) }
fab!(:unsolved_in_tag) { Fabricate(:topic, tags: [solvable_tag]) }
fab!(:unsolved_topic) { Fabricate(:topic) }
it "can filter by solved status" do
expect(
TopicsFilter
.new(guardian: Guardian.new)
.filter_from_query_string("status:solved")
.pluck(:id),
).to contain_exactly(solved_in_category.id, solved_in_tag.id)
end
it "can filter by unsolved status" do
expect(
TopicsFilter
.new(guardian: Guardian.new)
.filter_from_query_string("status:unsolved")
.pluck(:id),
).to contain_exactly(unsolved_in_category.id, unsolved_in_tag.id)
end
end
describe "search" do describe "search" do
before { SearchIndexer.enable } before { SearchIndexer.enable }