zulip/static/js/zcommand.js

191 lines
5.3 KiB
JavaScript
Raw Normal View History

/*
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
*/
exports.send = function (opts) {
const command = opts.command;
const on_success = opts.on_success;
const data = {
command: command,
};
channel.post({
url: "/json/zcommand",
data: data,
success: function (data) {
if (on_success) {
on_success(data);
}
},
error: function () {
exports.tell_user("server did not respond");
},
});
};
exports.tell_user = function (msg) {
// This is a bit hacky, but we don't have a super easy API now
// for just telling users stuff.
$("#compose-send-status").removeClass(common.status_classes)
.addClass("alert-error")
.stop(true).fadeTo(0, 1);
$("#compose-error-msg").text(msg);
};
exports.enter_day_mode = function () {
exports.send({
command: "/day",
on_success: function (data) {
night_mode.disable();
feedback_widget.show({
populate: function (container) {
const rendered_msg = marked(data.msg).trim();
container.html(rendered_msg);
},
on_undo: function () {
exports.send({
command: "/night",
});
},
title_text: i18n.t("Day mode"),
undo_button_text: i18n.t("Night"),
});
},
});
};
exports.enter_night_mode = function () {
exports.send({
command: "/night",
on_success: function (data) {
night_mode.enable();
feedback_widget.show({
populate: function (container) {
const rendered_msg = marked(data.msg).trim();
container.html(rendered_msg);
},
on_undo: function () {
exports.send({
command: "/day",
});
},
title_text: i18n.t("Night mode"),
undo_button_text: i18n.t("Day"),
});
},
});
};
exports.enter_fluid_mode = function () {
exports.send({
command: "/fluid-width",
on_success: function (data) {
scroll_bar.set_layout_width();
feedback_widget.show({
populate: function (container) {
const rendered_msg = marked(data.msg).trim();
container.html(rendered_msg);
},
on_undo: function () {
exports.send({
command: "/fixed-width",
});
},
title_text: i18n.t("Fluid width mode"),
undo_button_text: i18n.t("Fixed width"),
});
},
});
};
exports.enter_fixed_mode = function () {
exports.send({
command: "/fixed-width",
on_success: function (data) {
scroll_bar.set_layout_width();
feedback_widget.show({
populate: function (container) {
const rendered_msg = marked(data.msg).trim();
container.html(rendered_msg);
},
on_undo: function () {
exports.send({
command: "/fluid-width",
});
},
title_text: i18n.t("Fixed width mode"),
undo_button_text: i18n.t("Fluid width"),
});
},
});
};
exports.process = function (message_content) {
const content = message_content.trim();
if (content === "/ping") {
const start_time = new Date();
exports.send({
command: content,
on_success: function () {
const end_time = new Date();
let diff = end_time - start_time;
diff = Math.round(diff);
const msg = "ping time: " + diff + "ms";
exports.tell_user(msg);
},
});
return true;
}
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)) {
exports.enter_day_mode();
return true;
}
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)) {
exports.enter_night_mode();
return true;
}
if (content === "/fluid-width") {
exports.enter_fluid_mode();
return true;
}
if (content === "/fixed-width") {
exports.enter_fixed_mode();
return true;
}
if (content === "/settings") {
hashchange.go_to_location("settings/your-account");
2018-06-17 21:10:57 +02:00
return true;
}
// 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;
};
window.zcommand = exports;