2021-02-28 00:39:51 +01:00
|
|
|
import * as channel from "./channel";
|
2022-08-30 22:24:54 +02:00
|
|
|
import * as compose_banner from "./compose_banner";
|
2021-11-26 08:36:54 +01:00
|
|
|
import * as dark_theme from "./dark_theme";
|
2021-02-10 16:54:18 +01:00
|
|
|
import * as feedback_widget from "./feedback_widget";
|
2021-04-13 06:51:54 +02:00
|
|
|
import {$t} from "./i18n";
|
2022-03-31 14:16:20 +02:00
|
|
|
import * as markdown from "./markdown";
|
2021-02-28 01:01:53 +01:00
|
|
|
import * as scroll_bar from "./scroll_bar";
|
2021-02-10 16:47:06 +01:00
|
|
|
|
2018-06-02 13:59:02 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
What in the heck is a zcommand?
|
|
|
|
|
|
|
|
A zcommand is basically a specific type of slash
|
|
|
|
command where the client does almost no work and
|
|
|
|
the server just does something pretty simple like
|
|
|
|
flip a setting.
|
|
|
|
|
|
|
|
The first zcommand we wrote is for "/ping", and
|
|
|
|
the server just responds with a 200 for that.
|
|
|
|
|
|
|
|
Not all slash commands use zcommand under the hood.
|
|
|
|
For more exotic things like /poll see submessage.js
|
|
|
|
and widgetize.js
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2021-02-10 16:54:18 +01:00
|
|
|
export function send(opts) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const command = opts.command;
|
|
|
|
const on_success = opts.on_success;
|
|
|
|
const data = {
|
2020-07-20 22:18:43 +02:00
|
|
|
command,
|
2018-06-02 13:59:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
channel.post({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/zcommand",
|
2020-07-20 22:18:43 +02:00
|
|
|
data,
|
|
|
|
success(data) {
|
2018-12-21 16:45:42 +01:00
|
|
|
if (on_success) {
|
|
|
|
on_success(data);
|
|
|
|
}
|
2018-06-02 13:59:02 +02:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error() {
|
2021-02-10 16:54:18 +01:00
|
|
|
tell_user("server did not respond");
|
2018-06-02 13:59:02 +02:00
|
|
|
},
|
|
|
|
});
|
2021-02-10 16:54:18 +01:00
|
|
|
}
|
2018-06-02 13:59:02 +02:00
|
|
|
|
2021-02-10 16:54:18 +01:00
|
|
|
export function tell_user(msg) {
|
2018-06-02 13:59:02 +02:00
|
|
|
// This is a bit hacky, but we don't have a super easy API now
|
|
|
|
// for just telling users stuff.
|
2022-08-30 22:24:54 +02:00
|
|
|
compose_banner.show_error_message(msg, compose_banner.CLASSNAMES.generic_compose_error);
|
2021-02-10 16:54:18 +01:00
|
|
|
}
|
2018-06-02 13:59:02 +02:00
|
|
|
|
2021-11-26 08:41:05 +01:00
|
|
|
export function switch_to_light_theme() {
|
2021-02-10 16:54:18 +01:00
|
|
|
send({
|
2018-12-21 16:45:42 +01:00
|
|
|
command: "/day",
|
2020-07-20 22:18:43 +02:00
|
|
|
on_success(data) {
|
2021-11-26 08:36:54 +01:00
|
|
|
dark_theme.disable();
|
2018-12-21 16:45:42 +01:00
|
|
|
feedback_widget.show({
|
2022-01-25 11:36:19 +01:00
|
|
|
populate($container) {
|
2022-03-31 14:16:20 +02:00
|
|
|
const rendered_msg = markdown.parse_non_message(data.msg);
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.html(rendered_msg);
|
2018-12-21 16:45:42 +01:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
on_undo() {
|
2021-02-10 16:54:18 +01:00
|
|
|
send({
|
2018-12-21 16:45:42 +01:00
|
|
|
command: "/night",
|
|
|
|
});
|
|
|
|
},
|
2021-11-19 04:16:07 +01:00
|
|
|
title_text: $t({defaultMessage: "Light theme"}),
|
2021-11-18 11:44:31 +01:00
|
|
|
undo_button_text: $t({defaultMessage: "Dark theme"}),
|
2018-12-21 16:45:42 +01:00
|
|
|
});
|
2018-06-02 16:23:32 +02:00
|
|
|
},
|
|
|
|
});
|
2021-02-10 16:54:18 +01:00
|
|
|
}
|
2018-12-21 16:45:42 +01:00
|
|
|
|
2021-11-26 08:40:04 +01:00
|
|
|
export function switch_to_dark_theme() {
|
2021-02-10 16:54:18 +01:00
|
|
|
send({
|
2018-12-21 16:45:42 +01:00
|
|
|
command: "/night",
|
2020-07-20 22:18:43 +02:00
|
|
|
on_success(data) {
|
2021-11-26 08:36:54 +01:00
|
|
|
dark_theme.enable();
|
2018-12-21 16:45:42 +01:00
|
|
|
feedback_widget.show({
|
2022-01-25 11:36:19 +01:00
|
|
|
populate($container) {
|
2022-03-31 14:16:20 +02:00
|
|
|
const rendered_msg = markdown.parse_non_message(data.msg);
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.html(rendered_msg);
|
2018-12-21 16:45:42 +01:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
on_undo() {
|
2021-02-10 16:54:18 +01:00
|
|
|
send({
|
2018-12-21 16:45:42 +01:00
|
|
|
command: "/day",
|
|
|
|
});
|
|
|
|
},
|
2021-11-18 11:44:31 +01:00
|
|
|
title_text: $t({defaultMessage: "Dark theme"}),
|
2021-11-19 04:16:07 +01:00
|
|
|
undo_button_text: $t({defaultMessage: "Light theme"}),
|
2018-12-21 16:45:42 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
2021-02-10 16:54:18 +01:00
|
|
|
}
|
2018-06-02 16:23:32 +02:00
|
|
|
|
2021-02-10 16:54:18 +01:00
|
|
|
export function enter_fluid_mode() {
|
|
|
|
send({
|
2020-03-12 07:51:47 +01:00
|
|
|
command: "/fluid-width",
|
2020-07-20 22:18:43 +02:00
|
|
|
on_success(data) {
|
2020-03-12 07:51:47 +01:00
|
|
|
scroll_bar.set_layout_width();
|
|
|
|
feedback_widget.show({
|
2022-01-25 11:36:19 +01:00
|
|
|
populate($container) {
|
2022-03-31 14:16:20 +02:00
|
|
|
const rendered_msg = markdown.parse_non_message(data.msg);
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.html(rendered_msg);
|
2020-03-12 07:51:47 +01:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
on_undo() {
|
2021-02-10 16:54:18 +01:00
|
|
|
send({
|
2020-03-12 07:51:47 +01:00
|
|
|
command: "/fixed-width",
|
|
|
|
});
|
|
|
|
},
|
2021-04-13 06:51:54 +02:00
|
|
|
title_text: $t({defaultMessage: "Fluid width mode"}),
|
|
|
|
undo_button_text: $t({defaultMessage: "Fixed width"}),
|
2020-03-12 07:51:47 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
2021-02-10 16:54:18 +01:00
|
|
|
}
|
2020-03-12 07:51:47 +01:00
|
|
|
|
2021-02-10 16:54:18 +01:00
|
|
|
export function enter_fixed_mode() {
|
|
|
|
send({
|
2020-03-12 07:51:47 +01:00
|
|
|
command: "/fixed-width",
|
2020-07-20 22:18:43 +02:00
|
|
|
on_success(data) {
|
2020-03-12 07:51:47 +01:00
|
|
|
scroll_bar.set_layout_width();
|
|
|
|
feedback_widget.show({
|
2022-01-25 11:36:19 +01:00
|
|
|
populate($container) {
|
2022-03-31 14:16:20 +02:00
|
|
|
const rendered_msg = markdown.parse_non_message(data.msg);
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.html(rendered_msg);
|
2020-03-12 07:51:47 +01:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
on_undo() {
|
2021-02-10 16:54:18 +01:00
|
|
|
send({
|
2020-03-12 07:51:47 +01:00
|
|
|
command: "/fluid-width",
|
|
|
|
});
|
|
|
|
},
|
2021-04-13 06:51:54 +02:00
|
|
|
title_text: $t({defaultMessage: "Fixed width mode"}),
|
|
|
|
undo_button_text: $t({defaultMessage: "Fluid width"}),
|
2020-03-12 07:51:47 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
2021-02-10 16:54:18 +01:00
|
|
|
}
|
2020-03-12 07:51:47 +01:00
|
|
|
|
2021-02-10 16:54:18 +01:00
|
|
|
export function process(message_content) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const content = message_content.trim();
|
2018-06-02 13:59:02 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (content === "/ping") {
|
2019-11-02 00:06:25 +01:00
|
|
|
const start_time = new Date();
|
2018-06-02 13:59:02 +02:00
|
|
|
|
2021-02-10 16:54:18 +01:00
|
|
|
send({
|
2018-06-20 20:46:33 +02:00
|
|
|
command: content,
|
2020-07-20 22:18:43 +02:00
|
|
|
on_success() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const end_time = new Date();
|
|
|
|
let diff = end_time - start_time;
|
2018-06-02 13:59:02 +02:00
|
|
|
diff = Math.round(diff);
|
2019-11-02 00:06:25 +01:00
|
|
|
const msg = "ping time: " + diff + "ms";
|
2021-02-10 16:54:18 +01:00
|
|
|
tell_user(msg);
|
2018-06-02 13:59:02 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const day_commands = ["/day", "/light"];
|
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
|
|
|
if (day_commands.includes(content)) {
|
2021-11-26 08:41:05 +01:00
|
|
|
switch_to_light_theme();
|
2018-12-21 16:45:42 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const night_commands = ["/night", "/dark"];
|
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
|
|
|
if (night_commands.includes(content)) {
|
2021-11-26 08:40:04 +01:00
|
|
|
switch_to_dark_theme();
|
2018-06-02 16:23:32 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (content === "/fluid-width") {
|
2021-02-10 16:54:18 +01:00
|
|
|
enter_fluid_mode();
|
2020-03-12 07:51:47 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (content === "/fixed-width") {
|
2021-02-10 16:54:18 +01:00
|
|
|
enter_fixed_mode();
|
2020-03-12 07:51:47 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-02 13:59:02 +02:00
|
|
|
// It is incredibly important here to return false
|
|
|
|
// if we don't see an actual zcommand, so that compose.js
|
|
|
|
// knows this is a normal message.
|
|
|
|
return false;
|
2021-02-10 16:54:18 +01:00
|
|
|
}
|