Skip to content

A platform for community discussion. Free, open, simple.

DEV: Extract reusable PostsFilter into core (#40436)

Sam0bad05d036c0
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 212a24ddb11..f1587553cba 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -2986,6 +2986,32 @@ en:
audio: "[audio]"
video: "[video]"
+ posts_filter:
+ description:
+ category: "Show posts in a specific category"
+ category_any: "Show posts in any of the specified categories (comma-separated)"
+ exclude_category: "Exclude posts from a specific category"
+ category_without_subcategories: "Show posts only in the parent category, excluding subcategories"
+ exclude_category_without_subcategories: "Exclude posts only from the parent category, not subcategories"
+ tag: "Show posts with a specific tag"
+ tags_any: "Show posts with any of the specified tags (comma-separated)"
+ exclude_tag: "Exclude posts with a specific tag"
+ username: "Show posts by a specific user"
+ usernames_any: "Show posts by any of the specified users (comma-separated)"
+ group: "Show posts by members of a group"
+ groups_any: "Show posts by members of any specified group (comma-separated)"
+ topic: "Show posts in specific topics"
+ after: "Show posts created after a date (YYYY-MM-DD or days ago)"
+ before: "Show posts created before a date (YYYY-MM-DD or days ago)"
+ topic_after: "Show posts in topics created after a date (YYYY-MM-DD or days ago)"
+ topic_before: "Show posts in topics created before a date (YYYY-MM-DD or days ago)"
+ keywords: "Show posts containing any of the specified keywords"
+ topic_keywords: "Show posts in topics containing any of the specified keywords"
+ post_type: "Show first posts or replies"
+ status: "Filter posts by topic status"
+ order: "Sort posts"
+ max_results: "Maximum number of posts to return"
+
filter:
description:
status: "Filter topics by their status"
diff --git a/frontend/discourse/app/components/discovery/filter-navigation-menu.gjs b/frontend/discourse/app/components/discovery/filter-navigation-menu.gjs
index dda1acf17b4..c93cf3268cd 100644
--- a/frontend/discourse/app/components/discovery/filter-navigation-menu.gjs
+++ b/frontend/discourse/app/components/discovery/filter-navigation-menu.gjs
@@ -99,6 +99,10 @@ export default class FilterNavigationMenu extends Component {
return this.selectedIndex === -1;
}
+ get placeholder() {
+ return this.args.placeholder || i18n("filter.placeholder");
+ }
+
@action
storeInputElement(element) {
this.inputElement = element;
@@ -354,7 +358,7 @@ export default class FilterNavigationMenu extends Component {
type="text"
id="topic-query-filter-input"
autocomplete="off"
- placeholder={{i18n "filter.placeholder"}}
+ placeholder={{this.placeholder}}
{{didUpdate this.syncFromInitialValue @initialInputValue}}
/>
diff --git a/lib/posts_filter.rb b/lib/posts_filter.rb
new file mode 100644
index 00000000000..f61081bee5b
--- /dev/null
+++ b/lib/posts_filter.rb
@@ -0,0 +1,625 @@
+# frozen_string_literal: true
+
+class PostsFilter
+ FILTER_ALIASES = {
+ "category" => "category",
+ "categories" => "category",
+ "exclude_category" => "category",
+ "exclude_categories" => "category",
+ "tag" => "tag",
+ "tags" => "tag",
+ "exclude_tag" => "tag",
+ "exclude_tags" => "tag",
+ "username" => "usernames",
+ "usernames" => "usernames",
+ "group" => "groups",
+ "groups" => "groups",
+ "topic" => "topics",
+ "topics" => "topics",
+ "created_after" => "after",
+ "created_before" => "before",
+ "topic_created_after" => "topic_after",
+ "topic_created_before" => "topic_before",
+ }
+ private_constant :FILTER_ALIASES
+
+ FILTER_PREFIX_PATTERN = /\A(?<prefix>-=|=-|-|=)?(?<key>[\w-]+):(?<value>.+)\z/
+ private_constant :FILTER_PREFIX_PATTERN
+
+ TOKENIZER_PATTERN = /(?:[^\s"']+|"[^"]*"|'[^']*')+/
+ private_constant :TOKENIZER_PATTERN
+
+ ORDER_VALUES = %w[latest oldest latest_topic oldest_topic likes].freeze
+ POST_TYPE_VALUES = %w[first reply].freeze
+ STATUS_VALUES = %w[open closed archived noreplies single_user].freeze
+ CUSTOM_FILTER_PREFIXES = [nil, ""].freeze
+ private_constant :CUSTOM_FILTER_PREFIXES
+
+ attr_reader :term, :filters, :order, :guardian, :limit, :offset, :invalid_filters
+
+ def self.add_filter(name, aliases: [], enabled: -> { true }, &block)
+ raise ArgumentError, "block is required" if block.blank?
+
+ custom_filters[name.to_s] = { block: block, enabled: enabled, aliases: aliases.map(&:to_s) }
+ end
+
+ def self.remove_filter(name)
+ custom_filters.delete(name.to_s)
+ end
+
+ def self.custom_filters
+ PostsFilter.instance_variable_get(:@custom_filters) ||
+ PostsFilter.instance_variable_set(:@custom_filters, {})
+ end
+
+ def self.custom_filter_for(key)
+ custom_filters.find { |name, config| name == key || config[:aliases].include?(key) }&.last
+ end
+
+ def self.word_to_date(str)
+ ::Search.word_to_date(str)
+ end
+
+ def self.category_ids_from_param(category_param, exact: false)
+ category_param
+ .to_s
+ .split(",")
+ .map(&:strip)
+ .reject(&:blank?)
+ .flat_map { |category_value| category_ids_from_value(category_value, exact: exact) }
+ .uniq
+ end
+
+ def self.category_ids_from_value(category_value, exact: false)
+ category_value = category_value.to_s.strip
+ exact = true if category_value.start_with?("=")
+ category_value = category_value[1..] if category_value.start_with?("=")
+ category_value = strip_surrounding_quotes(category_value.strip)
+
+ category = find_category(category_value)
+ return [] if category.blank?
+
+ category_ids = [category.id]
+ category_ids.concat(Category.subcategory_ids(category.id)) if !exact
+ category_ids
+ end
+
+ def self.find_category(category_value)
+ parent_value, child_value = category_value.split(%r{[/:]}, 2)
+
+ if child_value.present?
+ parent_category_ids = matching_categories(parent_value).select(:id)
+
+ matching_categories(child_value)
+ .where(parent_category_id: parent_category_ids)
+ .order("case when parent_category_id is null then 0 else 1 end")
+ .first
+ else
+ matching_categories(category_value).order(
+ "case when parent_category_id is null then 0 else 1 end",
+ ).first
+ end
+ end
+
+ def self.matching_categories(category_value)
+ categories =
+ Category.where(
+ "#{Category.normalize_sql("slug")} = #{Category.normalize_sql("?")} OR " \
+ "#{Category.normalize_sql("name")} = #{Category.normalize_sql("?")}",
+ category_value,
+ category_value,
+ )
+
+ if category_value.match?(/\A\d{1,10}\z/)
+ categories = categories.or(Category.where(id: category_value.to_i))
+ end
+
+ categories
+ end
+
+ def self.strip_surrounding_quotes(value)
+ if value.length >= 2 &&
+ (
+ (value.start_with?("\"") && value.end_with?("\"")) ||
+ (value.start_with?("'") && value.end_with?("'"))
+ )
+ value[1...-1]
+ else
+ value
+ end
+ end
+
+ def self.option_info(guardian)
+ results = [
+ {
+ name: "category:",
+ alias: "categories:",
+ description: I18n.t("posts_filter.description.category"),
+ priority: 1,
+ type: "category",
+ delimiters: [{ name: ",", description: I18n.t("posts_filter.description.category_any") }],
+ prefixes: [
+ { name: "-", description: I18n.t("posts_filter.description.exclude_category") },
+ {
+ name: "=",
+ description: I18n.t("posts_filter.description.category_without_subcategories"),
+ },
+ {
+ name: "-=",
+ description: I18n.t("posts_filter.description.exclude_category_without_subcategories"),
+ },
+ ],
+ },
+ {
+ name: "tag:",
+ alias: "tags:",
+ description: I18n.t("posts_filter.description.tag"),
+ priority: 1,
+ type: "tag",
+ delimiters: [{ name: ",", description: I18n.t("posts_filter.description.tags_any") }],
+ prefixes: [{ name: "-", description: I18n.t("posts_filter.description.exclude_tag") }],
+ },
+ {
+ name: "username:",
+ alias: "usernames:",
+ description: I18n.t("posts_filter.description.username"),
+ type: "username",
+ delimiters: [{ name: ",", description: I18n.t("posts_filter.description.usernames_any") }],
+ },
+ {
+ name: "group:",
+ alias: "groups:",
+ description: I18n.t("posts_filter.description.group"),
+ type: "group",
+ delimiters: [{ name: ",", description: I18n.t("posts_filter.description.groups_any") }],
+ },
+ {
+ name: "topic:",
+ alias: "topics:",
+ description: I18n.t("posts_filter.description.topic"),
+ type: "number",
+ },
+ { name: "after:", description: I18n.t("posts_filter.description.after"), type: "date" },
+ { name: "before:", description: I18n.t("posts_filter.description.before"), type: "date" },
+ {
+ name: "topic_after:",
+ description: I18n.t("posts_filter.description.topic_after"),
+ type: "date",
+ },
+ {
+ name: "topic_before:",
+ description: I18n.t("posts_filter.description.topic_before"),
+ type: "date",
+ },
+ {
+ name: "keywords:",
+ description: I18n.t("posts_filter.description.keywords"),
+ type: "string",
+ },
+ {
+ name: "topic_keywords:",
+ description: I18n.t("posts_filter.description.topic_keywords"),
+ type: "string",
+ },
+ {
+ name: "post_type:",
+ description: I18n.t("posts_filter.description.post_type"),
+ type: "string",
+ extra_entries: POST_TYPE_VALUES.map { |value| { name: value } },
+ },
+ {
+ name: "status:",
+ description: I18n.t("posts_filter.description.status"),
+ type: "string",
+ extra_entries: STATUS_VALUES.map { |value| { name: value } },
+ },
+ {
+ name: "order:",
+ description: I18n.t("posts_filter.description.order"),
+ type: "string",
+ extra_entries: ORDER_VALUES.map { |value| { name: value } },
+ },
+ {
+ name: "max_results:",
+ description: I18n.t("posts_filter.description.max_results"),
+ type: "number",
+ },
+ ]
+
+ DiscoursePluginRegistry.apply_modifier(:posts_filter_options, results, guardian)
+ end
+
+ def initialize(query_string = nil, guardian: nil, limit: nil, offset: nil, scope: Post.all)
+ @guardian = guardian || Guardian.new
+ @base_scope = scope
+ @initial_limit = limit
+ @initial_offset = offset
+ @limit = limit
+ @offset = offset
+ @term = query_string.to_s.strip
+ reset_filter_state!
+ process_filters(@term)
+ end
+
+ def filter_from_query_string(query_string)
+ @term = query_string.to_s.strip
+ reset_filter_state!
+ process_filters(@term)
+ search
+ end
+
+ def set_order!(order)
+ @order = order
+ end
+
+ def limit_by_user!(limit)
+ @limit = limit if limit.to_i < @limit.to_i || @limit.nil?
+ end
+
+ def search
+ base_relation = secure_base_relation
+ filtered = filtered_relation(base_relation)
+ ordered = order_relation(filtered)
+ ordered = ordered.limit(@limit) if @limit.to_i > 0
+ ordered = ordered.offset(@offset) if @offset.to_i > 0
+ ordered
+ end
+
+ private
+
+ def reset_filter_state!
+ @filters = []
+ @limit = @initial_limit
+ @offset = @initial_offset
+ @order = :latest_post
+ @invalid_filters = []
+ @or_groups = []
+ end
+
+ def secure_base_relation
+ relation =
+ @base_scope
+ .secured(@guardian)
+ .joins(:topic)
+ .merge(Topic.secured(@guardian))
+ .where("topics.archetype = ?", Archetype.default)
+
+ relation = @guardian.filter_hidden_posts(relation)
+ relation = relation.where(topics: { visible: true }) if guardian_should_hide_unlisted_topics?
+
+ if SiteSetting.shared_drafts_category.present? && [email protected]_see_shared_draft?
+ relation =
+ relation.where.not(topics: { category_id: SiteSetting.shared_drafts_category.to_i })
+ end
+
+ relation
+ end
+
+ def guardian_should_hide_unlisted_topics?
+ @guardian.anonymous? || [email protected]_see_unlisted_topics?
+ end
+
+ def filtered_relation(base_relation)
+ if @or_groups.any?
+ or_relations =
+ @or_groups.map do |or_group|
+ or_group.reduce(base_relation) do |relation, parsed_filter|
+ apply_filter(relation, parsed_filter)
+ end
+ end
+
+ union_sql =
+ or_relations.map { |relation| relation.reselect("posts.id").to_sql }.join(" UNION ")
+ base_relation.where("posts.id IN (#{union_sql})")
+ else
+ @filters.reduce(base_relation) do |relation, parsed_filter|
+ apply_filter(relation, parsed_filter)
+ end
+ end
+ end
+
+ def order_relation(relation)
+ case @order
+ when :latest_post
+ relation.order("posts.created_at DESC")
+ when :oldest_post
+ relation.order("posts.created_at ASC")
+ when :latest_topic
+ relation.order("topics.created_at DESC, posts.post_number DESC")
+ when :oldest_topic
+ relation.order("topics.created_at ASC, posts.post_number ASC")
+ when :likes
+ relation.order("posts.like_count DESC, posts.created_at DESC")
+ else
+ relation
+ end
+ end
+
+ def process_filters(term)
+ return if term.blank?
+
+ or_parts = term.split(/\s+OR\s+/i)
+
+ if or_parts.size > 1
+ or_parts.each do |or_part|
+ group_filters = []
+ process_filter_group(or_part.strip, group_filters)
+ @or_groups << group_filters if group_filters.any?
+ end
+ else
+ process_filter_group(term, @filters)
+ end
+ end
+
+ def process_filter_group(term_part, filter_collection)
+ term_part
+ .to_s
+ .scan(TOKENIZER_PATTERN)
+ .each do |word|
+ next if word.blank?
+
+ parsed_filter = parse_filter(word)
+ if parsed_filter && valid_filter?(parsed_filter)
+ filter_collection << parsed_filter
+ else
+ invalid_filters << word
+ end
+ end
+ end
+
+ def parse_filter(word)
+ match = word.match(FILTER_PREFIX_PATTERN)
+ return if match.blank?
+
+ raw_key = match[:key].downcase
+ key = FILTER_ALIASES[raw_key] || raw_key
+ prefix = match[:prefix].to_s
+
+ {
+ token: word,
+ raw_key: raw_key,
+ key: key,
+ prefix: prefix,
+ value: self.class.strip_surrounding_quotes(match[:value].to_s.strip),
+ exclude: prefix.include?("-") || raw_key.start_with?("exclude_"),
+ exact: prefix.include?("="),
+ }
+ end
+
+ def valid_filter?(parsed_filter)
+ key = parsed_filter[:key]
+ value = parsed_filter[:value]
+
+ case key
+ when "category", "tag"
+ true
+ when "before", "after", "topic_before", "topic_after", "keywords", "topic_keywords",
+ "usernames", "groups", "topics"
+ !parsed_filter[:exclude] && parsed_filter[:prefix].exclude?("=")
+ when "status"
+ !parsed_filter[:exclude] && STATUS_VALUES.include?(value.downcase)
+ when "post_type"
+ !parsed_filter[:exclude] && POST_TYPE_VALUES.include?(value.downcase)
+ when "max_results"
+ !parsed_filter[:exclude] && value.match?(/\A\d+\z/)
+ when "order"
+ !parsed_filter[:exclude] && ORDER_VALUES.include?(value.downcase)
+ else
+ custom_filter = self.class.custom_filter_for(key)
+ custom_filter.present? && CUSTOM_FILTER_PREFIXES.include?(parsed_filter[:prefix]) &&
+ custom_filter[:enabled].call
+ end
+ end
+
+ def apply_filter(relation, parsed_filter)
+ case parsed_filter[:key]
+ when "status"
+ filter_status(relation, parsed_filter[:value].downcase)
+ when "before"
+ filter_by_date(relation, "posts.created_at < ?", parsed_filter[:value])
+ when "after"
+ filter_by_date(relation, "posts.created_at > ?", parsed_filter[:value])
+ when "topic_before"
+ filter_by_date(relation, "topics.created_at < ?", parsed_filter[:value])
+ when "topic_after"
+ filter_by_date(relation, "topics.created_at > ?", parsed_filter[:value])
+ when "tag"
+ filter_tags(relation, parsed_filter)
+ when "keywords"
+ filter_keywords(relation, parsed_filter[:value])
+ when "topic_keywords"
+ filter_topic_keywords(relation, parsed_filter[:value])
+ when "category"
+ filter_categories(relation, parsed_filter)
+ when "usernames"
+ filter_usernames(relation, parsed_filter[:value])
+ when "groups"
+ filter_groups(relation, parsed_filter[:value])
+ when "max_results"
+ limit_by_user!(parsed_filter[:value].to_i)
+ relation
+ when "order"
+ set_order!(order_value(parsed_filter[:value]))
+ relation
+ when "topics"
+ filter_topics(relation, parsed_filter[:value])
+ when "post_type"
+ filter_post_type(relation, parsed_filter[:value])
+ else
+ apply_custom_filter(relation, parsed_filter)
+ end
+ end
+
+ def filter_status(relation, status)
+ case status
+ when "open"
+ relation.where("topics.closed = false AND topics.archived = false")
+ when "closed"
+ relation.where("topics.closed = true")
+ when "archived"
+ relation.where("topics.archived = true")
+ when "noreplies"
+ relation.where("topics.posts_count = 1")
+ when "single_user"
+ relation.where("topics.participant_count = 1")
+ else
+ relation
+ end
+ end
+
+ def filter_by_date(relation, condition, date_str)
+ if date = self.class.word_to_date(date_str)
+ relation.where(condition, date)
+ else
+ relation
+ end
+ end
+
+ def filter_tags(relation, parsed_filter)
+ tag_names = split_values(parsed_filter[:value])
+ return relation if tag_names.empty?
+
+ tag_ids = DiscourseTagging.filter_visible(Tag, @guardian).where_name(tag_names).pluck(:id)
+ return relation.where("1 = 0") if tag_ids.empty? && !parsed_filter[:exclude]
+ return relation if tag_ids.empty?
+
+ topic_ids = TopicTag.where(tag_id: tag_ids).select(:topic_id)
+ if parsed_filter[:exclude]
+ relation.where.not(topic_id: topic_ids)
+ else
+ relation.where(topic_id: topic_ids)
+ end
+ end
+
+ def filter_keywords(relation, keywords_param)
+ keywords = split_values(keywords_param)
+ return relation if keywords.empty?
+
+ ts_query = keywords.map { |keyword| keyword.gsub(/['\\]/, " ") }.join(" | ")
+ relation.joins("JOIN post_search_data ON post_search_data.post_id = posts.id").where(
+ "post_search_data.search_data @@ to_tsquery(?, ?)",
+ ::Search.ts_config,
+ ts_query,
+ )
+ end
+
+ def filter_topic_keywords(relation, keywords_param)
+ keywords = split_values(keywords_param)
+ return relation if keywords.empty?
+
+ ts_query = keywords.map { |keyword| keyword.gsub(/['\\]/, " ") }.join(" | ")
+ hidden_posts_condition = @guardian.can_see_all_hidden_posts? ? "" : "AND posts2.hidden = false"
+ whisper_condition =
+ @guardian.can_see_whispers? ? "" : "AND posts2.post_type <> #{Post.types[:whisper]}"
+
+ relation.where(
+ "posts.topic_id IN (
+ SELECT posts2.topic_id
+ FROM posts posts2
+ JOIN post_search_data ON post_search_data.post_id = posts2.id
+ WHERE post_search_data.search_data @@ to_tsquery(?, ?)
+ AND posts2.deleted_at IS NULL
+ AND posts2.post_type IN (?)
+ #{hidden_posts_condition}
+ #{whisper_condition}
+ )",
+ ::Search.ts_config,
+ ts_query,
+ Topic.visible_post_types(@guardian.user),
+ )
+ end
+
+ def filter_categories(relation, parsed_filter)
+ category_ids =
+ self.class.category_ids_from_param(parsed_filter[:value], exact: parsed_filter[:exact])
+
+ return relation.where("1 = 0") if category_ids.empty? && !parsed_filter[:exclude]
+ return relation if category_ids.empty?
+
+ topic_ids = Topic.where(category_id: category_ids).select(:id)
+ if parsed_filter[:exclude]
+ relation.where.not(topic_id: topic_ids)
+ else
+ relation.where(topic_id: topic_ids)
+ end
+ end
+
+ def filter_usernames(relation, usernames_param)
+ usernames = split_values(usernames_param).map(&:downcase)
+ user_ids = User.where(username_lower: usernames).pluck(:id)
+
+ if user_ids.empty?
+ relation.where("1 = 0")
+ else
+ relation.where("posts.user_id IN (?)", user_ids)
+ end
+ end
+
+ def filter_groups(relation, groups_param)
+ group_names = split_values(groups_param).map(&:downcase)
+ found_group_ids =
+ Group
+ .visible_groups(@guardian.user)
+ .members_visible_groups(@guardian.user)
+ .where("LOWER(name) IN (?)", group_names)
+ .pluck(:id)
+
+ return relation.where("1 = 0") if found_group_ids.empty?
+
+ relation.where(
+ "posts.user_id IN (
+ SELECT group_users.user_id FROM group_users
+ WHERE group_users.group_id IN (?)
+ )",
+ found_group_ids,
+ )
+ end
+
+ def filter_topics(relation, topics_param)
+ topic_ids = split_values(topics_param).map(&:to_i).reject(&:zero?)
+ return relation.where("1 = 0") if topic_ids.empty?
+
+ relation.where("posts.topic_id IN (?)", topic_ids)
+ end
+
+ def filter_post_type(relation, post_type)
+ case post_type.downcase
+ when "first"
+ relation.where("posts.post_number = 1")
+ when "reply"
+ relation.where("posts.post_number > 1")
+ else
+ relation
+ end
+ end
+
+ def apply_custom_filter(relation, parsed_filter)
+ custom_filter = self.class.custom_filter_for(parsed_filter[:key])
+ return relation if custom_filter.blank?
+
+ custom_filter[:block].call(relation, [parsed_filter[:value]], @guardian) || relation
+ end
+
+ def order_value(value)
+ case value.downcase
+ when "latest"
+ :latest_post
+ when "oldest"
+ :oldest_post
+ when "latest_topic"
+ :latest_topic
+ when "oldest_topic"
+ :oldest_topic
+ when "likes"
+ :likes
+ end
+ end
+
+ def split_values(value)
+ value
+ .to_s
+ .split(",")
+ .map { |part| self.class.strip_surrounding_quotes(part.strip) }
+ .reject(&:blank?)
+ end
+end
diff --git a/plugins/discourse-ai/lib/agents/tools/researcher.rb b/plugins/discourse-ai/lib/agents/tools/researcher.rb
index 21dbfae4447..e134c7bceb7 100644
--- a/plugins/discourse-ai/lib/agents/tools/researcher.rb
+++ b/plugins/discourse-ai/lib/agents/tools/researcher.rb
@@ -42,8 +42,11 @@ module DiscourseAi
- topic:123 or topics:123,456 - specific topics by ID
- category:name1 or categories:name1,name2 - posts in categories and subcategories (by name/slug/id)
Prefix a category with = to exclude subcategories, for example category:=bugs.
+ Prefix a category filter with - to exclude it, for example -category:staff or -=category:staff.
+ Use exclude_category:name or exclude_categories:name1,name2 as exclusion aliases.
Use parent/child for subcategories when a slug is ambiguous, for example category:support/bugs.
- tag:tag1 or tags:tag1,tag2 - posts in topics with tags
+ Prefix with - or use exclude_tag:tag1 / exclude_tags:tag1,tag2 to exclude tags
- after:YYYY-MM-DD, before:YYYY-MM-DD - filter by post creation date
- topic_after:YYYY-MM-DD, topic_before:YYYY-MM-DD - filter by topic creation date
- status:open|closed|archived|noreplies|single_user - topic status filters
@@ -100,12 +103,7 @@ module DiscourseAi
guardian = nil
guardian = Guardian.new(context.user) if options[:include_private]
- filter =
- DiscourseAi::Utils::Research::Filter.new(
- @filter,
- limit: max_results,
- guardian: guardian,
- )
+ filter = PostsFilter.new(@filter, limit: max_results, guardian: guardian)
if filter.invalid_filters.present?
return(
diff --git a/plugins/discourse-ai/lib/automation/report_context_generator.rb b/plugins/discourse-ai/lib/automation/report_context_generator.rb
index 952908631ae..de1b4cd61e0 100644
--- a/plugins/discourse-ai/lib/automation/report_context_generator.rb
+++ b/plugins/discourse-ai/lib/automation/report_context_generator.rb
@@ -31,40 +31,10 @@ module DiscourseAi
@prioritized_group_ids = prioritized_group_ids
@posts =
- Post
- .where("posts.created_at >= ?", @start_date)
- .joins(topic: :category)
- .includes(:topic, :user)
- .where("topics.visible")
- .where("posts.created_at < ?", @start_date + @duration)
- .where("posts.post_type = ?", Post.types[:regular])
- .where("posts.hidden_at IS NULL")
- .where("topics.deleted_at IS NULL")
- .where("topics.archetype = ?", Archetype.default)
- @posts = @posts.where("categories.read_restricted = ?", false) if !@allow_secure_categories
- @posts = @posts.where("categories.id IN (?)", @category_ids) if @category_ids.present?
- @posts =
- @posts.where(
- "categories.id NOT IN (:ids) AND
- (parent_category_id NOT IN (:ids) OR parent_category_id IS NULL)",
- ids: exclude_category_ids,
- ) if exclude_category_ids.present?
-
- if exclude_tags.present?
- exclude_tag_ids = Tag.where_name(exclude_tags).select(:id)
- @posts =
- @posts.where.not(
- topics: {
- id: TopicTag.where(tag_id: exclude_tag_ids).select(:topic_id),
- },
- )
- end
-
- if @tags.present?
- tag_ids = Tag.where_name(@tags).select(:id)
- topic_ids_with_tags = TopicTag.where(tag_id: tag_ids).select(:topic_id)
- @posts = @posts.where(topic_id: topic_ids_with_tags)
- end
+ build_posts_relation(
+ exclude_category_ids: exclude_category_ids,
+ exclude_tags: exclude_tags,
+ )
if defined?(DiscourseSolved)
@solutions =
@@ -83,6 +53,35 @@ module DiscourseAi
end
end
+ def build_posts_relation(exclude_category_ids:, exclude_tags:)
+ scope =
+ Post
+ .where("posts.created_at >= ?", @start_date)
+ .joins(topic: :category)
+ .includes(:topic, :user)
+ .where("topics.visible")
+ .where("posts.created_at < ?", @start_date + @duration)
+ .where("posts.post_type = ?", Post.types[:regular])
+ .where("posts.hidden_at IS NULL")
+ .where("topics.deleted_at IS NULL")
+
+ filter_query =
+ posts_filter_query(exclude_category_ids: exclude_category_ids, exclude_tags: exclude_tags)
+ guardian = @allow_secure_categories ? Discourse.system_user.guardian : Guardian.new
+ PostsFilter.new(guardian: guardian, scope: scope).filter_from_query_string(filter_query)
+ end
+
+ def posts_filter_query(exclude_category_ids:, exclude_tags:)
+ parts = []
+ parts << "category:#{Array(@category_ids).join(",")}" if @category_ids.present?
+ parts << "tag:#{Array(@tags).join(",")}" if @tags.present?
+ if exclude_category_ids.present?
+ parts << "exclude_category:#{Array(exclude_category_ids).join(",")}"
+ end
+ parts << "exclude_tag:#{Array(exclude_tags).join(",")}" if exclude_tags.present?
+ parts.join(" ")
+ end
+
def format_topic(topic)
info = []
info << ""
diff --git a/plugins/discourse-ai/lib/utils/research/filter.rb b/plugins/discourse-ai/lib/utils/research/filter.rb
deleted file mode 100644
index 6af9d5d273d..00000000000
--- a/plugins/discourse-ai/lib/utils/research/filter.rb
+++ /dev/null
@@ -1,447 +0,0 @@
-# frozen_string_literal: true
-
-module DiscourseAi
- module Utils
- module Research
- class Filter
- def self.register_filter(matcher, &block)
- (@registered_filters ||= {})[matcher] = block
- end
-
- def self.registered_filters
- @registered_filters ||= {}
- end
-
- def self.word_to_date(str)
- ::Search.word_to_date(str)
- end
-
- def self.category_ids_from_param(category_param)
- category_param
- .to_s
- .split(",")
- .map(&:strip)
- .reject(&:blank?)
- .flat_map { |category_value| category_ids_from_value(category_value) }
- .uniq
- end
-
- def self.category_ids_from_value(category_value)
- exact = category_value.start_with?("=")
- category_value = category_value[1..] if exact
- category_value = strip_surrounding_quotes(category_value.strip)
-
- category = find_category(category_value)
- return [] if category.blank?
-
- category_ids = [category.id]
- category_ids.concat(Category.subcategory_ids(category.id)) if !exact
- category_ids
- end
-
- def self.find_category(category_value)
- parent_value, child_value = category_value.split(%r{[/:]}, 2)
-
- if child_value.present?
- parent_category_ids = matching_categories(parent_value).select(:id)
-
- matching_categories(child_value)
- .where(parent_category_id: parent_category_ids)
- .order("case when parent_category_id is null then 0 else 1 end")
- .first
- else
- matching_categories(category_value).order(
- "case when parent_category_id is null then 0 else 1 end",
- ).first
- end
- end
-
- def self.matching_categories(category_value)
- categories =
- Category.where(
- "#{Category.normalize_sql("slug")} = #{Category.normalize_sql("?")} OR " \
- "#{Category.normalize_sql("name")} = #{Category.normalize_sql("?")}",
- category_value,
- category_value,
- )
-
- if category_value.match?(/\A\d{1,10}\z/)
- categories = categories.or(Category.where(id: category_value.to_i))
- end
-
- categories
- end
-
- def self.strip_surrounding_quotes(value)
- if value.length >= 2 && value.start_with?("\"") && value.end_with?("\"")
- value[1...-1]
- else
- value
- end
- end
-
- attr_reader :term, :filters, :order, :guardian, :limit, :offset, :invalid_filters
-
- register_filter(/\Astatus:open\z/i) do |relation, _, _|
- relation.where("topics.closed = false AND topics.archived = false")
- end
-
- register_filter(/\Astatus:closed\z/i) do |relation, _, _|
- relation.where("topics.closed = true")
- end
-
- register_filter(/\Astatus:archived\z/i) do |relation, _, _|
- relation.where("topics.archived = true")
- end
-
- register_filter(/\Astatus:noreplies\z/i) do |relation, _, _|
- relation.where("topics.posts_count = 1")
- end
-
- register_filter(/\Astatus:single_user\z/i) do |relation, _, _|
- relation.where("topics.participant_count = 1")
- end
-
- # Date filters
- register_filter(/\Abefore:(.*)\z/i) do |relation, date_str, _|
- if date = Filter.word_to_date(date_str)
- relation.where("posts.created_at < ?", date)
- else
- relation
- end
- end
-
- register_filter(/\Aafter:(.*)\z/i) do |relation, date_str, _|
- if date = Filter.word_to_date(date_str)
- relation.where("posts.created_at > ?", date)
- else
- relation
- end
- end
-
- register_filter(/\Atopic_before:(.*)\z/i) do |relation, date_str, _|
- if date = Filter.word_to_date(date_str)
- relation.where("topics.created_at < ?", date)
- else
- relation
- end
- end
-
- register_filter(/\Atopic_after:(.*)\z/i) do |relation, date_str, _|
- if date = Filter.word_to_date(date_str)
- relation.where("topics.created_at > ?", date)
- else
- relation
- end
- end
-
- register_filter(/\A(?:tags?|tag):(.*)\z/i) do |relation, tag_param, _|
- if tag_param.include?(",")
- tag_names = tag_param.split(",").map(&:strip)
- tag_ids = Tag.where(name: tag_names).pluck(:id)
- return relation.where("1 = 0") if tag_ids.empty?
- relation.where(topic_id: TopicTag.where(tag_id: tag_ids).select(:topic_id))
- else
- if tag = Tag.find_by(name: tag_param)
- relation.where(topic_id: TopicTag.where(tag_id: tag.id).select(:topic_id))
- else
- relation.where("1 = 0")
- end
- end
- end
-
- register_filter(/\Akeywords?:(.*)\z/i) do |relation, keywords_param, _|
- if keywords_param.blank?
- relation
- else
- keywords = keywords_param.split(",").map(&:strip).reject(&:blank?)
- if keywords.empty?
- relation
- else
- # Build a ts_query string joined by | (OR)
- ts_query = keywords.map { |kw| kw.gsub(/['\\]/, " ") }.join(" | ")
- relation =
- relation.joins("JOIN post_search_data ON post_search_data.post_id = posts.id")
- relation.where(
- "post_search_data.search_data @@ to_tsquery(?, ?)",
- ::Search.ts_config,
- ts_query,
- )
- end
- end
- end
-
- register_filter(/\Atopic_keywords?:(.*)\z/i) do |relation, keywords_param, _|
- if keywords_param.blank?
- relation
- else
- keywords = keywords_param.split(",").map(&:strip).reject(&:blank?)
- if keywords.empty?
- relation
- else
- ts_query = keywords.map { |kw| kw.gsub(/['\\]/, " ") }.join(" | ")
-
- relation.where(
- "posts.topic_id IN (
- SELECT posts2.topic_id
- FROM posts posts2
- JOIN post_search_data ON post_search_data.post_id = posts2.id
- WHERE post_search_data.search_data @@ to_tsquery(?, ?)
- )",
- ::Search.ts_config,
- ts_query,
- )
- end
- end
- end
-
- register_filter(/\A(?:categories?|category):(.*)\z/i) do |relation, category_param, _|
- category_ids = Filter.category_ids_from_param(category_param)
-
- if category_ids.empty?
- relation.where("1 = 0")
- else
- relation.where(topic_id: Topic.where(category_id: category_ids).select(:id))
- end
- end
-
- register_filter(/\Ausernames?:(.+)\z/i) do |relation, username, filter|
- user_ids = User.where(username_lower: username.split(",").map(&:downcase)).pluck(:id)
- if user_ids.empty?
- relation.where("1 = 0")
- else
- relation.where("posts.user_id IN (?)", user_ids)
- end
- end
-
- def self.assign_allowed?(guardian)
- SiteSetting.respond_to?(:assign_enabled) && SiteSetting.assign_enabled &&
- (guardian.can_assign? || SiteSetting.assigns_public)
- end
-
- register_filter(/\Aassigned_to:(.+)\z/i) do |relation, name, filter|
- if !assign_allowed?(filter.guardian)
- raise Discourse::InvalidAccess.new(
- "Assigns are not enabled or you do not have permission to see assigns.",
- )
- end
-
- if (name == "nobody")
- relation.joins("LEFT JOIN assignments a ON a.topic_id = topics.id AND a.active").where(
- "a.assigned_to_id IS NULL",
- )
- elsif name == "*"
- relation
- .joins("JOIN assignments a ON a.topic_id = topics.id AND a.active")
- .where.not(a: { assigned_to_id: nil })
- else
- usernames = name.split(",").map(&:strip).map(&:downcase)
- relation.joins("JOIN assignments a ON a.topic_id = topics.id AND a.active").where(
- "a.assigned_to_id" => User.where(username_lower: usernames).select(:id),
- )
- end
- end
-
- register_filter(/\Agroups?:([a-zA-Z0-9_\-,]+)\z/i) do |relation, groups_param, filter|
- if groups_param.include?(",")
- group_names = groups_param.split(",").map(&:strip)
- found_group_ids = []
- group_names.each do |name|
- group = Group.find_by("name ILIKE ?", name)
- found_group_ids << group.id if group
- end
-
- return relation.where("1 = 0") if found_group_ids.empty?
- relation.where(
- "posts.user_id IN (
- SELECT gu.user_id FROM group_users gu
- WHERE gu.group_id IN (?)
- )",
- found_group_ids,
- )
- else
- group = Group.find_by("name ILIKE ?", groups_param)
- if group
- relation.where(
- "posts.user_id IN (
- SELECT gu.user_id FROM group_users gu
- WHERE gu.group_id = ?
- )",
- group.id,
- )
- else
- relation.where("1 = 0") # No results if group doesn't exist
- end
- end
- end
-
- register_filter(/\Amax_results:(\d+)\z/i) do |relation, limit_str, filter|
- filter.limit_by_user!(limit_str.to_i)
- relation
- end
-
- register_filter(/\Aorder:latest\z/i) do |relation, order_str, filter|
- filter.set_order!(:latest_post)
- relation
- end
-
- register_filter(/\Aorder:oldest\z/i) do |relation, order_str, filter|
- filter.set_order!(:oldest_post)
- relation
- end
-
- register_filter(/\Aorder:latest_topic\z/i) do |relation, order_str, filter|
- filter.set_order!(:latest_topic)
- relation
- end
-
- register_filter(/\Aorder:oldest_topic\z/i) do |relation, order_str, filter|
- filter.set_order!(:oldest_topic)
- relation
- end
-
- register_filter(/\Aorder:likes\z/i) do |relation, order_str, filter|
- filter.set_order!(:likes)
- relation
- end
-
- register_filter(/\Atopics?:(.*)\z/i) do |relation, topic_param, filter|
- if topic_param.include?(",")
- topic_ids = topic_param.split(",").map(&:strip).map(&:to_i).reject(&:zero?)
- return relation.where("1 = 0") if topic_ids.empty?
- relation.where("posts.topic_id IN (?)", topic_ids)
- else
- topic_id = topic_param.to_i
- if topic_id > 0
- relation.where("posts.topic_id = ?", topic_id)
- else
- relation.where("1 = 0") # No results if topic_id is invalid
- end
- end
- end
-
- register_filter(/\Apost_type:(first|reply)\z/i) do |relation, post_type, _|
- if post_type.downcase == "first"
- relation.where("posts.post_number = 1")
- elsif post_type.downcase == "reply"
- relation.where("posts.post_number > 1")
- else
- relation
- end
- end
-
- def initialize(term, guardian: nil, limit: nil, offset: nil)
- @guardian = guardian || Guardian.new
- @limit = limit
- @offset = offset
- @filters = []
- @valid = true
- @order = :latest_post
- @invalid_filters = []
- @term = term.to_s.strip
- @or_groups = []
-
- process_filters(@term)
- end
-
- def set_order!(order)
- @order = order
- end
-
- def limit_by_user!(limit)
- @limit = limit if limit.to_i < @limit.to_i || @limit.nil?
- end
-
- def search
- base_relation =
- Post
- .secured(@guardian)
- .joins(:topic)
- .merge(Topic.secured(@guardian))
- .where("topics.archetype = 'regular'")
-
- # Handle OR groups
- if @or_groups.any?
- or_relations =
- @or_groups.map do |or_group|
- group_relation = base_relation
- or_group.each do |filter_block, match_data|
- group_relation = filter_block.call(group_relation, match_data, self)
- end
- group_relation
- end
-
- # Combine OR groups
- filtered = or_relations.reduce { |combined, current| combined.or(current) }
- else
- filtered = base_relation
- end
-
- # Apply regular AND filters
- @filters.each do |filter_block, match_data|
- filtered = filter_block.call(filtered, match_data, self)
- end
-
- filtered = filtered.limit(@limit) if @limit.to_i > 0
- filtered = filtered.offset(@offset) if @offset.to_i > 0
-
- if @order == :latest_post
- filtered = filtered.order("posts.created_at DESC")
- elsif @order == :oldest_post
- filtered = filtered.order("posts.created_at ASC")
- elsif @order == :latest_topic
- filtered = filtered.order("topics.created_at DESC, posts.post_number DESC")
- elsif @order == :oldest_topic
- filtered = filtered.order("topics.created_at ASC, posts.post_number ASC")
- elsif @order == :likes
- filtered = filtered.order("posts.like_count DESC, posts.created_at DESC")
- end
-
- filtered
- end
-
- def process_filters(term)
- return if term.blank?
-
- # Split by OR first, then process each group
- or_parts = term.split(/\s+OR\s+/i)
-
- if or_parts.size > 1
- # Multiple OR groups
- or_parts.each do |or_part|
- group_filters = []
- process_filter_group(or_part.strip, group_filters)
- @or_groups << group_filters if group_filters.any?
- end
- else
- # Single group (AND logic)
- process_filter_group(term, @filters)
- end
- end
-
- private
-
- def process_filter_group(term_part, filter_collection)
- term_part
- .to_s
- .scan(/(([^" \t\n\x0B\f\r]+)?(("[^"]+")?))/)
- .to_a
- .map do |(word, _)|
- next if word.blank?
-
- found = false
- self.class.registered_filters.each do |matcher, block|
- if word =~ matcher
- filter_collection << [block, $1]
- found = true
- break
- end
- end
-
- invalid_filters << word if !found
- end
- end
- end
- end
- end
-end
diff --git a/plugins/discourse-ai/spec/lib/utils/research/filter_spec.rb b/plugins/discourse-ai/spec/lib/utils/research/filter_spec.rb
deleted file mode 100644
index 3cf6f7f06f4..00000000000
--- a/plugins/discourse-ai/spec/lib/utils/research/filter_spec.rb
+++ /dev/null
@@ -1,357 +0,0 @@
-# frozen_string_literal: true
-
-describe DiscourseAi::Utils::Research::Filter do
- before { enable_current_plugin }
-
- describe "integration tests" do
- before_all do
- SiteSetting.min_topic_title_length = 3
- SiteSetting.min_personal_message_title_length = 3
- end
-
- fab!(:user)
- fab!(:user2, :user)
-
- fab!(:feature_tag) { Fabricate(:tag, name: "feature") }
- fab!(:bug_tag) { Fabricate(:tag, name: "bug") }
-
- fab!(:announcement_category) { Fabricate(:category, name: "Announcements") }
- fab!(:feedback_category) { Fabricate(:category, name: "Feedback") }
-
- fab!(:group1) { Fabricate(:group, name: "group1") }
- fab!(:group2) { Fabricate(:group, name: "group2") }
-
- fab!(:feature_topic) do
- Fabricate(
- :topic,
- user: user,
- tags: [feature_tag],
- category: announcement_category,
- title: "New Feature Discussion",
- )
- end
-
- fab!(:bug_topic) do
- Fabricate(
- :topic,
- tags: [bug_tag],
- user: user,
- category: announcement_category,
- title: "Bug Report",
- )
- end
-
- fab!(:feature_bug_topic) do
- Fabricate(
- :topic,
- tags: [feature_tag, bug_tag],
- user: user,
- category: feedback_category,
- title: "Feature with Bug",
- )
- end
-
- fab!(:no_tag_topic) do
- Fabricate(:topic, user: user, category: feedback_category, title: "General Discussion")
- end
-
- fab!(:feature_post) { Fabricate(:post, topic: feature_topic, user: user) }
- fab!(:bug_post) { Fabricate(:post, topic: bug_topic, user: user) }
- fab!(:feature_bug_post) { Fabricate(:post, topic: feature_bug_topic, user: user) }
- fab!(:no_tag_post) { Fabricate(:post, topic: no_tag_topic, user: user) }
- fab!(:admin1) { Fabricate(:admin, username: "admin1") }
- fab!(:admin2) { Fabricate(:admin, username: "admin2") }
-
- describe "group filtering" do
- before do
- group1.add(user)
- group2.add(user2)
- end
-
- it "supports filtering by groups" do
- no_tag_post.update!(user_id: user2.id)
-
- filter = described_class.new("group:group1")
- expect(filter.search.pluck(:id)).to contain_exactly(
- feature_post.id,
- bug_post.id,
- feature_bug_post.id,
- )
-
- filter = described_class.new("groups:group1,group2")
- expect(filter.search.pluck(:id)).to contain_exactly(
- feature_post.id,
- bug_post.id,
- feature_bug_post.id,
- no_tag_post.id,
- )
- end
- end
-
- describe "security filtering" do
- fab!(:secure_group, :group)
- fab!(:secure_category) { Fabricate(:category, name: "Secure") }
-
- fab!(:secure_topic) do
- secure_category.set_permissions(secure_group => :readonly)
- secure_category.save!
- Fabricate(
- :topic,
- category: secure_category,
- user: user,
- title: "This is a secret Secret Topic",
- )
- end
-
- fab!(:secure_post) { Fabricate(:post, topic: secure_topic, user: user) }
-
- fab!(:pm_topic) { Fabricate(:private_message_topic, user: user) }
- fab!(:pm_post) { Fabricate(:post, topic: pm_topic, user: user) }
-
- it "omits secure categories when no guardian is supplied" do
- filter = described_class.new("")
- expect(filter.search.pluck(:id)).not_to include(secure_post.id)
-
- user.groups << secure_group
- guardian = Guardian.new(user)
- filter_with_guardian = described_class.new("", guardian: guardian)
- expect(filter_with_guardian.search.pluck(:id)).to include(secure_post.id)
- end
-
- it "omits PMs unconditionally" do
- filter = described_class.new("")
- expect(filter.search.pluck(:id)).not_to include(pm_post.id)
-
- guardian = Guardian.new(user)
- filter_with_guardian = described_class.new("", guardian: guardian)
- expect(filter_with_guardian.search.pluck(:id)).not_to include(pm_post.id)
- end
- end
-
- describe "tag filtering" do
- it "correctly filters posts by tags" do
- filter = described_class.new("tag:feature")
- expect(filter.search.pluck(:id)).to contain_exactly(feature_post.id, feature_bug_post.id)
-
- filter = described_class.new("tag:feature,bug")
- expect(filter.search.pluck(:id)).to contain_exactly(
- feature_bug_post.id,
- bug_post.id,
- feature_post.id,
- )
-
- filter = described_class.new("tags:bug")
- expect(filter.search.pluck(:id)).to contain_exactly(bug_post.id, feature_bug_post.id)
-
- filter = described_class.new("tag:nonexistent")
- expect(filter.search.count).to eq(0)
- end
- end
-
- describe "can find posts by users even with unicode usernames" do
- before { SiteSetting.unicode_usernames = true }
- let!(:unicode_user) { Fabricate(:user, username: "aאb") }
-
- it "can filter by unicode usernames" do
- post = Fabricate(:post, user: unicode_user, topic: feature_topic)
- filter = described_class.new("username:aאb")
- expect(filter.search.pluck(:id)).to contain_exactly(post.id)
-
- filter = described_class.new("usernames:aאb,#{user.username}")
- posts_ids = Post.where(user_id: [unicode_user.id, user.id]).pluck(:id)
- expect(filter.search.pluck(:id)).to contain_exactly(*posts_ids)
- end
- end
-
- describe "category filtering" do
- fab!(:announcement_child_category) do
- Fabricate(
- :category,
- name: "Announcements Child",
- parent_category_id: announcement_category.id,
- )
- end
- fab!(:announcement_child_topic) do
- Fabricate(
- :topic,
- user: user,
- category: announcement_child_category,
- title: "Child Category Discussion",
- )
- end
- fab!(:announcement_child_post) do
- Fabricate(:post, topic: announcement_child_topic, user: user)
- end
-
- it "correctly filters posts by categories" do
- filter = described_class.new("category:Announcements")
- expect(filter.search.pluck(:id)).to contain_exactly(
- feature_post.id,
- bug_post.id,
- announcement_child_post.id,
- )
-
- filter = described_class.new("category:=Announcements")
- expect(filter.search.pluck(:id)).to contain_exactly(feature_post.id, bug_post.id)
-
- filter = described_class.new("category:#{announcement_category.id}")
- expect(filter.search.pluck(:id)).to contain_exactly(
- feature_post.id,
- bug_post.id,
- announcement_child_post.id,
- )
-
- filter =
- described_class.new(
- "category:#{announcement_category.slug}/#{announcement_child_category.slug}",
- )
- expect(filter.search.pluck(:id)).to contain_exactly(announcement_child_post.id)
-
- filter = described_class.new('category:"Announcements Child"')
- expect(filter.search.pluck(:id)).to contain_exactly(announcement_child_post.id)
-
- filter = described_class.new('category:="Announcements Child"')
- expect(filter.search.pluck(:id)).to contain_exactly(announcement_child_post.id)
-
- # it can tack on topics
- filter =
- described_class.new(
- "category:Announcements OR topic:#{feature_bug_post.topic.id},#{no_tag_post.topic.id}",
- )
- expect(filter.search.pluck(:id)).to contain_exactly(
- feature_post.id,
- bug_post.id,
- announcement_child_post.id,
- feature_bug_post.id,
- no_tag_post.id,
- )
-
- filter = described_class.new("category:Announcements,Feedback")
- expect(filter.search.pluck(:id)).to contain_exactly(
- feature_post.id,
- bug_post.id,
- announcement_child_post.id,
- feature_bug_post.id,
- no_tag_post.id,
- )
-
- filter = described_class.new("categories:Feedback")
- expect(filter.search.pluck(:id)).to contain_exactly(feature_bug_post.id, no_tag_post.id)
-
- filter = described_class.new("category:Feedback tag:feature")
- expect(filter.search.pluck(:id)).to contain_exactly(feature_bug_post.id)
- end
- end
-
- if SiteSetting.respond_to?(:assign_enabled)
- describe "assign filtering" do
- before do
- SiteSetting.assign_enabled = true
- assigner = Assigner.new(feature_topic, admin1)
- assigner.assign(admin1)
-
- assigner = Assigner.new(bug_topic, admin1)
- assigner.assign(admin2)
- end
-
- let(:admin_guardian) { Guardian.new(admin1) }
-
- it "can find topics assigned to a user" do
- filter = described_class.new("assigned_to:#{admin1.username}", guardian: admin_guardian)
- expect(filter.search.pluck(:id)).to contain_exactly(feature_post.id)
- end
-
- it "can find topics assigned to multiple users" do
- filter =
- described_class.new(
- "assigned_to:#{admin1.username},#{admin2.username}",
- guardian: admin_guardian,
- )
- expect(filter.search.pluck(:id)).to contain_exactly(feature_post.id, bug_post.id)
- end
-
- it "can find topics assigned to nobody" do
- filter = described_class.new("assigned_to:nobody", guardian: admin_guardian)
- expect(filter.search.pluck(:id)).to contain_exactly(feature_bug_post.id, no_tag_post.id)
- end
-
- it "can find all assigned topics" do
- filter = described_class.new("assigned_to:*", guardian: admin_guardian)
- expect(filter.search.pluck(:id)).to contain_exactly(feature_post.id, bug_post.id)
- end
-
- it "raises an error if assigns are disabled" do
- SiteSetting.assign_enabled = false
- filter = described_class.new("assigned_to:sam")
- expect { filter.search }.to raise_error(Discourse::InvalidAccess)
- end
- end
- end
-
- it "can limit number of results" do
- filter = described_class.new("category:Feedback max_results:1", limit: 5)
- expect(filter.search.pluck(:id).length).to eq(1)
- end
-
- describe "full text keyword searching" do
- before_all { SearchIndexer.enable }
- fab!(:post_with_apples) do
- Fabricate(:post, raw: "This post contains apples", topic: feature_topic, user: user)
- end
-
- fab!(:post_with_bananas) do
- Fabricate(:post, raw: "This post mentions bananas", topic: bug_topic, user: user)
- end
-
- fab!(:post_with_both) do
- Fabricate(
- :post,
- raw: "This post has apples and bananas",
- topic: feature_bug_topic,
- user: user,
- )
- end
-
- fab!(:post_with_none) do
- Fabricate(:post, raw: "No fruits here", topic: no_tag_topic, user: user)
- end
-
- fab!(:reply_on_bananas) do
- Fabricate(:post, raw: "Just a reply", topic: post_with_bananas.topic, user: user)
- end
-
- it "correctly filters posts by topic_keywords" do
- topic1 = post_with_bananas.topic
- topic2 = post_with_both.topic
-
- filter = described_class.new("topic_keywords:banana")
- expected = topic1.posts.pluck(:id) + topic2.posts.pluck(:id)
- expect(filter.search.pluck(:id)).to contain_exactly(*expected)
-
- filter = described_class.new("topic_keywords:banana post_type:first")
- expect(filter.search.pluck(:id)).to contain_exactly(
- topic1.posts.order(:post_number).first.id,
- topic2.posts.order(:post_number).first.id,
- )
- end
-
- it "correctly filters posts by full text keywords" do
- filter = described_class.new("keywords:apples")
- expect(filter.search.pluck(:id)).to contain_exactly(post_with_apples.id, post_with_both.id)
-
- filter = described_class.new("keywords:bananas")
- expect(filter.search.pluck(:id)).to contain_exactly(post_with_bananas.id, post_with_both.id)
-
- filter = described_class.new("keywords:apples,bananas")
- expect(filter.search.pluck(:id)).to contain_exactly(
- post_with_apples.id,
- post_with_bananas.id,
- post_with_both.id,
- )
-
- filter = described_class.new("keywords:oranges")
- expect(filter.search.count).to eq(0)
- end
- end
- end
-end
diff --git a/plugins/discourse-ai/spec/lib/utils/research/llm_formatter_spec.rb b/plugins/discourse-ai/spec/lib/utils/research/llm_formatter_spec.rb
index 3021ccc71de..7d41c22cbc4 100644
--- a/plugins/discourse-ai/spec/lib/utils/research/llm_formatter_spec.rb
+++ b/plugins/discourse-ai/spec/lib/utils/research/llm_formatter_spec.rb
@@ -5,7 +5,7 @@ describe DiscourseAi::Utils::Research::LlmFormatter do
fab!(:topic) { Fabricate(:topic, title: "This is a Test Topic", user: user) }
fab!(:post) { Fabricate(:post, topic: topic, user: user) }
let(:tokenizer) { DiscourseAi::Tokenizer::OpenAiTokenizer }
- let(:filter) { DiscourseAi::Utils::Research::Filter.new("@#{user.username}") }
+ let(:filter) { PostsFilter.new("username:#{user.username}") }
before { enable_current_plugin }
diff --git a/plugins/discourse-assign/plugin.rb b/plugins/discourse-assign/plugin.rb
index c070e1cfe14..410a226e17a 100644
--- a/plugins/discourse-assign/plugin.rb
+++ b/plugins/discourse-assign/plugin.rb
@@ -955,6 +955,47 @@ after_initialize do
results
end
+ register_modifier(:posts_filter_options) do |results, guardian|
+ if SiteSetting.assign_enabled && (guardian.can_assign? || SiteSetting.assigns_public)
+ results << {
+ name: "assigned_to:",
+ description: I18n.t("discourse_assign.filter.description.assigned"),
+ type: "username",
+ extra_entries: [
+ { name: "nobody", description: I18n.t("discourse_assign.filter.description.nobody") },
+ { name: "*", description: I18n.t("discourse_assign.filter.description.anyone") },
+ ],
+ priority: 1,
+ }
+ end
+ results
+ end
+
+ PostsFilter.add_filter("assigned_to") do |scope, filter_values, guardian|
+ if !SiteSetting.assign_enabled || !(guardian.can_assign? || SiteSetting.assigns_public)
+ raise Discourse::InvalidAccess.new(
+ "Assigns are not enabled or you do not have permission to see assigns.",
+ )
+ end
+
+ names =
+ filter_values.compact.flat_map { |value| value.to_s.split(",") }.map(&:strip).reject(&:blank?)
+ next scope if names.blank?
+
+ next scope.where.not(topic_id: Assignment.active.select(:topic_id)) if names.include?("nobody")
+
+ next scope.where(topic_id: Assignment.active.select(:topic_id)) if names.include?("*")
+
+ user_ids = User.where(username_lower: names.map(&:downcase)).select(:id)
+ scope.where(
+ topic_id:
+ Assignment
+ .active
+ .where(assigned_to_type: "User", assigned_to_id: user_ids)
+ .select(:topic_id),
+ )
+ end
+
register_search_advanced_filter(/in:assigned/) do |posts|
next if [email protected]_assign?
diff --git a/plugins/discourse-assign/spec/plugin_spec.rb b/plugins/discourse-assign/spec/plugin_spec.rb
index a913971e92b..a27364b1833 100644
--- a/plugins/discourse-assign/spec/plugin_spec.rb
+++ b/plugins/discourse-assign/spec/plugin_spec.rb
@@ -116,6 +116,80 @@ RSpec.describe DiscourseAssign do
end
end
+ describe "discourse-assign posts_filter_options modifier" do
+ let(:user) { Fabricate(:user) }
+
+ before do
+ SiteSetting.assign_allowed_on_groups = Group::AUTO_GROUPS[:staff]
+ user.update!(admin: true)
+ end
+
+ it "adds assigned_to filter option for users who can assign" do
+ options = PostsFilter.option_info(user.guardian)
+
+ assigned_option = options.find { |option| option[:name] == "assigned_to:" }
+ expect(assigned_option).to include(
+ name: "assigned_to:",
+ description: I18n.t("discourse_assign.filter.description.assigned"),
+ type: "username",
+ priority: 1,
+ )
+ end
+
+ it "does not add assigned_to filter option for users who cannot assign" do
+ options = PostsFilter.option_info(Fabricate(:user).guardian)
+
+ assigned_option = options.find { |option| option[:name] == "assigned_to:" }
+ expect(assigned_option).to be_nil
+ end
+ end
+
+ describe "discourse-assign PostsFilter filtering" do
+ fab!(:group)
+ fab!(:user)
+ fab!(:assigned_post, :post)
+ fab!(:other_assigned_post, :post)
+ fab!(:unassigned_post, :post)
+ fab!(:post_assignment) { Fabricate(:post_assignment, post: assigned_post, assigned_to: user) }
+ fab!(:other_assignment) do
+ Fabricate(:post_assignment, post: other_assigned_post, assigned_to: Fabricate(:user))
+ end
+
+ before do
+ SiteSetting.assign_allowed_on_groups = "#{group.id}"
+ group.add(user)
+ end
+
+ it "filters posts by assigned user" do
+ filtered_post_ids =
+ PostsFilter
+ .new("assigned_to:#{user.username}", guardian: Guardian.new(user))
+ .search
+ .pluck(:id)
+
+ expect(filtered_post_ids).to contain_exactly(assigned_post.id)
+ end
+
+ it "filters posts by assigned and unassigned topics" do
+ assigned_post_ids =
+ PostsFilter.new("assigned_to:*", guardian: Guardian.new(user)).search.pluck(:id)
+ unassigned_post_ids =
+ PostsFilter.new("assigned_to:nobody", guardian: Guardian.new(user)).search.pluck(:id)
+
+ expect(assigned_post_ids).to contain_exactly(assigned_post.id, other_assigned_post.id)
+ expect(unassigned_post_ids).to include(unassigned_post.id)
+ end
+
+ it "raises when the user cannot see assignments" do
+ expect do
+ PostsFilter
+ .new("assigned_to:#{user.username}", guardian: Guardian.new(Fabricate(:user)))
+ .search
+ .load
+ end.to raise_error(Discourse::InvalidAccess)
+ end
+ end
+
describe "Events" do
describe "on 'user_removed_from_group'" do
let(:group) { Fabricate(:group) }
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/filter-query.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/filter-query.gjs
index 1da99e39ab9..0a45aa2d4e0 100644
--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/filter-query.gjs
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/filter-query.gjs
@@ -5,6 +5,11 @@ import FilterNavigationMenu from "discourse/components/discovery/filter-navigati
import { ajax } from "discourse/lib/ajax";
import ExpressionWrapper from "./expression-wrapper";
+const FILTER_OPTIONS_URLS = {
+ posts: "/admin/plugins/discourse-workflows/filter-options/posts.json",
+ topics: "/filter.json",
+};
+
export default class FilterQuery extends Component {
@tracked tips = null;
@@ -13,10 +18,21 @@ export default class FilterQuery extends Component {
this.loadTips();
}
+ get filterType() {
+ return this.args.schema?.ui?.filter || "topics";
+ }
+
+ get optionsUrl() {
+ return FILTER_OPTIONS_URLS[this.filterType] || FILTER_OPTIONS_URLS.topics;
+ }
+
async loadTips() {
try {
- const result = await ajax("/filter.json");
- this.tips = result.topic_list?.filter_option_info || [];
+ const result = await ajax(this.optionsUrl);
+ this.tips =
+ result.topic_list?.filter_option_info ||
+ result.filter_option_info ||
+ [];
} catch {
this.tips = [];
}
@@ -39,6 +55,7 @@ export default class FilterQuery extends Component {
<FilterNavigationMenu
@initialInputValue={{@field.value}}
@onChange={{this.handleChange}}
+ @placeholder={{@placeholder}}
@tips={{this.tips}}
/>
{{/if}}
diff --git a/plugins/discourse-workflows/app/controllers/discourse_workflows/filter_options_controller.rb b/plugins/discourse-workflows/app/controllers/discourse_workflows/filter_options_controller.rb
new file mode 100644
index 00000000000..29ec7d144e7
--- /dev/null
+++ b/plugins/discourse-workflows/app/controllers/discourse_workflows/filter_options_controller.rb
@@ -0,0 +1,11 @@
+# frozen_string_literal: true
+
+module DiscourseWorkflows
+ class FilterOptionsController < ::Admin::AdminController
+ requires_plugin DiscourseWorkflows::PLUGIN_NAME
+
+ def posts
+ render json: { filter_option_info: PostsFilter.option_info(guardian) }
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/app/serializers/discourse_workflows/post_serializer.rb b/plugins/discourse-workflows/app/serializers/discourse_workflows/post_serializer.rb
new file mode 100644
index 00000000000..fb5e738fea8
--- /dev/null
+++ b/plugins/discourse-workflows/app/serializers/discourse_workflows/post_serializer.rb
@@ -0,0 +1,88 @@
+# frozen_string_literal: true
+
+module DiscourseWorkflows
+ class PostSerializer < ApplicationSerializer
+ attributes :id,
+ :topic_id,
+ :topic_title,
+ :topic_slug,
+ :post_number,
+ :reply_to_post_number,
+ :post_url,
+ :username,
+ :user_id,
+ :created_at,
+ :updated_at,
+ :excerpt,
+ :like_count,
+ :reply_count,
+ :score,
+ :category_id,
+ :category_name,
+ :tags,
+ :raw,
+ :cooked
+
+ def initialize(object, opts)
+ super
+ @include_raw = opts.fetch(:include_raw, true)
+ @include_cooked = opts.fetch(:include_cooked, false)
+ end
+
+ def topic_title
+ topic&.title
+ end
+
+ def topic_slug
+ topic&.slug
+ end
+
+ def post_url
+ object.url
+ end
+
+ def username
+ object.user&.username
+ end
+
+ def created_at
+ object.created_at&.utc&.iso8601
+ end
+
+ def updated_at
+ object.updated_at&.utc&.iso8601
+ end
+
+ def excerpt
+ object.excerpt(300, strip_links: true, text_entities: true)
+ end
+
+ def category_id
+ topic&.category_id
+ end
+
+ def category_name
+ topic&.category&.name
+ end
+
+ def tags
+ return [] if topic.blank? || !SiteSetting.tagging_enabled
+
+ topic.tags.visible(scope).pluck(:name)
+ end
+
+ def include_raw?
+ @include_raw
+ end
+
+ def include_cooked?
+ @include_cooked
+ end
+
+ private
+
+ def topic
+ @topic ||= object.topic
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.yml
index 26eca1cfb43..7e44e47a937 100644
--- a/plugins/discourse-workflows/config/locales/client.en.yml
+++ b/plugins/discourse-workflows/config/locales/client.en.yml
@@ -141,6 +141,7 @@ en:
"action:topic_tags": "Topic tags"
"action:code": "Code"
"action:topic": "Topic"
+ "action:post": "Post"
"action:create_post": "Create post"
"action:set_fields": "Set fields"
"trigger:stale_topic": "Stale topic"
@@ -175,6 +176,7 @@ en:
"action:topic_tags": "Add or remove tags on a topic"
"action:code": "Run custom JavaScript code"
"action:topic": "Create, retrieve, or list topics"
+ "action:post": "Create, retrieve, or list posts"
"action:create_post": "Create a reply in an existing topic"
"action:set_fields": "Set or modify data fields in the workflow context"
"trigger:stale_topic": "Fires when a topic has had no activity for a period"
@@ -265,6 +267,70 @@ en:
reply_to_post_number: "Reply to post number"
reply_to_post_number_description: "Reply to a specific post number in the topic"
author_username: "Author"
+ post:
+ operation: "Operation"
+ operations:
+ create: "Create post"
+ get: "Get post"
+ list: "List posts"
+ topic_id: "Topic ID"
+ raw: "Body"
+ raw_description: "Raw content for the post"
+ reply_to_post_number: "Reply to post number"
+ reply_to_post_number_description: "Reply to a specific post number in the topic"
+ author_username: "Author"
+ post_id: "Post ID"
+ include_raw: "Include raw"
+ include_cooked: "Include cooked"
+ query: "Query"
+ query_description: "Filter query using PostsFilter syntax (e.g. category:support username:system order:latest)"
+ query_placeholder: "Filter posts by category, tag, author, keywords, or other criteria"
+ created_after: "Created after"
+ created_before: "Created before"
+ topic_created_after: "Topic created after"
+ topic_created_before: "Topic created before"
+ categories: "Categories"
+ categories_description: "Category IDs, slugs, or names, separated by commas"
+ exclude_categories: "Exclude categories"
+ exact_category_match: "Exact category match"
+ exact_category_match_description: "Exclude subcategories when matching categories"
+ tags: "Tags"
+ exclude_tags: "Exclude tags"
+ topics: "Topics"
+ topics_description: "Topic IDs separated by commas"
+ usernames: "Users"
+ usernames_description: "Usernames separated by commas"
+ groups: "Groups"
+ groups_description: "Group names separated by commas"
+ post_type: "Post type"
+ post_types:
+ all: "All posts"
+ first: "First posts only"
+ reply: "Replies only"
+ status: "Status"
+ statuses:
+ any: "Any status"
+ open: "Open"
+ closed: "Closed"
+ archived: "Archived"
+ noreplies: "No replies"
+ single_user: "Single user"
+ keywords: "Keywords"
+ keywords_description: "Keywords separated by commas"
+ topic_keywords: "Topic keywords"
+ order: "Order"
+ orders:
+ latest: "Latest posts"
+ oldest: "Oldest posts"
+ latest_topic: "Latest topics"
+ oldest_topic: "Oldest topics"
+ likes: "Most liked"
+ limit: "Limit"
+ limit_description: "Maximum number of posts to return (1-500, default 30)"
+ offset: "Offset"
+ advanced_filter: "Advanced filter"
+ advanced_filter_description: "Additional PostsFilter query string appended to the configured filters"
+ actor_username: "Performed by user"
topic:
operation: "Operation"
operations:
@@ -282,6 +348,8 @@ en:
query_placeholder: "category:support status:open"
limit: "Limit"
limit_description: "Maximum number of topics to return (1-100, default 30)"
+ offset: "Offset"
+ offset_description: "Number of matching topics to skip before returning results"
actor_username: "Performed by user"
set_fields:
mode: "Mode"
diff --git a/plugins/discourse-workflows/config/locales/server.en.yml b/plugins/discourse-workflows/config/locales/server.en.yml
index 368ebac2513..6b6eb45b0d5 100644
--- a/plugins/discourse-workflows/config/locales/server.en.yml
+++ b/plugins/discourse-workflows/config/locales/server.en.yml
@@ -42,6 +42,9 @@ en:
topic:
create_failed: "Failed to create topic."
unknown_operation: "Unknown operation: %{operation}."
+ post:
+ unknown_operation: "Unknown operation: %{operation}."
+ invalid_filter: "Invalid post filter fragment: %{fragments}."
credential:
missing_required_field: "is missing required field '%{field}'"
wait:
diff --git a/plugins/discourse-workflows/config/routes.rb b/plugins/discourse-workflows/config/routes.rb
index a8dac8a6713..b3790c582cb 100644
--- a/plugins/discourse-workflows/config/routes.rb
+++ b/plugins/discourse-workflows/config/routes.rb
@@ -36,6 +36,7 @@ DiscourseWorkflows::Engine.routes.draw do
"webhook_test_listeners#destroy"
delete "/workflows/:id" => "workflows#destroy"
get "/node-types" => "node_types#index"
+ get "/filter-options/posts" => "filter_options#posts"
post "/dynamic-node-parameters/options" => "dynamic_node_parameters#options"
get "/templates" => "templates#index"
get "/templates/:id" => "templates#show"
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/executor.rb b/plugins/discourse-workflows/lib/discourse_workflows/executor.rb
index a3f5e82e4cd..7b3173ade91 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/executor.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/executor.rb
@@ -4,6 +4,7 @@ module DiscourseWorkflows
class Executor
MAX_ITERATIONS = 1000
MAX_WAIT_DURATION_SECONDS = 30.days.to_i
+ MAX_NODE_OUTPUT_BYTES = 50.megabytes
class WaitRequested < StandardError
attr_reader :waiting_until
@@ -266,8 +267,8 @@ module DiscourseWorkflows
end
step_log = collect_step_log(exec_ctx, resolver)
- attach_step_log(step, step_log)
if step_log&.errors?
+ attach_step_log(step, step_log)
step.fail!(step_log.error_summary)
raise StandardError, step.error
end
@@ -275,6 +276,8 @@ module DiscourseWorkflows
ports = node_type_class.ports(node.parameters)
output_arrays = normalize_result(result, node, ports, input_groups)
output_arrays = apply_always_output_data(output_arrays, node, input_groups)
+ output_arrays = enforce_node_output_budget(output_arrays, step_log)
+ attach_step_log(step, step_log)
all_items = output_arrays.flatten(1)
primary_empty = output_arrays.fetch(0) { [] }.empty?
@@ -295,11 +298,10 @@ module DiscourseWorkflows
rescue WaitRequested
raise
rescue => e
- unless step.metadata&.key?("logs")
- attach_step_log(step, collect_step_log(exec_ctx, resolver))
- end
-
if (handled_outputs = continued_error_outputs(node, input_groups, e))
+ step_log = collect_step_log(exec_ctx, resolver)
+ handled_outputs = enforce_node_output_budget(handled_outputs, step_log)
+ attach_step_log(step, step_log)
step.add_metadata("handled_error", error_metadata(e))
all_items = handled_outputs.flatten(1)
step.succeed!(output: all_items)
@@ -315,6 +317,10 @@ module DiscourseWorkflows
return
end
+ unless step.metadata&.key?("logs")
+ attach_step_log(step, collect_step_log(exec_ctx, resolver))
+ end
+
step.fail!(e.message) unless step.error?
raise
ensure
@@ -471,6 +477,39 @@ module DiscourseWorkflows
result
end
+ def enforce_node_output_budget(output_arrays, step_log)
+ total_bytes = 0
+ truncated = false
+
+ bounded_arrays =
+ output_arrays.map do |items|
+ bounded_items = []
+
+ items.each do |item|
+ item_bytes = JSON.generate(item).bytesize
+
+ if total_bytes + item_bytes > MAX_NODE_OUTPUT_BYTES
+ truncated = true
+ break
+ end
+
+ total_bytes += item_bytes
+ bounded_items << item
+ end
+
+ bounded_items
+ end
+
+ if truncated
+ step_log&.warn(
+ "Node output truncated at #{bounded_arrays.flatten(1).length} items because serialized " \
+ "output exceeded #{MAX_NODE_OUTPUT_BYTES} bytes",
+ )
+ end
+
+ bounded_arrays
+ end
+
def apply_item_linking_defaults!(output_arrays, input_groups:)
input_lookup = input_item_lookup(input_groups)
sole_input_pair = sole_input_pair(input_groups)
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb b/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb
index 661eed54a65..50e2cb875ae 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb
@@ -11,6 +11,23 @@ module DiscourseWorkflows
JobResult = Data.define(:ok, :result, :error)
WaitRequest = Data.define(:waiting_until)
+ def self.serialize_post(
+ post,
+ guardian: Discourse.system_user.guardian,
+ include_raw: true,
+ include_cooked: false
+ )
+ MultiJson.load(
+ DiscourseWorkflows::PostSerializer.new(
+ post,
+ scope: guardian,
+ root: false,
+ include_raw: include_raw,
+ include_cooked: include_cooked,
+ ).to_json,
+ ).deep_symbolize_keys
+ end
+
class RuntimeState
attr_reader :condition_step_details, :execution_hints, :log, :metadata, :wait_request
@@ -257,6 +274,34 @@ module DiscourseWorkflows
HttpClient.new(self, item_index).request(method:, url:, headers:, body:, options:)
end
+ def create_post(user:, raw:, topic_id:, reply_to_post_number: nil)
+ topic = ::Topic.find(topic_id)
+ user.guardian.ensure_can_see!(topic)
+
+ if topic.closed? || topic.archived?
+ raise DiscourseWorkflows::NodeError,
+ I18n.t("discourse_workflows.errors.create_post.topic_closed_or_archived")
+ end
+
+ post_args = {
+ topic_id: topic.id,
+ raw: raw,
+ reply_to_post_number: reply_to_post_number.presence,
+ skip_workflows: true,
+ }.compact
+
+ PostCreator.new(user, post_args).create!
+ end
+
+ def serialize_post(
+ post,
+ guardian: Discourse.system_user.guardian,
+ include_raw: true,
+ include_cooked: false
+ )
+ self.class.serialize_post(post, guardian:, include_raw:, include_cooked:)
+ end
+
def put_execution_to_wait(waiting_until = nil)
@runtime_state.request_wait(waiting_until)
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/node_type.rb b/plugins/discourse-workflows/lib/discourse_workflows/node_type.rb
index 5075b4b8b5d..adca0364d24 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/node_type.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/node_type.rb
@@ -184,8 +184,28 @@ module DiscourseWorkflows
Item.wrap(data, paired_item:)
end
- def serialize_record(record, serializer, scope: Discourse.system_user.guardian, root: false)
- MultiJson.load(serializer.new(record, scope:, root:).to_json).deep_symbolize_keys
+ def serialize_record(
+ record,
+ serializer,
+ scope: Discourse.system_user.guardian,
+ root: false,
+ **opts
+ )
+ MultiJson.load(serializer.new(record, scope:, root:, **opts).to_json).deep_symbolize_keys
+ end
+
+ def serialize_post(
+ post,
+ guardian: Discourse.system_user.guardian,
+ include_raw: true,
+ include_cooked: false
+ )
+ DiscourseWorkflows::Executor::NodeExecutionContext.serialize_post(
+ post,
+ guardian: guardian,
+ include_raw: include_raw,
+ include_cooked: include_cooked,
+ )
end
def with_paired_item(item, paired_item)
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/create_post/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/create_post/v1.rb
index 97b902bba07..3e7dc81347a 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/create_post/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/create_post/v1.rb
@@ -61,29 +61,16 @@ module DiscourseWorkflows
private
def process(exec_ctx, config, item_index)
- topic = ::Topic.find(config["topic_id"])
author = exec_ctx.actor_from_parameter("author_username", item_index)
- author.guardian.ensure_can_see!(topic)
-
- if topic.closed? || topic.archived?
- raise_node_error!(
- I18n.t("discourse_workflows.errors.create_post.topic_closed_or_archived"),
+ post =
+ exec_ctx.create_post(
+ user: author,
+ raw: config["raw"],
+ topic_id: config["topic_id"],
+ reply_to_post_number: config["reply_to_post_number"],
)
- end
-
- post_args = {
- topic_id: topic.id,
- raw: config["raw"],
- reply_to_post_number: config["reply_to_post_number"].presence,
- skip_workflows: true,
- }.compact
- post = PostCreator.new(author, post_args).create!
-
- { post: post_data(post) }
- end
- def post_data(post)
- serialize_record(post, WebHookPostSerializer)
+ { post: exec_ctx.serialize_post(post, guardian: author.guardian, include_cooked: true) }
end
end
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rb
new file mode 100644
index 00000000000..6165d6afbaa
--- /dev/null
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rb
@@ -0,0 +1,486 @@
+# frozen_string_literal: true
+
+module DiscourseWorkflows
+ module Nodes
+ module Post
+ class V1 < NodeType
+ OPERATIONS = %w[create get list].freeze
+ STATUS_OPTIONS = %w[any open closed archived noreplies single_user].freeze
+ POST_TYPE_OPTIONS = %w[all first reply].freeze
+ ORDER_OPTIONS = %w[latest oldest latest_topic oldest_topic likes].freeze
+ DEFAULT_LIMIT = 30
+ MAX_LIMIT = 500
+
+ def self.list_string_property(control: nil, hidden: false)
+ property = {
+ type: :string,
+ required: false,
+ display_options: {
+ show: {
+ operation: ["list"],
+ },
+ },
+ }
+
+ ui = {}
+ ui[:control] = control if control
+ ui[:hidden] = true if hidden
+ property[:ui] = ui if ui.present?
+
+ property
+ end
+
+ description(
+ name: "action:post",
+ version: "1.0",
+ defaults: {
+ icon: "reply",
+ color: "teal",
+ },
+ group: "discourse_actions",
+ capabilities: {
+ run_scope: "per_item",
+ },
+ properties: {
+ operation: {
+ type: :options,
+ required: true,
+ options: OPERATIONS,
+ default: "list",
+ },
+ topic_id: {
+ type: :string,
+ required: true,
+ display_options: {
+ show: {
+ operation: ["create"],
+ },
+ },
+ },
+ raw: {
+ type: :string,
+ required: true,
+ ui: {
+ control: :textarea,
+ },
+ display_options: {
+ show: {
+ operation: ["create"],
+ },
+ },
+ },
+ reply_to_post_number: {
+ type: :integer,
+ required: false,
+ display_options: {
+ show: {
+ operation: ["create"],
+ },
+ },
+ },
+ author_username: {
+ type: :string,
+ required: false,
+ default: "system",
+ ui: {
+ control: :user,
+ },
+ display_options: {
+ show: {
+ operation: ["create"],
+ },
+ },
+ },
+ post_id: {
+ type: :string,
+ required: true,
+ display_options: {
+ show: {
+ operation: ["get"],
+ },
+ },
+ },
+ query: {
+ type: :string,
+ required: false,
+ ui: {
+ control: :filter_query,
+ filter: :posts,
+ },
+ display_options: {
+ show: {
+ operation: ["list"],
+ },
+ },
+ },
+ include_raw: {
+ type: :boolean,
+ required: false,
+ default: true,
+ ui: {
+ control: :boolean,
+ },
+ display_options: {
+ show: {
+ operation: %w[get list],
+ },
+ },
+ },
+ include_cooked: {
+ type: :boolean,
+ required: false,
+ default: false,
+ ui: {
+ control: :boolean,
+ },
+ display_options: {
+ show: {
+ operation: %w[get list],
+ },
+ },
+ },
+ created_after: list_string_property(hidden: true),
+ created_before: list_string_property(hidden: true),
+ topic_created_after: list_string_property(hidden: true),
+ topic_created_before: list_string_property(hidden: true),
+ categories: list_string_property(control: :category, hidden: true),
+ exclude_categories: list_string_property(hidden: true),
+ exact_category_match: {
+ type: :boolean,
+ required: false,
+ default: false,
+ ui: {
+ control: :boolean,
+ hidden: true,
+ },
+ display_options: {
+ show: {
+ operation: ["list"],
+ },
+ },
+ },
+ tags: list_string_property(control: :tags, hidden: true),
+ exclude_tags: list_string_property(control: :tags, hidden: true),
+ topics: list_string_property(hidden: true),
+ usernames: list_string_property(hidden: true),
+ groups: list_string_property(hidden: true),
+ post_type: {
+ type: :options,
+ required: false,
+ options: POST_TYPE_OPTIONS,
+ default: "all",
+ ui: {
+ hidden: true,
+ },
+ display_options: {
+ show: {
+ operation: ["list"],
+ },
+ },
+ },
+ status: {
+ type: :options,
+ required: false,
+ options: STATUS_OPTIONS,
+ default: "any",
+ ui: {
+ hidden: true,
+ },
+ display_options: {
+ show: {
+ operation: ["list"],
+ },
+ },
+ },
+ keywords: list_string_property(hidden: true),
+ topic_keywords: list_string_property(hidden: true),
+ order: {
+ type: :options,
+ required: false,
+ options: ORDER_OPTIONS,
+ default: "latest",
+ ui: {
+ hidden: true,
+ },
+ display_options: {
+ show: {
+ operation: ["list"],
+ },
+ },
+ },
+ limit: {
+ type: :integer,
+ required: false,
+ default: DEFAULT_LIMIT,
+ display_options: {
+ show: {
+ operation: ["list"],
+ },
+ },
+ },
+ offset: {
+ type: :integer,
+ required: false,
+ default: 0,
+ display_options: {
+ show: {
+ operation: ["list"],
+ },
+ },
+ },
+ advanced_filter: {
+ type: :string,
+ required: false,
+ ui: {
+ control: :textarea,
+ hidden: true,
+ },
+ display_options: {
+ show: {
+ operation: ["list"],
+ },
+ },
+ },
+ actor_username: {
+ type: :string,
+ required: false,
+ default: "system",
+ ui: {
+ control: :user,
+ },
+ display_options: {
+ show: {
+ operation: %w[get list],
+ },
+ },
+ },
+ },
+ )
+
+ def execute(exec_ctx)
+ items =
+ exec_ctx.input_items.flat_map.with_index do |_item, item_index|
+ config = config_for(exec_ctx, item_index)
+ Array.wrap(execute_with_config(exec_ctx, config, item_index))
+ end
+
+ [items]
+ end
+
+ private
+
+ def config_for(exec_ctx, item_index)
+ {
+ "operation" => exec_ctx.get_node_parameter("operation", item_index, default: "list"),
+ "topic_id" => exec_ctx.get_node_parameter("topic_id", item_index),
+ "raw" => exec_ctx.get_node_parameter("raw", item_index),
+ "reply_to_post_number" =>
+ exec_ctx.get_node_parameter("reply_to_post_number", item_index),
+ "post_id" => exec_ctx.get_node_parameter("post_id", item_index),
+ "include_raw" => exec_ctx.get_node_parameter("include_raw", item_index, default: true),
+ "include_cooked" =>
+ exec_ctx.get_node_parameter("include_cooked", item_index, default: false),
+ "query" => exec_ctx.get_node_parameter("query", item_index),
+ "created_after" => exec_ctx.get_node_parameter("created_after", item_index),
+ "created_before" => exec_ctx.get_node_parameter("created_before", item_index),
+ "topic_created_after" => exec_ctx.get_node_parameter("topic_created_after", item_index),
+ "topic_created_before" =>
+ exec_ctx.get_node_parameter("topic_created_before", item_index),
+ "categories" => exec_ctx.get_node_parameter("categories", item_index),
+ "exclude_categories" => exec_ctx.get_node_parameter("exclude_categories", item_index),
+ "exact_category_match" =>
+ exec_ctx.get_node_parameter("exact_category_match", item_index),
+ "tags" => exec_ctx.get_node_parameter("tags", item_index),
+ "exclude_tags" => exec_ctx.get_node_parameter("exclude_tags", item_index),
+ "topics" => exec_ctx.get_node_parameter("topics", item_index),
+ "usernames" => exec_ctx.get_node_parameter("usernames", item_index),
+ "groups" => exec_ctx.get_node_parameter("groups", item_index),
+ "post_type" => exec_ctx.get_node_parameter("post_type", item_index, default: "all"),
+ "status" => exec_ctx.get_node_parameter("status", item_index, default: "any"),
+ "keywords" => exec_ctx.get_node_parameter("keywords", item_index),
+ "topic_keywords" => exec_ctx.get_node_parameter("topic_keywords", item_index),
+ "order" => exec_ctx.get_node_parameter("order", item_index, default: "latest"),
+ "limit" => exec_ctx.get_node_parameter("limit", item_index, default: DEFAULT_LIMIT),
+ "offset" => exec_ctx.get_node_parameter("offset", item_index, default: 0),
+ "advanced_filter" => exec_ctx.get_node_parameter("advanced_filter", item_index),
+ }
+ end
+
+ def execute_with_config(exec_ctx, config, item_index)
+ case config["operation"]
+ when "create"
+ wrap(create_post(exec_ctx, config, item_index))
+ when "get"
+ wrap(get_post(exec_ctx, config, item_index))
+ when "list"
+ list_posts(exec_ctx, config, item_index).map { |data| wrap(data) }
+ else
+ raise_node_error!(
+ I18n.t(
+ "discourse_workflows.errors.post.unknown_operation",
+ operation: config["operation"],
+ ),
+ )
+ end
+ end
+
+ def create_post(exec_ctx, config, item_index)
+ actor = exec_ctx.actor_from_parameter("author_username", item_index)
+ post =
+ exec_ctx.create_post(
+ user: actor,
+ raw: config["raw"],
+ topic_id: config["topic_id"],
+ reply_to_post_number: config["reply_to_post_number"],
+ )
+
+ {
+ post:
+ exec_ctx.serialize_post(
+ post,
+ guardian: actor.guardian,
+ include_raw: true,
+ include_cooked: true,
+ ),
+ }
+ end
+
+ def get_post(exec_ctx, config, item_index)
+ post = ::Post.find(config["post_id"])
+ actor = exec_ctx.actor_from_parameter("actor_username", item_index)
+ raise Discourse::InvalidAccess if !actor.guardian.can_see?(post)
+
+ {
+ post:
+ exec_ctx.serialize_post(
+ post,
+ guardian: actor.guardian,
+ include_raw: truthy?(config["include_raw"]),
+ include_cooked: truthy?(config["include_cooked"]),
+ ),
+ }
+ end
+
+ def list_posts(exec_ctx, config, item_index)
+ actor = exec_ctx.actor_from_parameter("actor_username", item_index)
+ limit = bounded_integer(config["limit"], default: DEFAULT_LIMIT, min: 1, max: MAX_LIMIT)
+ offset = bounded_integer(config["offset"], default: 0, min: 0)
+ query = query_from_config(config)
+ filter = PostsFilter.new(query, guardian: actor.guardian, limit: limit, offset: offset)
+
+ if filter.invalid_filters.present?
+ raise_node_error!(
+ I18n.t(
+ "discourse_workflows.errors.post.invalid_filter",
+ fragments: filter.invalid_filters.join(" "),
+ ),
+ )
+ end
+
+ posts = filter.search.includes(:user, topic: %i[category tags])
+ posts.map do |post|
+ {
+ post:
+ exec_ctx.serialize_post(
+ post,
+ guardian: actor.guardian,
+ include_raw: truthy?(config["include_raw"]),
+ include_cooked: truthy?(config["include_cooked"]),
+ ),
+ }
+ end
+ end
+
+ def query_from_config(config)
+ query = config["query"].to_s.strip
+ return query if query.present?
+
+ parts = []
+ add_query_part(parts, "after", config["created_after"])
+ add_query_part(parts, "before", config["created_before"])
+ add_query_part(parts, "topic_after", config["topic_created_after"])
+ add_query_part(parts, "topic_before", config["topic_created_before"])
+ add_category_part(
+ parts,
+ "category",
+ config["categories"],
+ exact: truthy?(config["exact_category_match"]),
+ )
+ add_category_part(
+ parts,
+ "exclude_category",
+ config["exclude_categories"],
+ exact: truthy?(config["exact_category_match"]),
+ )
+ add_query_part(parts, "tag", config["tags"])
+ add_query_part(parts, "exclude_tag", config["exclude_tags"])
+ add_query_part(parts, "topics", config["topics"])
+ add_query_part(parts, "usernames", config["usernames"])
+ add_query_part(parts, "groups", config["groups"])
+ add_query_part(parts, "keywords", config["keywords"])
+ add_query_part(parts, "topic_keywords", config["topic_keywords"])
+ if POST_TYPE_OPTIONS.include?(config["post_type"]) && config["post_type"] != "all"
+ add_query_part(parts, "post_type", config["post_type"])
+ end
+ if STATUS_OPTIONS.include?(config["status"]) && config["status"] != "any"
+ add_query_part(parts, "status", config["status"])
+ end
+ add_query_part(parts, "order", config["order"]) if ORDER_OPTIONS.include?(config["order"])
+
+ advanced_filter = config["advanced_filter"].to_s.strip
+ parts << advanced_filter if advanced_filter.present?
+ parts.join(" ")
+ end
+
+ def add_query_part(parts, key, value)
+ normalized = list_value(value)
+ parts << "#{key}:#{normalized}" if normalized.present?
+ end
+
+ def add_category_part(parts, key, value, exact:)
+ normalized = list_value(value)
+ return if normalized.blank?
+
+ if exact
+ normalized =
+ normalized
+ .split(",")
+ .map { |part| part.start_with?("=") ? part : "=#{part}" }
+ .join(",")
+ end
+ parts << "#{key}:#{normalized}"
+ end
+
+ def list_value(value)
+ Array
+ .wrap(value)
+ .flat_map { |item| item.to_s.split(",") }
+ .map(&:strip)
+ .reject(&:blank?)
+ .map { |item| quote_query_value(item) }
+ .join(",")
+ end
+
+ def quote_query_value(value)
+ return value if value.match?(/\A["'].*["']\z/)
+ return value if !value.match?(/\s/)
+
+ %("#{value.delete('"')}")
+ end
+
+ def bounded_integer(value, default:, min:, max: nil)
+ integer = Integer(value.presence || default)
+ integer = [integer, min].max
+ integer = [integer, max].min if max
+ integer
+ rescue ArgumentError, TypeError
+ default
+ end
+
+ def truthy?(value)
+ value == true || value.to_s == "true" || value.to_s == "1"
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_created/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_created/v1.rb
index 34045c7bd27..dc4ca9015c5 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_created/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_created/v1.rb
@@ -38,7 +38,7 @@ module DiscourseWorkflows
end
def valid?
- @post.present? && @post.topic.present? && @post.post_type == Post.types[:regular] &&
+ @post.present? && @post.topic.present? && @post.post_type == ::Post.types[:regular] &&
!@opts&.dig(:skip_workflows)
end
@@ -56,7 +56,7 @@ module DiscourseWorkflows
private
def post_data(post)
- serialize_record(post, WebHookPostSerializer)
+ serialize_post(post)
end
def topic_data(topic)
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rb
index 8251d91d3fb..e4be4bfeb15 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rb
@@ -82,7 +82,7 @@ module DiscourseWorkflows
},
query: {
type: :string,
- required: true,
+ required: false,
ui: {
control: :filter_query,
},
@@ -102,6 +102,16 @@ module DiscourseWorkflows
},
},
},
+ offset: {
+ type: :integer,
+ required: false,
+ default: 0,
+ display_options: {
+ show: {
+ operation: ["list"],
+ },
+ },
+ },
actor_username: {
type: :string,
required: false,
@@ -126,6 +136,7 @@ module DiscourseWorkflows
"tag_names" => exec_ctx.get_node_parameter("tag_names", item_index),
"query" => exec_ctx.get_node_parameter("query", item_index),
"limit" => exec_ctx.get_node_parameter("limit", item_index, default: DEFAULT_LIMIT),
+ "offset" => exec_ctx.get_node_parameter("offset", item_index, default: 0),
}
Array.wrap(execute_with_config(exec_ctx, config, item_index))
@@ -194,16 +205,19 @@ module DiscourseWorkflows
def list_topics(exec_ctx, config, item_index)
limit = [[Integer(config["limit"] || DEFAULT_LIMIT), 1].max, MAX_LIMIT].min
+ offset = [Integer(config["offset"] || 0), 0].max
actor = exec_ctx.actor_from_parameter("actor_username", item_index)
- topic_query = TopicQuery.new(actor, q: config["query"], per_page: limit)
+ topic_query =
+ TopicQuery.new(actor, q: config["query"], per_page: [limit + offset, MAX_LIMIT].min)
topic_list = topic_query.list_filter
- posts = topic_list.topics.map(&:first_post).compact
+ topics = topic_list.topics.slice(offset, limit) || []
+ posts = topics.map(&:first_post).compact
if posts.any?
ActiveRecord::Associations::Preloader.new(records: posts, associations: :user).call
end
- topic_list.topics.map { |topic| { topic: topic_data(topic, actor.guardian) } }
+ topics.map { |topic| { topic: topic_data(topic, actor.guardian) } }
end
def topic_data(topic, guardian)
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_created/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_created/v1.rb
index 120fcc13394..8365acfc030 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_created/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_created/v1.rb
@@ -57,7 +57,7 @@ module DiscourseWorkflows
end
def post_data(post)
- serialize_record(post, WebHookPostSerializer)
+ serialize_post(post)
end
def matches_category?(category_id)
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/property_schema_validator.rb b/plugins/discourse-workflows/lib/discourse_workflows/property_schema_validator.rb
index 0933d8164b7..4052e9c373b 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/property_schema_validator.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/property_schema_validator.rb
@@ -59,6 +59,7 @@ module DiscourseWorkflows
control
dynamic_value
expression
+ filter
flat
format
hidden
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/node_execution_context_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/node_execution_context_spec.rb
index 1f0d284c1ef..1d88830212f 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/node_execution_context_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/node_execution_context_spec.rb
@@ -666,6 +666,84 @@ RSpec.describe DiscourseWorkflows::Executor::NodeExecutionContext do
end
end
+ describe "#create_post" do
+ fab!(:admin)
+ fab!(:user)
+ fab!(:first_post) { Fabricate(:post, user: user, raw: "First post", post_number: 1) }
+ fab!(:topic) { first_post.topic }
+
+ it "creates a reply as the provided user" do
+ ctx = described_class.new(input_items: [], resolver: nil)
+
+ expect do
+ post =
+ ctx.create_post(
+ user: admin,
+ raw: "Workflow reply",
+ topic_id: topic.id,
+ reply_to_post_number: first_post.post_number,
+ )
+
+ expect(post.raw).to eq("Workflow reply")
+ expect(post.user_id).to eq(admin.id)
+ expect(post.reply_to_post_number).to eq(first_post.post_number)
+ end.to change { topic.posts.count }.by(1)
+ end
+
+ it "requires the user to see the topic" do
+ group = Fabricate(:group)
+ private_category = Fabricate(:private_category, group: group)
+ hidden_topic = Fabricate(:topic, category: private_category)
+ ctx = described_class.new(input_items: [], resolver: nil)
+
+ expect do
+ ctx.create_post(user: user, raw: "Hidden reply", topic_id: hidden_topic.id)
+ end.to raise_error(Discourse::InvalidAccess).and not_change { hidden_topic.posts.count }
+ end
+
+ it "raises a node error when the topic is closed or archived" do
+ topic.update!(closed: true)
+ ctx = described_class.new(input_items: [], resolver: nil)
+
+ expect do
+ ctx.create_post(user: admin, raw: "Workflow reply", topic_id: topic.id)
+ end.to raise_error(
+ DiscourseWorkflows::NodeError,
+ /Cannot create a post in a closed or archived topic/,
+ )
+ end
+ end
+
+ describe "#serialize_post" do
+ fab!(:user)
+ fab!(:post) { Fabricate(:post, user: user, raw: "Workflow post body") }
+
+ it "serializes a post for workflow output" do
+ ctx = described_class.new(input_items: [], resolver: nil)
+
+ result = ctx.serialize_post(post, guardian: user.guardian, include_cooked: true)
+
+ expect(result).to include(
+ id: post.id,
+ topic_id: post.topic_id,
+ topic_title: post.topic.title,
+ post_number: post.post_number,
+ username: user.username,
+ raw: "Workflow post body",
+ cooked: post.cooked,
+ )
+ end
+
+ it "can omit raw and cooked body fields" do
+ ctx = described_class.new(input_items: [], resolver: nil)
+
+ result =
+ ctx.serialize_post(post, guardian: user.guardian, include_raw: false, include_cooked: false)
+
+ expect(result).not_to include(:raw, :cooked)
+ end
+ end
+
describe "#http_request" do
it "returns a parsed response object" do
stub_request(:get, "https://api.example.com/data").to_return(
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_spec.rb
index 8021416f773..7481dca1445 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_spec.rb
@@ -733,6 +733,16 @@ RSpec.describe DiscourseWorkflows::Executor do
end
end
+ let(:oversized_output_node_class) do
+ Class.new(DiscourseWorkflows::NodeType) do
+ description(name: "action:oversized_output_test", version: "1.0")
+
+ def execute(_exec_ctx)
+ [[{ "json" => { "body" => "x" * 200 } }, { "json" => { "body" => "small" } }]]
+ end
+ end
+ end
+
before do
plugin = Plugin::Instance.new
DiscoursePluginRegistry.register_discourse_workflows_node(raw_array_node_class, plugin)
@@ -741,14 +751,21 @@ RSpec.describe DiscourseWorkflows::Executor do
malformed_array_node_class,
plugin,
)
+ DiscoursePluginRegistry.register_discourse_workflows_node(
+ oversized_output_node_class,
+ plugin,
+ )
DiscourseWorkflows::Registry.reset_indexes!
end
after do
DiscoursePluginRegistry._raw_discourse_workflows_nodes.reject! do |entry|
- [raw_array_node_class, named_outputs_node_class, malformed_array_node_class].include?(
- entry[:value],
- )
+ [
+ raw_array_node_class,
+ named_outputs_node_class,
+ malformed_array_node_class,
+ oversized_output_node_class,
+ ].include?(entry[:value])
end
DiscourseWorkflows::Registry.reset_indexes!
end
@@ -802,6 +819,31 @@ RSpec.describe DiscourseWorkflows::Executor do
expect(execution.status).to eq("error")
expect(execution.error).to include("execute must return Array<Array<Item>>")
end
+
+ it "truncates oversized node outputs before routing downstream" do
+ stub_const(described_class, :MAX_NODE_OUTPUT_BYTES, 100) do
+ graph =
+ build_workflow_graph do |g|
+ g.node "trigger-1", "trigger:topic_closed"
+ g.node "oversized-1", "action:oversized_output_test", name: "Oversized"
+ g.node "code-1", "action:code", configuration: { "code" => "return $input.all();" }
+ g.chain "trigger-1", "oversized-1", "code-1"
+ end
+ workflow =
+ Fabricate(:discourse_workflows_workflow, created_by: user, published: true, **graph)
+
+ execution = described_class.new(workflow, "trigger-1", { topic_id: topic.id }).run
+
+ expect(execution.status).to eq("success")
+
+ oversized_step = execution.execution_data.find_step(node_id: "oversized-1")
+ expect(oversized_step["output"]).to eq([])
+ expect(oversized_step.dig("metadata", "logs").first["message"]).to include(
+ "Node output truncated",
+ )
+ expect(execution.execution_data.find_step(node_id: "code-1")).to be_nil
+ end
+ end
end
end
end
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/create_post_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/create_post_spec.rb
index d50f04f676f..775ca7e5872 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/create_post_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/create_post_spec.rb
@@ -30,11 +30,17 @@ RSpec.describe DiscourseWorkflows::Nodes::CreatePost::V1 do
expect(reply.user_id).to eq(admin.id)
expect(result["post"]).to include(
"id" => reply.id,
+ "topic_id" => topic.id,
+ "topic_title" => topic.title,
+ "topic_slug" => topic.slug,
"post_number" => reply.post_number,
+ "post_url" => reply.url,
"raw" => "Workflow reply",
- "reply_to_post_number" => nil,
+ "cooked" => reply.cooked,
"user_id" => admin.id,
"username" => admin.username,
+ "category_id" => topic.category_id,
+ "tags" => [],
)
end
@@ -174,8 +180,10 @@ RSpec.describe DiscourseWorkflows::Nodes::CreatePost::V1 do
expect(reply.user_id).to eq(admin.id)
expect(result.first["json"]["post"]).to include(
"id" => reply.id,
+ "topic_id" => topic.id,
"post_number" => reply.post_number,
"raw" => "Hello Ada",
+ "cooked" => reply.cooked,
)
ensure
resolver&.dispose
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_spec.rb
new file mode 100644
index 00000000000..28c280a29fe
--- /dev/null
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_spec.rb
@@ -0,0 +1,221 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseWorkflows::Nodes::Post::V1 do
+ fab!(:admin)
+ fab!(:user)
+ fab!(:other_user, :user)
+ fab!(:category)
+ fab!(:other_category, :category)
+ fab!(:tag) { Fabricate(:tag, name: "weekly-report") }
+
+ before { SiteSetting.tagging_enabled = true }
+
+ describe "#execute" do
+ let(:item) { { "json" => {} } }
+
+ it "creates a post for the configured author" do
+ first_post = Fabricate(:post, user: user, raw: "First post", post_number: 1)
+ topic = first_post.topic
+ result = nil
+
+ expect do
+ result =
+ execute_node(
+ configuration: {
+ "operation" => "create",
+ "topic_id" => topic.id.to_s,
+ "raw" => "Workflow reply",
+ "author_username" => admin.username,
+ },
+ item: item,
+ )
+ end.to change { topic.posts.count }.by(1)
+
+ reply = topic.posts.order(:id).last
+ expect(reply.raw).to eq("Workflow reply")
+ expect(reply.user_id).to eq(admin.id)
+ expect(result["post"]).to include(
+ "id" => reply.id,
+ "topic_id" => topic.id,
+ "topic_title" => topic.title,
+ "post_number" => reply.post_number,
+ "username" => admin.username,
+ "raw" => "Workflow reply",
+ )
+ end
+
+ it "gets a visible post with selected body fields" do
+ post = Fabricate(:post, user: user, raw: "Visible post body", post_number: 1)
+
+ result =
+ execute_node(
+ configuration: {
+ "operation" => "get",
+ "post_id" => post.id.to_s,
+ "include_raw" => true,
+ "include_cooked" => false,
+ },
+ item: item,
+ )
+
+ expect(result["post"]).to include(
+ "id" => post.id,
+ "topic_id" => post.topic_id,
+ "post_url" => post.url,
+ "username" => user.username,
+ "raw" => "Visible post body",
+ )
+ expect(result["post"]).not_to have_key("cooked")
+ end
+
+ it "raises when the actor cannot see the post" do
+ group = Fabricate(:group)
+ private_category = Fabricate(:private_category, group: group)
+ hidden_post = create_post(user: admin, category: private_category)
+
+ expect do
+ execute_node(
+ configuration: {
+ "operation" => "get",
+ "post_id" => hidden_post.id.to_s,
+ "actor_username" => other_user.username,
+ },
+ item: item,
+ )
+ end.to raise_error(Discourse::InvalidAccess)
+ end
+
+ it "lists posts matching UI filter fields" do
+ topic_1 =
+ Fabricate(:topic, category: category, tags: [tag], user: user, title: "First report topic")
+ matching_post =
+ Fabricate(:post, topic: topic_1, user: user, post_number: 1, raw: "alpha body")
+ topic_2 =
+ Fabricate(:topic, category: other_category, user: other_user, title: "Other ignored topic")
+ Fabricate(:post, topic: topic_2, user: other_user, post_number: 1, raw: "beta body")
+
+ result =
+ execute_node_output(
+ configuration: {
+ "operation" => "list",
+ "created_after" => "7 days ago",
+ "categories" => category.slug,
+ "tags" => [tag.name],
+ "usernames" => user.username,
+ "post_type" => "first",
+ "order" => "latest",
+ "limit" => "10",
+ },
+ item: item,
+ ).first
+
+ expect(result.map { |output_item| output_item.dig("json", "post", "id") }).to contain_exactly(
+ matching_post.id,
+ )
+ expect(result.first.dig("json", "post")).to include(
+ "category_name" => category.name,
+ "tags" => [tag.name],
+ "raw" => "alpha body",
+ )
+ end
+
+ it "lists posts matching the query field" do
+ topic = Fabricate(:topic, category: category, user: user, title: "Query field topic")
+ matching_post = Fabricate(:post, topic: topic, user: user, post_number: 1, raw: "query body")
+ ignored_topic =
+ Fabricate(:topic, category: other_category, user: other_user, title: "Ignored query topic")
+ Fabricate(:post, topic: ignored_topic, user: other_user, post_number: 1, raw: "ignored body")
+
+ result =
+ execute_node_output(
+ configuration: {
+ "operation" => "list",
+ "query" => "category:#{category.slug} username:#{user.username}",
+ "categories" => other_category.slug,
+ "limit" => "10",
+ },
+ item: item,
+ ).first
+
+ expect(result.map { |output_item| output_item.dig("json", "post", "id") }).to contain_exactly(
+ matching_post.id,
+ )
+ end
+
+ it "lists posts matching the advanced filter" do
+ matching_post = Fabricate(:post, user: user, raw: "advanced body")
+ Fabricate(:post, user: other_user, raw: "ignored body")
+
+ result =
+ execute_node_output(
+ configuration: {
+ "operation" => "list",
+ "advanced_filter" => "username:#{user.username}",
+ "limit" => "10",
+ },
+ item: item,
+ ).first
+
+ expect(result.map { |output_item| output_item.dig("json", "post", "id") }).to contain_exactly(
+ matching_post.id,
+ )
+ end
+
+ it "respects actor permissions when listing posts" do
+ restricted = Fabricate(:category, read_restricted: true)
+ Fabricate(:category_group, category: restricted, group: Group[:staff], permission_type: 0)
+ restricted_topic = Fabricate(:topic, category: restricted)
+ restricted_post = Fabricate(:post, topic: restricted_topic, post_number: 1)
+
+ system_result =
+ execute_node_output(
+ configuration: {
+ "operation" => "list",
+ "categories" => restricted.slug,
+ },
+ item: item,
+ ).first
+ user_result =
+ execute_node_output(
+ configuration: {
+ "operation" => "list",
+ "categories" => restricted.slug,
+ "actor_username" => other_user.username,
+ },
+ item: item,
+ ).first
+
+ expect(
+ system_result.map { |output_item| output_item.dig("json", "post", "id") },
+ ).to contain_exactly(restricted_post.id)
+ expect(user_result).to eq([])
+ end
+
+ it "enforces the result count cap" do
+ 3.times { |index| Fabricate(:post, raw: "post #{index}", post_number: index + 1) }
+
+ limited_result =
+ execute_node_output(
+ configuration: {
+ "operation" => "list",
+ "limit" => "2",
+ },
+ item: item,
+ ).first
+
+ expect(limited_result.length).to eq(2)
+ end
+
+ it "raises for invalid advanced filters" do
+ expect do
+ execute_node(
+ configuration: {
+ "operation" => "list",
+ "advanced_filter" => "invalidfilter",
+ },
+ item: item,
+ )
+ end.to raise_error(DiscourseWorkflows::NodeError, /Invalid post filter fragment/)
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_spec.rb
index 31083d0690f..84cb11b483f 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_spec.rb
@@ -218,6 +218,15 @@ RSpec.describe DiscourseWorkflows::Nodes::Topic::V1 do
)
end
+ it "returns topics when query is not provided" do
+ result = execute_list(configuration: { "operation" => "list", "limit" => "10" })
+
+ expect(result.map { |output_item| output_item["json"]["topic"]["id"] }).to include(
+ topic_1.id,
+ topic_2.id,
+ )
+ end
+
it "respects the limit parameter" do
result =
execute_list(
@@ -231,6 +240,30 @@ RSpec.describe DiscourseWorkflows::Nodes::Topic::V1 do
expect(result.length).to eq(1)
end
+ it "respects the offset parameter" do
+ unoffset_result =
+ execute_list(
+ configuration: {
+ "operation" => "list",
+ "query" => "category:#{category.slug}",
+ "limit" => "2",
+ },
+ )
+ offset_result =
+ execute_list(
+ configuration: {
+ "operation" => "list",
+ "query" => "category:#{category.slug}",
+ "limit" => "1",
+ "offset" => "1",
+ },
+ )
+
+ expect(offset_result.map { |output_item| output_item["json"]["topic"]["id"] }).to eq(
+ [unoffset_result[1]["json"]["topic"]["id"]],
+ )
+ end
+
it "defaults limit to 30 when not provided" do
result =
execute_list(
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/property_schema_validator_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/property_schema_validator_spec.rb
index 6484e7b90c9..35e1ecfb60a 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/property_schema_validator_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/property_schema_validator_spec.rb
@@ -75,6 +75,12 @@ RSpec.describe DiscourseWorkflows::PropertySchemaValidator do
expect(errors.first).to include(":rowz")
end
+ it "accepts filter query ui metadata" do
+ schema = { query: { type: :string, ui: { control: :filter_query, filter: :posts } } }
+
+ expect(validate(schema)).to eq([])
+ end
+
it "flags unknown ui.control values" do
errors = validate(title: { type: :string, ui: { control: :neon } })
expect(errors.first).to include("title.ui.control")
diff --git a/plugins/discourse-workflows/spec/requests/discourse_workflows/filter_options_controller_spec.rb b/plugins/discourse-workflows/spec/requests/discourse_workflows/filter_options_controller_spec.rb
new file mode 100644
index 00000000000..759ec39bdd2
--- /dev/null
+++ b/plugins/discourse-workflows/spec/requests/discourse_workflows/filter_options_controller_spec.rb
@@ -0,0 +1,30 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseWorkflows::FilterOptionsController do
+ fab!(:admin)
+
+ before { sign_in(admin) }
+
+ context "when not logged in as admin" do
+ fab!(:user)
+
+ before { sign_in(user) }
+
+ it "returns 404" do
+ get "/admin/plugins/discourse-workflows/filter-options/posts.json"
+
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+
+ describe "GET /admin/plugins/discourse-workflows/filter-options/posts" do
+ it "returns post filter option info" do
+ get "/admin/plugins/discourse-workflows/filter-options/posts.json"
+
+ expect(response).to have_http_status(:ok)
+ option_names = response.parsed_body["filter_option_info"].map { |option| option["name"] }
+
+ expect(option_names).to include("category:", "keywords:", "post_type:")
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/spec/requests/discourse_workflows/node_types_controller_spec.rb b/plugins/discourse-workflows/spec/requests/discourse_workflows/node_types_controller_spec.rb
index 78baf66e671..9e2bd5b78ec 100644
--- a/plugins/discourse-workflows/spec/requests/discourse_workflows/node_types_controller_spec.rb
+++ b/plugins/discourse-workflows/spec/requests/discourse_workflows/node_types_controller_spec.rb
@@ -31,6 +31,23 @@ RSpec.describe DiscourseWorkflows::NodeTypesController do
expect(identifiers).to include("condition:if")
end
+ it "returns List posts query control metadata" do
+ get "/admin/plugins/discourse-workflows/node-types.json"
+
+ post_node =
+ response.parsed_body["node_types"].find do |node_type|
+ node_type["identifier"] == "action:post"
+ end
+ properties = post_node["properties"]
+
+ expect(properties["query"]).to include(
+ "type" => "string",
+ "ui" => include("control" => "filter_query", "filter" => "posts"),
+ )
+ expect(properties["categories"]["ui"]).to include("hidden" => true)
+ expect(properties["advanced_filter"]["ui"]).to include("hidden" => true)
+ end
+
it "does not include metadata key in node type response" do
get "/admin/plugins/discourse-workflows/node-types.json"
diff --git a/plugins/discourse-workflows/test/javascripts/integration/components/workflows/filter-query-test.gjs b/plugins/discourse-workflows/test/javascripts/integration/components/workflows/filter-query-test.gjs
new file mode 100644
index 00000000000..d92105c1d22
--- /dev/null
+++ b/plugins/discourse-workflows/test/javascripts/integration/components/workflows/filter-query-test.gjs
@@ -0,0 +1,70 @@
+import { render, waitUntil } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import pretender, { response } from "discourse/tests/helpers/create-pretender";
+import FilterQuery from "discourse/plugins/discourse-workflows/admin/components/workflows/configurators/filter-query";
+
+module("Integration | Component | workflows filter query", function (hooks) {
+ setupRenderingTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.field = { value: "", set() {} };
+ });
+
+ test("loads topic filter options by default", async function (assert) {
+ let topicRequests = 0;
+ pretender.get("/filter.json", () => {
+ topicRequests++;
+ return response(200, { topic_list: { filter_option_info: [] } });
+ });
+
+ await render(
+ <template>
+ <FilterQuery @field={{this.field}} @supportsExpression={{false}} />
+ </template>
+ );
+
+ await waitUntil(() => topicRequests === 1);
+
+ assert.strictEqual(topicRequests, 1);
+ });
+
+ test("loads post filter options and uses the supplied placeholder", async function (assert) {
+ let postRequests = 0;
+ pretender.get(
+ "/admin/plugins/discourse-workflows/filter-options/posts.json",
+ () => {
+ postRequests++;
+ return response(200, {
+ filter_option_info: [
+ { name: "keywords:", description: "Show posts matching keywords" },
+ ],
+ });
+ }
+ );
+
+ this.setProperties({
+ placeholder:
+ "Filter posts by category, tag, author, keywords, or other criteria",
+ schema: { ui: { filter: "posts" } },
+ });
+
+ await render(
+ <template>
+ <FilterQuery
+ @field={{this.field}}
+ @placeholder={{this.placeholder}}
+ @schema={{this.schema}}
+ @supportsExpression={{false}}
+ />
+ </template>
+ );
+
+ await waitUntil(() => postRequests === 1);
+
+ assert.strictEqual(postRequests, 1);
+ assert
+ .dom(".topic-query-filter__filter-term")
+ .hasAttribute("placeholder", this.placeholder);
+ });
+});
diff --git a/spec/lib/posts_filter_spec.rb b/spec/lib/posts_filter_spec.rb
new file mode 100644
index 00000000000..4b4105fa84d
--- /dev/null
+++ b/spec/lib/posts_filter_spec.rb
@@ -0,0 +1,328 @@
+# frozen_string_literal: true
+
+RSpec.describe PostsFilter do
+ before do
+ SiteSetting.tagging_enabled = true
+ SiteSetting.min_topic_title_length = 3
+ end
+
+ fab!(:user)
+ fab!(:user2, :user)
+ fab!(:feature_tag) { Fabricate(:tag, name: "feature") }
+ fab!(:bug_tag) { Fabricate(:tag, name: "bug") }
+ fab!(:announcement_category) { Fabricate(:category, name: "Announcements") }
+ fab!(:feedback_category) { Fabricate(:category, name: "Feedback") }
+
+ fab!(:feature_topic) do
+ Fabricate(
+ :topic,
+ user: user,
+ tags: [feature_tag],
+ category: announcement_category,
+ title: "New Feature Discussion",
+ )
+ end
+
+ fab!(:bug_topic) do
+ Fabricate(
+ :topic,
+ tags: [bug_tag],
+ user: user,
+ category: announcement_category,
+ title: "Bug Report Discussion",
+ )
+ end
+
+ fab!(:feature_bug_topic) do
+ Fabricate(
+ :topic,
+ tags: [feature_tag, bug_tag],
+ user: user,
+ category: feedback_category,
+ title: "Feature with Bug",
+ )
+ end
+
+ fab!(:no_tag_topic) do
+ Fabricate(:topic, user: user, category: feedback_category, title: "General Discussion")
+ end
+
+ fab!(:feature_post) { Fabricate(:post, topic: feature_topic, user: user, post_number: 1) }
+ fab!(:bug_post) { Fabricate(:post, topic: bug_topic, user: user, post_number: 1) }
+ fab!(:feature_bug_post) { Fabricate(:post, topic: feature_bug_topic, user: user, post_number: 1) }
+ fab!(:no_tag_post) { Fabricate(:post, topic: no_tag_topic, user: user, post_number: 1) }
+
+ def filtered_post_ids(query, guardian: nil, limit: nil, offset: nil, scope: Post.all)
+ described_class
+ .new(query, guardian: guardian, limit: limit, offset: offset, scope: scope)
+ .search
+ .pluck(:id)
+ end
+
+ it "filters posts by tags and categories" do
+ expect(filtered_post_ids("tag:feature")).to contain_exactly(
+ feature_post.id,
+ feature_bug_post.id,
+ )
+ expect(filtered_post_ids("tags:bug")).to contain_exactly(bug_post.id, feature_bug_post.id)
+ expect(filtered_post_ids("category:Announcements")).to contain_exactly(
+ feature_post.id,
+ bug_post.id,
+ )
+ expect(filtered_post_ids("categories:Feedback tag:feature")).to contain_exactly(
+ feature_bug_post.id,
+ )
+ end
+
+ it "supports exact and nested category matching" do
+ child_category =
+ Fabricate(
+ :category,
+ name: "Announcements Child",
+ parent_category_id: announcement_category.id,
+ )
+ child_topic =
+ Fabricate(:topic, user: user, category: child_category, title: "Child Category Discussion")
+ child_post = Fabricate(:post, topic: child_topic, user: user, post_number: 1)
+
+ expect(filtered_post_ids("category:Announcements")).to contain_exactly(
+ feature_post.id,
+ bug_post.id,
+ child_post.id,
+ )
+ expect(filtered_post_ids("category:=Announcements")).to contain_exactly(
+ feature_post.id,
+ bug_post.id,
+ )
+ expect(
+ filtered_post_ids("category:#{announcement_category.slug}/#{child_category.slug}"),
+ ).to contain_exactly(child_post.id)
+ expect(filtered_post_ids('category:"Announcements Child"')).to contain_exactly(child_post.id)
+ end
+
+ it "supports category and tag exclusions" do
+ expect(filtered_post_ids("-category:Announcements")).to contain_exactly(
+ feature_bug_post.id,
+ no_tag_post.id,
+ )
+ expect(filtered_post_ids("exclude_categories:Feedback")).to contain_exactly(
+ feature_post.id,
+ bug_post.id,
+ )
+ expect(filtered_post_ids("-tag:feature")).to contain_exactly(bug_post.id, no_tag_post.id)
+ expect(filtered_post_ids("exclude_tags:bug")).to contain_exactly(
+ feature_post.id,
+ no_tag_post.id,
+ )
+ end
+
+ it "filters posts by users and groups" do
+ group1 = Fabricate(:group, name: "group1")
+ group2 = Fabricate(:group, name: "group2")
+ group1.add(user)
+ group2.add(user2)
+ no_tag_post.update!(user_id: user2.id)
+
+ expect(filtered_post_ids("username:#{user.username}")).to contain_exactly(
+ feature_post.id,
+ bug_post.id,
+ feature_bug_post.id,
+ )
+ expect(filtered_post_ids("groups:group1,group2")).to contain_exactly(
+ feature_post.id,
+ bug_post.id,
+ feature_bug_post.id,
+ no_tag_post.id,
+ )
+ end
+
+ it "supports unicode usernames" do
+ SiteSetting.unicode_usernames = true
+ unicode_user = Fabricate(:user, username: "aאb")
+ unicode_post = Fabricate(:post, user: unicode_user, topic: feature_topic, post_number: 2)
+
+ expect(filtered_post_ids("username:aאb")).to contain_exactly(unicode_post.id)
+ end
+
+ it "filters by topic, post type, status, dates, and limits" do
+ feature_post.update_columns(created_at: 3.days.ago)
+ bug_post.update_columns(created_at: 2.days.ago)
+ feature_bug_post.update_columns(created_at: 1.day.ago)
+ feature_topic.update_columns(created_at: 4.days.ago)
+ bug_topic.update_columns(closed: true, created_at: 2.days.ago)
+
+ reply_post = Fabricate(:post, topic: feature_topic, user: user, post_number: 2)
+
+ expect(filtered_post_ids("topic:#{feature_topic.id}")).to contain_exactly(
+ feature_post.id,
+ reply_post.id,
+ )
+ expect(filtered_post_ids("post_type:first")).to contain_exactly(
+ feature_post.id,
+ bug_post.id,
+ feature_bug_post.id,
+ no_tag_post.id,
+ )
+ expect(filtered_post_ids("post_type:reply")).to contain_exactly(reply_post.id)
+ expect(filtered_post_ids("status:closed")).to contain_exactly(bug_post.id)
+ expect(filtered_post_ids("after:#{2.days.ago.to_date}")).to include(feature_bug_post.id)
+ expect(filtered_post_ids("topic_before:#{3.days.ago.to_date}")).to contain_exactly(
+ feature_post.id,
+ reply_post.id,
+ )
+ expect(filtered_post_ids("category:Feedback max_results:1", limit: 5).length).to eq(1)
+ end
+
+ it "orders posts" do
+ feature_post.update_columns(created_at: 3.days.ago, like_count: 1)
+ bug_post.update_columns(created_at: 2.days.ago, like_count: 5)
+ feature_bug_post.update_columns(created_at: 1.day.ago, like_count: 2)
+
+ expect(filtered_post_ids("category:Announcements order:oldest")).to eq(
+ [feature_post.id, bug_post.id],
+ )
+ expect(filtered_post_ids("order:likes").first).to eq(bug_post.id)
+ end
+
+ it "supports OR groups" do
+ expect(filtered_post_ids("category:Announcements OR tag:feature")).to contain_exactly(
+ feature_post.id,
+ bug_post.id,
+ feature_bug_post.id,
+ )
+ end
+
+ it "filters posts and topics by full text keywords" do
+ SearchIndexer.enable
+ post_with_apples = Fabricate(:post, raw: "This post contains apples", topic: feature_topic)
+ post_with_bananas = Fabricate(:post, raw: "This post mentions bananas", topic: bug_topic)
+ post_with_both =
+ Fabricate(:post, raw: "This post has apples and bananas", topic: feature_bug_topic)
+ Fabricate(:post, raw: "No fruits here", topic: no_tag_topic)
+ reply_on_bananas_topic = Fabricate(:post, raw: "Just a reply", topic: post_with_bananas.topic)
+
+ expect(filtered_post_ids("keywords:apples")).to contain_exactly(
+ post_with_apples.id,
+ post_with_both.id,
+ )
+ expect(filtered_post_ids("keywords:apples,bananas")).to contain_exactly(
+ post_with_apples.id,
+ post_with_bananas.id,
+ post_with_both.id,
+ )
+ expect(filtered_post_ids("topic_keywords:banana")).to contain_exactly(
+ bug_post.id,
+ post_with_bananas.id,
+ reply_on_bananas_topic.id,
+ feature_bug_post.id,
+ post_with_both.id,
+ )
+ end
+
+ it "tracks invalid filter fragments" do
+ filter = described_class.new("invalidfilter tag:feature order:missing")
+
+ expect(filter.invalid_filters).to contain_exactly("invalidfilter", "order:missing")
+ end
+
+ it "omits secure categories without a guardian and always omits PMs" do
+ secure_group = Fabricate(:group)
+ secure_category = Fabricate(:category, name: "Secure")
+ secure_category.set_permissions(secure_group => :readonly)
+ secure_category.save!
+ secure_topic =
+ Fabricate(:topic, category: secure_category, user: user, title: "Secret Topic Discussion")
+ secure_post = Fabricate(:post, topic: secure_topic, user: user)
+ pm_topic = Fabricate(:private_message_topic, user: user)
+ pm_post = Fabricate(:post, topic: pm_topic, user: user)
+
+ expect(filtered_post_ids("")).not_to include(secure_post.id, pm_post.id)
+
+ secure_group.add(user)
+ expect(filtered_post_ids("", guardian: Guardian.new(user))).to include(secure_post.id)
+ expect(filtered_post_ids("", guardian: Guardian.new(user))).not_to include(pm_post.id)
+ end
+
+ it "supports filter_from_query_string and custom filters" do
+ described_class.add_filter("min_likes") do |scope, values, _guardian|
+ scope.where("posts.like_count >= ?", values.first.to_i)
+ end
+ bug_post.update!(like_count: 3)
+
+ result = described_class.new(guardian: Guardian.new).filter_from_query_string("min_likes:2")
+
+ expect(result.pluck(:id)).to contain_exactly(bug_post.id)
+ ensure
+ described_class.remove_filter("min_likes")
+ end
+
+ it "applies guardian visibility to hidden posts and unlisted topics" do
+ hidden_post = Fabricate(:post, topic: feature_topic, user: user2, post_number: 2, hidden: true)
+ unlisted_topic = Fabricate(:topic, user: user, visible: false, title: "Unlisted Discussion")
+ unlisted_post = Fabricate(:post, topic: unlisted_topic, user: user, post_number: 1)
+
+ expect(filtered_post_ids("", guardian: Guardian.new(user))).not_to include(
+ hidden_post.id,
+ unlisted_post.id,
+ )
+
+ expect(filtered_post_ids("", guardian: Guardian.new(Fabricate(:admin)))).to include(
+ hidden_post.id,
+ unlisted_post.id,
+ )
+ end
+
+ it "only filters by tags visible to the guardian" do
+ hidden_tag = Fabricate(:tag, name: "hidden")
+ Fabricate(:tag_group, permissions: { "staff" => 1 }, tag_names: [hidden_tag.name])
+ hidden_tag_topic = Fabricate(:topic, user: user, tags: [hidden_tag], title: "Hidden Tag Topic")
+ hidden_tag_post = Fabricate(:post, topic: hidden_tag_topic, user: user, post_number: 1)
+
+ expect(
+ filtered_post_ids("tag:#{hidden_tag.name}", guardian: Guardian.new(user)),
+ ).not_to include(hidden_tag_post.id)
+ expect(
+ filtered_post_ids("tag:#{hidden_tag.name}", guardian: Guardian.new(Fabricate(:admin))),
+ ).to include(hidden_tag_post.id)
+ end
+
+ it "only filters by groups with visible memberships" do
+ private_group =
+ Fabricate(
+ :group,
+ name: "private_group",
+ visibility_level: Group.visibility_levels[:staff],
+ members_visibility_level: Group.visibility_levels[:staff],
+ )
+ private_group.add(user2)
+ private_group_post = Fabricate(:post, topic: feature_topic, user: user2, post_number: 2)
+
+ expect(
+ filtered_post_ids("group:#{private_group.name}", guardian: Guardian.new(user)),
+ ).not_to include(private_group_post.id)
+ expect(
+ filtered_post_ids("group:#{private_group.name}", guardian: Guardian.new(Fabricate(:admin))),
+ ).to include(private_group_post.id)
+ end
+
+ it "does not match topic keywords from hidden posts for users who cannot see them" do
+ SearchIndexer.enable
+ hidden_post =
+ Fabricate(
+ :post,
+ topic: feature_topic,
+ user: user,
+ post_number: 2,
+ raw: "needleword only in a hidden post",
+ hidden: true,
+ )
+ SearchIndexer.index(hidden_post, force: true)
+
+ expect(filtered_post_ids("topic_keywords:needleword", guardian: Guardian.new(user))).to be_empty
+ expect(
+ filtered_post_ids("topic_keywords:needleword", guardian: Guardian.new(Fabricate(:admin))),
+ ).to include(feature_post.id, hidden_post.id)
+ ensure
+ SearchIndexer.disable
+ end
+end