Skip to content

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

FEATURE: Add `in:polls` filter to search (#19885)

Penar Musaraj3e197deec9f7
diff --git a/plugins/poll/assets/javascripts/initializers/extend-for-poll.js b/plugins/poll/assets/javascripts/initializers/extend-for-poll.js
index a22b322130e..109fc1a081d 100644
--- a/plugins/poll/assets/javascripts/initializers/extend-for-poll.js
+++ b/plugins/poll/assets/javascripts/initializers/extend-for-poll.js
@@ -134,6 +134,11 @@ function initializePolls(api) {
id: "discourse-poll",
});
api.cleanupStream(cleanUpPolls);
+
+ const siteSettings = api.container.lookup("site-settings:main");
+ if (siteSettings.poll_enabled) {
+ api.addSearchSuggestion("in:polls");
+ }
}
export default {
diff --git a/plugins/poll/plugin.rb b/plugins/poll/plugin.rb
index b6815930273..84b337a823b 100644
--- a/plugins/poll/plugin.rb
+++ b/plugins/poll/plugin.rb
@@ -231,4 +231,12 @@ after_initialize do
SiteSetting.poll_enabled && scope.user&.id.present? && preloaded_polls.present? &&
preloaded_polls.any? { |p| p.has_voted?(scope.user) }
end
+
+ register_search_advanced_filter(/in:polls/) do |posts, match|
+ if SiteSetting.poll_enabled
+ posts.joins(:polls)
+ else
+ posts
+ end
+ end
end
diff --git a/plugins/poll/spec/lib/search_spec.rb b/plugins/poll/spec/lib/search_spec.rb
new file mode 100644
index 00000000000..e102d9ff05f
--- /dev/null
+++ b/plugins/poll/spec/lib/search_spec.rb
@@ -0,0 +1,42 @@
+# frozen_string_literal: true
+
+RSpec.describe Search do
+ fab!(:topic) { Fabricate(:topic) }
+ fab!(:topic2) { Fabricate(:topic) }
+ fab!(:regular_post) { Fabricate(:post, topic: topic, raw: <<~RAW) }
+ Somewhere over the rainbow but no poll.
+ RAW
+
+ fab!(:post_with_poll) { Fabricate(:post, topic: topic2, raw: <<~RAW) }
+ Somewhere over the rainbow with a poll.
+ [poll]
+ * Like
+ * Dislike
+ [/poll]
+ RAW
+
+ before do
+ SearchIndexer.enable
+ Jobs.run_immediately!
+
+ SearchIndexer.index(topic2, force: true)
+ SearchIndexer.index(topic, force: true)
+ end
+
+ after { SearchIndexer.disable }
+
+ context "when using in:polls" do
+ it "displays only posts containing polls" do
+ results = Search.execute("rainbow in:polls", guardian: Guardian.new)
+ expect(results.posts).to contain_exactly(post_with_poll)
+ end
+ end
+
+ context "when polls are disabled" do
+ it "ignores in:polls filter" do
+ SiteSetting.poll_enabled = false
+ results = Search.execute("rainbow in:polls", guardian: Guardian.new)
+ expect(results.posts).to contain_exactly(regular_post, post_with_poll)
+ end
+ end
+end