2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-06-21 11:11:30 +02:00
|
|
|
import render_settings_resend_invite_modal from "../templates/confirm_dialog/confirm_resend_invite.hbs";
|
2021-06-21 11:15:47 +02:00
|
|
|
import render_settings_revoke_invite_modal from "../templates/confirm_dialog/confirm_revoke_invite.hbs";
|
2021-04-22 18:34:53 +02:00
|
|
|
import render_admin_invites_list from "../templates/settings/admin_invites_list.hbs";
|
2021-02-28 01:22:20 +01:00
|
|
|
|
2021-03-16 23:38:59 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
2021-02-28 01:22:20 +01:00
|
|
|
import * as channel from "./channel";
|
2021-05-26 09:37:20 +02:00
|
|
|
import * as confirm_dialog from "./confirm_dialog";
|
2021-04-13 06:51:54 +02:00
|
|
|
import {$t, $t_html} from "./i18n";
|
2021-02-28 01:22:20 +01:00
|
|
|
import * as ListWidget from "./list_widget";
|
|
|
|
import * as loading from "./loading";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2021-02-28 01:22:20 +01:00
|
|
|
import * as people from "./people";
|
|
|
|
import * as settings_config from "./settings_config";
|
|
|
|
import * as timerender from "./timerender";
|
|
|
|
import * as ui_report from "./ui_report";
|
|
|
|
import * as util from "./util";
|
2020-07-24 06:02:07 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const meta = {
|
2017-10-21 03:15:12 +02:00
|
|
|
loaded: false,
|
|
|
|
};
|
|
|
|
|
2021-02-28 01:22:20 +01:00
|
|
|
export function reset() {
|
2017-10-21 03:15:12 +02:00
|
|
|
meta.loaded = false;
|
2021-02-28 01:22:20 +01:00
|
|
|
}
|
2017-10-21 03:15:12 +02:00
|
|
|
|
|
|
|
function failed_listing_invites(xhr) {
|
2020-07-15 01:29:15 +02:00
|
|
|
loading.destroy_indicator($("#admin_page_invites_loading_indicator"));
|
2021-04-13 05:18:25 +02:00
|
|
|
ui_report.error(
|
|
|
|
$t_html({defaultMessage: "Error listing invites"}),
|
|
|
|
xhr,
|
|
|
|
$("#invites-field-status"),
|
|
|
|
);
|
2017-10-21 03:15:12 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 16:59:21 +01:00
|
|
|
function add_invited_as_text(invites) {
|
2020-02-09 05:08:58 +01:00
|
|
|
for (const data of invites) {
|
2020-06-23 15:32:18 +02:00
|
|
|
data.invited_as_text = settings_config.user_role_map.get(data.invited_as);
|
2020-02-09 05:08:58 +01:00
|
|
|
}
|
2019-01-03 16:59:21 +01:00
|
|
|
}
|
|
|
|
|
2020-04-11 20:06:01 +02:00
|
|
|
function sort_invitee(a, b) {
|
|
|
|
// multi-invite links don't have an email field,
|
|
|
|
// so we set them to empty strings to let them
|
|
|
|
// sort to the top
|
2020-07-15 01:29:15 +02:00
|
|
|
const str1 = (a.email || "").toUpperCase();
|
|
|
|
const str2 = (b.email || "").toUpperCase();
|
2020-04-11 20:06:01 +02:00
|
|
|
|
|
|
|
return util.strcmp(str1, str2);
|
|
|
|
}
|
2019-01-03 16:59:21 +01:00
|
|
|
|
2017-10-21 03:15:12 +02:00
|
|
|
function populate_invites(invites_data) {
|
|
|
|
if (!meta.loaded) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-03 16:59:21 +01:00
|
|
|
|
|
|
|
add_invited_as_text(invites_data.invites);
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const invites_table = $("#admin_invites_table").expectOne();
|
2017-10-21 03:15:12 +02:00
|
|
|
|
2021-01-29 10:27:56 +01:00
|
|
|
ListWidget.create(invites_table, invites_data.invites, {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "admin_invites_list",
|
2020-07-20 22:18:43 +02:00
|
|
|
modifier(item) {
|
2020-04-11 15:17:26 +02:00
|
|
|
item.invited_absolute_time = timerender.absolute_time(item.invited * 1000);
|
2020-04-30 21:41:21 +02:00
|
|
|
item.is_admin = page_params.is_admin;
|
2020-07-15 00:34:28 +02:00
|
|
|
item.disable_buttons =
|
|
|
|
item.invited_as === settings_config.user_role_values.owner.code &&
|
|
|
|
!page_params.is_owner;
|
2020-07-03 22:03:39 +02:00
|
|
|
item.referrer_name = people.get_by_user_id(item.invited_by_user_id).full_name;
|
2020-07-16 22:40:18 +02:00
|
|
|
return render_admin_invites_list({invite: item});
|
2020-04-11 15:17:26 +02:00
|
|
|
},
|
|
|
|
filter: {
|
|
|
|
element: invites_table.closest(".settings-section").find(".search"),
|
2020-07-20 22:18:43 +02:00
|
|
|
predicate(item, value) {
|
2020-07-03 22:03:39 +02:00
|
|
|
const referrer = people.get_by_user_id(item.invited_by_user_id);
|
|
|
|
const referrer_email = referrer.email;
|
|
|
|
const referrer_name = referrer.full_name;
|
|
|
|
const referrer_name_matched = referrer_name.toLowerCase().includes(value);
|
2020-06-26 21:26:57 +02:00
|
|
|
const referrer_email_matched = referrer_email.toLowerCase().includes(value);
|
2020-04-11 15:17:26 +02:00
|
|
|
if (item.is_multiuse) {
|
2020-07-03 22:03:39 +02:00
|
|
|
return referrer_name_matched || referrer_email_matched;
|
2020-04-11 15:17:26 +02:00
|
|
|
}
|
|
|
|
const invitee_email_matched = item.email.toLowerCase().includes(value);
|
2020-07-03 22:03:39 +02:00
|
|
|
return referrer_email_matched || referrer_name_matched || invitee_email_matched;
|
2017-10-21 03:15:12 +02:00
|
|
|
},
|
2020-04-11 15:17:26 +02:00
|
|
|
},
|
|
|
|
parent_container: $("#admin-invites-list").expectOne(),
|
2020-04-11 16:23:29 +02:00
|
|
|
init_sort: [sort_invitee],
|
|
|
|
sort_fields: {
|
|
|
|
invitee: sort_invitee,
|
|
|
|
},
|
2020-07-15 01:29:15 +02:00
|
|
|
simplebar_container: $("#admin-invites-list .progressive-table-wrapper"),
|
2020-04-11 15:17:26 +02:00
|
|
|
});
|
2019-08-22 07:34:19 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
loading.destroy_indicator($("#admin_page_invites_loading_indicator"));
|
2017-10-21 03:15:12 +02:00
|
|
|
}
|
|
|
|
|
2019-02-15 23:12:41 +01:00
|
|
|
function do_revoke_invite() {
|
2021-07-05 12:41:37 +02:00
|
|
|
const modal_invite_id = $(".dialog_submit_button").attr("data-invite-id");
|
|
|
|
const modal_is_multiuse = $(".dialog_submit_button").attr("data-is-multiuse");
|
2019-11-02 00:06:25 +01:00
|
|
|
const revoke_button = meta.current_revoke_invite_user_modal_row.find("button.revoke");
|
2019-02-15 23:12:41 +01:00
|
|
|
|
2019-02-15 19:09:25 +01:00
|
|
|
if (modal_invite_id !== meta.invite_id || modal_is_multiuse !== meta.is_multiuse) {
|
2019-02-15 23:12:41 +01:00
|
|
|
blueslip.error("Invite revoking canceled due to non-matching fields.");
|
2021-01-27 08:16:28 +01:00
|
|
|
ui_report.client_error(
|
2021-04-13 05:18:25 +02:00
|
|
|
$t_html({
|
|
|
|
defaultMessage: "Resending encountered an error. Please reload and try again.",
|
|
|
|
}),
|
2020-07-15 00:34:28 +02:00
|
|
|
$("#home-error"),
|
|
|
|
);
|
2019-02-15 23:12:41 +01:00
|
|
|
}
|
2021-05-26 09:37:20 +02:00
|
|
|
|
2021-04-13 06:51:54 +02:00
|
|
|
revoke_button.prop("disabled", true).text($t({defaultMessage: "Working…"}));
|
2020-07-15 01:29:15 +02:00
|
|
|
let url = "/json/invites/" + meta.invite_id;
|
2019-02-15 19:09:25 +01:00
|
|
|
|
|
|
|
if (modal_is_multiuse === "true") {
|
2020-07-15 01:29:15 +02:00
|
|
|
url = "/json/invites/multiuse/" + meta.invite_id;
|
2019-02-15 19:09:25 +01:00
|
|
|
}
|
2019-02-15 23:12:41 +01:00
|
|
|
channel.del({
|
2020-07-20 22:18:43 +02:00
|
|
|
url,
|
|
|
|
error(xhr) {
|
2019-02-15 23:12:41 +01:00
|
|
|
ui_report.generic_row_button_error(xhr, revoke_button);
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
success() {
|
2019-02-15 23:12:41 +01:00
|
|
|
meta.current_revoke_invite_user_modal_row.remove();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2017-10-21 03:15:12 +02:00
|
|
|
|
2021-05-26 12:30:18 +02:00
|
|
|
function do_resend_invite() {
|
2021-07-05 12:41:37 +02:00
|
|
|
const modal_invite_id = $(".dialog_submit_button").attr("data-invite-id");
|
2021-05-26 12:30:18 +02:00
|
|
|
const resend_button = meta.current_resend_invite_user_modal_row.find("button.resend");
|
|
|
|
|
|
|
|
if (modal_invite_id !== meta.invite_id) {
|
|
|
|
blueslip.error("Invite resending canceled due to non-matching fields.");
|
|
|
|
ui_report.client_error(
|
|
|
|
$t_html({
|
|
|
|
defaultMessage: "Resending encountered an error. Please reload and try again.",
|
|
|
|
}),
|
|
|
|
$("#home-error"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
resend_button.prop("disabled", true).text($t({defaultMessage: "Working…"}));
|
|
|
|
channel.post({
|
|
|
|
url: "/json/invites/" + meta.invite_id + "/resend",
|
|
|
|
error(xhr) {
|
|
|
|
ui_report.generic_row_button_error(xhr, resend_button);
|
|
|
|
},
|
|
|
|
success(data) {
|
|
|
|
resend_button.text($t({defaultMessage: "Sent!"}));
|
|
|
|
resend_button.removeClass("resend btn-warning").addClass("sea-green");
|
|
|
|
data.timestamp = timerender.absolute_time(data.timestamp * 1000);
|
|
|
|
meta.current_resend_invite_user_modal_row.find(".invited_at").text(data.timestamp);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-24 20:14:12 +01:00
|
|
|
export function set_up(initialize_event_handlers = true) {
|
2017-10-21 03:15:12 +02:00
|
|
|
meta.loaded = true;
|
|
|
|
|
|
|
|
// create loading indicators
|
2020-07-15 01:29:15 +02:00
|
|
|
loading.make_indicator($("#admin_page_invites_loading_indicator"));
|
2017-10-21 03:15:12 +02:00
|
|
|
|
|
|
|
// Populate invites table
|
|
|
|
channel.get({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/invites",
|
2017-10-21 03:15:12 +02:00
|
|
|
idempotent: true,
|
2018-12-18 19:34:45 +01:00
|
|
|
timeout: 10 * 1000,
|
2020-07-20 22:18:43 +02:00
|
|
|
success(data) {
|
2021-02-28 01:22:20 +01:00
|
|
|
on_load_success(data, initialize_event_handlers);
|
2019-02-15 19:09:25 +01:00
|
|
|
},
|
2017-10-21 03:15:12 +02:00
|
|
|
error: failed_listing_invites,
|
|
|
|
});
|
2021-02-28 01:22:20 +01:00
|
|
|
}
|
2017-10-21 03:15:12 +02:00
|
|
|
|
2021-02-28 01:22:20 +01:00
|
|
|
export function on_load_success(invites_data, initialize_event_handlers) {
|
2017-10-21 03:15:12 +02:00
|
|
|
meta.loaded = true;
|
|
|
|
populate_invites(invites_data);
|
2019-02-15 19:09:25 +01:00
|
|
|
if (!initialize_event_handlers) {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-02 01:45:54 +02:00
|
|
|
$(".admin_invites_table").on("click", ".revoke", (e) => {
|
2018-05-23 22:29:00 +02:00
|
|
|
// This click event must not get propagated to parent container otherwise the modal
|
|
|
|
// will not show up because of a call to `close_active_modal` in `settings.js`.
|
2017-10-21 03:15:12 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2019-11-02 00:06:25 +01:00
|
|
|
const row = $(e.target).closest(".invite_row");
|
2020-07-15 01:29:15 +02:00
|
|
|
const email = row.find(".email").text();
|
|
|
|
const referred_by = row.find(".referred_by").text();
|
2017-10-21 03:15:12 +02:00
|
|
|
meta.current_revoke_invite_user_modal_row = row;
|
|
|
|
meta.invite_id = $(e.currentTarget).attr("data-invite-id");
|
2019-02-15 19:09:25 +01:00
|
|
|
meta.is_multiuse = $(e.currentTarget).attr("data-is-multiuse");
|
2020-07-15 00:34:28 +02:00
|
|
|
const ctx = {
|
|
|
|
is_multiuse: meta.is_multiuse === "true",
|
2020-07-20 22:18:43 +02:00
|
|
|
email,
|
|
|
|
referred_by,
|
2020-07-15 00:34:28 +02:00
|
|
|
};
|
2021-05-26 09:37:20 +02:00
|
|
|
const modal_parent = $("#admin_invites_table");
|
|
|
|
const html_body = render_settings_revoke_invite_modal(ctx);
|
|
|
|
|
|
|
|
confirm_dialog.launch({
|
|
|
|
parent: modal_parent,
|
|
|
|
html_heading: ctx.is_multiuse
|
|
|
|
? $t_html({defaultMessage: "Revoke invitation link"})
|
|
|
|
: $t_html({defaultMessage: "Revoke invitation to {email}"}, {email}),
|
|
|
|
html_body,
|
|
|
|
on_click: do_revoke_invite,
|
2021-06-09 16:35:08 +02:00
|
|
|
fade: true,
|
2021-05-26 09:37:20 +02:00
|
|
|
});
|
|
|
|
|
2021-07-05 12:41:37 +02:00
|
|
|
$(".dialog_submit_button").attr("data-invite-id", meta.invite_id);
|
|
|
|
$(".dialog_submit_button").attr("data-is-multiuse", meta.is_multiuse);
|
2017-10-21 03:15:12 +02:00
|
|
|
});
|
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
$(".admin_invites_table").on("click", ".resend", (e) => {
|
2018-05-23 22:29:00 +02:00
|
|
|
// This click event must not get propagated to parent container otherwise the modal
|
|
|
|
// will not show up because of a call to `close_active_modal` in `settings.js`.
|
2017-10-21 03:15:12 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const row = $(e.target).closest(".invite_row");
|
2020-07-15 01:29:15 +02:00
|
|
|
const email = row.find(".email").text();
|
2017-10-21 03:15:12 +02:00
|
|
|
meta.current_resend_invite_user_modal_row = row;
|
|
|
|
meta.invite_id = $(e.currentTarget).attr("data-invite-id");
|
2021-05-26 12:30:18 +02:00
|
|
|
const modal_parent = $("#admin_invites_table");
|
|
|
|
const html_body = render_settings_resend_invite_modal({email});
|
2017-10-21 03:15:12 +02:00
|
|
|
|
2021-05-26 12:30:18 +02:00
|
|
|
confirm_dialog.launch({
|
|
|
|
parent: modal_parent,
|
2021-05-28 10:26:32 +02:00
|
|
|
html_heading: $t_html({defaultMessage: "Resend invitation"}),
|
2021-05-26 12:30:18 +02:00
|
|
|
html_body,
|
|
|
|
on_click: do_resend_invite,
|
2021-06-09 16:35:08 +02:00
|
|
|
fade: true,
|
2017-10-21 03:15:12 +02:00
|
|
|
});
|
2021-05-26 12:30:18 +02:00
|
|
|
|
2021-07-05 12:41:37 +02:00
|
|
|
$(".dialog_submit_button").attr("data-invite-id", meta.invite_id);
|
2017-10-21 03:15:12 +02:00
|
|
|
});
|
2021-02-28 01:22:20 +01:00
|
|
|
}
|