2019-11-02 00:06:25 +01:00
|
|
|
const render_admin_invites_list = require("../templates/admin_invites_list.hbs");
|
|
|
|
const render_settings_revoke_invite_modal = require("../templates/settings/revoke_invite_modal.hbs");
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const meta = {
|
2017-10-21 03:15:12 +02:00
|
|
|
loaded: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.reset = function () {
|
|
|
|
meta.loaded = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
function failed_listing_invites(xhr) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-09 05:08:58 +01:00
|
|
|
exports.invited_as_values = new Map([
|
|
|
|
[1, i18n.t("Member")],
|
|
|
|
[2, i18n.t("Organization administrator")],
|
|
|
|
[3, i18n.t("Guest")],
|
|
|
|
]);
|
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) {
|
|
|
|
data.invited_as_text = exports.invited_as_values.get(data.invited_as);
|
|
|
|
}
|
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
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const admin_invites_list = list_render.get("admin_invites_list");
|
2018-02-23 00:26:12 +01:00
|
|
|
|
|
|
|
if (admin_invites_list) {
|
|
|
|
admin_invites_list.data(invites_data.invites);
|
|
|
|
admin_invites_list.set_container(invites_table);
|
|
|
|
admin_invites_list.render();
|
|
|
|
} else {
|
2019-11-02 00:06:25 +01:00
|
|
|
const invites_list = list_render.create(invites_table, invites_data.invites, {
|
2018-02-23 00:26:12 +01:00
|
|
|
name: "admin_invites_list",
|
|
|
|
modifier: function (item) {
|
2019-02-15 19:28:03 +01:00
|
|
|
item.invited_absolute_time = timerender.absolute_time(item.invited * 1000);
|
2019-07-09 21:24:00 +02:00
|
|
|
return render_admin_invites_list({ invite: item });
|
2018-02-23 00:26:12 +01:00
|
|
|
},
|
|
|
|
filter: {
|
|
|
|
element: invites_table.closest(".settings-section").find(".search"),
|
2019-12-30 17:44:24 +01:00
|
|
|
predicate: function (item, value) {
|
js: Convert a.indexOf(…) !== -1 to a.includes(…).
Babel polyfills this for us for Internet Explorer.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import K from "ast-types/gen/kinds";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
recast.visit(ast, {
visitBinaryExpression(path) {
const { operator, left, right } = path.node;
if (
n.CallExpression.check(left) &&
n.MemberExpression.check(left.callee) &&
!left.callee.computed &&
n.Identifier.check(left.callee.property) &&
left.callee.property.name === "indexOf" &&
left.arguments.length === 1 &&
checkExpression(left.arguments[0]) &&
((["===", "!==", "==", "!=", ">", "<="].includes(operator) &&
n.UnaryExpression.check(right) &&
right.operator == "-" &&
n.Literal.check(right.argument) &&
right.argument.value === 1) ||
([">=", "<"].includes(operator) &&
n.Literal.check(right) &&
right.value === 0))
) {
const test = b.callExpression(
b.memberExpression(left.callee.object, b.identifier("includes")),
[left.arguments[0]]
);
path.replace(
["!==", "!=", ">", ">="].includes(operator)
? test
: b.unaryExpression("!", test)
);
changed = true;
}
this.traverse(path);
},
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-08 04:55:06 +01:00
|
|
|
const referrer_email_matched = item.ref.toLowerCase().includes(value);
|
2019-02-15 19:09:25 +01:00
|
|
|
if (item.is_multiuse) {
|
|
|
|
return referrer_email_matched;
|
|
|
|
}
|
js: Convert a.indexOf(…) !== -1 to a.includes(…).
Babel polyfills this for us for Internet Explorer.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import K from "ast-types/gen/kinds";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
recast.visit(ast, {
visitBinaryExpression(path) {
const { operator, left, right } = path.node;
if (
n.CallExpression.check(left) &&
n.MemberExpression.check(left.callee) &&
!left.callee.computed &&
n.Identifier.check(left.callee.property) &&
left.callee.property.name === "indexOf" &&
left.arguments.length === 1 &&
checkExpression(left.arguments[0]) &&
((["===", "!==", "==", "!=", ">", "<="].includes(operator) &&
n.UnaryExpression.check(right) &&
right.operator == "-" &&
n.Literal.check(right.argument) &&
right.argument.value === 1) ||
([">=", "<"].includes(operator) &&
n.Literal.check(right) &&
right.value === 0))
) {
const test = b.callExpression(
b.memberExpression(left.callee.object, b.identifier("includes")),
[left.arguments[0]]
);
path.replace(
["!==", "!=", ">", ">="].includes(operator)
? test
: b.unaryExpression("!", test)
);
changed = true;
}
this.traverse(path);
},
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-08 04:55:06 +01:00
|
|
|
const invitee_email_matched = item.email.toLowerCase().includes(value);
|
2019-02-15 19:09:25 +01:00
|
|
|
return referrer_email_matched || invitee_email_matched;
|
2018-02-23 00:26:12 +01:00
|
|
|
},
|
2017-10-21 03:15:12 +02:00
|
|
|
},
|
2019-08-22 07:34:19 +02:00
|
|
|
parent_container: $("#admin-invites-list").expectOne(),
|
2018-02-23 00:26:12 +01:00
|
|
|
}).init();
|
2019-08-22 07:34:19 +02:00
|
|
|
|
|
|
|
invites_list.sort("alphabetic", "email");
|
2018-02-23 00:26:12 +01:00
|
|
|
}
|
2017-10-21 03:15:12 +02:00
|
|
|
|
|
|
|
loading.destroy_indicator($('#admin_page_invites_loading_indicator'));
|
|
|
|
}
|
|
|
|
|
2019-02-15 23:12:41 +01:00
|
|
|
function do_revoke_invite() {
|
2019-11-02 00:06:25 +01: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");
|
|
|
|
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.");
|
|
|
|
ui_report.message(i18n.t("Resending encountered an error. Please reload and try again."),
|
|
|
|
$("#home-error"), 'alert-error');
|
|
|
|
}
|
|
|
|
$("#revoke_invite_modal").modal("hide");
|
|
|
|
revoke_button.prop("disabled", true).text(i18n.t("Working…"));
|
2019-11-02 00:06:25 +01:00
|
|
|
let url = '/json/invites/' + meta.invite_id;
|
2019-02-15 19:09:25 +01:00
|
|
|
|
|
|
|
if (modal_is_multiuse === "true") {
|
|
|
|
url = '/json/invites/multiuse/' + meta.invite_id;
|
|
|
|
}
|
2019-02-15 23:12:41 +01:00
|
|
|
channel.del({
|
2019-02-15 19:09:25 +01:00
|
|
|
url: url,
|
2019-02-15 23:12:41 +01:00
|
|
|
error: function (xhr) {
|
|
|
|
ui_report.generic_row_button_error(xhr, revoke_button);
|
|
|
|
},
|
|
|
|
success: function () {
|
|
|
|
meta.current_revoke_invite_user_modal_row.remove();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2017-10-21 03:15:12 +02:00
|
|
|
|
2019-02-15 19:09:25 +01:00
|
|
|
exports.set_up = function (initialize_event_handlers) {
|
2017-10-21 03:15:12 +02:00
|
|
|
meta.loaded = true;
|
2019-02-15 19:09:25 +01:00
|
|
|
if (typeof initialize_event_handlers === 'undefined') {
|
|
|
|
initialize_event_handlers = true;
|
|
|
|
}
|
2017-10-21 03:15:12 +02:00
|
|
|
|
|
|
|
// create loading indicators
|
|
|
|
loading.make_indicator($('#admin_page_invites_loading_indicator'));
|
|
|
|
|
|
|
|
// Populate invites table
|
|
|
|
channel.get({
|
|
|
|
url: '/json/invites',
|
|
|
|
idempotent: true,
|
2018-12-18 19:34:45 +01:00
|
|
|
timeout: 10 * 1000,
|
2019-02-15 19:09:25 +01:00
|
|
|
success: function (data) {
|
|
|
|
exports.on_load_success(data, initialize_event_handlers);
|
|
|
|
},
|
2017-10-21 03:15:12 +02:00
|
|
|
error: failed_listing_invites,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-02-15 19:09:25 +01:00
|
|
|
exports.on_load_success = function (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;
|
|
|
|
}
|
2017-10-21 03:15:12 +02:00
|
|
|
$(".admin_invites_table").on("click", ".revoke", function (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");
|
|
|
|
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");
|
2019-11-02 00:06:25 +01:00
|
|
|
const ctx = {is_multiuse: meta.is_multiuse === "true", email: email, referred_by: referred_by};
|
|
|
|
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);
|
2019-02-15 19:09:25 +01: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");
|
2019-02-15 19:09:25 +01:00
|
|
|
$("#do_revoke_invite_button").unbind("click");
|
|
|
|
$("#do_revoke_invite_button").click(do_revoke_invite);
|
2017-10-21 03:15:12 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$(".admin_invites_table").on("click", ".resend", function (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");
|
|
|
|
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");
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#do_resend_invite_button").click(function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const modal_invite_id = $("#resend_invite_modal #do_resend_invite_button").attr("data-invite-id");
|
|
|
|
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.");
|
2017-11-04 15:42:38 +01:00
|
|
|
ui_report.message(i18n.t("Resending encountered an error. Please reload and try again."),
|
2018-05-06 21:43:17 +02:00
|
|
|
$("#home-error"), 'alert-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({
|
|
|
|
url: '/json/invites/' + meta.invite_id + "/resend",
|
|
|
|
error: function (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
|
|
|
},
|
|
|
|
success: function (data) {
|
2017-12-05 06:31:29 +01:00
|
|
|
resend_button.text(i18n.t("Sent!"));
|
2017-10-21 03:15:12 +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);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.settings_invites = exports;
|