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.

22.5 KB
Raw Blame
import {$} from "jquery";
import type * as tippy from "tippy.js";
import * as z from "zod/mini";

import render_export_modal_warning_notes from "../templates/export_modal_warning_notes.hbs";
import render_allow_private_data_export_banner from "../templates/modal_banner/allow_private_data_export_banner.hbs";
import render_admin_export_consent_list from "../templates/settings/admin_export_consent_list.hbs";
import render_admin_export_list from "../templates/settings/admin_export_list.hbs";
import render_start_export_modal from "../templates/start_export_modal.hbs";

import * as channel from "./channel.ts";
import * as components from "./components.ts";
import * as compose_banner from "./compose_banner.ts";
import * as confirm_dialog from "./confirm_dialog.ts";
import * as dialog_widget from "./dialog_widget.ts";
import * as dropdown_widget from "./dropdown_widget.ts";
import type {DropdownWidget, Option} from "./dropdown_widget.ts";
import {$t, $t_html} from "./i18n.ts";
import * as ListWidget from "./list_widget.ts";
import type {ListWidget as ListWidgetType} from "./list_widget.ts";
import * as loading from "./loading.ts";
import * as people from "./people.ts";
import * as scroll_util from "./scroll_util.ts";
import * as settings_config from "./settings_config.ts";
import {current_user, realm} from "./state_data.ts";
import * as timerender from "./timerender.ts";
import type {HTMLSelectOneElement} from "./types.ts";
import * as ui_report from "./ui_report.ts";
import {user_settings} from "./user_settings.ts";

export const export_consent_schema = z.object({
    user_id: z.number(),
    consented: z.boolean(),
    email_address_visibility: z.number(),
});
type ExportConsent = z.output<typeof export_consent_schema>;

const realm_export_type_schema = z.enum([
    settings_config.export_type_values.public.slug,
    settings_config.export_type_values.full_with_consent.slug,
    settings_config.export_type_values.full_without_consent.slug,
]);

export const realm_export_schema = z.object({
    id: z.number(),
    export_time: z.number(),
    acting_user_id: z.number(),
    export_url: z.nullable(z.string()),
    deleted_timestamp: z.nullable(z.number()),
    failed_timestamp: z.nullable(z.number()),
    pending: z.boolean(),
    export_from_prior_server: z.boolean(),
    export_type: realm_export_type_schema,
});
type RealmExport = z.output<typeof realm_export_schema>;

const meta = {
    loaded: false,
};

function get_export_type_options_to_render(): settings_config.ExportTypeOption[] {
    const standard_option =
        realm.realm_owner_full_content_access && current_user.is_owner
            ? settings_config.export_type_values.full_without_consent
            : settings_config.export_type_values.full_with_consent;
    return [settings_config.export_type_values.public, standard_option];
}

function get_export_type_from_select(
    $export_type: JQuery<HTMLSelectOneElement>,
): settings_config.ExportTypeSlug {
    return realm_export_type_schema.parse($export_type.val());
}

let users_consented_for_export_count: number;
let total_users_count: number;

export function reset(): void {
    meta.loaded = false;
}

function sort_user(a: RealmExport, b: RealmExport): number {
    const a_name = people.get_full_name(a.acting_user_id).toLowerCase();
    const b_name = people.get_full_name(b.acting_user_id).toLowerCase();
    if (a_name > b_name) {
        return 1;
    }
    if (a_name === b_name) {
        return 0;
    }
    return -1;
}

