user_groups: Pass user group object instead of id to get_recursive_subgroups.

We directly pass the user group object to get_recursive_subgroups as we
already have the object in the caller. We can add separate function which
will accept id as parameter in the future if required.
This commit is contained in:
Sahil Batra 2022-05-17 19:47:04 +05:30 committed by Tim Abbott
parent 6b82cbe0a6
commit f38ab85f02
2 changed files with 9 additions and 17 deletions

View File

@ -173,18 +173,16 @@ run_test("get_recursive_subgroups", () => {
// when determining recursive subgroups.
// A test case that can occur in practice and would be problematic without this
// optimization is a tree where each layer connects to every node in the next layer.
assert.deepEqual(user_groups.get_recursive_subgroups(admins.id), new Set([4]));
assert.deepEqual(user_groups.get_recursive_subgroups(all.id), new Set([4, 1, 2, 3]));
assert.deepEqual(user_groups.get_recursive_subgroups(test.id), new Set([2, 4, 1, 3]));
assert.deepEqual(user_groups.get_recursive_subgroups(foo.id), new Set([]));
blueslip.expect("error", "Could not find user group with ID 1111");
assert.deepEqual(user_groups.get_recursive_subgroups(1111), undefined);
assert.deepEqual(user_groups.get_recursive_subgroups(admins), new Set([4]));
assert.deepEqual(user_groups.get_recursive_subgroups(all), new Set([4, 1, 2, 3]));
assert.deepEqual(user_groups.get_recursive_subgroups(test), new Set([2, 4, 1, 3]));
assert.deepEqual(user_groups.get_recursive_subgroups(foo), new Set([]));
user_groups.add_subgroups(foo.id, [9999]);
const foo_group = user_groups.get_user_group_from_id(foo.id);
blueslip.expect("error", "Could not find subgroup with ID 9999", 2);
assert.deepEqual(user_groups.get_recursive_subgroups(foo.id), undefined);
assert.deepEqual(user_groups.get_recursive_subgroups(test.id), undefined);
assert.deepEqual(user_groups.get_recursive_subgroups(foo_group), undefined);
assert.deepEqual(user_groups.get_recursive_subgroups(test), undefined);
});
run_test("is_user_in_group", () => {

View File

@ -154,13 +154,7 @@ export function get_user_groups_of_user(user_id: number): UserGroup[] {
return groups_of_user;
}
export function get_recursive_subgroups(target_group_id: number): Set<number> | undefined {
const target_user_group = user_group_by_id_dict.get(target_group_id);
if (target_user_group === undefined) {
blueslip.error(`Could not find user group with ID ${target_group_id}`);
return undefined;
}
export function get_recursive_subgroups(target_user_group: UserGroup): Set<number> | undefined {
// Correctness of this algorithm relying on the ES6 Set
// implementation having the property that a `for of` loop will
// visit all items that are added to the set during the loop.
@ -189,7 +183,7 @@ export function is_user_in_group(user_group_id: number, user_id: number): boolea
return true;
}
const subgroup_ids = get_recursive_subgroups(user_group_id);
const subgroup_ids = get_recursive_subgroups(user_group);
if (subgroup_ids === undefined) {
return false;
}