Skip to content

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

UX: Prioritize chat search results by relevance (#36959)

Régis Hanol5834fcca5077
diff --git a/plugins/chat/assets/javascripts/discourse/components/chat/message-creator/lib/chatables-loader.js b/plugins/chat/assets/javascripts/discourse/components/chat/message-creator/lib/chatables-loader.js
index 39aaa4852be..fca38ec76b9 100644
--- a/plugins/chat/assets/javascripts/discourse/components/chat/message-creator/lib/chatables-loader.js
+++ b/plugins/chat/assets/javascripts/discourse/components/chat/message-creator/lib/chatables-loader.js
@@ -7,6 +7,69 @@ import ChatChatable from "discourse/plugins/chat/discourse/models/chat-chatable"
const MAX_RESULTS = 10;
+const MATCH_PRIORITY = {
+ EXACT: 0,
+ PREFIX: 1,
+ PARTIAL: 2,
+};
+
+const TYPE_PRIORITY = {
+ CHANNEL: 0,
+ ENABLED: 1,
+ DISABLED: 2,
+};
+
+function getChatatableName(chatable) {
+ switch (chatable.type) {
+ case "user":
+ return chatable.model.username?.toLowerCase();
+ case "group":
+ return chatable.model.name?.toLowerCase();
+ case "channel":
+ return (
+ chatable.model.slug?.toLowerCase() ||
+ chatable.model.title?.toLowerCase()
+ );
+ default:
+ return "";
+ }
+}
+
+function matchPriority(chatable, term) {
+ const name = getChatatableName(chatable);
+ const lowerTerm = term.toLowerCase();
+
+ if (name === lowerTerm) {
+ return MATCH_PRIORITY.EXACT;
+ }
+ if (name.startsWith(lowerTerm)) {
+ return MATCH_PRIORITY.PREFIX;
+ }
+ return MATCH_PRIORITY.PARTIAL;
+}
+
+function typePriority(chatable) {
+ if (chatable.type === "channel") {
+ return TYPE_PRIORITY.CHANNEL;
+ }
+ if (chatable.enabled) {
+ return TYPE_PRIORITY.ENABLED;
+ }
+ return TYPE_PRIORITY.DISABLED;
+}
+
+export function sortChatables(chatables, term) {
+ return chatables.sort((a, b) => {
+ const matchA = matchPriority(a, term);
+ const matchB = matchPriority(b, term);
+ if (matchA !== matchB) {
+ return matchA - matchB;
+ }
+
+ return typePriority(a) - typePriority(b);
+ });
+}
+
export default class ChatablesLoader {
@service chatChannelsManager;
@service loadingSlider;
@@ -23,7 +86,6 @@ export default class ChatablesLoader {
includeGroups: true,
includeCategoryChannels: true,
includeDirectMessageChannels: true,
- excludedUserIds: null,
preloadChannels: false,
}
) {
@@ -51,25 +113,25 @@ export default class ChatablesLoader {
const results = await this.request;
this.loadingSlider.transitionEnded();
- return [
+ const chatables = [
...results.users,
...results.groups,
...results.direct_message_channels,
...results.category_channels,
- ]
- .map((item) => {
- const chatable = ChatChatable.create(item);
- const channel = this.#findChannel(chatable);
-
- if (channel) {
- chatable.tracking = channel.tracking;
- chatable.unread_thread_count =
- channel.unreadThreadsCountSinceLastViewed;
- }
-
- return chatable;
- })
- .slice(0, MAX_RESULTS);
+ ].map((item) => {
+ const chatable = ChatChatable.create(item);
+ const channel = this.#findChannel(chatable);
+
+ if (channel) {
+ chatable.tracking = channel.tracking;
+ chatable.unread_thread_count =
+ channel.unreadThreadsCountSinceLastViewed;
+ }
+
+ return chatable;
+ });
+
+ return sortChatables(chatables, term).slice(0, MAX_RESULTS);
} catch (e) {
popupAjaxError(e);
}
diff --git a/plugins/chat/test/javascripts/unit/lib/chatables-loader-test.js b/plugins/chat/test/javascripts/unit/lib/chatables-loader-test.js
new file mode 100644
index 00000000000..cb9ee574803
--- /dev/null
+++ b/plugins/chat/test/javascripts/unit/lib/chatables-loader-test.js
@@ -0,0 +1,146 @@
+import { module, test } from "qunit";
+import { sortChatables } from "discourse/plugins/chat/discourse/components/chat/message-creator/lib/chatables-loader";
+import ChatChatable from "discourse/plugins/chat/discourse/models/chat-chatable";
+
+module("Discourse Chat | Unit | lib | chatables-loader", function () {
+ function createUserChatable(username, has_chat_enabled = true) {
+ return ChatChatable.create({
+ type: "user",
+ identifier: `u-${username}`,
+ model: { id: Math.random(), username, has_chat_enabled },
+ });
+ }
+
+ function createGroupChatable(name, can_chat = true) {
+ return ChatChatable.create({
+ type: "group",
+ identifier: `g-${name}`,
+ model: { id: Math.random(), name, can_chat },
+ });
+ }
+
+ function createChannelChatable(title, slug = null) {
+ return ChatChatable.create({
+ type: "channel",
+ identifier: `c-${title}`,
+ model: { id: Math.random(), title, slug: slug || title.toLowerCase() },
+ });
+ }
+
+ test("exact matches come first", function (assert) {
+ const chatables = [
+ createUserChatable("developer"),
+ createUserChatable("dev"),
+ createUserChatable("devops"),
+ ];
+
+ const sorted = sortChatables(chatables, "dev");
+ assert.strictEqual(sorted[0].model.username, "dev");
+ });
+
+ test("channels come before users with same match quality", function (assert) {
+ const chatables = [
+ createUserChatable("dev"),
+ createChannelChatable("Dev", "dev"),
+ ];
+
+ const sorted = sortChatables(chatables, "dev");
+ assert.strictEqual(sorted[0].type, "channel");
+ assert.strictEqual(sorted[1].type, "user");
+ });
+
+ test("enabled users come before disabled users", function (assert) {
+ const chatables = [
+ createUserChatable("developer", false),
+ createUserChatable("devops", true),
+ ];
+
+ const sorted = sortChatables(chatables, "dev");
+ assert.strictEqual(sorted[0].model.username, "devops");
+ assert.strictEqual(sorted[1].model.username, "developer");
+ });
+
+ test("starts-with matches come before contains matches", function (assert) {
+ const chatables = [
+ createUserChatable("mydev"),
+ createUserChatable("developer"),
+ ];
+
+ const sorted = sortChatables(chatables, "dev");
+ assert.strictEqual(sorted[0].model.username, "developer");
+ assert.strictEqual(sorted[1].model.username, "mydev");
+ });
+
+ test("full prioritization order", function (assert) {
+ const chatables = [
+ createUserChatable("developer", false),
+ createUserChatable("devhelper", true),
+ createChannelChatable("Dev Talk", "dev-talk"),
+ createUserChatable("dev", true),
+ createChannelChatable("Dev", "dev"),
+ createGroupChatable("developers", true),
+ createUserChatable("superdev", false),
+ ];
+
+ const sorted = sortChatables(chatables, "dev");
+
+ const firstChannelName = sorted[0].model.slug ?? sorted[0].model.title;
+ assert.strictEqual(firstChannelName, "dev", "exact match channel first");
+ assert.strictEqual(
+ sorted[1].model.username,
+ "dev",
+ "exact match enabled user second"
+ );
+ assert.strictEqual(sorted[2].type, "channel", "starts-with channel third");
+ assert.strictEqual(
+ sorted[3].model.username,
+ "devhelper",
+ "starts-with enabled user"
+ );
+ assert.strictEqual(
+ sorted[4].model.name,
+ "developers",
+ "starts-with enabled group"
+ );
+ assert.strictEqual(
+ sorted[5].model.username,
+ "developer",
+ "starts-with disabled user"
+ );
+ assert.strictEqual(
+ sorted[6].model.username,
+ "superdev",
+ "contains disabled user last"
+ );
+ });
+
+ test("case insensitive matching", function (assert) {
+ const chatables = [
+ createUserChatable("DEV"),
+ createUserChatable("Dev"),
+ createUserChatable("dev"),
+ ];
+
+ const sorted = sortChatables(chatables, "DEV");
+
+ assert.strictEqual(sorted.length, 3);
+ sorted.forEach((chatable) => {
+ assert.strictEqual(
+ chatable.model.username.toLowerCase(),
+ "dev",
+ `${chatable.model.username} should match`
+ );
+ });
+ });
+
+ test("disabled groups come after enabled users", function (assert) {
+ const chatables = [
+ createGroupChatable("devs", false),
+ createUserChatable("devs", true),
+ ];
+
+ const sorted = sortChatables(chatables, "devs");
+ assert.strictEqual(sorted[0].type, "user");
+ assert.strictEqual(sorted[1].type, "group");
+ });
+});