export function populate_exports_table(exports: RealmExport[]): void {
    if (!meta.loaded) {
        return;
    }

    const $exports_table = $("#admin_exports_table").expectOne();
    ListWidget.create($exports_table, Object.values(exports), {
        name: "admin_exports_list",
        get_item: ListWidget.default_get_item,
        modifier_html(data) {
            let failed_timestamp = null;
            let deleted_timestamp = null;

            if (data.failed_timestamp !== null) {
                failed_timestamp = timerender.relative_time_string_from_date(
                    new Date(data.failed_timestamp * 1000),
                );
            }

            if (data.deleted_timestamp !== null) {
                deleted_timestamp = timerender.relative_time_string_from_date(
                    new Date(data.deleted_timestamp * 1000),
                );
            }

            return render_admin_export_list({
                realm_export: {
                    id: data.id,
                    acting_user: people.get_full_name(data.acting_user_id),
                    // Convert seconds -> milliseconds
                    event_time: timerender.relative_time_string_from_date(
                        new Date(data.export_time * 1000),
                    ),
                    url: data.export_url,
                    time_failed: failed_timestamp,
                    pending: data.pending,
                    time_deleted: deleted_timestamp,
                    export_from_prior_server: data.export_from_prior_server,
                    export_type_description:
                        settings_config.export_type_values[data.export_type].description,
                },
            });
        },
        filter: {
            $element: $exports_table
                .closest(".settings-section")
                .find<HTMLInputElement>("input.search"),
            predicate(item, value) {
                return people.get_full_name(item.acting_user_id).toLowerCase().includes(value);
            },
            onupdate() {
                scroll_util.reset_scrollbar($exports_table);
            },
        },
        $parent_container: $('[data-export-section="data-exports"]').expectOne(),
        init_sort: sort_user,
        sort_fields: {
            user: sort_user,
            ...ListWidget.generic_sort_functions("numeric", ["export_time"]),
        },
        $simplebar_container: $('[data-export-section="data-exports"] .progressive-table-wrapper'),
    });

    const $spinner = $(".export_row .export_url_spinner");
    if ($spinner.length > 0) {
        loading.make_indicator($spinner);
    } else {
        loading.destroy_indicator($spinner);
    }
}

function sort_user_by_name(a: ExportConsent, b: ExportConsent): number {
    const a_name = people.get_full_name(a.user_id).toLowerCase();
    const b_name = people.get_full_name(b.user_id).toLowerCase();
    if (a_name > b_name) {
        return 1;
    }
    if (a_name === b_name) {
        return 0;
    }
    return -1;
}
type UserConsentInfo = {
    consented: boolean;
    email_address_visibility: number;
};
const export_consents = new Map<number, UserConsentInfo>();
const queued_export_consents: (ExportConsent | number)[] = [];
let export_consent_list_widget: ListWidgetType<ExportConsent>;
let filter_by_consent_dropdown_widget: DropdownWidget;
const filter_by_consent_options: Option[] = [
    {
        unique_id: 0,
        name: $t({defaultMessage: "Granted"}),
    },
    {
        unique_id: 1,
        name: $t({defaultMessage: "Not granted"}),
    },
];

function get_export_consents_having_consent_value(consent: boolean): ExportConsent[] {
    const export_consent_list: ExportConsent[] = [];
    for (const [user_id, user_consent_info] of export_consents) {
        const consented = user_consent_info.consented;
        const email_address_visibility = user_consent_info.email_address_visibility;
        if (consent === user_consent_info.consented) {
            export_consent_list.push({
                user_id,
                consented,
                email_address_visibility,
            });
        }
    }
    return export_consent_list;
}

function get_export_consents_having_email_visibility_value(
    email_address_visibility_code: number,
): ExportConsent[] {
    const export_consent_list: ExportConsent[] = [];
    for (const [user_id, user_consent_info] of export_consents) {
        const consented = user_consent_info.consented;
        const email_address_visibility = user_consent_info.email_address_visibility;
        if (email_address_visibility_code === email_address_visibility) {
            export_consent_list.push({
                user_id,
                consented,
                email_address_visibility,
            });
        }
    }
    return export_consent_list;
}

export function redraw_export_consents_list(): void {
    let new_list_data;
    if (filter_by_consent_dropdown_widget.value() === filter_by_consent_options[0]!.unique_id) {
        new_list_data = get_export_consents_having_consent_value(true);
    } else {
        new_list_data = get_export_consents_having_consent_value(false);
    }
    export_consent_list_widget.replace_list_data(new_list_data);
}

