mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
REFACTOR: redo DiscourseTagging.filter_allowed_tags (#8328)
This method had grown into a monster. Its query had bugs that I couldn't fix, and new features would be hard to add. Also I don't understand how it all works anymore... Replace it with common table expressions that can be queried to generate the results we need, instead of subtracting results using lots of "NOT IN" clauses. Fixed are bugs with tag schemas that use combinations of tag groups, parent tags, and one-tag-per-topic restrictions. For example: https://meta.discourse.org/t/130991/6
This commit is contained in:
@@ -195,7 +195,8 @@ class TagsController < ::ApplicationController
|
||||
def search
|
||||
filter_params = {
|
||||
for_input: params[:filterForInput],
|
||||
selected_tags: params[:selected_tags]
|
||||
selected_tags: params[:selected_tags],
|
||||
limit: params[:limit]
|
||||
}
|
||||
|
||||
if params[:categoryId]
|
||||
@@ -205,19 +206,14 @@ class TagsController < ::ApplicationController
|
||||
if params[:q]
|
||||
clean_name = DiscourseTagging.clean_tag(params[:q])
|
||||
filter_params[:term] = clean_name
|
||||
|
||||
# Prioritize exact matches when ordering
|
||||
order_query = Tag.sanitize_sql_for_order(
|
||||
filter_params[:order] = Tag.sanitize_sql_for_order(
|
||||
["lower(name) = lower(?) DESC, topic_count DESC", clean_name]
|
||||
)
|
||||
|
||||
tag_query = Tag.order(order_query).limit(params[:limit])
|
||||
else
|
||||
tag_query = Tag.limit(params[:limit])
|
||||
filter_params[:order] = "topic_count DESC"
|
||||
end
|
||||
|
||||
tags_with_counts = DiscourseTagging.filter_allowed_tags(
|
||||
tag_query,
|
||||
guardian,
|
||||
filter_params
|
||||
)
|
||||
|
||||
@@ -327,10 +327,9 @@ class TopicsController < ApplicationController
|
||||
if category && topic_tags = (params[:tags] || topic.tags.pluck(:name)).reject { |c| c.empty? }
|
||||
if topic_tags.present?
|
||||
allowed_tags = DiscourseTagging.filter_allowed_tags(
|
||||
Tag.all,
|
||||
guardian,
|
||||
category: category
|
||||
).pluck("tags.name")
|
||||
).map(&:name)
|
||||
|
||||
invalid_tags = topic_tags - allowed_tags
|
||||
|
||||
|
||||
Reference in New Issue
Block a user