Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

user_groups: Add get_recursive_subgroups function in frontend.

Sahil Batra41880759cf76
diff --git a/frontend_tests/node_tests/user_groups.js b/frontend_tests/node_tests/user_groups.js
index 12591d2402e..054d4b169a0 100644
--- a/frontend_tests/node_tests/user_groups.js
+++ b/frontend_tests/node_tests/user_groups.js
@@ -127,3 +127,59 @@ run_test("user_groups", () => {
user_groups.add_subgroups(-9999);
user_groups.remove_subgroups(-9999);
});
+
+run_test("get_recursive_subgroups", () => {
+ const admins = {
+ name: "Admins",
+ description: "foo",
+ id: 1,
+ members: new Set([1]),
+ is_system_group: false,
+ subgroups: new Set([4]),
+ };
+ const all = {
+ name: "Everyone",
+ id: 2,
+ members: new Set([2, 3]),
+ is_system_group: false,
+ subgroups: new Set([1, 3]),
+ };
+ const test = {
+ name: "Test",
+ id: 3,
+ members: new Set([3, 4, 5]),
+ is_system_group: false,
+ subgroups: new Set([2]),
+ };
+ const foo = {
+ name: "Foo",
+ id: 4,
+ members: new Set([6, 7]),
+ is_system_group: false,
+ subgroups: new Set([]),
+ };
+
+ user_groups.add(admins);
+ user_groups.add(all);
+ user_groups.add(test);
+ user_groups.add(foo);
+
+ // This test setup has a state that won't appear in real data: Groups 2 and 3
+ // each contain the other. We test this corner case because it is a simple way
+ // to verify whether our algorithm correctly avoids visiting groups multiple times
+ // 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);
+
+ user_groups.add_subgroups(foo.id, [9999]);
+ 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);
+});
diff --git a/static/js/user_groups.ts b/static/js/user_groups.ts
index 7d43bc9ab23..0b597e0e8e7 100644
--- a/static/js/user_groups.ts
+++ b/static/js/user_groups.ts
@@ -153,3 +153,28 @@ 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;
+ }
+
+ // 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.
+ const subgroup_ids = new Set(target_user_group.subgroups);
+ for (const subgroup_id of subgroup_ids) {
+ const subgroup = user_group_by_id_dict.get(subgroup_id);
+ if (subgroup === undefined) {
+ blueslip.error(`Could not find subgroup with ID ${subgroup_id}`);
+ return undefined;
+ }
+
+ for (const direct_subgroup_id of subgroup.subgroups) {
+ subgroup_ids.add(direct_subgroup_id);
+ }
+ }
+ return subgroup_ids;
+}