Skip to content

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

FEATURE: improve bulk selection in search (#36373)

Samb9056effe40d
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 021fc41784b..e1a5ad2f028 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -3515,6 +3515,15 @@ en:
reset_bump_dates: "Reset bump dates"
defer: "Mark unread"
delete: "Delete Topics"
+ delete_topics_count:
+ one: "Delete Topic"
+ other: "Delete Topics"
+ delete_posts_count:
+ one: "Delete Post"
+ other: "Delete Posts"
+ delete_posts_confirmation:
+ one: "Are you sure you want to delete %{count} post?"
+ other: "Are you sure you want to delete %{count} posts?"
dismiss: "Dismiss"
dismiss_read: "Dismiss all unread"
dismiss_read_with_selected:
diff --git a/frontend/discourse/app/components/bulk-select-topics-dropdown.gjs b/frontend/discourse/app/components/bulk-select-topics-dropdown.gjs
index 809f4db93f8..f4e1c65013f 100644
--- a/frontend/discourse/app/components/bulk-select-topics-dropdown.gjs
+++ b/frontend/discourse/app/components/bulk-select-topics-dropdown.gjs
@@ -148,7 +148,13 @@ export default class BulkSelectTopicsDropdown extends Component {
},
];
- return [...options, ..._customButtons].filter(({ visible }) => {
+ const excludedButtonIds = this.args.excludedButtonIds || [];
+ const baseOptions = [...options, ..._customButtons].filter(
+ (button) => !excludedButtonIds.includes(button.id)
+ );
+ const extraButtons = this.args.extraButtons || [];
+
+ return [...baseOptions, ...extraButtons].filter(({ visible }) => {
if (visible) {
return visible({
topics: this.args.bulkSelectHelper.selected,
@@ -287,6 +293,11 @@ export default class BulkSelectTopicsDropdown extends Component {
setComponent: _customOnSelection[actionId].setComponent,
}
);
+ return;
+ }
+
+ if (this.args.onAction) {
+ this.args.onAction(actionId);
}
}
}
diff --git a/frontend/discourse/app/components/search-bulk-select-dropdown.gjs b/frontend/discourse/app/components/search-bulk-select-dropdown.gjs
new file mode 100644
index 00000000000..cd883eb2148
--- /dev/null
+++ b/frontend/discourse/app/components/search-bulk-select-dropdown.gjs
@@ -0,0 +1,98 @@
+import Component from "@glimmer/component";
+import { array } from "@ember/helper";
+import { action } from "@ember/object";
+import { service } from "@ember/service";
+import TopicBulkSelectDropdown from "discourse/components/topic-list/topic-bulk-select-dropdown";
+import { popupAjaxError } from "discourse/lib/ajax-error";
+import BulkSelectHelper from "discourse/lib/bulk-select-helper";
+import Post from "discourse/models/post";
+import { i18n } from "discourse-i18n";
+
+export default class SearchBulkSelectDropdown extends Component {
+ @service dialog;
+
+ _bulkSelectHelper = null;
+
+ get topics() {
+ const topics = new Map();
+ (this.args.bulkSelectHelper.selected || []).forEach((post) => {
+ if (post.topic) {
+ topics.set(post.topic.id, post.topic);
+ }
+ });
+ return Array.from(topics.values());
+ }
+
+ get topicBulkSelectHelper() {
+ if (!this._bulkSelectHelper) {
+ this._bulkSelectHelper = new BulkSelectHelper(this, this.topics);
+ this._bulkSelectHelper.onBulkSelectToggle = () => {
+ this.args.bulkSelectHelper.toggleBulkSelect();
+ };
+ return this._bulkSelectHelper;
+ }
+ this._bulkSelectHelper.setTopics(this.topics);
+ return this._bulkSelectHelper;
+ }
+
+ get extraButtons() {
+ return [
+ {
+ id: "delete-topics",
+ icon: "trash-can",
+ name: i18n("topics.bulk.delete_topics_count", {
+ count: this.topics.length,
+ }),
+ visible: ({ currentUser }) => currentUser?.staff,
+ },
+ {
+ id: "delete-posts",
+ icon: "trash-can",
+ name: i18n("topics.bulk.delete_posts_count", {
+ count: this.args.bulkSelectHelper.selected.length,
+ }),
+ visible: ({ currentUser }) => currentUser?.staff,
+ },
+ ];
+ }
+
+ @action
+ handleAction(actionId) {
+ if (actionId === "delete-posts") {
+ this.deletePosts();
+ }
+ }
+
+ @action
+ deletePosts() {
+ const posts = this.args.bulkSelectHelper.selected;
+ if (!posts.length) {
+ return;
+ }
+
+ this.dialog.confirm({
+ message: i18n("topics.bulk.delete_posts_confirmation", {
+ count: posts.length,
+ }),
+ didConfirm: async () => {
+ try {
+ await Post.deleteMany(posts.map((p) => p.id));
+ this.args.bulkSelectHelper.clear();
+ await this.args.afterBulkActionComplete?.();
+ } catch (error) {
+ popupAjaxError(error);
+ }
+ },
+ });
+ }
+
+ <template>
+ <TopicBulkSelectDropdown
+ @bulkSelectHelper={{this.topicBulkSelectHelper}}
+ @afterBulkActionComplete={{@afterBulkActionComplete}}
+ @extraButtons={{this.extraButtons}}
+ @excludedButtonIds={{array "delete-topics"}}
+ @onAction={{this.handleAction}}
+ />
+ </template>
+}
diff --git a/frontend/discourse/app/components/search-result-entry.gjs b/frontend/discourse/app/components/search-result-entry.gjs
index 675b1edb819..30d3c5db2de 100644
--- a/frontend/discourse/app/components/search-result-entry.gjs
+++ b/frontend/discourse/app/components/search-result-entry.gjs
@@ -70,7 +70,7 @@ export default class SearchResultEntry extends Component {
{{#if this.bulkSelectEnabled}}
<TrackSelected
@selectedList={{this.selected}}
- @selectedId={{this.post.topic}}
+ @selectedId={{this.post}}
class="bulk-select"
/>
{{/if}}
diff --git a/frontend/discourse/app/components/topic-list/topic-bulk-select-dropdown.gjs b/frontend/discourse/app/components/topic-list/topic-bulk-select-dropdown.gjs
index 478ad15f53b..e9d2111d320 100644
--- a/frontend/discourse/app/components/topic-list/topic-bulk-select-dropdown.gjs
+++ b/frontend/discourse/app/components/topic-list/topic-bulk-select-dropdown.gjs
@@ -12,6 +12,9 @@ const TopicBulkSelectDropdown = <template>
<BulkSelectTopicsDropdown
@bulkSelectHelper={{@bulkSelectHelper}}
@afterBulkActionComplete={{@afterBulkActionComplete}}
+ @extraButtons={{@extraButtons}}
+ @excludedButtonIds={{@excludedButtonIds}}
+ @onAction={{@onAction}}
/>
</div>
</template>;
diff --git a/frontend/discourse/app/controllers/full-page-search.js b/frontend/discourse/app/controllers/full-page-search.js
index dd53691f2b2..9c2a9971eb1 100644
--- a/frontend/discourse/app/controllers/full-page-search.js
+++ b/frontend/discourse/app/controllers/full-page-search.js
@@ -7,10 +7,10 @@ import { observes } from "@ember-decorators/object";
import { Promise } from "rsvp";
import { ajax } from "discourse/lib/ajax";
import { addUniqueValuesToArray } from "discourse/lib/array-tools";
-import BulkSelectHelper from "discourse/lib/bulk-select-helper";
import { search as searchCategoryTag } from "discourse/lib/category-tag-search";
import discourseComputed, { bind } from "discourse/lib/decorators";
import { setTransient } from "discourse/lib/page-tracker";
+import PostBulkSelectHelper from "discourse/lib/post-bulk-select-helper";
import { scrollTop } from "discourse/lib/scroll-top";
import {
getSearchKey,
@@ -136,7 +136,7 @@ export default class FullPageSearchController extends Controller {
});
}
- this.bulkSelectHelper = new BulkSelectHelper(this);
+ this.bulkSelectHelper = new PostBulkSelectHelper(this);
}
@discourseComputed("resultCount")
@@ -386,7 +386,7 @@ export default class FullPageSearchController extends Controller {
if (args.page === 1) {
this.set("bulkSelectEnabled", false);
- this.bulkSelectHelper.clear();
+ this.bulkSelectHelper.clearAll();
this.set("searching", true);
scrollTop();
} else {
@@ -508,7 +508,7 @@ export default class FullPageSearchController extends Controller {
page: 1,
resultCount: null,
});
- this.bulkSelectHelper.clear();
+ this.bulkSelectHelper.clearAll();
}
@action
@@ -551,7 +551,7 @@ export default class FullPageSearchController extends Controller {
selectAll() {
addUniqueValuesToArray(
this.bulkSelectHelper.selected,
- this.searchResultPosts.map((item) => item.topic)
+ this.searchResultPosts.map((item) => item)
);
// Doing this the proper way is a HUGE pain,
@@ -568,7 +568,7 @@ export default class FullPageSearchController extends Controller {
@action
clearAll() {
- this.bulkSelectHelper.selected = [];
+ this.bulkSelectHelper.clearAll();
document
.querySelectorAll(".fps-result input[type=checkbox]")
@@ -580,7 +580,7 @@ export default class FullPageSearchController extends Controller {
@action
toggleBulkSelect() {
this.toggleProperty("bulkSelectEnabled");
- this.bulkSelectHelper.selected = [];
+ this.bulkSelectHelper.clearAll();
}
@action
diff --git a/frontend/discourse/app/lib/bulk-select-helper.js b/frontend/discourse/app/lib/bulk-select-helper.js
index 7254ed3e0b4..5c54bc1ffb2 100644
--- a/frontend/discourse/app/lib/bulk-select-helper.js
+++ b/frontend/discourse/app/lib/bulk-select-helper.js
@@ -18,9 +18,14 @@ export default class BulkSelectHelper {
@tracked autoAddBookmarksToBulkSelect = false;
@tracked lastCheckedElementId = null;
- @trackedArray selected = [];
+ @trackedArray selected;
- constructor(context) {
+ constructor(context, topics) {
+ if (topics) {
+ this.selected = topics;
+ } else {
+ this.selected = [];
+ }
setOwner(this, getOwner(context));
}
@@ -30,7 +35,19 @@ export default class BulkSelectHelper {
}
addTopics(topics) {
- this.selected.concat(topics);
+ this.selected = this.selected.concat(topics);
+ }
+
+ setTopics(topics) {
+ this.selected = topics;
+ }
+
+ get onBulkSelectToggle() {
+ return this._onBulkSelectToggle;
+ }
+
+ set onBulkSelectToggle(callback) {
+ this._onBulkSelectToggle = callback;
}
get selectedCategoryIds() {
@@ -40,6 +57,7 @@ export default class BulkSelectHelper {
@action
toggleBulkSelect(event) {
event?.preventDefault();
+ this._onBulkSelectToggle?.(this.bulkSelectEnabled);
this.bulkSelectEnabled = !this.bulkSelectEnabled;
this.clear();
}
diff --git a/frontend/discourse/app/lib/post-bulk-select-helper.js b/frontend/discourse/app/lib/post-bulk-select-helper.js
index e337cf52e31..985f03bc087 100644
--- a/frontend/discourse/app/lib/post-bulk-select-helper.js
+++ b/frontend/discourse/app/lib/post-bulk-select-helper.js
@@ -16,6 +16,7 @@ import { TrackedArray } from "@ember-compat/tracked-built-ins";
export default class PostBulkSelectHelper {
@tracked loading = false;
@tracked lastClickedPost = null;
+ @tracked bulkSelectEnabled = false;
@tracked posts = null;
selected = new TrackedArray();
@@ -153,6 +154,18 @@ export default class PostBulkSelectHelper {
this.lastClickedPost = null;
}
+ @action
+ toggleBulkSelect(event) {
+ event?.preventDefault();
+ this.bulkSelectEnabled = !this.bulkSelectEnabled;
+ this.clearAll();
+ }
+
+ // Backward-compatible alias used by existing components
+ clear() {
+ this.clearAll();
+ }
+
isSelected(post) {
return this.selected.some(
(p) => this.getPostId(p) === this.getPostId(post)
diff --git a/frontend/discourse/app/templates/full-page-search.gjs b/frontend/discourse/app/templates/full-page-search.gjs
index ee36337bca3..8fefee5b127 100644
--- a/frontend/discourse/app/templates/full-page-search.gjs
+++ b/frontend/discourse/app/templates/full-page-search.gjs
@@ -8,9 +8,9 @@ import GoogleSearch from "discourse/components/google-search";
import LoadMore from "discourse/components/load-more";
import PluginOutlet from "discourse/components/plugin-outlet";
import SearchAdvancedOptions from "discourse/components/search-advanced-options";
+import SearchBulkSelectDropdown from "discourse/components/search-bulk-select-dropdown";
import SearchResultEntries from "discourse/components/search-result-entries";
import SearchTextField from "discourse/components/search-text-field";
-import TopicBulkSelectDropdown from "discourse/components/topic-list/topic-bulk-select-dropdown";
import UserLink from "discourse/components/user-link";
import avatar from "discourse/helpers/avatar";
import bodyClass from "discourse/helpers/body-class";
@@ -155,7 +155,7 @@ export default <template>
class="btn-default bulk-select"
/>
{{#if @controller.bulkSelectHelper.selected}}
- <TopicBulkSelectDropdown
+ <SearchBulkSelectDropdown
@bulkSelectHelper={{@controller.bulkSelectHelper}}
@afterBulkActionComplete={{@controller.afterBulkActionComplete}}
/>
diff --git a/frontend/discourse/tests/integration/components/bulk-select-topics-dropdown-test.gjs b/frontend/discourse/tests/integration/components/bulk-select-topics-dropdown-test.gjs
index 6720fce80c9..e3392ab1cde 100644
--- a/frontend/discourse/tests/integration/components/bulk-select-topics-dropdown-test.gjs
+++ b/frontend/discourse/tests/integration/components/bulk-select-topics-dropdown-test.gjs
@@ -243,4 +243,68 @@ module("Integration | Component | BulkSelectTopicsDropdown", function (hooks) {
.dom(".fk-d-menu__inner-content .dropdown-menu__item .archive-topics")
.doesNotExist();
});
+
+ test("supports excluding built-in buttons and handling custom actions", async function (assert) {
+ this.currentUser.admin = true;
+ this.bulkSelectHelper = createBulkSelectHelper(this);
+ this.extraButtons = [
+ {
+ id: "custom-action",
+ icon: "trash-can",
+ name: "Custom Action",
+ visible: () => true,
+ },
+ ];
+ this.excludedButtonIds = ["delete-topics"];
+ this.onAction = (actionId) => assert.step(actionId);
+
+ await render(
+ <template>
+ <BulkSelectTopicsDropdown
+ @bulkSelectHelper={{this.bulkSelectHelper}}
+ @extraButtons={{this.extraButtons}}
+ @excludedButtonIds={{this.excludedButtonIds}}
+ @onAction={{this.onAction}}
+ />
+ </template>
+ );
+
+ await click(".bulk-select-topics-dropdown-trigger");
+ assert
+ .dom(".fk-d-menu__inner-content .dropdown-menu__item .delete-topics")
+ .doesNotExist();
+ await click(
+ ".fk-d-menu__inner-content .dropdown-menu__item .custom-action"
+ );
+ assert.verifySteps(["custom-action"]);
+ });
+
+ test("allows overriding built-in delete label with extra buttons", async function (assert) {
+ this.currentUser.admin = true;
+ this.bulkSelectHelper = createBulkSelectHelper(this);
+ this.extraButtons = [
+ {
+ id: "delete-topics",
+ icon: "trash-can",
+ name: "Delete Topics (override)",
+ visible: () => true,
+ },
+ ];
+ this.excludedButtonIds = ["delete-topics"];
+
+ await render(
+ <template>
+ <BulkSelectTopicsDropdown
+ @bulkSelectHelper={{this.bulkSelectHelper}}
+ @extraButtons={{this.extraButtons}}
+ @excludedButtonIds={{this.excludedButtonIds}}
+ />
+ </template>
+ );
+
+ await click(".bulk-select-topics-dropdown-trigger");
+ assert
+ .dom(".fk-d-menu__inner-content .dropdown-menu__item .delete-topics")
+ .hasTextContaining("Delete Topics (override)");
+ });
});
diff --git a/lib/search.rb b/lib/search.rb
index 59194946922..67c56a53fbd 100644
--- a/lib/search.rb
+++ b/lib/search.rb
@@ -472,6 +472,8 @@ class Search
advanced_filter(/\Ain:first|^f\z/i) { |posts| posts.where("posts.post_number = 1") }
+ advanced_filter(/\Ain:replies\z/i) { |posts| posts.where("posts.post_number > 1") }
+
advanced_filter(/\Ain:pinned\z/i) { |posts| posts.where.not(topics: { pinned_at: nil }) }
advanced_filter(/\Ain:wiki\z/i) { |posts, match| posts.where(wiki: true) }
@@ -984,6 +986,9 @@ class Search
elsif word =~ /\Ain:all\z/i
@search_all_topics = true
nil
+ elsif word =~ /\Ain:all-posts\z/i
+ @all_posts = true
+ nil
elsif word =~ /\Ain:personal\z/i
@search_pms = true
nil
@@ -1542,6 +1547,9 @@ class Search
@search_context.id,
)
+ posts.each { |post| @results.add(post) }
+ elsif @all_posts
+ posts = posts_scope(posts_eager_loads(posts_query(limit)))
posts.each { |post| @results.add(post) }
else
aggregate_search
diff --git a/spec/lib/search_spec.rb b/spec/lib/search_spec.rb
index 502be6c91e9..ed365fb4ec6 100644
--- a/spec/lib/search_spec.rb
+++ b/spec/lib/search_spec.rb
@@ -1026,6 +1026,22 @@ RSpec.describe Search do
expect(results.posts.map(&:id)).to eq([post.id, post3.id])
end
+ it "returns multiple posts per topic when using in:all-posts" do
+ post1 = Fabricate(:post, topic: topic, raw: "this is a zebra post")
+ post2 = Fabricate(:post, topic: topic, raw: "zebra zebra playing")
+ post3 = Fabricate(:post, topic: topic, raw: "another zebra mention")
+ post4 = Fabricate(:post, raw: "this is a zebra in another topic")
+
+ results = Search.execute("zebra")
+ expect(results.posts.map(&:id)).to contain_exactly(post1.id, post4.id)
+
+ results = Search.execute("zebra in:all-posts")
+ expect(results.posts.map(&:id)).to contain_exactly(post1.id, post2.id, post3.id, post4.id)
+
+ results = Search.execute("zebra IN:ALL-POSTS")
+ expect(results.posts.map(&:id)).to contain_exactly(post1.id, post2.id, post3.id, post4.id)
+ end
+
it "is able to search with an offset when configured" do
post_1 = Fabricate(:post, raw: "this is a play post")
SiteSetting.search_recent_regular_posts_offset_post_id = post_1.id + 1
@@ -2082,21 +2098,27 @@ RSpec.describe Search do
).to contain_exactly(post_1.id)
end
- it "supports in:first, user:, @username" do
+ it "supports in:first, in:replies, user:, @username" do
post_1 = Fabricate(:post, raw: "hi this is a test 123 123", topic: topic)
post_2 = Fabricate(:post, raw: "boom boom shake the room test", topic: topic)
expect(Search.execute("test in:first").posts).to contain_exactly(post_1)
expect(Search.execute("test IN:FIRST").posts).to contain_exactly(post_1)
+ expect(Search.execute("test in:replies").posts).to contain_exactly(post_2)
+
expect(Search.execute("boom").posts).to contain_exactly(post_2)
expect(Search.execute("boom in:first").posts).to eq([])
expect(Search.execute("boom f").posts).to eq([])
+ expect(Search.execute("boom in:replies").posts).to contain_exactly(post_2)
+
expect(Search.execute("123 in:first").posts).to contain_exactly(post_1)
expect(Search.execute("123 f").posts).to contain_exactly(post_1)
+ expect(Search.execute("123 in:replies").posts).to eq([])
+
expect(Search.execute("user:nobody").posts).to eq([])
expect(Search.execute("user:#{post_1.user.username}").posts).to contain_exactly(post_1)
expect(Search.execute("user:#{post_1.user_id}").posts).to contain_exactly(post_1)
diff --git a/spec/system/search_spec.rb b/spec/system/search_spec.rb
index 09b24c389db..589350a718b 100644
--- a/spec/system/search_spec.rb
+++ b/spec/system/search_spec.rb
@@ -3,6 +3,7 @@
describe "Search", type: :system do
let(:search_page) { PageObjects::Pages::Search.new }
fab!(:topic)
+ fab!(:op) { Fabricate(:post, topic: topic) }
fab!(:post) { Fabricate(:post, topic: topic, raw: "This is a test post in a test topic") }
fab!(:topic2) { Fabricate(:topic, title: "Another test topic") }
fab!(:post2) { Fabricate(:post, topic: topic2, raw: "This is another test post in a test topic") }
@@ -241,6 +242,7 @@ describe "Search", type: :system do
before do
SearchIndexer.enable
SearchIndexer.index(topic, force: true)
+ SearchIndexer.index(post, force: true)
SearchIndexer.index(topic2, force: true)
Fabricate(:theme_site_setting_with_service, name: "enable_welcome_banner", value: false)
sign_in(admin)
@@ -265,6 +267,23 @@ describe "Search", type: :system do
find(".fps-result .fps-topic[data-topic-id=\"#{topic.id}\"] .discourse-tags"),
).to have_content(tag1.name)
end
+
+ it "allows the user to delete posts in bulk" do
+ visit("/search?q=This%20is%20a%20test%20post")
+ expect(page).to have_content(post.raw)
+
+ find(".search-info .bulk-select").click
+ find(".fps-result .fps-topic[data-topic-id=\"#{topic.id}\"] .bulk-select input").click
+ find(".search-info .bulk-select-topics-dropdown-trigger").click
+
+ find(".bulk-select-topics-dropdown-content .delete-posts").click
+
+ find(".dialog-content")
+ click_button "OK"
+
+ expect(page).to have_no_content(post.raw)
+ expect(Post.with_deleted.find_by(id: post.id).deleted_at).to be_present
+ end
end
describe "Private Message Icon in Search Results" do