export function populate_export_consents_table(): void {
    if (!meta.loaded) {
        return;
    }

    const $export_consents_table = $("#admin_export_consents_table").expectOne();
    export_consent_list_widget = ListWidget.create(
        $export_consents_table,
        get_export_consents_having_consent_value(true),
        {
            name: "admin_export_consents_list",
            get_item: ListWidget.default_get_item,
            modifier_html(item) {
                const person = people.get_by_user_id(item.user_id);
                let consent = $t({defaultMessage: "Not granted"});
                if (item.consented) {
                    consent = $t({defaultMessage: "Granted"});
                }
                return render_admin_export_consent_list({
                    export_consent: {
                        user_id: person.user_id,
                        full_name: person.full_name,
                        img_src: people.small_avatar_url_for_person(person),
                        consent,
                    },
                });
            },
            filter: {
                $element: $export_consents_table
                    .closest(".export_section")
                    .find<HTMLInputElement>("input.search"),
                predicate(item, value) {
                    return people.get_full_name(item.user_id).toLowerCase().includes(value);
                },
                onupdate() {
                    scroll_util.reset_scrollbar($export_consents_table);
                },
            },
            $parent_container: $('[data-export-section="export-permissions"]').expectOne(),
            init_sort: sort_user_by_name,
            sort_fields: {
                full_name: sort_user_by_name,
            },
            $simplebar_container: $(
                '[data-export-section="export-permissions"] .progressive-table-wrapper',
            ),
        },
    );

    filter_by_consent_dropdown_widget = new dropdown_widget.DropdownWidget({
        widget_name: "filter_by_consent",
        unique_id_type: "number",
        get_options: () => filter_by_consent_options,
        item_click_callback(
            event: JQuery.ClickEvent,
            dropdown: tippy.Instance,
            widget: dropdown_widget.DropdownWidget,
        ) {
            event.preventDefault();
            event.stopPropagation();

            redraw_export_consents_list();

            dropdown.hide();
            widget.render();
        },
        $events_container: $("#data-exports"),
        default_id: filter_by_consent_options[0]!.unique_id,
        hide_search_box: true,
    });
    filter_by_consent_dropdown_widget.setup();
}

function maybe_show_allow_private_data_export_banner(): void {
    if (!user_settings.allow_private_data_export) {
        const context = {
            banner_type: compose_banner.WARNING,
            classname: "allow_private_data_export_warning",
            hide_close_button: true,
        };
        $("#allow_private_data_export_banner_container").html(
            render_allow_private_data_export_banner(context),
        );
    }
}

export function refresh_allow_private_data_export_banner(): void {
    if (user_settings.allow_private_data_export) {
        $(".allow_private_data_export_warning").remove();
    } else if ($("#allow_private_data_export_banner_container").length > 0) {
        maybe_show_allow_private_data_export_banner();
        const $export_type = $<HTMLSelectOneElement>("select:not([multiple])#export_type");
        const selected_export_type = get_export_type_from_select($export_type);
        if (selected_export_type === settings_config.export_type_values.public.slug) {
            $(".allow_private_data_export_warning").hide();
        }
    }
}

