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.

16.2 KB
Raw Blame
import assert from "minimalistic-assert";

import * as blueslip from "./blueslip.ts";
import {Typeahead} from "./bootstrap_typeahead.ts";
import type {TypeaheadInputElement} from "./bootstrap_typeahead.ts";
import * as people from "./people.ts";
import type {User} from "./people.ts";
import * as stream_pill from "./stream_pill.ts";
import type {StreamPillData, StreamPillWidget} from "./stream_pill.ts";
import * as typeahead from "./typeahead.ts";
import * as typeahead_helper from "./typeahead_helper.ts";
import type {CombinedPillContainer, GroupSettingPillContainer} from "./typeahead_helper.ts";
import * as user_group_pill from "./user_group_pill.ts";
import type {UserGroupPillData} from "./user_group_pill.ts";
import type {UserGroup} from "./user_groups.ts";
import * as user_pill from "./user_pill.ts";
import type {UserPillData, UserPillWidget} from "./user_pill.ts";

function person_matcher(query: string, item: UserPillData): boolean {
    return (
        people.is_known_user_id(item.user.user_id) &&
        typeahead_helper.query_matches_person(query, item)
    );
}

function group_matcher(query: string, item: UserGroupPillData): boolean {
    return typeahead_helper.query_matches_group_name(query, item);
}

type TypeaheadItem = UserGroupPillData | StreamPillData | UserPillData;
type GroupSettingTypeaheadItem = UserGroupPillData | UserPillData;

export function set_up_user(
    $input: JQuery,
    pills: UserPillWidget,
    opts: {
        exclude_bots?: boolean;
    },
): void {
    const exclude_bots = opts.exclude_bots;
    const bootstrap_typeahead_input: TypeaheadInputElement = {
        $element: $input,
        type: "contenteditable",
    };
    new Typeahead(bootstrap_typeahead_input, {
        dropup: true,
        source(_query: string): UserPillData[] {
            return user_pill.typeahead_source(pills, exclude_bots);
        },
        item_html(_query: string): (item: UserPillData) => string {
            return (item: UserPillData) => typeahead_helper.render_person(item);
        },
        matcher(query: string): (item: UserPillData) => boolean {
            query = typeahead.clean_query_lowercase(query);
            return (item: UserPillData) => person_matcher(query, item);
        },
        sorter(matches: UserPillData[], query: string): UserPillData[] {
            const users = matches.filter((match) => people.is_known_user_id(match.user.user_id));
            return typeahead_helper.sort_recipients({users, query}).map((item) => {
                assert(item.type === "user");
                return item;
            });
        },
        updater(item: UserPillData, _query: string): undefined {
            if (people.is_known_user_id(item.user.user_id)) {
                user_pill.append_user(item.user, pills);
            }
            $input.trigger("focus");
        },
        stopAdvance: true,
    });
}

export function set_up_stream(
    $input: JQuery,
    pills: StreamPillWidget,
    opts: {
        help_on_empty_strings?: boolean;
        hide_on_empty_after_backspace?: boolean;
        invite_streams?: boolean;
    },
): void {
    const bootstrap_typeahead_input: TypeaheadInputElement = {
        $element: $input,
        type: "contenteditable",
    };
    opts.hide_on_empty_after_backspace ??= false;
    new Typeahead(bootstrap_typeahead_input, {
        dropup: true,
        helpOnEmptyStrings: () => opts.help_on_empty_strings ?? false,
        hideOnEmptyAfterBackspace: opts.hide_on_empty_after_backspace,
        source(_query: string): StreamPillData[] {
            return stream_pill.typeahead_source(pills, opts.invite_streams);
        },
        item_html(_query: string): (item: StreamPillData) => string {
            return (item: StreamPillData) => typeahead_helper.render_stream(item);
        },
        matcher(query: string): (item: StreamPillData) => boolean {
            query = query.toLowerCase();
            query = query.replaceAll("\u{A0}", " ");
            query = query.trim();
            if (query.startsWith("#")) {
                query = query.slice(1);
            }
            return (item: StreamPillData) => item.name.toLowerCase().includes(query);
        },
        sorter(matches: StreamPillData[], query: string): StreamPillData[] {
            const stream_matches: StreamPillData[] = [];
            for (const match of matches) {
                assert(match.type === "stream");
                stream_matches.push(match);
            }
            query = query.trim();
            if (query.startsWith("#")) {
                query = query.slice(1);
            }
            return typeahead_helper.sort_streams_by_name(stream_matches, query);
        },
        updater(item: StreamPillData, _query: string): undefined {
            stream_pill.append_stream(item, pills);
            $input.trigger("focus");
        },
        stopAdvance: true,
    });
}

