2021-02-28 01:19:16 +01:00
|
|
|
import ClipboardJS from "clipboard";
|
2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2022-09-15 14:07:39 +02:00
|
|
|
import render_add_new_bot_form from "../templates/settings/add_new_bot_form.hbs";
|
2021-05-14 21:36:58 +02:00
|
|
|
import render_bot_avatar_row from "../templates/settings/bot_avatar_row.hbs";
|
2023-03-03 14:14:01 +01:00
|
|
|
import render_bot_settings_tip from "../templates/settings/bot_settings_tip.hbs";
|
2020-07-28 00:14:57 +02:00
|
|
|
|
2021-02-28 01:19:16 +01:00
|
|
|
import * as avatar from "./avatar";
|
2021-02-28 01:24:30 +01:00
|
|
|
import * as bot_data from "./bot_data";
|
2021-02-28 01:19:16 +01:00
|
|
|
import * as channel from "./channel";
|
2024-01-11 12:16:28 +01:00
|
|
|
import * as components from "./components";
|
2023-09-23 10:07:32 +02:00
|
|
|
import {show_copied_confirmation} from "./copied_tooltip";
|
2021-03-25 23:20:18 +01:00
|
|
|
import {csrf_token} from "./csrf";
|
2021-07-18 08:43:09 +02:00
|
|
|
import * as dialog_widget from "./dialog_widget";
|
|
|
|
import {$t, $t_html} from "./i18n";
|
2023-09-25 19:13:40 +02:00
|
|
|
import * as integration_url_modal from "./integration_url_modal";
|
2023-08-19 18:13:46 +02:00
|
|
|
import * as list_widget from "./list_widget";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2021-02-28 01:19:16 +01:00
|
|
|
import * as people from "./people";
|
2023-09-25 10:15:19 +02:00
|
|
|
import * as settings_data from "./settings_data";
|
2024-02-13 02:08:16 +01:00
|
|
|
import {current_user} from "./state_data";
|
2022-07-06 16:48:35 +02:00
|
|
|
import * as ui_report from "./ui_report";
|
2023-09-25 19:19:40 +02:00
|
|
|
import * as user_deactivation_ui from "./user_deactivation_ui";
|
2022-08-24 10:54:02 +02:00
|
|
|
import * as user_profile from "./user_profile";
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2023-06-27 11:14:31 +02:00
|
|
|
const INCOMING_WEBHOOK_BOT_TYPE = 2;
|
2022-07-07 14:11:55 +02:00
|
|
|
const OUTGOING_WEBHOOK_BOT_TYPE = "3";
|
2024-01-18 10:21:23 +01:00
|
|
|
const OUTGOING_WEBHOOK_BOT_TYPE_INT = 3;
|
2022-07-07 14:11:55 +02:00
|
|
|
const EMBEDDED_BOT_TYPE = "4";
|
|
|
|
|
2017-04-06 22:47:27 +02:00
|
|
|
function add_bot_row(info) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $row = $(render_bot_avatar_row(info));
|
2017-04-06 22:47:27 +02:00
|
|
|
if (info.is_active) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$("#active_bots_list").append($row);
|
2017-04-06 22:47:27 +02:00
|
|
|
} else {
|
2022-01-25 11:36:19 +01:00
|
|
|
$("#inactive_bots_list").append($row);
|
2017-04-06 22:47:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-15 14:07:39 +02:00
|
|
|
function is_local_part(value) {
|
2017-04-06 22:47:27 +02:00
|
|
|
// Adapted from Django's EmailValidator
|
2022-09-15 14:07:39 +02:00
|
|
|
return /^[\w!#$%&'*+/=?^`{|}~-]+(\.[\w!#$%&'*+/=?^`{|}~-]+)*$/i.test(value);
|
2017-04-06 22:47:27 +02:00
|
|
|
}
|
|
|
|
|
2021-07-23 18:47:08 +02:00
|
|
|
export function render_bots() {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#active_bots_list").empty();
|
|
|
|
$("#inactive_bots_list").empty();
|
2017-04-06 22:47:27 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const all_bots_for_current_user = bot_data.get_all_bots_for_current_user();
|
2024-01-18 10:21:23 +01:00
|
|
|
let user_owns_an_active_outgoing_webhook_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,
|
2023-09-26 05:52:27 +02:00
|
|
|
type: settings_data.bot_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,
|
2023-06-27 11:14:31 +02:00
|
|
|
is_incoming_webhook_bot: elem.bot_type === INCOMING_WEBHOOK_BOT_TYPE,
|
2020-07-15 01:29:15 +02:00
|
|
|
zuliprc: "zuliprc", // Most browsers do not allow filename starting with `.`
|
2017-04-06 22:47:27 +02:00
|
|
|
});
|
2024-01-18 10:21:23 +01:00
|
|
|
user_owns_an_active_outgoing_webhook_bot =
|
|
|
|
user_owns_an_active_outgoing_webhook_bot ||
|
|
|
|
(elem.is_active && elem.bot_type === OUTGOING_WEBHOOK_BOT_TYPE_INT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user_owns_an_active_outgoing_webhook_bot) {
|
|
|
|
$("#active_bots_list_container .config-download-text").show();
|
|
|
|
} else {
|
|
|
|
$("#active_bots_list_container .config-download-text").hide();
|
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
|
|
|
}
|
2023-08-19 18:13:46 +02:00
|
|
|
|
|
|
|
list_widget.render_empty_list_message_if_needed($("#active_bots_list"));
|
|
|
|
list_widget.render_empty_list_message_if_needed($("#inactive_bots_list"));
|
2021-02-28 01:19:16 +01:00
|
|
|
}
|
2017-04-06 22:47:27 +02:00
|
|
|
|
2023-04-09 05:22:23 +02:00
|
|
|
export function generate_zuliprc_url(bot_id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const bot = bot_data.get(bot_id);
|
2021-02-28 01:19:16 +01:00
|
|
|
const data = generate_zuliprc_content(bot);
|
2023-04-09 05:22:23 +02:00
|
|
|
return encode_zuliprc_as_url(data);
|
2021-02-28 01:19:16 +01:00
|
|
|
}
|
2018-06-04 16:20:14 +02:00
|
|
|
|
2023-04-09 05:22:23 +02:00
|
|
|
export function encode_zuliprc_as_url(zuliprc) {
|
2018-06-04 16:20:14 +02:00
|
|
|
return "data:application/octet-stream;charset=utf-8," + encodeURIComponent(zuliprc);
|
2021-02-28 01:19:16 +01:00
|
|
|
}
|
2017-04-06 22:47:27 +02:00
|
|
|
|
2021-02-28 01:19:16 +01:00
|
|
|
export function generate_zuliprc_content(bot) {
|
2020-02-01 00:04:23 +01:00
|
|
|
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;
|
|
|
|
}
|
2020-07-15 00:34:28 +02:00
|
|
|
return (
|
|
|
|
"[api]" +
|
|
|
|
"\nemail=" +
|
|
|
|
bot.email +
|
|
|
|
"\nkey=" +
|
|
|
|
bot.api_key +
|
|
|
|
"\nsite=" +
|
|
|
|
page_params.realm_uri +
|
|
|
|
(token === undefined ? "" : "\ntoken=" + token) +
|
|
|
|
// Some tools would not work in files without a trailing new line.
|
|
|
|
"\n"
|
|
|
|
);
|
2021-02-28 01:19:16 +01:00
|
|
|
}
|
2017-04-06 22:47:27 +02:00
|
|
|
|
2021-02-28 01:19:16 +01:00
|
|
|
export function generate_botserverrc_content(email, api_key, token) {
|
2020-07-15 00:34:28 +02:00
|
|
|
return (
|
|
|
|
"[]" +
|
|
|
|
"\nemail=" +
|
|
|
|
email +
|
|
|
|
"\nkey=" +
|
|
|
|
api_key +
|
|
|
|
"\nsite=" +
|
|
|
|
page_params.realm_uri +
|
|
|
|
"\ntoken=" +
|
|
|
|
token +
|
|
|
|
"\n"
|
|
|
|
);
|
2021-02-28 01:19:16 +01:00
|
|
|
}
|
2017-05-30 16:12:02 +02:00
|
|
|
|
2021-02-28 01:19:16 +01:00
|
|
|
export const bot_creation_policy_values = {
|
2018-05-30 18:43:07 +02:00
|
|
|
admins_only: {
|
|
|
|
code: 3,
|
2021-04-13 06:51:54 +02:00
|
|
|
description: $t({defaultMessage: "Admins"}),
|
2019-05-22 07:58:00 +02:00
|
|
|
},
|
|
|
|
everyone: {
|
|
|
|
code: 1,
|
2021-08-23 14:58:02 +02:00
|
|
|
description: $t({defaultMessage: "Admins, moderators and members"}),
|
2018-05-30 18:43:07 +02:00
|
|
|
},
|
|
|
|
restricted: {
|
|
|
|
code: 2,
|
2021-04-13 06:51:54 +02:00
|
|
|
description: $t({
|
2021-08-23 14:58:02 +02:00
|
|
|
defaultMessage: "Admins, moderators and members, but only admins can add generic bots",
|
2021-04-13 06:51:54 +02:00
|
|
|
}),
|
2018-05-30 18:43:07 +02:00
|
|
|
},
|
2018-01-29 16:10:54 +01:00
|
|
|
};
|
|
|
|
|
2021-02-28 01:19:16 +01:00
|
|
|
export function can_create_new_bots() {
|
2024-02-13 02:08:16 +01:00
|
|
|
if (current_user.is_admin) {
|
2018-06-13 16:59:15 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-02-13 02:08:16 +01:00
|
|
|
if (current_user.is_guest) {
|
2018-06-13 17:31:58 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:19:16 +01:00
|
|
|
return page_params.realm_bot_creation_policy !== bot_creation_policy_values.admins_only.code;
|
|
|
|
}
|
2018-06-13 16:59:15 +02:00
|
|
|
|
2023-03-03 14:14:01 +01:00
|
|
|
export function update_bot_settings_tip($tip_container, for_org_settings) {
|
|
|
|
if (
|
2024-02-13 02:08:16 +01:00
|
|
|
!current_user.is_admin &&
|
2023-03-03 14:14:01 +01:00
|
|
|
page_params.realm_bot_creation_policy === bot_creation_policy_values.everyone.code
|
|
|
|
) {
|
|
|
|
$tip_container.hide();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-13 02:08:16 +01:00
|
|
|
if (current_user.is_admin && !for_org_settings) {
|
2023-03-03 14:14:01 +01:00
|
|
|
$tip_container.hide();
|
|
|
|
return;
|
2018-01-29 16:10:54 +01:00
|
|
|
}
|
2023-03-03 14:14:01 +01:00
|
|
|
|
|
|
|
const rendered_tip = render_bot_settings_tip({
|
|
|
|
realm_bot_creation_policy: page_params.realm_bot_creation_policy,
|
|
|
|
permission_type: bot_creation_policy_values,
|
|
|
|
});
|
|
|
|
$tip_container.show();
|
|
|
|
$tip_container.html(rendered_tip);
|
2021-02-28 01:19:16 +01:00
|
|
|
}
|
2018-01-29 16:10:54 +01:00
|
|
|
|
2022-09-16 12:43:43 +02:00
|
|
|
function update_add_bot_button() {
|
|
|
|
if (can_create_new_bots()) {
|
|
|
|
$("#bot-settings .add-a-new-bot").show();
|
2023-10-11 16:01:46 +02:00
|
|
|
$("#admin-bot-list .add-new-bots").show();
|
|
|
|
$("#admin-bot-list .manage-your-bots").hide();
|
2023-10-11 16:59:14 +02:00
|
|
|
$(".org-settings-list li[data-section='bot-list-admin'] .locked").hide();
|
2022-09-16 12:43:43 +02:00
|
|
|
} else {
|
|
|
|
$("#bot-settings .add-a-new-bot").hide();
|
2023-10-11 16:01:46 +02:00
|
|
|
$("#admin-bot-list .add-new-bots").hide();
|
2023-10-11 16:59:14 +02:00
|
|
|
$(".org-settings-list li[data-section='bot-list-admin'] .locked").show();
|
2023-10-11 16:01:46 +02:00
|
|
|
|
|
|
|
if (bot_data.get_all_bots_for_current_user().length > 0) {
|
|
|
|
$("#admin-bot-list .manage-your-bots").show();
|
|
|
|
}
|
2022-09-16 12:43:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:19:16 +01:00
|
|
|
export function update_bot_permissions_ui() {
|
2023-03-03 14:14:01 +01:00
|
|
|
update_bot_settings_tip($("#admin-bot-settings-tip"), true);
|
|
|
|
update_bot_settings_tip($("#personal-bot-settings-tip"), false);
|
2022-09-16 12:43:43 +02:00
|
|
|
update_add_bot_button();
|
2018-01-29 16:10:54 +01:00
|
|
|
$("#id_realm_bot_creation_policy").val(page_params.realm_bot_creation_policy);
|
2022-09-15 14:07:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function add_a_new_bot() {
|
|
|
|
const html_body = render_add_new_bot_form({
|
|
|
|
bot_types: page_params.bot_types,
|
|
|
|
realm_embedded_bots: page_params.realm_embedded_bots,
|
|
|
|
realm_bot_domain: page_params.realm_bot_domain,
|
|
|
|
});
|
|
|
|
|
|
|
|
let create_avatar_widget;
|
|
|
|
|
|
|
|
function create_a_new_bot() {
|
2023-05-02 06:59:01 +02:00
|
|
|
const bot_type = $("#create_bot_type").val();
|
2022-09-15 14:07:39 +02:00
|
|
|
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();
|
2023-05-02 06:59:01 +02:00
|
|
|
const service_name = $("#select_service_name").val();
|
2022-09-15 14:07:39 +02:00
|
|
|
const formData = new FormData();
|
|
|
|
|
|
|
|
formData.append("csrfmiddlewaretoken", csrf_token);
|
|
|
|
formData.append("bot_type", bot_type);
|
|
|
|
formData.append("full_name", full_name);
|
|
|
|
formData.append("short_name", short_name);
|
|
|
|
|
|
|
|
// If the selected bot_type is Outgoing webhook
|
|
|
|
if (bot_type === OUTGOING_WEBHOOK_BOT_TYPE) {
|
|
|
|
formData.append("payload_url", JSON.stringify(payload_url));
|
|
|
|
formData.append("interface_type", interface_type);
|
|
|
|
} else if (bot_type === EMBEDDED_BOT_TYPE) {
|
|
|
|
formData.append("service_name", service_name);
|
|
|
|
const config_data = {};
|
|
|
|
$(`#config_inputbox [name*='${CSS.escape(service_name)}'] input`).each(function () {
|
|
|
|
config_data[$(this).attr("name")] = $(this).val();
|
|
|
|
});
|
|
|
|
formData.append("config_data", JSON.stringify(config_data));
|
|
|
|
}
|
|
|
|
for (const [i, file] of Array.prototype.entries.call(
|
|
|
|
$("#bot_avatar_file_input")[0].files,
|
|
|
|
)) {
|
|
|
|
formData.append("file-" + i, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
channel.post({
|
|
|
|
url: "/json/bots",
|
|
|
|
data: formData,
|
|
|
|
cache: false,
|
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
|
|
|
success() {
|
|
|
|
create_avatar_widget.clear();
|
2023-10-10 14:32:20 +02:00
|
|
|
dialog_widget.close();
|
2022-09-15 14:07:39 +02:00
|
|
|
},
|
|
|
|
error(xhr) {
|
|
|
|
ui_report.error($t_html({defaultMessage: "Failed"}), xhr, $("#dialog_error"));
|
|
|
|
dialog_widget.hide_dialog_spinner();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function set_up_form_fields() {
|
|
|
|
$("#payload_url_inputbox").hide();
|
|
|
|
$("#create_payload_url").val("");
|
|
|
|
$("#service_name_list").hide();
|
|
|
|
$("#config_inputbox").hide();
|
|
|
|
const selected_embedded_bot = "converter";
|
|
|
|
$("#select_service_name").val(selected_embedded_bot); // TODO: Use 'select a bot'.
|
|
|
|
$("#config_inputbox").children().hide();
|
|
|
|
$(`[name*='${CSS.escape(selected_embedded_bot)}']`).show();
|
|
|
|
|
|
|
|
create_avatar_widget = avatar.build_bot_create_widget();
|
|
|
|
|
|
|
|
$("#create_bot_type").on("change", () => {
|
2023-05-02 06:59:01 +02:00
|
|
|
const bot_type = $("#create_bot_type").val();
|
2022-09-15 14:07:39 +02:00
|
|
|
// For "generic bot" or "incoming webhook" both these fields need not be displayed.
|
|
|
|
$("#service_name_list").hide();
|
|
|
|
$("#select_service_name").removeClass("required");
|
|
|
|
$("#config_inputbox").hide();
|
|
|
|
|
|
|
|
$("#payload_url_inputbox").hide();
|
|
|
|
$("#create_payload_url").removeClass("required");
|
|
|
|
if (bot_type === OUTGOING_WEBHOOK_BOT_TYPE) {
|
|
|
|
$("#payload_url_inputbox").show();
|
|
|
|
$("#create_payload_url").addClass("required");
|
|
|
|
} else if (bot_type === EMBEDDED_BOT_TYPE) {
|
|
|
|
$("#service_name_list").show();
|
|
|
|
$("#select_service_name").addClass("required");
|
|
|
|
$("#select_service_name").trigger("change");
|
|
|
|
$("#config_inputbox").show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#select_service_name").on("change", () => {
|
|
|
|
$("#config_inputbox").children().hide();
|
2023-05-02 06:59:01 +02:00
|
|
|
const selected_bot = $("#select_service_name").val();
|
2022-09-15 14:07:39 +02:00
|
|
|
$(`[name*='${CSS.escape(selected_bot)}']`).show();
|
|
|
|
});
|
2018-01-29 16:10:54 +01:00
|
|
|
}
|
2022-09-15 14:07:39 +02:00
|
|
|
|
2023-06-28 12:18:33 +02:00
|
|
|
function validate_input() {
|
2022-09-15 14:07:39 +02:00
|
|
|
const bot_short_name = $("#create_bot_short_name").val();
|
|
|
|
|
|
|
|
if (is_local_part(bot_short_name)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
ui_report.error(
|
|
|
|
$t_html({
|
|
|
|
defaultMessage: "Please only use characters that are valid in an email address",
|
|
|
|
}),
|
|
|
|
undefined,
|
|
|
|
$("#dialog_error"),
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
dialog_widget.launch({
|
|
|
|
form_id: "create_bot_form",
|
|
|
|
help_link: "/help/add-a-bot-or-integration",
|
|
|
|
html_body,
|
|
|
|
html_heading: $t_html({defaultMessage: "Add a new bot"}),
|
|
|
|
html_submit_button: $t_html({defaultMessage: "Add"}),
|
|
|
|
loading_spinner: true,
|
|
|
|
on_click: create_a_new_bot,
|
|
|
|
on_shown: () => $("#create_bot_type").trigger("focus"),
|
|
|
|
post_render: set_up_form_fields,
|
|
|
|
validate_input,
|
|
|
|
});
|
2021-02-28 01:19:16 +01:00
|
|
|
}
|
2018-01-29 16:10:54 +01:00
|
|
|
|
2021-02-28 01:19:16 +01:00
|
|
|
export function set_up() {
|
2020-07-20 21:26:58 +02:00
|
|
|
$("#download_botserverrc").on("click", function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
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;
|
2021-02-28 01:19:16 +01:00
|
|
|
content += 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
|
|
|
}
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
$(this).attr(
|
|
|
|
"href",
|
|
|
|
"data:application/octet-stream;charset=utf-8," + encodeURIComponent(content),
|
|
|
|
);
|
2017-05-30 16:12:02 +02:00
|
|
|
});
|
|
|
|
|
2024-01-11 12:16:28 +01:00
|
|
|
const toggler = components.toggle({
|
|
|
|
child_wants_focus: true,
|
|
|
|
values: [
|
|
|
|
{label: $t({defaultMessage: "Active bots"}), key: "active-bots"},
|
|
|
|
{label: $t({defaultMessage: "Inactive bots"}), key: "inactive-bots"},
|
|
|
|
],
|
|
|
|
callback(_name, key) {
|
2024-01-18 10:21:23 +01:00
|
|
|
$(".bots_section").hide();
|
2024-01-11 12:16:28 +01:00
|
|
|
$(`[data-bot-settings-section="${CSS.escape(key)}"]`).show();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#bot-settings .tab-container").prepend(toggler.get());
|
|
|
|
toggler.goto("active-bots");
|
|
|
|
|
2021-02-28 01:19:16 +01:00
|
|
|
render_bots();
|
2017-04-06 22:47:27 +02:00
|
|
|
|
2022-03-26 08:01:27 +01:00
|
|
|
$("#active_bots_list").on("click", "button.deactivate_bot", (e) => {
|
2020-10-07 09:17:30 +02:00
|
|
|
const bot_id = Number.parseInt($(e.currentTarget).attr("data-user-id"), 10);
|
2018-05-15 15:26:04 +02:00
|
|
|
|
2022-07-24 11:41:58 +02:00
|
|
|
function handle_confirm() {
|
|
|
|
const url = "/json/bots/" + encodeURIComponent(bot_id);
|
|
|
|
const opts = {
|
|
|
|
success_continuation() {
|
|
|
|
const $row = $(e.currentTarget).closest("li");
|
|
|
|
$row.hide("slow", () => {
|
|
|
|
$row.remove();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
dialog_widget.submit_api_request(channel.del, url, {}, opts);
|
|
|
|
}
|
2023-09-25 19:19:40 +02:00
|
|
|
user_deactivation_ui.confirm_bot_deactivation(bot_id, handle_confirm, true);
|
2017-04-06 22:47:27 +02:00
|
|
|
});
|
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
$("#inactive_bots_list").on("click", "button.reactivate_bot", (e) => {
|
2020-10-07 09:17:30 +02:00
|
|
|
const user_id = Number.parseInt($(e.currentTarget).attr("data-user-id"), 10);
|
2022-10-25 19:39:16 +02:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2017-04-06 22:47:27 +02:00
|
|
|
|
2022-10-25 19:39:16 +02:00
|
|
|
function handle_confirm() {
|
|
|
|
channel.post({
|
|
|
|
url: "/json/users/" + encodeURIComponent(user_id) + "/reactivate",
|
|
|
|
success() {
|
2023-10-10 14:32:20 +02:00
|
|
|
dialog_widget.close();
|
2022-10-25 19:39:16 +02:00
|
|
|
},
|
|
|
|
error(xhr) {
|
|
|
|
ui_report.error($t_html({defaultMessage: "Failed"}), xhr, $("#dialog_error"));
|
|
|
|
dialog_widget.hide_dialog_spinner();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-09-27 18:51:02 +02:00
|
|
|
user_deactivation_ui.confirm_reactivation(user_id, handle_confirm, true);
|
2017-04-06 22:47:27 +02:00
|
|
|
});
|
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
$("#active_bots_list").on("click", "button.regenerate_bot_api_key", (e) => {
|
2020-10-07 09:17:30 +02:00
|
|
|
const bot_id = Number.parseInt($(e.currentTarget).attr("data-user-id"), 10);
|
2017-04-06 22:47:27 +02:00
|
|
|
channel.post({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/bots/" + encodeURIComponent(bot_id) + "/api_key/regenerate",
|
2020-07-20 22:18:43 +02:00
|
|
|
success(data) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $row = $(e.currentTarget).closest("li");
|
|
|
|
$row.find(".api_key").find(".value").text(data.api_key);
|
|
|
|
$row.find("api_key_error").hide();
|
2017-04-06 22:47:27 +02:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error(xhr) {
|
2023-07-18 20:19:22 +02:00
|
|
|
if (xhr.responseJSON?.msg) {
|
|
|
|
const $row = $(e.currentTarget).closest("li");
|
|
|
|
$row.find(".api_key_error").text(xhr.responseJSON.msg).show();
|
|
|
|
}
|
2017-04-06 22:47:27 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
$("#active_bots_list").on("click", "button.open_edit_bot_form", (e) => {
|
2020-03-10 11:08:00 +01:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2022-01-25 11:36:19 +01:00
|
|
|
const $li = $(e.currentTarget).closest("li");
|
|
|
|
const bot_id = Number.parseInt($li.find(".bot_info").attr("data-user-id"), 10);
|
2023-07-20 13:50:04 +02:00
|
|
|
const bot = people.get_by_user_id(bot_id);
|
|
|
|
user_profile.show_user_profile(bot, "manage-profile-tab");
|
2017-04-06 22:47:27 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$("#active_bots_list").on("click", "a.download_bot_zuliprc", function () {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $bot_info = $(this).closest(".bot-information-box").find(".bot_info");
|
|
|
|
const bot_id = Number.parseInt($bot_info.attr("data-user-id"), 10);
|
2023-04-09 05:22:23 +02:00
|
|
|
$(this).attr("href", generate_zuliprc_url(bot_id));
|
2017-04-06 22:47:27 +02:00
|
|
|
});
|
|
|
|
|
2022-08-24 10:54:02 +02:00
|
|
|
$("#active_bots_list").on("click", "button.open_bots_subscribed_streams", (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
const bot_id = Number.parseInt($(e.currentTarget).attr("data-user-id"), 10);
|
|
|
|
const bot = people.get_by_user_id(bot_id);
|
|
|
|
user_profile.show_user_profile(bot, "user-profile-streams-tab");
|
|
|
|
});
|
|
|
|
|
2023-06-27 11:14:31 +02:00
|
|
|
$("#active_bots_list").on("click", "button.open-generate-integration-url-modal", (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
const api_key = $(e.currentTarget).attr("data-api-key");
|
2023-09-25 19:13:40 +02:00
|
|
|
integration_url_modal.show_generate_integration_url_modal(api_key);
|
2023-06-27 11:14:31 +02:00
|
|
|
});
|
|
|
|
|
2023-07-09 20:47:32 +02:00
|
|
|
const clipboard = new ClipboardJS("#copy_zuliprc", {
|
2020-07-20 22:18:43 +02:00
|
|
|
text(trigger) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $bot_info = $(trigger).closest(".bot-information-box").find(".bot_info");
|
|
|
|
const bot_id = Number.parseInt($bot_info.attr("data-user-id"), 10);
|
2020-02-01 00:04:23 +01:00
|
|
|
const bot = bot_data.get(bot_id);
|
2021-02-28 01:19:16 +01:00
|
|
|
const data = generate_zuliprc_content(bot);
|
2018-06-27 09:35:30 +02:00
|
|
|
return data;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-07-09 20:47:32 +02:00
|
|
|
// Show a tippy tooltip when the bot zuliprc is copied
|
|
|
|
clipboard.on("success", (e) => {
|
|
|
|
show_copied_confirmation(e.trigger);
|
|
|
|
});
|
|
|
|
|
2022-09-15 14:07:39 +02:00
|
|
|
$("#bot-settings .add-a-new-bot").on("click", (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
add_a_new_bot();
|
|
|
|
});
|
2021-02-28 01:19:16 +01:00
|
|
|
}
|