2019-11-02 00:06:25 +01:00
|
|
|
const render_bot_avatar_row = require('../templates/bot_avatar_row.hbs');
|
|
|
|
const render_edit_bot = require('../templates/edit_bot.hbs');
|
|
|
|
const render_settings_edit_embedded_bot_service = require("../templates/settings/edit_embedded_bot_service.hbs");
|
|
|
|
const render_settings_edit_outgoing_webhook_service = require("../templates/settings/edit_outgoing_webhook_service.hbs");
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2018-07-02 21:19:18 +02:00
|
|
|
exports.hide_errors = function () {
|
|
|
|
$('#bot_table_error').hide();
|
|
|
|
$('.bot_error').hide();
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const focus_tab = {
|
2018-03-30 12:04:40 +02:00
|
|
|
add_a_new_bot_tab: function () {
|
|
|
|
$("#bots_lists_navbar .active").removeClass("active");
|
|
|
|
$("#bots_lists_navbar .add-a-new-bot-tab").addClass("active");
|
|
|
|
$("#add-a-new-bot-form").show();
|
|
|
|
$("#active_bots_list").hide();
|
|
|
|
$("#inactive_bots_list").hide();
|
2018-07-02 21:19:18 +02:00
|
|
|
exports.hide_errors();
|
2018-03-30 12:04:40 +02:00
|
|
|
},
|
|
|
|
active_bots_tab: function () {
|
|
|
|
$("#bots_lists_navbar .active").removeClass("active");
|
|
|
|
$("#bots_lists_navbar .active-bots-tab").addClass("active");
|
|
|
|
$("#add-a-new-bot-form").hide();
|
|
|
|
$("#active_bots_list").show();
|
|
|
|
$("#inactive_bots_list").hide();
|
2018-07-02 21:19:18 +02:00
|
|
|
exports.hide_errors();
|
2018-03-30 12:04:40 +02:00
|
|
|
},
|
|
|
|
inactive_bots_tab: function () {
|
|
|
|
$("#bots_lists_navbar .active").removeClass("active");
|
|
|
|
$("#bots_lists_navbar .inactive-bots-tab").addClass("active");
|
|
|
|
$("#add-a-new-bot-form").hide();
|
|
|
|
$("#active_bots_list").hide();
|
|
|
|
$("#inactive_bots_list").show();
|
2018-07-02 21:19:18 +02:00
|
|
|
exports.hide_errors();
|
2018-03-30 12:04:40 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-07-02 21:19:18 +02:00
|
|
|
exports.get_bot_info_div = function (bot_id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const sel = '.bot_info[data-user-id="' + bot_id + '"]';
|
2018-07-02 21:19:18 +02:00
|
|
|
return $(sel).expectOne();
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.bot_error = function (bot_id, xhr) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const bot_info = exports.get_bot_info_div(bot_id);
|
|
|
|
const bot_error_div = bot_info.find('.bot_error');
|
2018-07-02 21:19:18 +02:00
|
|
|
bot_error_div.text(JSON.parse(xhr.responseText).msg);
|
|
|
|
bot_error_div.show();
|
2019-11-02 00:06:25 +01:00
|
|
|
const bot_box = bot_info.closest('.bot-information-box');
|
2018-07-03 01:56:08 +02:00
|
|
|
bot_box.scrollTop(bot_box[0].scrollHeight - bot_box[0].clientHeight);
|
2018-07-02 21:19:18 +02:00
|
|
|
};
|
|
|
|
|
2017-04-06 22:47:27 +02:00
|
|
|
function add_bot_row(info) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const row = $(render_bot_avatar_row(info));
|
2017-04-06 22:47:27 +02:00
|
|
|
if (info.is_active) {
|
|
|
|
$('#active_bots_list').append(row);
|
|
|
|
} else {
|
|
|
|
$('#inactive_bots_list').append(row);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function is_local_part(value, element) {
|
|
|
|
// Adapted from Django's EmailValidator
|
|
|
|
return this.optional(element) || /^[\-!#$%&'*+\/=?\^_`{}|~0-9A-Z]+(\.[\-!#$%&'*+\/=?\^_`{}|~0-9A-Z]+)*$/i.test(value);
|
|
|
|
}
|
|
|
|
|
2017-06-10 15:29:01 +02:00
|
|
|
exports.type_id_to_string = function (type_id) {
|
2020-02-08 05:31:13 +01:00
|
|
|
const name = page_params.bot_types.find(bot_type => bot_type.type_id === type_id).name;
|
2017-11-22 23:58:58 +01:00
|
|
|
return i18n.t(name);
|
2017-06-10 15:29:01 +02:00
|
|
|
};
|
2017-06-12 19:50:03 +02:00
|
|
|
|
2018-07-02 20:06:09 +02:00
|
|
|
exports.render_bots = function () {
|
2017-04-06 22:47:27 +02:00
|
|
|
$('#active_bots_list').empty();
|
|
|
|
$('#inactive_bots_list').empty();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const all_bots_for_current_user = bot_data.get_all_bots_for_current_user();
|
|
|
|
let user_owns_an_active_bot = false;
|
2018-03-29 22:11:32 +02:00
|
|
|
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
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 { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
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);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.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;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
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-06 06:19:47 +01:00
|
|
|
for (const elem of all_bots_for_current_user) {
|
2017-04-06 22:47:27 +02:00
|
|
|
add_bot_row({
|
|
|
|
name: elem.full_name,
|
|
|
|
email: elem.email,
|
2018-01-22 18:36:53 +01:00
|
|
|
user_id: elem.user_id,
|
2017-06-10 15:29:01 +02:00
|
|
|
type: exports.type_id_to_string(elem.bot_type),
|
2017-04-06 22:47:27 +02:00
|
|
|
avatar_url: elem.avatar_url,
|
|
|
|
api_key: elem.api_key,
|
|
|
|
is_active: elem.is_active,
|
|
|
|
zuliprc: 'zuliprc', // Most browsers do not allow filename starting with `.`
|
|
|
|
});
|
2018-03-29 22:11:32 +02:00
|
|
|
user_owns_an_active_bot = user_owns_an_active_bot || elem.is_active;
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
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 { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
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);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.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;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
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-06 06:19:47 +01:00
|
|
|
}
|
2017-04-06 22:47:27 +02:00
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
if (exports.can_create_new_bots()) {
|
2018-03-29 22:11:32 +02:00
|
|
|
if (!user_owns_an_active_bot) {
|
|
|
|
focus_tab.add_a_new_bot_tab();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-29 07:38:53 +02:00
|
|
|
if ($("#bots_lists_navbar .add-a-new-bot-tab").hasClass("active")) {
|
|
|
|
$("#add-a-new-bot-form").show();
|
|
|
|
$("#active_bots_list").hide();
|
|
|
|
$("#inactive_bots_list").hide();
|
|
|
|
} else if ($("#bots_lists_navbar .active-bots-tab").hasClass("active")) {
|
|
|
|
$("#add-a-new-bot-form").hide();
|
2017-04-06 22:47:27 +02:00
|
|
|
$("#active_bots_list").show();
|
|
|
|
$("#inactive_bots_list").hide();
|
|
|
|
} else {
|
2017-06-29 07:38:53 +02:00
|
|
|
$("#add-a-new-bot-form").hide();
|
2017-04-06 22:47:27 +02:00
|
|
|
$("#active_bots_list").hide();
|
|
|
|
$("#inactive_bots_list").show();
|
|
|
|
}
|
2018-07-02 20:06:09 +02:00
|
|
|
};
|
2017-04-06 22:47:27 +02:00
|
|
|
|
2018-06-01 11:27:02 +02:00
|
|
|
exports.generate_zuliprc_uri = function (bot_id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const bot = bot_data.get(bot_id);
|
2020-02-01 00:04:23 +01:00
|
|
|
const data = exports.generate_zuliprc_content(bot);
|
2018-06-04 16:20:14 +02:00
|
|
|
return exports.encode_zuliprc_as_uri(data);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.encode_zuliprc_as_uri = function (zuliprc) {
|
|
|
|
return "data:application/octet-stream;charset=utf-8," + encodeURIComponent(zuliprc);
|
2017-04-06 22:47:27 +02:00
|
|
|
};
|
|
|
|
|
2020-02-01 00:04:23 +01:00
|
|
|
exports.generate_zuliprc_content = function (bot) {
|
|
|
|
let token;
|
|
|
|
// For outgoing webhooks, include the token in the zuliprc.
|
|
|
|
// It's needed for authenticating to the Botserver.
|
|
|
|
if (bot.bot_type === 3) {
|
|
|
|
token = bot_data.get_services(bot.user_id)[0].token;
|
|
|
|
}
|
2017-04-06 22:47:27 +02:00
|
|
|
return "[api]" +
|
2020-02-01 00:04:23 +01:00
|
|
|
"\nemail=" + bot.email +
|
|
|
|
"\nkey=" + bot.api_key +
|
2017-04-06 22:47:27 +02:00
|
|
|
"\nsite=" + page_params.realm_uri +
|
2018-06-06 18:19:09 +02:00
|
|
|
(token === undefined ? "" : "\ntoken=" + token) +
|
2017-04-06 22:47:27 +02:00
|
|
|
// Some tools would not work in files without a trailing new line.
|
|
|
|
"\n";
|
|
|
|
};
|
|
|
|
|
2018-05-30 11:09:35 +02:00
|
|
|
exports.generate_botserverrc_content = function (email, api_key, token) {
|
2018-05-23 16:28:41 +02:00
|
|
|
return "[]" +
|
2017-05-30 16:12:02 +02:00
|
|
|
"\nemail=" + email +
|
|
|
|
"\nkey=" + api_key +
|
|
|
|
"\nsite=" + page_params.realm_uri +
|
2018-05-30 11:09:35 +02:00
|
|
|
"\ntoken=" + token +
|
2017-05-30 16:12:02 +02:00
|
|
|
"\n";
|
|
|
|
};
|
|
|
|
|
2018-05-30 18:43:07 +02:00
|
|
|
exports.bot_creation_policy_values = {
|
|
|
|
admins_only: {
|
|
|
|
code: 3,
|
2019-05-22 07:58:00 +02:00
|
|
|
description: i18n.t("Admins"),
|
|
|
|
},
|
|
|
|
everyone: {
|
|
|
|
code: 1,
|
|
|
|
description: i18n.t("Admins and members"),
|
2018-05-30 18:43:07 +02:00
|
|
|
},
|
|
|
|
restricted: {
|
|
|
|
code: 2,
|
2019-05-22 07:58:00 +02:00
|
|
|
description: i18n.t("Admins and members, but only admins can add generic bots"),
|
2018-05-30 18:43:07 +02:00
|
|
|
},
|
2018-01-29 16:10:54 +01:00
|
|
|
};
|
|
|
|
|
2018-06-13 16:59:15 +02:00
|
|
|
exports.can_create_new_bots = function () {
|
|
|
|
if (page_params.is_admin) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-13 17:31:58 +02:00
|
|
|
if (page_params.is_guest) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-13 16:59:15 +02:00
|
|
|
return page_params.realm_bot_creation_policy !==
|
|
|
|
exports.bot_creation_policy_values.admins_only.code;
|
|
|
|
};
|
|
|
|
|
2018-01-29 16:10:54 +01:00
|
|
|
exports.update_bot_settings_tip = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const permission_type = exports.bot_creation_policy_values;
|
|
|
|
const current_permission = page_params.realm_bot_creation_policy;
|
|
|
|
let tip_text;
|
2018-01-29 16:10:54 +01:00
|
|
|
if (current_permission === permission_type.admins_only.code) {
|
|
|
|
tip_text = i18n.t("Only organization administrators can add bots to this organization");
|
|
|
|
} else if (current_permission === permission_type.restricted.code) {
|
2018-03-29 11:13:55 +02:00
|
|
|
tip_text = i18n.t("Only organization administrators can add generic bots");
|
2018-01-29 16:10:54 +01:00
|
|
|
} else {
|
|
|
|
tip_text = i18n.t("Anyone in this organization can add bots");
|
|
|
|
}
|
|
|
|
$(".bot-settings-tip").text(tip_text);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.update_bot_permissions_ui = function () {
|
|
|
|
exports.update_bot_settings_tip();
|
2018-07-02 21:19:18 +02:00
|
|
|
exports.hide_errors();
|
2018-01-29 16:10:54 +01:00
|
|
|
$("#id_realm_bot_creation_policy").val(page_params.realm_bot_creation_policy);
|
2018-06-13 16:59:15 +02:00
|
|
|
if (!exports.can_create_new_bots()) {
|
2018-01-29 16:10:54 +01:00
|
|
|
$('#create_bot_form').hide();
|
|
|
|
$('.add-a-new-bot-tab').hide();
|
2018-03-30 12:04:40 +02:00
|
|
|
focus_tab.active_bots_tab();
|
2018-01-29 16:10:54 +01:00
|
|
|
} else {
|
|
|
|
$('#create_bot_form').show();
|
|
|
|
$('.add-a-new-bot-tab').show();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-06 22:47:27 +02:00
|
|
|
exports.set_up = function () {
|
2017-06-10 18:43:31 +02:00
|
|
|
$('#payload_url_inputbox').hide();
|
|
|
|
$('#create_payload_url').val('');
|
2017-07-15 18:23:44 +02:00
|
|
|
$('#service_name_list').hide();
|
2018-01-07 19:24:14 +01:00
|
|
|
$('#config_inputbox').hide();
|
2019-11-02 00:06:25 +01:00
|
|
|
const selected_embedded_bot = 'converter';
|
2018-01-07 19:24:14 +01:00
|
|
|
$('#select_service_name').val(selected_embedded_bot); // TODO: Use 'select a bot'.
|
|
|
|
$('#config_inputbox').children().hide();
|
2018-06-04 21:13:07 +02:00
|
|
|
$("[name*='" + selected_embedded_bot + "']").show();
|
2017-04-06 22:47:27 +02:00
|
|
|
|
2018-05-29 10:18:06 +02:00
|
|
|
$('#download_botserverrc').click(function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const OUTGOING_WEBHOOK_BOT_TYPE_INT = 3;
|
|
|
|
let content = "";
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
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 { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
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);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.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;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
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-06 06:19:47 +01:00
|
|
|
|
|
|
|
for (const bot of bot_data.get_all_bots_for_current_user()) {
|
2018-01-22 18:36:53 +01:00
|
|
|
if (bot.is_active && bot.bot_type === OUTGOING_WEBHOOK_BOT_TYPE_INT) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const bot_token = bot_data.get_services(bot.user_id)[0].token;
|
2018-05-30 11:09:35 +02:00
|
|
|
content += exports.generate_botserverrc_content(bot.email, bot.api_key, bot_token);
|
2017-05-30 16:12:02 +02:00
|
|
|
}
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
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 { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
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);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.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;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
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-06 06:19:47 +01:00
|
|
|
}
|
|
|
|
|
2017-05-30 16:12:02 +02:00
|
|
|
$(this).attr("href", "data:application/octet-stream;charset=utf-8," + encodeURIComponent(content));
|
|
|
|
});
|
|
|
|
|
2018-07-02 20:06:09 +02:00
|
|
|
exports.render_bots();
|
2017-04-06 22:47:27 +02:00
|
|
|
|
|
|
|
$.validator.addMethod("bot_local_part",
|
|
|
|
function (value, element) {
|
|
|
|
return is_local_part.call(this, value + "-bot", element);
|
|
|
|
},
|
|
|
|
"Please only use characters that are valid in an email address");
|
|
|
|
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const create_avatar_widget = avatar.build_bot_create_widget();
|
|
|
|
const OUTGOING_WEBHOOK_BOT_TYPE = '3';
|
|
|
|
const GENERIC_BOT_TYPE = '1';
|
|
|
|
const EMBEDDED_BOT_TYPE = '4';
|
2017-04-06 22:47:27 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const GENERIC_INTERFACE = '1';
|
2017-07-03 18:35:12 +02:00
|
|
|
|
2017-04-06 22:47:27 +02:00
|
|
|
$('#create_bot_form').validate({
|
|
|
|
errorClass: 'text-error',
|
|
|
|
success: function () {
|
2018-07-02 21:19:18 +02:00
|
|
|
exports.hide_errors();
|
2017-04-06 22:47:27 +02:00
|
|
|
},
|
|
|
|
submitHandler: function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const bot_type = $('#create_bot_type :selected').val();
|
|
|
|
const full_name = $('#create_bot_name').val();
|
|
|
|
const short_name = $('#create_bot_short_name').val() || $('#create_bot_short_name').text();
|
|
|
|
const payload_url = $('#create_payload_url').val();
|
|
|
|
const interface_type = $('#create_interface_type').val();
|
|
|
|
const service_name = $('#select_service_name :selected').val();
|
|
|
|
const formData = new FormData();
|
|
|
|
const spinner = $('.create_bot_spinner');
|
2017-04-06 22:47:27 +02:00
|
|
|
|
|
|
|
formData.append('csrfmiddlewaretoken', csrf_token);
|
2017-05-30 19:19:48 +02:00
|
|
|
formData.append('bot_type', bot_type);
|
2017-04-06 22:47:27 +02:00
|
|
|
formData.append('full_name', full_name);
|
|
|
|
formData.append('short_name', short_name);
|
2017-06-10 18:43:31 +02:00
|
|
|
|
|
|
|
// If the selected bot_type is Outgoing webhook
|
|
|
|
if (bot_type === OUTGOING_WEBHOOK_BOT_TYPE) {
|
|
|
|
formData.append('payload_url', JSON.stringify(payload_url));
|
2017-07-03 18:35:12 +02:00
|
|
|
formData.append('interface_type', interface_type);
|
2017-07-15 18:23:44 +02:00
|
|
|
} else if (bot_type === EMBEDDED_BOT_TYPE) {
|
|
|
|
formData.append('service_name', service_name);
|
2019-11-02 00:06:25 +01:00
|
|
|
const config_data = {};
|
2018-06-04 21:13:07 +02:00
|
|
|
$("#config_inputbox [name*='" + service_name + "'] input").each(function () {
|
2018-01-07 19:24:14 +01:00
|
|
|
config_data[$(this).attr('name')] = $(this).val();
|
|
|
|
});
|
|
|
|
formData.append('config_data', JSON.stringify(config_data));
|
2017-06-10 18:43:31 +02:00
|
|
|
}
|
2020-02-08 01:53:49 +01:00
|
|
|
for (const [i, file] of Array.prototype.entries.call($('#bot_avatar_file_input')[0].files)) {
|
2018-06-04 21:13:07 +02:00
|
|
|
formData.append('file-' + i, file);
|
2020-02-08 01:53:49 +01:00
|
|
|
}
|
2018-02-14 09:29:33 +01:00
|
|
|
loading.make_indicator(spinner, {text: i18n.t('Creating bot')});
|
2017-04-06 22:47:27 +02:00
|
|
|
channel.post({
|
|
|
|
url: '/json/bots',
|
|
|
|
data: formData,
|
|
|
|
cache: false,
|
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
|
|
|
success: function () {
|
2018-07-02 21:19:18 +02:00
|
|
|
exports.hide_errors();
|
2017-04-06 22:47:27 +02:00
|
|
|
$('#create_bot_name').val('');
|
|
|
|
$('#create_bot_short_name').val('');
|
2017-06-10 18:43:31 +02:00
|
|
|
$('#create_payload_url').val('');
|
|
|
|
$('#payload_url_inputbox').hide();
|
2018-01-07 19:24:14 +01:00
|
|
|
$('#config_inputbox').hide();
|
2018-06-04 21:13:07 +02:00
|
|
|
$("[name*='" + service_name + "'] input").each(function () {
|
2018-01-07 19:24:14 +01:00
|
|
|
$(this).val('');
|
|
|
|
});
|
2017-06-10 18:43:31 +02:00
|
|
|
$('#create_bot_type').val(GENERIC_BOT_TYPE);
|
2018-01-07 18:51:16 +01:00
|
|
|
$('#select_service_name').val('converter'); // TODO: Later we can change this to hello bot or similar
|
2017-07-15 18:23:44 +02:00
|
|
|
$('#service_name_list').hide();
|
2017-04-06 22:47:27 +02:00
|
|
|
$('#create_bot_button').show();
|
2017-07-03 18:35:12 +02:00
|
|
|
$('#create_interface_type').val(GENERIC_INTERFACE);
|
2017-04-06 22:47:27 +02:00
|
|
|
create_avatar_widget.clear();
|
2017-07-07 13:45:53 +02:00
|
|
|
$("#bots_lists_navbar .add-a-new-bot-tab").removeClass("active");
|
|
|
|
$("#bots_lists_navbar .active-bots-tab").addClass("active");
|
2017-04-06 22:47:27 +02:00
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
$('#bot_table_error').text(JSON.parse(xhr.responseText).msg).show();
|
|
|
|
},
|
|
|
|
complete: function () {
|
2018-02-14 09:29:33 +01:00
|
|
|
loading.destroy_indicator(spinner);
|
2017-04-06 22:47:27 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2017-06-10 18:43:31 +02:00
|
|
|
$("#create_bot_type").on("change", function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const bot_type = $('#create_bot_type :selected').val();
|
2017-07-15 18:23:44 +02:00
|
|
|
// For "generic bot" or "incoming webhook" both these fields need not be displayed.
|
|
|
|
$('#service_name_list').hide();
|
|
|
|
$('#select_service_name').removeClass('required');
|
2018-01-07 19:24:14 +01:00
|
|
|
$('#config_inputbox').hide();
|
2017-07-15 18:23:44 +02:00
|
|
|
|
|
|
|
$('#payload_url_inputbox').hide();
|
|
|
|
$('#create_payload_url').removeClass('required');
|
2017-06-10 18:43:31 +02:00
|
|
|
if (bot_type === OUTGOING_WEBHOOK_BOT_TYPE) {
|
|
|
|
$('#payload_url_inputbox').show();
|
|
|
|
$('#create_payload_url').addClass('required');
|
2017-07-15 18:23:44 +02:00
|
|
|
|
|
|
|
} else if (bot_type === EMBEDDED_BOT_TYPE) {
|
|
|
|
$('#service_name_list').show();
|
|
|
|
$('#select_service_name').addClass('required');
|
2018-01-07 19:24:14 +01:00
|
|
|
$("#select_service_name").trigger('change');
|
|
|
|
$('#config_inputbox').show();
|
2017-06-10 18:43:31 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-01-07 19:24:14 +01:00
|
|
|
$("#select_service_name").on("change", function () {
|
|
|
|
$('#config_inputbox').children().hide();
|
2019-11-02 00:06:25 +01:00
|
|
|
const selected_bot = $('#select_service_name :selected').val();
|
2018-06-04 21:13:07 +02:00
|
|
|
$("[name*='" + selected_bot + "']").show();
|
2018-01-07 19:24:14 +01:00
|
|
|
});
|
|
|
|
|
2017-04-06 22:47:27 +02:00
|
|
|
$("#active_bots_list").on("click", "button.delete_bot", function (e) {
|
2020-01-16 20:40:20 +01:00
|
|
|
const bot_id = parseInt($(e.currentTarget).attr('data-user-id'), 10);
|
2018-05-15 15:26:04 +02:00
|
|
|
|
2017-04-06 22:47:27 +02:00
|
|
|
channel.del({
|
2018-05-15 15:26:04 +02:00
|
|
|
url: '/json/bots/' + encodeURIComponent(bot_id),
|
2017-04-06 22:47:27 +02:00
|
|
|
success: function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const row = $(e.currentTarget).closest("li");
|
2017-04-06 22:47:27 +02:00
|
|
|
row.hide('slow', function () { row.remove(); });
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
2018-07-02 21:19:18 +02:00
|
|
|
exports.bot_error(bot_id, xhr);
|
2017-04-06 22:47:27 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#inactive_bots_list").on("click", "button.reactivate_bot", function (e) {
|
2020-01-16 20:40:20 +01:00
|
|
|
const user_id = parseInt($(e.currentTarget).attr('data-user-id'), 10);
|
2017-04-06 22:47:27 +02:00
|
|
|
|
|
|
|
channel.post({
|
2018-05-17 19:45:13 +02:00
|
|
|
url: '/json/users/' + encodeURIComponent(user_id) + "/reactivate",
|
2017-04-06 22:47:27 +02:00
|
|
|
error: function (xhr) {
|
2018-07-02 21:19:18 +02:00
|
|
|
exports.bot_error(user_id, xhr);
|
2017-04-06 22:47:27 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#active_bots_list").on("click", "button.regenerate_bot_api_key", function (e) {
|
2020-01-16 20:40:20 +01:00
|
|
|
const bot_id = parseInt($(e.currentTarget).attr('data-user-id'), 10);
|
2017-04-06 22:47:27 +02:00
|
|
|
channel.post({
|
2018-05-15 18:13:07 +02:00
|
|
|
url: '/json/bots/' + encodeURIComponent(bot_id) + '/api_key/regenerate',
|
2017-04-06 22:47:27 +02:00
|
|
|
idempotent: true,
|
|
|
|
success: function (data) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const row = $(e.currentTarget).closest("li");
|
2017-04-06 22:47:27 +02:00
|
|
|
row.find(".api_key").find(".value").text(data.api_key);
|
|
|
|
row.find("api_key_error").hide();
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const row = $(e.currentTarget).closest("li");
|
2017-04-06 22:47:27 +02:00
|
|
|
row.find(".api_key_error").text(JSON.parse(xhr.responseText).msg).show();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let image_version = 0;
|
2017-04-06 22:47:27 +02:00
|
|
|
|
|
|
|
$("#active_bots_list").on("click", "button.open_edit_bot_form", function (e) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const li = $(e.currentTarget).closest('li');
|
2020-01-16 20:40:20 +01:00
|
|
|
const bot_id = parseInt(li.find('.bot_info').attr('data-user-id'), 10);
|
2019-11-02 00:06:25 +01:00
|
|
|
const bot = bot_data.get(bot_id);
|
2020-03-21 18:09:45 +01:00
|
|
|
const users_list = people.get_active_humans();
|
2018-02-07 15:58:53 +01:00
|
|
|
$("#edit_bot").empty();
|
2019-07-09 21:24:00 +02:00
|
|
|
$("#edit_bot").append(render_edit_bot({
|
|
|
|
bot: bot,
|
|
|
|
users_list: users_list,
|
|
|
|
}));
|
2019-11-02 00:06:25 +01:00
|
|
|
const avatar_widget = avatar.build_bot_edit_widget($("#settings_page"));
|
|
|
|
const form = $('#settings_page .edit_bot_form');
|
|
|
|
const image = li.find(".image");
|
|
|
|
const errors = form.find('.bot_edit_errors');
|
2017-04-06 22:47:27 +02:00
|
|
|
|
2018-02-07 16:24:21 +01:00
|
|
|
$("#settings_page .edit_bot .edit-bot-owner select").val(bot.owner);
|
2019-11-02 00:06:25 +01:00
|
|
|
const service = bot_data.get_services(bot_id)[0];
|
2018-02-07 12:37:05 +01:00
|
|
|
if (bot.bot_type.toString() === OUTGOING_WEBHOOK_BOT_TYPE) {
|
2019-07-09 21:24:00 +02:00
|
|
|
$("#service_data").append(render_settings_edit_outgoing_webhook_service({
|
|
|
|
service: service,
|
|
|
|
}));
|
2018-05-18 14:39:02 +02:00
|
|
|
$("#edit_service_interface").val(service.interface);
|
2018-01-16 20:40:03 +01:00
|
|
|
}
|
2018-02-01 13:57:24 +01:00
|
|
|
if (bot.bot_type.toString() === EMBEDDED_BOT_TYPE) {
|
2019-07-09 21:24:00 +02:00
|
|
|
$("#service_data").append(render_settings_edit_embedded_bot_service({
|
|
|
|
service: service,
|
|
|
|
}));
|
2018-02-01 13:57:24 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 20:40:03 +01:00
|
|
|
avatar_widget.clear();
|
2017-04-06 22:47:27 +02:00
|
|
|
|
|
|
|
form.validate({
|
|
|
|
errorClass: 'text-error',
|
|
|
|
success: function () {
|
|
|
|
errors.hide();
|
|
|
|
},
|
|
|
|
submitHandler: function () {
|
2020-01-16 20:40:20 +01:00
|
|
|
const bot_id = parseInt(form.attr('data-user-id'), 10);
|
2019-11-02 00:06:25 +01:00
|
|
|
const type = form.attr('data-type');
|
2018-02-07 16:24:21 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const full_name = form.find('.edit_bot_name').val();
|
|
|
|
const bot_owner = form.find('.edit-bot-owner select').val();
|
|
|
|
const file_input = $(".edit_bot").find('.edit_bot_avatar_file_input');
|
|
|
|
const spinner = form.find('.edit_bot_spinner');
|
|
|
|
const edit_button = form.find('.edit_bot_button');
|
2018-02-07 16:24:21 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const formData = new FormData();
|
2017-04-06 22:47:27 +02:00
|
|
|
formData.append('csrfmiddlewaretoken', csrf_token);
|
|
|
|
formData.append('full_name', full_name);
|
2018-06-19 07:42:04 +02:00
|
|
|
formData.append('bot_owner_id', people.get_by_email(bot_owner).user_id);
|
2018-02-07 12:37:05 +01:00
|
|
|
|
2018-01-22 18:36:53 +01:00
|
|
|
if (type === OUTGOING_WEBHOOK_BOT_TYPE) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const service_payload_url = $("#edit_service_base_url").val();
|
|
|
|
const service_interface = $("#edit_service_interface :selected").val();
|
2018-01-16 20:40:03 +01:00
|
|
|
formData.append('service_payload_url', JSON.stringify(service_payload_url));
|
|
|
|
formData.append('service_interface', service_interface);
|
2018-02-01 13:57:24 +01:00
|
|
|
} else if (type === EMBEDDED_BOT_TYPE) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const config_data = {};
|
2018-02-01 13:57:24 +01:00
|
|
|
$("#config_edit_inputbox input").each(function () {
|
|
|
|
config_data[$(this).attr('name')] = $(this).val();
|
|
|
|
});
|
|
|
|
formData.append('config_data', JSON.stringify(config_data));
|
2018-01-16 20:40:03 +01:00
|
|
|
}
|
2020-02-08 01:53:49 +01:00
|
|
|
for (const [i, file] of Array.prototype.entries.call(file_input[0].files)) {
|
2018-06-04 21:13:07 +02:00
|
|
|
formData.append('file-' + i, file);
|
2020-02-08 01:53:49 +01:00
|
|
|
}
|
2017-04-06 22:47:27 +02:00
|
|
|
loading.make_indicator(spinner, {text: 'Editing bot'});
|
|
|
|
edit_button.hide();
|
|
|
|
channel.patch({
|
2018-05-15 15:26:04 +02:00
|
|
|
url: '/json/bots/' + encodeURIComponent(bot_id),
|
2017-04-06 22:47:27 +02:00
|
|
|
data: formData,
|
|
|
|
cache: false,
|
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
|
|
|
success: function (data) {
|
|
|
|
loading.destroy_indicator(spinner);
|
|
|
|
errors.hide();
|
|
|
|
edit_button.show();
|
|
|
|
avatar_widget.clear();
|
2018-02-05 17:07:23 +01:00
|
|
|
typeahead_helper.clear_rendered_person(bot_id);
|
2017-04-06 22:47:27 +02:00
|
|
|
if (data.avatar_url) {
|
|
|
|
// Note that the avatar_url won't actually change on the back end
|
|
|
|
// when the user had a previous uploaded avatar. Only the content
|
|
|
|
// changes, so we version it to get an uncached copy.
|
|
|
|
image_version += 1;
|
2018-06-04 21:13:07 +02:00
|
|
|
image.find('img').attr('src', data.avatar_url + '&v=' + image_version.toString());
|
2017-04-06 22:47:27 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
loading.destroy_indicator(spinner);
|
|
|
|
edit_button.show();
|
|
|
|
errors.text(JSON.parse(xhr.responseText).msg).show();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#active_bots_list").on("click", "a.download_bot_zuliprc", function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const bot_info = $(this).closest(".bot-information-box").find(".bot_info");
|
2020-01-16 20:40:20 +01:00
|
|
|
const bot_id = parseInt(bot_info.attr("data-user-id"), 10);
|
2018-06-01 11:27:02 +02:00
|
|
|
$(this).attr("href", exports.generate_zuliprc_uri(bot_id));
|
2017-04-06 22:47:27 +02:00
|
|
|
});
|
|
|
|
|
2018-06-27 09:35:30 +02:00
|
|
|
new ClipboardJS('#copy_zuliprc', {
|
|
|
|
text: function (trigger) {
|
2020-02-01 00:04:23 +01:00
|
|
|
const bot_info = $(trigger).closest(".bot-information-box").find(".bot_info");
|
|
|
|
const bot_id = parseInt(bot_info.attr("data-user-id"), 10);
|
|
|
|
const bot = bot_data.get(bot_id);
|
|
|
|
const data = exports.generate_zuliprc_content(bot);
|
2018-06-27 09:35:30 +02:00
|
|
|
return data;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2017-06-29 07:38:53 +02:00
|
|
|
$("#bots_lists_navbar .add-a-new-bot-tab").click(function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2018-03-30 12:04:40 +02:00
|
|
|
focus_tab.add_a_new_bot_tab();
|
2017-06-29 07:38:53 +02:00
|
|
|
});
|
|
|
|
|
2017-04-06 22:47:27 +02:00
|
|
|
$("#bots_lists_navbar .active-bots-tab").click(function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2018-03-30 12:04:40 +02:00
|
|
|
focus_tab.active_bots_tab();
|
2017-04-06 22:47:27 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$("#bots_lists_navbar .inactive-bots-tab").click(function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2018-03-30 12:04:40 +02:00
|
|
|
focus_tab.inactive_bots_tab();
|
2017-04-06 22:47:27 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.settings_bots = exports;
|