export function set_up_user_group(
    $input: JQuery,
    pills: user_group_pill.UserGroupPillWidget,
    opts: {
        user_group_source: () => UserGroup[];
    },
): void {
    const bootstrap_typeahead_input: TypeaheadInputElement = {
        $element: $input,
        type: "contenteditable",
    };
    new Typeahead(bootstrap_typeahead_input, {
        dropup: true,
        source(_query: string): UserGroupPillData[] {
            return opts
                .user_group_source()
                .map((user_group) => ({type: "user_group", ...user_group}));
        },
        item_html(_query: string): (item: UserGroupPillData) => string {
            return (item: UserGroupPillData) => typeahead_helper.render_user_group(item);
        },
        matcher(query: string): (item: UserGroupPillData) => boolean {
            query = typeahead.clean_query_lowercase(query);
            return (item: UserGroupPillData) => group_matcher(query, item);
        },
        sorter(matches: UserGroupPillData[], query: string): UserGroupPillData[] {
            return typeahead_helper.sort_user_groups(matches, query);
        },
        updater(item: UserGroupPillData, _query: string): undefined {
            user_group_pill.append_user_group(item, pills);
            $input.trigger("focus");
        },
        stopAdvance: true,
        helpOnEmptyStrings: () => true,
        hideOnEmptyAfterBackspace: true,
    });
}

export function set_up_group_setting_typeahead(
    $input: JQuery,
    pills: GroupSettingPillContainer,
    opts: {
        setting_name: string;
        setting_type: "realm" | "stream" | "group";
        group?: UserGroup | undefined;
    },
): void {
    const bootstrap_typeahead_input: TypeaheadInputElement = {
        $element: $input,
        type: "contenteditable",
    };
    new Typeahead(bootstrap_typeahead_input, {
        dropup: true,
        source(_query: string): GroupSettingTypeaheadItem[] {
            return [
                ...user_group_pill.typeahead_source(pills, opts.setting_name, opts.setting_type),
                ...user_pill.typeahead_source(pills, false, opts.setting_name, opts.setting_type),
            ];
        },
        item_html(_query: string): (item: GroupSettingTypeaheadItem) => string {
            return (item: GroupSettingTypeaheadItem): string => {
                if (item.type === "user_group") {
                    return typeahead_helper.render_user_group(item);
                }

                assert(item.type === "user");
                return typeahead_helper.render_person(item);
            };
        },
        matcher(query: string): (item: GroupSettingTypeaheadItem) => boolean {
            query = typeahead.clean_query_lowercase(query);

            return (item: GroupSettingTypeaheadItem): boolean => {
                let matches = false;
                if (item.type === "user_group") {
                    matches ||= group_matcher(query, item);
                } else if (item.type === "user") {
                    matches ||= person_matcher(query, item);
                }
                return matches;
            };
        },
        sorter(matches: GroupSettingTypeaheadItem[], query: string): GroupSettingTypeaheadItem[] {
            const users: UserPillData[] = [];
            for (const match of matches) {
                if (match.type === "user" && people.is_known_user_id(match.user.user_id)) {
                    users.push(match);
                }
            }

            const groups: UserGroupPillData[] = [];
            for (const match of matches) {
                if (match.type === "user_group") {
                    groups.push(match);
                }
            }

            return typeahead_helper.sort_group_setting_options({
                users,
                query,
                groups,
                target_group: opts.group,
            });
        },
        updater(item: GroupSettingTypeaheadItem, _query: string): undefined {
            if (item.type === "user_group") {
                user_group_pill.append_user_group(item, pills);
            } else if (item.type === "user" && people.is_known_user_id(item.user.user_id)) {
                user_pill.append_user(item.user, pills);
            }

            $input.trigger("focus");
        },
        stopAdvance: true,
        helpOnEmptyStrings: () => true,
        hideOnEmptyAfterBackspace: true,
    });
}

