From ea4f7fb660cb4cb5cba3763e9a28352123d39591 Mon Sep 17 00:00:00 2001 From: Roman Rizzi Date: Fri, 31 Mar 2023 15:13:24 -0300 Subject: [PATCH] FEATURE: Experimental API for custom full-page search types. (#20915) This change adds an experimental API tagged as "Do not use", only intended to conduct a PoC to test semantic search in the AI plugin. --- .../app/controllers/full-page-search.js | 36 +++++++++++++++++-- .../discourse/app/lib/plugin-api.js | 13 +++++++ .../app/templates/full-page-search.hbs | 2 +- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/discourse/app/controllers/full-page-search.js b/app/assets/javascripts/discourse/app/controllers/full-page-search.js index 59157ac2646..e27ce685fb8 100644 --- a/app/assets/javascripts/discourse/app/controllers/full-page-search.js +++ b/app/assets/javascripts/discourse/app/controllers/full-page-search.js @@ -37,6 +37,16 @@ export const SEARCH_TYPE_USERS = "users"; const PAGE_LIMIT = 10; +const customSearchTypes = []; + +export function registerFullPageSearchType( + translationKey, + searchTypeId, + searchFunc +) { + customSearchTypes.push({ translationKey, searchTypeId, searchFunc }); +} + export default Controller.extend({ application: controller(), composer: controller(), @@ -68,7 +78,7 @@ export default Controller.extend({ init() { this._super(...arguments); - this.set("searchTypes", [ + const searchTypes = [ { name: I18n.t("search.type.default"), id: SEARCH_TYPE_DEFAULT }, { name: this.siteSettings.tagging_enabled @@ -77,7 +87,16 @@ export default Controller.extend({ id: SEARCH_TYPE_CATS_TAGS, }, { name: I18n.t("search.type.users"), id: SEARCH_TYPE_USERS }, - ]); + ]; + + customSearchTypes.forEach((type) => { + searchTypes.push({ + name: I18n.t(type.translationKey), + id: type.searchTypeId, + }); + }); + + this.set("searchTypes", searchTypes); }, @discourseComputed("resultCount") @@ -274,6 +293,13 @@ export default Controller.extend({ return searchType === SEARCH_TYPE_DEFAULT; }, + @discourseComputed("search_type") + customSearchType(searchType) { + return customSearchTypes.find( + (type) => searchType === type["searchTypeId"] + ); + }, + @discourseComputed("bulkSelectEnabled") searchInfoClassNames(bulkSelectEnabled) { return bulkSelectEnabled @@ -324,6 +350,12 @@ export default Controller.extend({ const searchKey = getSearchKey(args); + if (this.customSearchType) { + const customSearch = this.customSearchType["searchFunc"]; + customSearch(this, args, searchKey); + return; + } + switch (this.search_type) { case SEARCH_TYPE_CATS_TAGS: const categoryTagSearch = searchCategoryTag( diff --git a/app/assets/javascripts/discourse/app/lib/plugin-api.js b/app/assets/javascripts/discourse/app/lib/plugin-api.js index 95f4da95eee..0d456f64d48 100644 --- a/app/assets/javascripts/discourse/app/lib/plugin-api.js +++ b/app/assets/javascripts/discourse/app/lib/plugin-api.js @@ -111,6 +111,7 @@ import { registerNotificationTypeRenderer } from "discourse/lib/notification-typ import { registerUserMenuTab } from "discourse/lib/user-menu/tab"; import { registerModelTransformer } from "discourse/lib/model-transformers"; import { registerCustomUserNavMessagesDropdownRow } from "discourse/controllers/user-private-messages"; +import { registerFullPageSearchType } from "discourse/controllers/full-page-search"; // If you add any methods to the API ensure you bump up the version number // based on Semantic Versioning 2.0.0. Please update the changelog at @@ -2124,6 +2125,18 @@ class PluginApi { addUserMessagesNavigationDropdownRow(routeName, name, icon) { registerCustomUserNavMessagesDropdownRow(routeName, name, icon); } + + /** + * EXPERIMENTAL. Do not use. + * Adds a new search type which can be selected when visiting the full page search UI. + * + * @param {string} translationKey + * @param {string} searchTypeId + * @param {function} searchFunc - Available arguments: fullPage controller, search args, searchKey. + */ + addFullPageSearchType(translationKey, searchTypeId, searchFunc) { + registerFullPageSearchType(translationKey, searchTypeId, searchFunc); + } } // from http://stackoverflow.com/questions/6832596/how-to-compare-software-version-number-using-js-only-number diff --git a/app/assets/javascripts/discourse/app/templates/full-page-search.hbs b/app/assets/javascripts/discourse/app/templates/full-page-search.hbs index 47e85ab567f..139017a085f 100644 --- a/app/assets/javascripts/discourse/app/templates/full-page-search.hbs +++ b/app/assets/javascripts/discourse/app/templates/full-page-search.hbs @@ -150,7 +150,7 @@ {{else}}
- {{#if this.usingDefaultSearchType}} + {{#if (or this.usingDefaultSearchType this.customSearchType)}}