function maybe_show_notes_about_unusable_users_if_exported(
    export_type: settings_config.ExportTypeSlug,
): void {
    // Show warnings if there are users whose accounts will be inaccessible
    // once exported. A user account will be inaccessible if:
    //  - In a standard export: they don’t consent to their private data
    //    being exported AND their email visibility is set to "nobody".
    //  - In a public export: they have set their email visibility to
    //    “nobody”.
    let unusable_user_ids;
    const $warning_container = $("div#unusable-user-accounts-warning");
    $warning_container.empty();
    if (export_type === settings_config.export_type_values.full_with_consent.slug) {
        const non_consenting_users = get_export_consents_having_consent_value(false);
        unusable_user_ids = non_consenting_users
            .filter(
                (user) =>
                    user.email_address_visibility ===
                    settings_config.email_address_visibility_values.nobody.code,
            )
            .map((user) => user.user_id);
    } else if (export_type === settings_config.export_type_values.public.slug) {
        const users_with_inaccessible_email = get_export_consents_having_email_visibility_value(
            settings_config.email_address_visibility_values.nobody.code,
        );
        unusable_user_ids = users_with_inaccessible_email.map((user) => user.user_id);
    } else {
        return;
    }

    if (unusable_user_ids.length === 0) {
        return;
    }

    // List admins/owners who won’t be able to log in.
    const admins_or_owners_with_unusable_account = people.get_users_that_match_role_ids(
        new Set(unusable_user_ids),
        new Set([
            settings_config.user_role_values.admin.code,
            settings_config.user_role_values.owner.code,
        ]),
    );

    const $export_warnings = render_export_modal_warning_notes({
        unusable_user_count: unusable_user_ids.length,
        unusable_admin_users: admins_or_owners_with_unusable_account,
        unusable_admin_user_count: admins_or_owners_with_unusable_account.length,
    });
    $warning_container.append($export_warnings);
}

function show_start_export_modal(): void {
    const modal_content_html = render_start_export_modal({
        export_type_values: get_export_type_options_to_render(),
    });

    function start_export(): void {
        dialog_widget.show_dialog_spinner();
        const $export_status = $("#export_status");
        const export_type = get_export_type_from_select(
            $<HTMLSelectOneElement>("select:not([multiple])#export_type"),
        );

        void channel.post({
            url: "/json/export/realm",
            data: {export_type},
            success() {
                dialog_widget.hide_dialog_spinner();
                ui_report.success(
                    $t_html({defaultMessage: "Export started. Check back in a few minutes."}),
                    $export_status,
                    4000,
                );
                dialog_widget.close();
            },
            error(xhr) {
                dialog_widget.hide_dialog_spinner();
                ui_report.error($t_html({defaultMessage: "Export failed"}), xhr, $export_status);
                dialog_widget.close();
            },
        });
    }

    function start_export_modal_post_render(): void {
        $("#allow_private_data_export_stats").text(
            $t(
                {
                    defaultMessage: `
                        Exporting private data for {users_consented_for_export_count,
                        plural, one {# user} other {# users}} ({total_users_count,
                        plural, one {# user} other {# users}} total).
                    `,
                },
                {users_consented_for_export_count, total_users_count},
            ),
        );

        maybe_show_allow_private_data_export_banner();

        const $export_type = $<HTMLSelectOneElement>("select:not([multiple])#export_type");
        $export_type.on("change", () => {
            const selected_export_type = get_export_type_from_select($export_type);
            if (
                selected_export_type === settings_config.export_type_values.full_with_consent.slug
            ) {
                $("#allow_private_data_export_stats").show();
                $(".allow_private_data_export_warning").show();
            } else {
                $("#allow_private_data_export_stats").hide();
                $(".allow_private_data_export_warning").hide();
            }
            maybe_show_notes_about_unusable_users_if_exported(selected_export_type);
        });
        $export_type.trigger("change");
    }

    dialog_widget.launch({
        modal_title_html: $t_html({defaultMessage: "Start export?"}),
        modal_content_html,
        modal_submit_button_text: $t({defaultMessage: "Start export"}),
        id: "start-export-modal",
        loading_spinner: true,
        on_click: start_export,
        post_render: start_export_modal_post_render,
    });
}

