composebox_typeahead: Hide user groups from mention typeahead.

This commit adds code to hide the user groups which a user is
not allowed to mention from the mention typeahead.

Fixes a part of #25927.
This commit is contained in:
Sahil Batra
2023-06-15 22:04:55 +05:30
committed by Tim Abbott
parent 767c2ebbe4
commit 632191b8c4
3 changed files with 52 additions and 10 deletions

View File

@@ -424,6 +424,7 @@ export function filter_and_sort_mentions(is_silent, query, opts) {
opts = {
want_broadcast: !is_silent,
filter_pills: false,
filter_groups: !is_silent,
...opts,
};
return get_person_suggestions(query, opts);
@@ -460,7 +461,12 @@ export function get_person_suggestions(query, opts) {
return persons.filter((item) => query_matches_person(query, item));
}
const groups = user_groups.get_realm_user_groups();
let groups;
if (opts.filter_groups) {
groups = user_groups.get_user_groups_allowed_to_mention();
} else {
groups = user_groups.get_realm_user_groups();
}
const filtered_groups = groups.filter((item) => query_matches_name(query, item));

View File

@@ -1,6 +1,7 @@
import * as blueslip from "./blueslip";
import {FoldDict} from "./fold_dict";
import * as group_permission_settings from "./group_permission_settings";
import {page_params} from "./page_params";
import * as settings_config from "./settings_config";
import type {User, UserGroupUpdateEvent} from "./types";
@@ -88,6 +89,21 @@ export function get_realm_user_groups(): UserGroup[] {
return user_groups.filter((group) => !group.is_system_group);
}
export function get_user_groups_allowed_to_mention(): UserGroup[] {
if (page_params.user_id === undefined) {
return [];
}
const user_groups = get_realm_user_groups();
return user_groups.filter((group) => {
const can_mention_group_id = group.can_mention_group_id;
return (
page_params.user_id !== undefined &&
is_user_in_group(can_mention_group_id, page_params.user_id)
);
});
}
export function is_direct_member_of(user_id: number, user_group_id: number): boolean {
const user_group = user_group_by_id_dict.get(user_group_id);
if (user_group === undefined) {

View File

@@ -7,7 +7,7 @@ const {mock_banners} = require("./lib/compose_banner");
const {mock_esm, set_global, with_overrides, zrequire} = require("./lib/namespace");
const {run_test} = require("./lib/test");
const $ = require("./lib/zjquery");
const {user_settings} = require("./lib/zpage_params");
const {page_params, user_settings} = require("./lib/zpage_params");
const noop = () => {};
@@ -324,7 +324,7 @@ const hamletcharacters = {
description: "Characters of Hamlet",
members: new Set([100, 104]),
is_system_group: false,
direct_subgroup_ids: new Set([10, 11]),
direct_subgroup_ids: new Set([]),
can_mention_group_id: 2,
};
@@ -332,9 +332,9 @@ const backend = {
name: "Backend",
id: 2,
description: "Backend team",
members: new Set([]),
members: new Set([101]),
is_system_group: false,
direct_subgroup_ids: new Set([]),
direct_subgroup_ids: new Set([1]),
can_mention_group_id: 1,
};
@@ -342,7 +342,7 @@ const call_center = {
name: "Call Center",
id: 3,
description: "folks working in support",
members: new Set([]),
members: new Set([102]),
is_system_group: false,
direct_subgroup_ids: new Set([]),
can_mention_group_id: 2,
@@ -1533,19 +1533,39 @@ test("content_highlighter", ({override_rewire}) => {
test("filter_and_sort_mentions (normal)", () => {
compose_state.set_message_type("stream");
const is_silent = false;
const suggestions = ct.filter_and_sort_mentions(is_silent, "al");
page_params.user_id = 101;
let suggestions = ct.filter_and_sort_mentions(is_silent, "al");
const mention_all = ct.broadcast_mentions()[0];
assert.deepEqual(suggestions, [mention_all, ali, alice, hal, call_center]);
// call_center group is shown in typeahead even when user is member of
// one of the subgroups of can_mention_group.
page_params.user_id = 104;
suggestions = ct.filter_and_sort_mentions(is_silent, "al");
assert.deepEqual(suggestions, [mention_all, ali, alice, hal, call_center]);
// call_center group is not shown in typeahead when user is neither
// a direct member of can_mention_group nor a member of any of its
// recursive subgroups.
page_params.user_id = 102;
suggestions = ct.filter_and_sort_mentions(is_silent, "al");
assert.deepEqual(suggestions, [mention_all, ali, alice, hal]);
});
test("filter_and_sort_mentions (silent)", () => {
const is_silent = true;
const suggestions = ct.filter_and_sort_mentions(is_silent, "al");
let suggestions = ct.filter_and_sort_mentions(is_silent, "al");
assert.deepEqual(suggestions, [ali, alice, hal, call_center]);
// call_center group is shown in typeahead irrespective of whether
// user is member of can_mention_group or its subgroups for a
// silent mention.
page_params.user_id = 102;
suggestions = ct.filter_and_sort_mentions(is_silent, "al");
assert.deepEqual(suggestions, [ali, alice, hal, call_center]);
});
test("typeahead_results", () => {
@@ -1654,7 +1674,7 @@ test("typeahead_results", () => {
// Earlier user group and stream mentions were autocompleted by their
// description too. This is now removed as it often led to unexpected
// behaviour, and did not have any great discoverability advantage.
page_params.user_id = 101;
// Autocomplete user group mentions by group name.
assert_mentions_matches("hamletchar", [hamletcharacters]);