user_groups: Add get_recursive_group_members.

This function goes through all subgroups recursively and returns the
resultant set of the members of those subgroups in addition to the
members of our target_group.

This function is required in order to add the `everyone` pill to create
channel flow.

For the tests written in this commit, it uses the same pattern as the
`get_recursive_subgroups` test.

`is_user_in_group` could have been technically refactored to use
`get_recursive_group_members`, but since the former returns early for
direct members, I've let it be for now.
This commit is contained in:
Shubham Padia
2024-07-08 15:34:16 +00:00
committed by Tim Abbott
parent c7e59ed761
commit 9f7dc596f8
2 changed files with 79 additions and 0 deletions

View File

@@ -1,3 +1,4 @@
import assert from "minimalistic-assert";
import type {z} from "zod";
import * as blueslip from "./blueslip";
@@ -204,6 +205,24 @@ export function get_recursive_subgroups(target_user_group: UserGroup): Set<numbe
return subgroup_ids;
}
export function get_recursive_group_members(target_user_group: UserGroup): Set<number> {
const members = new Set(target_user_group.members);
const subgroup_ids = get_recursive_subgroups(target_user_group);
if (subgroup_ids === undefined) {
return members;
}
for (const subgroup_id of subgroup_ids) {
const subgroup = user_group_by_id_dict.get(subgroup_id);
assert(subgroup !== undefined);
for (const member of subgroup.members) {
members.add(member);
}
}
return members;
}
export function is_user_in_group(user_group_id: number, user_id: number): boolean {
const user_group = user_group_by_id_dict.get(user_group_id);
if (user_group === undefined) {