mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	user_pill: Add support to not allow adding inaccessible users manually.
We previously allowed adding pills for inaccessible user by typing the email manually. This commit updates it to handle inaccessible users like we handle invalid emails.
This commit is contained in:
		@@ -12,6 +12,7 @@ export let widget: UserPillWidget;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
const pill_config: InputPillConfig = {
 | 
					const pill_config: InputPillConfig = {
 | 
				
			||||||
    show_user_status_emoji: true,
 | 
					    show_user_status_emoji: true,
 | 
				
			||||||
 | 
					    exclude_inaccessible_users: true,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function initialize_pill(): UserPillWidget {
 | 
					export function initialize_pill(): UserPillWidget {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -93,7 +93,10 @@ export function initialize_custom_user_type_fields(
 | 
				
			|||||||
            const $pill_container = $(element_id)
 | 
					            const $pill_container = $(element_id)
 | 
				
			||||||
                .find(`.custom_user_field[data-field-id="${CSS.escape(field.id)}"] .pill-container`)
 | 
					                .find(`.custom_user_field[data-field-id="${CSS.escape(field.id)}"] .pill-container`)
 | 
				
			||||||
                .expectOne();
 | 
					                .expectOne();
 | 
				
			||||||
            const pills = user_pill.create_pills($pill_container);
 | 
					            const pill_config = {
 | 
				
			||||||
 | 
					                exclude_inaccessible_users: is_editable,
 | 
				
			||||||
 | 
					            };
 | 
				
			||||||
 | 
					            const pills = user_pill.create_pills($pill_container, pill_config);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (field_value_raw) {
 | 
					            if (field_value_raw) {
 | 
				
			||||||
                const field_value = JSON.parse(field_value_raw);
 | 
					                const field_value = JSON.parse(field_value_raw);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -23,6 +23,7 @@ export type InputPillItem<T> = {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
export type InputPillConfig = {
 | 
					export type InputPillConfig = {
 | 
				
			||||||
    show_user_status_emoji?: boolean;
 | 
					    show_user_status_emoji?: boolean;
 | 
				
			||||||
 | 
					    exclude_inaccessible_users?: boolean;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type InputPillCreateOptions<T> = {
 | 
					type InputPillCreateOptions<T> = {
 | 
				
			||||||
@@ -31,6 +32,7 @@ type InputPillCreateOptions<T> = {
 | 
				
			|||||||
    create_item_from_text: (
 | 
					    create_item_from_text: (
 | 
				
			||||||
        text: string,
 | 
					        text: string,
 | 
				
			||||||
        existing_items: InputPillItem<T>[],
 | 
					        existing_items: InputPillItem<T>[],
 | 
				
			||||||
 | 
					        pill_config?: InputPillConfig | undefined,
 | 
				
			||||||
    ) => InputPillItem<T> | undefined;
 | 
					    ) => InputPillItem<T> | undefined;
 | 
				
			||||||
    get_text_from_item: (item: InputPillItem<T>) => string;
 | 
					    get_text_from_item: (item: InputPillItem<T>) => string;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
@@ -117,7 +119,7 @@ export function create<T>(opts: InputPillCreateOptions<T>): InputPillContainer<T
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        create_item(text: string) {
 | 
					        create_item(text: string) {
 | 
				
			||||||
            const existing_items = funcs.items();
 | 
					            const existing_items = funcs.items();
 | 
				
			||||||
            const item = store.create_item_from_text(text, existing_items);
 | 
					            const item = store.create_item_from_text(text, existing_items, store.pill_config);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (!item?.display_value) {
 | 
					            if (!item?.display_value) {
 | 
				
			||||||
                store.$input.addClass("shake");
 | 
					                store.$input.addClass("shake");
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -19,6 +19,7 @@ export type UserPillWidget = InputPillContainer<UserPill>;
 | 
				
			|||||||
export function create_item_from_email(
 | 
					export function create_item_from_email(
 | 
				
			||||||
    email: string,
 | 
					    email: string,
 | 
				
			||||||
    current_items: InputPillItem<UserPill>[],
 | 
					    current_items: InputPillItem<UserPill>[],
 | 
				
			||||||
 | 
					    pill_config?: InputPillConfig | undefined,
 | 
				
			||||||
): InputPillItem<UserPill> | undefined {
 | 
					): InputPillItem<UserPill> | undefined {
 | 
				
			||||||
    // For normal Zulip use, we need to validate the email for our realm.
 | 
					    // For normal Zulip use, we need to validate the email for our realm.
 | 
				
			||||||
    const user = people.get_by_email(email);
 | 
					    const user = people.get_by_email(email);
 | 
				
			||||||
@@ -45,6 +46,10 @@ export function create_item_from_email(
 | 
				
			|||||||
        return undefined;
 | 
					        return undefined;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (pill_config?.exclude_inaccessible_users && user.is_inaccessible_user) {
 | 
				
			||||||
 | 
					        return undefined;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const existing_ids = current_items.map((item) => item.user_id);
 | 
					    const existing_ids = current_items.map((item) => item.user_id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (existing_ids.includes(user.user_id)) {
 | 
					    if (existing_ids.includes(user.user_id)) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,7 +2,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
const {strict: assert} = require("assert");
 | 
					const {strict: assert} = require("assert");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const {zrequire} = require("./lib/namespace");
 | 
					const {mock_esm, zrequire} = require("./lib/namespace");
 | 
				
			||||||
const {run_test} = require("./lib/test");
 | 
					const {run_test} = require("./lib/test");
 | 
				
			||||||
const blueslip = require("./lib/zblueslip");
 | 
					const blueslip = require("./lib/zblueslip");
 | 
				
			||||||
const {page_params} = require("./lib/zpage_params");
 | 
					const {page_params} = require("./lib/zpage_params");
 | 
				
			||||||
@@ -10,6 +10,8 @@ const {page_params} = require("./lib/zpage_params");
 | 
				
			|||||||
const people = zrequire("people");
 | 
					const people = zrequire("people");
 | 
				
			||||||
const user_pill = zrequire("user_pill");
 | 
					const user_pill = zrequire("user_pill");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const settings_data = mock_esm("../src/settings_data");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const alice = {
 | 
					const alice = {
 | 
				
			||||||
    email: "alice@example.com",
 | 
					    email: "alice@example.com",
 | 
				
			||||||
    user_id: 99,
 | 
					    user_id: 99,
 | 
				
			||||||
@@ -40,6 +42,19 @@ const isaac_item = {
 | 
				
			|||||||
    should_add_guest_user_indicator: false,
 | 
					    should_add_guest_user_indicator: false,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const inaccessible_user_id = 103;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const inaccessible_user_item = {
 | 
				
			||||||
 | 
					    email: "user103@example.com",
 | 
				
			||||||
 | 
					    display_value: "translated: Unknown user",
 | 
				
			||||||
 | 
					    type: "user",
 | 
				
			||||||
 | 
					    user_id: inaccessible_user_id,
 | 
				
			||||||
 | 
					    deactivated: false,
 | 
				
			||||||
 | 
					    img_src: `http://zulip.zulipdev.com/avatar/${inaccessible_user_id}?s=50`,
 | 
				
			||||||
 | 
					    status_emoji_info: undefined,
 | 
				
			||||||
 | 
					    should_add_guest_user_indicator: false,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
let pill_widget = {};
 | 
					let pill_widget = {};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function test(label, f) {
 | 
					function test(label, f) {
 | 
				
			||||||
@@ -53,8 +68,8 @@ function test(label, f) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test("create_item", () => {
 | 
					test("create_item", () => {
 | 
				
			||||||
    function test_create_item(email, current_items, expected_item) {
 | 
					    function test_create_item(email, current_items, expected_item, pill_config) {
 | 
				
			||||||
        const item = user_pill.create_item_from_email(email, current_items);
 | 
					        const item = user_pill.create_item_from_email(email, current_items, pill_config);
 | 
				
			||||||
        assert.deepEqual(item, expected_item);
 | 
					        assert.deepEqual(item, expected_item);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -71,6 +86,15 @@ test("create_item", () => {
 | 
				
			|||||||
    test_create_item("bogus@example.com", [], undefined);
 | 
					    test_create_item("bogus@example.com", [], undefined);
 | 
				
			||||||
    test_create_item("isaac@example.com", [], isaac_item);
 | 
					    test_create_item("isaac@example.com", [], isaac_item);
 | 
				
			||||||
    test_create_item("isaac@example.com", [isaac_item], undefined);
 | 
					    test_create_item("isaac@example.com", [isaac_item], undefined);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    settings_data.user_can_access_all_other_users = () => false;
 | 
				
			||||||
 | 
					    page_params.realm_bot_domain = "example.com";
 | 
				
			||||||
 | 
					    people.add_inaccessible_user(inaccessible_user_id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    test_create_item("user103@example.com", [], undefined, {exclude_inaccessible_users: true});
 | 
				
			||||||
 | 
					    test_create_item("user103@example.com", [], inaccessible_user_item, {
 | 
				
			||||||
 | 
					        exclude_inaccessible_users: false,
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test("get_email", () => {
 | 
					test("get_email", () => {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user