people: Update get_mention_syntax to better account for wildcards.

In `composebox_typeahead`, it is completely possible to call
`people.get_mention_syntax` for a wildcard mention, which will not
have a `user_id`.

Updates `get_mention_syntax` to not raise a blueslip error if the
mention syntax is for a wildcard mention.

Adds "topic" to the array of possible wildcard mention strings.

Tightens the types for the parameters for `get_mention_syntax`, by
setting `user_id` to be number or undefined, and by setting `silent`
to default to false (which is expected in a number of modules where
this function is called).
This commit is contained in:
Lauryn Menard
2024-04-03 17:38:44 +02:00
committed by Tim Abbott
parent 6b31823c23
commit 48a486a584
2 changed files with 14 additions and 9 deletions

View File

@@ -1353,7 +1353,7 @@ export function is_duplicate_full_name(full_name: string): boolean {
return ids !== undefined && ids.size > 1;
}
export function get_mention_syntax(full_name: string, user_id: number, silent: boolean): string {
export function get_mention_syntax(full_name: string, user_id?: number, silent = false): string {
let mention = "";
if (silent) {
mention += "@_**";
@@ -1361,13 +1361,11 @@ export function get_mention_syntax(full_name: string, user_id: number, silent: b
mention += "@**";
}
mention += full_name;
if (!user_id) {
const wildcard_match = full_name_matches_wildcard_mention(full_name);
if (user_id === undefined && !wildcard_match) {
blueslip.warn("get_mention_syntax called without user_id.");
}
if (
(is_duplicate_full_name(full_name) || full_name_matches_wildcard_mention(full_name)) &&
user_id
) {
if ((is_duplicate_full_name(full_name) || wildcard_match) && user_id !== undefined) {
mention += `|${user_id}`;
}
mention += "**";
@@ -1375,7 +1373,7 @@ export function get_mention_syntax(full_name: string, user_id: number, silent: b
}
function full_name_matches_wildcard_mention(full_name: string): boolean {
return ["all", "everyone", "stream"].includes(full_name);
return ["all", "everyone", "stream", "topic"].includes(full_name);
}
export function _add_user(person: User): void {

View File

@@ -1150,6 +1150,12 @@ test_people("track_duplicate_full_names", () => {
});
test_people("get_mention_syntax", () => {
// blueslip warning is not raised for wildcard mentions without a user_id
assert.equal(people.get_mention_syntax("all"), "@**all**");
assert.equal(people.get_mention_syntax("everyone", undefined, true), "@_**everyone**");
assert.equal(people.get_mention_syntax("stream"), "@**stream**");
assert.equal(people.get_mention_syntax("topic"), "@**topic**");
people.add_active_user(stephen1);
people.add_active_user(stephen2);
people.add_active_user(maria);
@@ -1161,7 +1167,7 @@ test_people("get_mention_syntax", () => {
blueslip.reset();
assert.equal(people.get_mention_syntax("Stephen King", 601), "@**Stephen King|601**");
assert.equal(people.get_mention_syntax("Stephen King", 602), "@**Stephen King|602**");
assert.equal(people.get_mention_syntax("Stephen King", 602, true), "@_**Stephen King|602**");
assert.equal(people.get_mention_syntax("Maria Athens", 603), "@**Maria Athens**");
// Following tests handle a special case when `full_name` matches with a wildcard.
@@ -1172,7 +1178,8 @@ test_people("get_mention_syntax", () => {
assert.equal(people.get_mention_syntax("all", 1202), "@**all|1202**");
people.add_active_user(all2);
assert.equal(people.get_mention_syntax("all", 1203), "@**all|1203**");
assert.ok(people.is_duplicate_full_name("all"));
assert.equal(people.get_mention_syntax("all", 1203, true), "@_**all|1203**");
});
test_people("initialize", () => {