export function set_up(): void {
    meta.loaded = true;

    const toggler = components.toggle({
        child_wants_focus: true,
        values: [
            {label: $t({defaultMessage: "Data exports"}), key: "data-exports"},
            {label: $t({defaultMessage: "Export permissions"}), key: "export-permissions"},
        ],
        callback(_name, key) {
            $(".export_section").hide();
            $(`[data-export-section="${CSS.escape(key)}"]`).show();
        },
    });

    toggler.get().prependTo($("#data-exports .tab-container"));
    toggler.goto("data-exports");

    // Do an initial population of the 'Export permissions' table
    void channel.get({
        url: "/json/export/realm/consents",
        success(raw_data) {
            const data = z
                .object({export_consents: z.array(export_consent_schema)})
                .parse(raw_data);

            for (const export_consent of data.export_consents) {
                export_consents.set(export_consent.user_id, {
                    consented: export_consent.consented,
                    email_address_visibility: export_consent.email_address_visibility,
                });
            }

            // Apply queued_export_consents on top of the received response.
            for (const item of queued_export_consents) {
                if (typeof item === "number") {
                    // user deactivated; item is user_id in this case.
                    export_consents.delete(item);
                    continue;
                }
                export_consents.set(item.user_id, {
                    consented: item.consented,
                    email_address_visibility: item.email_address_visibility,
                });
            }
            queued_export_consents.length = 0;

            total_users_count = export_consents.size;
            users_consented_for_export_count =
                get_export_consents_having_consent_value(true).length;
            populate_export_consents_table();
        },
    });

    $("#start-export-button").on("click", (e) => {
        e.preventDefault();
        e.stopPropagation();
        show_start_export_modal();
    });

    // Do an initial population of the 'Data exports' table
    void channel.get({
        url: "/json/export/realm",
        success(raw_data) {
            const data = z.object({exports: z.array(realm_export_schema)}).parse(raw_data);
            populate_exports_table(data.exports);
        },
    });

    $(".admin_exports_table").on("click", ".delete", function (e) {
        e.preventDefault();
        e.stopPropagation();
        const $button = $(this);
        const url =
            "/json/export/realm/" +
            encodeURIComponent($button.closest("tr").attr("data-export-id")!);

        confirm_dialog.launch({
            modal_title_html: $t_html({defaultMessage: "Delete data export?"}),
            modal_content_html: $t({defaultMessage: "This action cannot be undone."}),
            is_compact: true,
            on_click() {
                dialog_widget.submit_api_request(channel.del, url, {});
            },
            loading_spinner: true,
        });
    });
}

function maybe_store_export_consent_data_and_return(export_consent: ExportConsent): boolean {
    // Handles a race where the client has requested the server for export consents
    // to populate 'Export permissions' table but hasn't received the response yet,
    // but received a few updated events which should be applied on top of the received
    // response to avoid outdated table.
    // We store the export_consent data received via events to apply them on top of
    // the received response.
    if (export_consents === undefined) {
        queued_export_consents.push(export_consent);
        return true;
    }
    return false;
}

function update_start_export_modal_stats(): void {
    total_users_count = export_consents.size;
    users_consented_for_export_count = get_export_consents_having_consent_value(true).length;
    if ($("#allow_private_data_export_stats").length > 0) {
        $("#allow_private_data_export_stats").text(
            $t(
                {
                    defaultMessage: `
                        Exporting private data for {users_consented_for_export_count,
                        plural, one {# user} other {# users}} ({total_users_count,
                        plural, one {# user} other {# users}} total).
                    `,
                },
                {users_consented_for_export_count, total_users_count},
            ),
        );
    }
}

export function remove_export_consent_data_and_redraw(user_id: number): void {
    if (!meta.loaded) {
        return;
    }

    if (export_consents === undefined) {
        queued_export_consents.push(user_id);
        return;
    }

    export_consents.delete(user_id);
    redraw_export_consents_list();
    update_start_export_modal_stats();
}

export function update_export_consent_data_and_redraw(export_consent: ExportConsent): void {
    if (!meta.loaded || maybe_store_export_consent_data_and_return(export_consent)) {
        return;
    }

    export_consents.set(export_consent.user_id, {
        consented: export_consent.consented,
        email_address_visibility: export_consent.email_address_visibility,
    });
    redraw_export_consents_list();
    update_start_export_modal_stats();
}