export function set_up_combined(
    $input: JQuery,
    pills: CombinedPillContainer,
    opts: {
        user: boolean;
        user_group?: boolean;
        stream?: boolean;
        user_source?: () => User[];
        user_group_source?: () => UserGroup[];
        exclude_bots?: boolean;
        for_stream_subscribers: boolean;
    },
): void {
    if (!opts.user && !opts.user_group && !opts.stream) {
        blueslip.error("Unspecified possible item types");
        return;
    }
    const include_streams = (query: string): boolean =>
        opts.stream !== undefined && query.trimStart().startsWith("#");
    const include_user_groups = opts.user_group;
    const include_users = opts.user;
    const exclude_bots = opts.exclude_bots;

    const bootstrap_typeahead_input: TypeaheadInputElement = {
        $element: $input,
        type: "contenteditable",
    };
    new Typeahead(bootstrap_typeahead_input, {
        dropup: true,
        helpOnEmptyStrings: () => true,
        hideOnEmptyAfterBackspace: true,
        source(query: string): TypeaheadItem[] {
            let source: TypeaheadItem[] = [];
            if (include_streams(query)) {
                // If query starts with # we expect,
                // only stream suggestions so we simply
                // return stream source.
                return stream_pill.typeahead_source(pills);
            }

            if (include_user_groups) {
                if (query.startsWith("@")) {
                    // If query starts with @ we expect only user group
                    // suggestions so we simply return user group source.
                    if (opts.user_group_source !== undefined) {
                        return opts
                            .user_group_source()
                            .map((user_group) => ({type: "user_group", ...user_group}));
                    }
                    return user_group_pill.typeahead_source(pills);
                }
                if (opts.user_group_source !== undefined) {
                    const groups: UserGroupPillData[] = opts
                        .user_group_source()
                        .map((user_group) => ({type: "user_group", ...user_group}));
                    source = [...source, ...groups];
                } else {
                    source = [...source, ...user_group_pill.typeahead_source(pills)];
                }
            }

            if (include_users) {
                if (opts.user_source !== undefined) {
                    // If user_source is specified in opts, it
                    // is given priority. Otherwise we use
                    // default user_pill.typeahead_source.
                    const users: UserPillData[] = opts
                        .user_source()
                        .map((user) => ({type: "user", user}));
                    source = [...source, ...users];
                } else {
                    source = [...source, ...user_pill.typeahead_source(pills, exclude_bots)];
                }
            }
            return source;
        },
        item_html(query: string): (item: TypeaheadItem) => string {
            return (item: TypeaheadItem): string => {
                if (include_streams(query) && item.type === "stream") {
                    return typeahead_helper.render_stream(item);
                }

                if (include_user_groups && item.type === "user_group") {
                    return typeahead_helper.render_user_group(item);
                }

                // After reaching this point, it is sure
                // that given item is a person. So this
                // handles `include_users` cases along with
                // default cases.
                assert(item.type === "user");
                return typeahead_helper.render_person(item);
            };
        },
        matcher(query: string): (item: TypeaheadItem) => boolean {
            query = typeahead.clean_query_lowercase(query);

            return (item: TypeaheadItem): boolean => {
                if (include_streams(query) && item.type === "stream") {
                    const stream_query = query.trim().slice(1);
                    return item.name.toLowerCase().includes(stream_query);
                }

                if (include_user_groups && query.startsWith("@")) {
                    if (item.type === "user_group") {
                        const normalized_query = query.slice(1);
                        return group_matcher(normalized_query, item);
                    }
                    return false;
                }

                let matches = false;
                if (include_user_groups && item.type === "user_group") {
                    matches = group_matcher(query, item);
                }

                if (include_users && item.type === "user") {
                    matches ||= person_matcher(query, item);
                }
                return matches;
            };
        },
        sorter(matches: TypeaheadItem[], query: string): TypeaheadItem[] {
            if (include_streams(query)) {
                const stream_matches: StreamPillData[] = [];
                for (const match of matches) {
                    assert(match.type === "stream");
                    stream_matches.push(match);
                }
                return typeahead_helper.sort_streams(stream_matches, query.trim().slice(1));
            }

            const users: UserPillData[] = [];
            if (include_users) {
                for (const match of matches) {
                    if (match.type === "user" && people.is_known_user_id(match.user.user_id)) {
                        users.push(match);
                    }
                }
            }

            const groups: UserGroupPillData[] = [];
            if (include_user_groups) {
                for (const match of matches) {
                    if (match.type === "user_group") {
                        groups.push(match);
                    }
                }
            }

            const normalized_query = query.startsWith("@") ? query.slice(1) : query;
            return typeahead_helper.sort_stream_or_group_members_options({
                users,
                query: normalized_query,
                groups,
                for_stream_subscribers: opts.for_stream_subscribers,
            });
        },
        updater(item: TypeaheadItem, query: string): undefined {
            if (include_streams(query) && item.type === "stream") {
                stream_pill.append_stream(item, pills);
            } else if (include_user_groups && item.type === "user_group") {
                const show_expand_button =
                    !opts.for_stream_subscribers &&
                    (item.members.size > 0 || item.direct_subgroup_ids.size > 0);
                user_group_pill.append_user_group(item, pills, true, show_expand_button);
            } else if (
                include_users &&
                item.type === "user" &&
                people.is_known_user_id(item.user.user_id)
            ) {
                user_pill.append_user(item.user, pills);
            }

            $input.trigger("focus");
        },
        stopAdvance: true,
    });
}