realm_export: Add 'Export permissions' table.

This commit adds a "Export permissions" table
in the 'Data exports' setting panel.

The table lists the active human users and their
configuration of 'allow_private_data_export' setting.

Fixes part of #31201.
This commit is contained in:
Prakhar Pratyush
2024-10-10 18:08:44 +05:30
committed by Tim Abbott
parent a1aa52cff4
commit b8e0e08f01
8 changed files with 298 additions and 22 deletions

View File

@@ -1,15 +1,21 @@
import $ from "jquery";
import type * as tippy from "tippy.js";
import {z} from "zod";
import render_confirm_delete_data_export from "../templates/confirm_dialog/confirm_delete_data_export.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";
import * as components from "./components";
import * as confirm_dialog from "./confirm_dialog";
import * as dialog_widget from "./dialog_widget";
import * as dropdown_widget from "./dropdown_widget";
import type {DropdownWidget, Option} from "./dropdown_widget";
import {$t, $t_html} from "./i18n";
import * as ListWidget from "./list_widget";
import type {ListWidget as ListWidgetType} from "./list_widget";
import * as loading from "./loading";
import * as people from "./people";
import * as scroll_util from "./scroll_util";
@@ -22,6 +28,7 @@ const export_consent_schema = z.object({
user_id: z.number(),
consented: z.boolean(),
});
type ExportConsent = z.output<typeof export_consent_schema>;
const realm_export_schema = z.object({
id: z.number(),
@@ -115,13 +122,13 @@ export function populate_exports_table(exports: RealmExport[]): void {
scroll_util.reset_scrollbar($exports_table);
},
},
$parent_container: $("#data-exports").expectOne(),
$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-exports .progressive-table-wrapper"),
$simplebar_container: $('[data-export-section="data-exports"] .progressive-table-wrapper'),
});
const $spinner = $(".export_row .export_url_spinner");
@@ -132,6 +139,124 @@ export function populate_exports_table(exports: RealmExport[]): void {
}
}
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;
} else if (a_name === b_name) {
return 0;
}
return -1;
}
const export_consents = new Map<number, boolean>();
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, consented] of export_consents.entries()) {
if (consent === consented) {
export_consent_list.push({user_id, consented});
}
}
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: dropdown_widget.DataTypes.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,
});
filter_by_consent_dropdown_widget.setup();
}
function show_start_export_modal(): void {
const html_body = render_start_export_modal({
export_type_values: settings_config.export_type_values,
@@ -203,16 +328,48 @@ function show_start_export_modal(): void {
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);
total_users_count = data.export_consents.length;
users_consented_for_export_count = data.export_consents.filter(
(export_consent) => export_consent.consented,
).length;
for (const export_consent of data.export_consents) {
export_consents.set(export_consent.user_id, export_consent.consented);
}
// 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, item.consented);
}
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();
},
});
@@ -222,7 +379,7 @@ export function set_up(): void {
show_start_export_modal();
});
// Do an initial population of the table
// Do an initial population of the 'Data exports' table
void channel.get({
url: "/json/export/realm",
success(raw_data) {
@@ -248,3 +405,44 @@ export function set_up(): void {
});
});
}
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;
}
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();
}
export function update_export_consent_data_and_redraw(export_consent: ExportConsent): void {
if (!meta.loaded) {
return;
}
if (maybe_store_export_consent_data_and_return(export_consent)) {
return;
}
export_consents.set(export_consent.user_id, export_consent.consented);
redraw_export_consents_list();
}