settings: Add new module to handle pills for group settings.

This commit creates a new module similar to add_subscribers_pill
for handling the pill widget to be used for group settings.
This commit is contained in:
Sahil Batra 2024-08-29 19:32:17 +05:30 committed by Tim Abbott
parent acd578c769
commit 2fff124ec9
5 changed files with 124 additions and 2 deletions

View File

@ -114,6 +114,7 @@ EXEMPT_FILES = make_set(
"web/src/giphy.js",
"web/src/giphy_state.ts",
"web/src/global.ts",
"web/src/group_setting_pill.ts",
"web/src/hash_util.ts",
"web/src/hashchange.js",
"web/src/hbs.d.ts",

View File

@ -0,0 +1,104 @@
import assert from "minimalistic-assert";
import render_input_pill from "../templates/input_pill.hbs";
import * as group_permission_settings from "./group_permission_settings";
import * as input_pill from "./input_pill";
import type {InputPillConfig} from "./input_pill";
import type {GroupSettingPill, GroupSettingPillContainer} from "./typeahead_helper";
import * as user_group_pill from "./user_group_pill";
import type {UserGroupPill} from "./user_group_pill";
import * as user_groups from "./user_groups";
import * as user_pill from "./user_pill";
function check_group_allowed_for_setting(group_item: UserGroupPill, setting_name: string): boolean {
const group_setting_config = group_permission_settings.get_group_permission_setting_config(
setting_name,
"group",
);
assert(group_setting_config !== undefined);
const group = user_groups.get_user_group_from_id(group_item.group_id);
if (!group.is_system_group) {
if (group_setting_config.require_system_group) {
return false;
}
return true;
}
return user_groups.check_system_user_group_allowed_for_setting(
group.name,
group_setting_config,
true,
);
}
export function create_item_from_text(
text: string,
current_items: GroupSettingPill[],
pill_config?: InputPillConfig,
): GroupSettingPill | undefined {
const group_item = user_group_pill.create_item_from_group_name(text, current_items);
if (group_item) {
const setting_name = pill_config?.setting_name;
assert(setting_name !== undefined);
if (check_group_allowed_for_setting(group_item, setting_name)) {
return group_item;
}
return undefined;
}
return user_pill.create_item_from_email(text, current_items);
}
export function get_text_from_item(item: GroupSettingPill): string {
let text: string;
switch (item.type) {
case "user_group":
text = user_group_pill.get_group_name_from_item(item);
break;
case "user":
text = user_pill.get_email_from_item(item);
break;
}
return text;
}
export function get_display_value_from_item(item: GroupSettingPill): string {
if (item.type === "user_group") {
return user_group_pill.display_pill(user_groups.get_user_group_from_id(item.group_id));
}
assert(item.type === "user");
return user_pill.get_display_value_from_item(item);
}
export function generate_pill_html(item: GroupSettingPill): string {
if (item.type === "user_group") {
return render_input_pill({
display_value: get_display_value_from_item(item),
group_id: item.group_id,
});
}
assert(item.type === "user");
return user_pill.generate_pill_html(item);
}
export function create_pills(
$pill_container: JQuery,
setting_name: string,
): GroupSettingPillContainer {
const pill_widget = input_pill.create<GroupSettingPill>({
$container: $pill_container,
create_item_from_text,
get_text_from_item,
get_display_value_from_item,
generate_pill_html,
pill_config: {
setting_name,
},
});
return pill_widget;
}

View File

@ -13,6 +13,7 @@ import * as util from "./util";
export type InputPillConfig = {
exclude_inaccessible_users?: boolean;
setting_name?: string;
};
type InputPillCreateOptions<ItemType> = {

View File

@ -37,6 +37,9 @@ export type UserOrMentionPillData = UserOrMention & {
export type CombinedPill = StreamPill | UserGroupPill | UserPill;
export type CombinedPillContainer = InputPillContainer<CombinedPill>;
export type GroupSettingPill = UserGroupPill | UserPill;
export type GroupSettingPillContainer = InputPillContainer<GroupSettingPill>;
export function build_highlight_regex(query: string): RegExp {
const regex = new RegExp("(" + _.escapeRegExp(query) + ")", "ig");
return regex;

View File

@ -13,7 +13,7 @@ import type {
StateData,
user_group_schema,
} from "./state_data";
import {current_user} from "./state_data";
import {current_user, realm} from "./state_data";
import type {UserOrMention} from "./typeahead_helper";
import type {UserGroupUpdateEvent} from "./types";
@ -342,6 +342,7 @@ function get_display_name_for_system_group_option(setting_name: string, name: st
export function check_system_user_group_allowed_for_setting(
group_name: string,
group_setting_config: GroupPermissionSetting,
for_new_settings_ui = false,
): boolean {
const {
allow_internet_group,
@ -359,7 +360,7 @@ export function check_system_user_group_allowed_for_setting(
return false;
}
if (!allow_nobody_group && group_name === "role:nobody") {
if ((!allow_nobody_group || for_new_settings_ui) && group_name === "role:nobody") {
return false;
}
@ -371,6 +372,18 @@ export function check_system_user_group_allowed_for_setting(
return false;
}
if (
group_name === "role:fullmembers" &&
for_new_settings_ui &&
realm.realm_waiting_period_threshold === 0
) {
// We hide the full members group in the typeahead when
// there is no separation between member and full member
// users due to organization not having set a waiting
// period for member users to become full members.
return false;
}
return true;
}