user_groups: Add `is_empty_group` function to check for nobody group.

Removes hardcoded checks for "role:nobody" by checking if the given group
is empty.
This commit is contained in:
Vector73 2024-07-11 06:33:54 +05:30 committed by Tim Abbott
parent c40698b6b3
commit 28ce2684bb
4 changed files with 34 additions and 7 deletions

View File

@ -259,8 +259,7 @@ function get_options_for_recipient_widget(): Option[] {
name: $t({defaultMessage: "Direct message"}),
};
const {name} = user_groups.get_user_group_from_id(realm.realm_direct_message_permission_group);
if (name !== "role:nobody") {
if (!user_groups.is_empty_group(realm.realm_direct_message_permission_group)) {
options.unshift(direct_messages_option);
} else {
options.push(direct_messages_option);

View File

@ -114,10 +114,7 @@ export function needs_subscribe_warning(user_id: number, stream_id: number): boo
export function check_dm_permissions_and_get_error_string(user_ids_string: string): string {
if (!people.user_can_direct_message(user_ids_string)) {
const {name} = user_groups.get_user_group_from_id(
realm.realm_direct_message_permission_group,
);
if (name === "role:nobody") {
if (user_groups.is_empty_group(realm.realm_direct_message_permission_group)) {
return $t({
defaultMessage: "Direct messages are disabled in this organization.",
});

View File

@ -370,7 +370,7 @@ function set_create_web_public_stream_dropdown_visibility() {
}
export function check_disable_direct_message_initiator_group_dropdown(current_value) {
if (current_value === user_groups.get_user_group_from_name("role:nobody").id) {
if (user_groups.is_empty_group(current_value)) {
$("#realm_direct_message_initiator_group_widget").prop("disabled", true);
} else {
$("#realm_direct_message_initiator_group_widget").prop("disabled", false);

View File

@ -178,6 +178,37 @@ export function is_user_group(
return item.members !== undefined;
}
export function is_empty_group(user_group_id: number): boolean {
const user_group = user_group_by_id_dict.get(user_group_id);
if (user_group === undefined) {
blueslip.error("Could not find user group", {user_group_id});
return false;
}
if (user_group.members.size > 0) {
return false;
}
// Check if all the recursive subgroups are empty.
// 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(user_group.direct_subgroup_ids);
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", {subgroup_id});
return false;
}
if (subgroup.members.size > 0) {
return false;
}
for (const direct_subgroup_id of subgroup.direct_subgroup_ids) {
subgroup_ids.add(direct_subgroup_id);
}
}
return true;
}
export function get_user_groups_of_user(user_id: number): UserGroup[] {
const user_groups_realm = get_realm_user_groups();
const groups_of_user = user_groups_realm.filter((group) =>