2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-02-28 01:22:20 +01:00
|
|
|
import render_admin_invites_list from "../templates/admin_invites_list.hbs";
|
|
|
|
import render_settings_revoke_invite_modal from "../templates/settings/revoke_invite_modal.hbs";
|
|
|
|
|
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";
|
|
|
|
import * as ListWidget from "./list_widget";
|
|
|
|
import * as loading from "./loading";
|
|
|
|
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"));
|
2019-01-15 09:38:52 +01:00
|
|
|
ui_report.error(i18n.t("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() {
|
2020-07-15 00:34:28 +02:00
|
|
|
const modal_invite_id = $("#revoke_invite_modal #do_revoke_invite_button").attr(
|
|
|
|
"data-invite-id",
|
|
|
|
);
|
|
|
|
const modal_is_multiuse = $("#revoke_invite_modal #do_revoke_invite_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(
|
2020-07-15 00:34:28 +02:00
|
|
|
i18n.t("Resending encountered an error. Please reload and try again."),
|
|
|
|
$("#home-error"),
|
|
|
|
);
|
2019-02-15 23:12:41 +01:00
|
|
|
}
|
|
|
|
$("#revoke_invite_modal").modal("hide");
|
|
|
|
revoke_button.prop("disabled", true).text(i18n.t("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-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
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const rendered_revoke_modal = render_settings_revoke_invite_modal(ctx);
|
2019-02-15 19:09:25 +01:00
|
|
|
$("#revoke_invite_modal_holder").html(rendered_revoke_modal);
|
2017-10-21 03:15:12 +02:00
|
|
|
$("#revoke_invite_modal #do_revoke_invite_button").attr("data-invite-id", meta.invite_id);
|
2020-07-15 00:34:28 +02:00
|
|
|
$("#revoke_invite_modal #do_revoke_invite_button").attr(
|
|
|
|
"data-is-multiuse",
|
|
|
|
meta.is_multiuse,
|
|
|
|
);
|
2017-10-21 03:15:12 +02:00
|
|
|
$("#revoke_invite_modal").modal("show");
|
2020-07-22 03:47:08 +02:00
|
|
|
$("#do_revoke_invite_button").off("click");
|
2020-07-20 21:26:58 +02:00
|
|
|
$("#do_revoke_invite_button").on("click", do_revoke_invite);
|
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");
|
|
|
|
|
|
|
|
$("#resend_invite_modal .email").text(email);
|
|
|
|
$("#resend_invite_modal #do_resend_invite_button").attr("data-invite-id", meta.invite_id);
|
|
|
|
$("#resend_invite_modal").modal("show");
|
|
|
|
});
|
|
|
|
|
2020-07-20 21:26:58 +02:00
|
|
|
$("#do_resend_invite_button").on("click", () => {
|
2020-07-15 00:34:28 +02:00
|
|
|
const modal_invite_id = $("#resend_invite_modal #do_resend_invite_button").attr(
|
|
|
|
"data-invite-id",
|
|
|
|
);
|
2019-11-02 00:06:25 +01:00
|
|
|
const resend_button = meta.current_resend_invite_user_modal_row.find("button.resend");
|
2017-10-21 03:15:12 +02:00
|
|
|
|
|
|
|
if (modal_invite_id !== meta.invite_id) {
|
|
|
|
blueslip.error("Invite resending canceled due to non-matching fields.");
|
2021-01-27 08:16:28 +01:00
|
|
|
ui_report.client_error(
|
2020-07-15 00:34:28 +02:00
|
|
|
i18n.t("Resending encountered an error. Please reload and try again."),
|
|
|
|
$("#home-error"),
|
|
|
|
);
|
2017-10-21 03:15:12 +02:00
|
|
|
}
|
|
|
|
$("#resend_invite_modal").modal("hide");
|
|
|
|
resend_button.prop("disabled", true).text(i18n.t("Working…"));
|
|
|
|
channel.post({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/invites/" + meta.invite_id + "/resend",
|
2020-07-20 22:18:43 +02:00
|
|
|
error(xhr) {
|
2018-03-25 11:12:06 +02:00
|
|
|
ui_report.generic_row_button_error(xhr, resend_button);
|
2017-10-21 03:15:12 +02:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
success(data) {
|
2017-12-05 06:31:29 +01:00
|
|
|
resend_button.text(i18n.t("Sent!"));
|
2020-07-15 01:29:15 +02:00
|
|
|
resend_button.removeClass("resend btn-warning").addClass("sea-green");
|
2017-12-19 08:23:03 +01:00
|
|
|
data.timestamp = timerender.absolute_time(data.timestamp * 1000);
|
2017-10-21 03:15:12 +02:00
|
|
|
meta.current_resend_invite_user_modal_row.find(".invited_at").text(data.timestamp);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2021-02-28 01:22:20 +01:00
|
|
|
}
|