2021-02-28 00:46:27 +01:00
|
|
|
import * as channel from "./channel";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-02-28 00:46:27 +01:00
|
|
|
export function get_message_events(message) {
|
2018-02-12 15:12:40 +01:00
|
|
|
if (message.locally_echoed) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-02-12 15:12:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!message.submessages) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-02-12 15:12:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (message.submessages.length === 0) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-02-12 15:12:40 +01:00
|
|
|
}
|
|
|
|
|
2020-10-07 09:17:30 +02:00
|
|
|
message.submessages.sort((m1, m2) => Number.parseInt(m1.id, 10) - Number.parseInt(m2.id, 10));
|
2018-02-12 15:12:40 +01:00
|
|
|
|
2020-07-02 01:39:34 +02:00
|
|
|
const events = message.submessages.map((obj) => ({
|
js: Convert _.map(a, …) to a.map(…).
And convert the corresponding function expressions to arrow style
while we’re here.
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, {
visitCallExpression(path) {
const { callee, arguments: args } = path.node;
if (
n.MemberExpression.check(callee) &&
!callee.computed &&
n.Identifier.check(callee.object) &&
callee.object.name === "_" &&
n.Identifier.check(callee.property) &&
callee.property.name === "map" &&
args.length === 2 &&
checkExpression(args[0]) &&
checkExpression(args[1])
) {
const [arr, fn] = args;
path.replace(
b.callExpression(b.memberExpression(arr, b.identifier("map")), [
n.FunctionExpression.check(fn) ||
n.ArrowFunctionExpression.check(fn)
? b.arrowFunctionExpression(
fn.params,
n.BlockStatement.check(fn.body) &&
fn.body.body.length === 1 &&
n.ReturnStatement.check(fn.body.body[0])
? fn.body.body[0].argument || b.identifier("undefined")
: fn.body
)
: fn,
])
);
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 02:43:49 +01:00
|
|
|
sender_id: obj.sender_id,
|
|
|
|
data: JSON.parse(obj.content),
|
|
|
|
}));
|
2018-02-12 15:12:40 +01:00
|
|
|
|
|
|
|
return events;
|
2021-02-28 00:46:27 +01:00
|
|
|
}
|
2018-02-12 15:12:40 +01:00
|
|
|
|
2021-02-28 00:46:27 +01:00
|
|
|
export function process_submessages(in_opts) {
|
2018-05-23 12:35:52 +02:00
|
|
|
// This happens in our rendering path, so we try to limit any
|
|
|
|
// damage that may be triggered by one rogue message.
|
|
|
|
try {
|
2021-02-28 00:46:27 +01:00
|
|
|
return do_process_submessages(in_opts);
|
2020-10-07 10:20:41 +02:00
|
|
|
} catch (error) {
|
|
|
|
blueslip.error("in process_submessages: " + error.message);
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-05-23 12:35:52 +02:00
|
|
|
}
|
2021-02-28 00:46:27 +01:00
|
|
|
}
|
2018-05-23 12:35:52 +02:00
|
|
|
|
2021-02-28 00:46:27 +01:00
|
|
|
export function do_process_submessages(in_opts) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_id = in_opts.message_id;
|
|
|
|
const message = message_store.get(message_id);
|
2018-02-12 15:12:40 +01:00
|
|
|
|
|
|
|
if (!message) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-28 00:46:27 +01:00
|
|
|
const events = get_message_events(message);
|
2018-02-12 15:12:40 +01:00
|
|
|
|
|
|
|
if (!events) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const row = in_opts.row;
|
2018-02-23 16:20:33 +01:00
|
|
|
|
|
|
|
// Right now, our only use of submessages is widgets.
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = events[0].data;
|
2018-02-23 16:20:33 +01:00
|
|
|
|
|
|
|
if (data === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const widget_type = data.widget_type;
|
2018-02-23 16:20:33 +01:00
|
|
|
|
|
|
|
if (widget_type === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-28 00:46:27 +01:00
|
|
|
const post_to_server = make_server_callback(message_id);
|
2018-02-23 16:20:33 +01:00
|
|
|
|
|
|
|
widgetize.activate({
|
2020-07-20 22:18:43 +02:00
|
|
|
widget_type,
|
2018-02-23 16:20:33 +01:00
|
|
|
extra_data: data.extra_data,
|
2020-07-20 22:18:43 +02:00
|
|
|
events,
|
|
|
|
row,
|
|
|
|
message,
|
|
|
|
post_to_server,
|
2018-02-23 16:20:33 +01:00
|
|
|
});
|
2021-02-28 00:46:27 +01:00
|
|
|
}
|
2018-02-12 15:12:40 +01:00
|
|
|
|
2021-02-28 00:46:27 +01:00
|
|
|
export function update_message(submsg) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = message_store.get(submsg.message_id);
|
2018-05-30 22:52:11 +02:00
|
|
|
|
|
|
|
if (message === undefined) {
|
|
|
|
// This is generally not a problem--the server
|
|
|
|
// can send us events without us having received
|
|
|
|
// the original message, since the server doesn't
|
|
|
|
// track that.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-08 05:34:29 +01:00
|
|
|
if (message.submessages === undefined) {
|
|
|
|
message.submessages = [];
|
|
|
|
}
|
|
|
|
|
2020-07-02 01:39:34 +02:00
|
|
|
const existing = message.submessages.find((sm) => sm.id === submsg.id);
|
2018-05-30 22:52:11 +02:00
|
|
|
|
|
|
|
if (existing !== undefined) {
|
|
|
|
blueslip.warn("Got submessage multiple times: " + submsg.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
message.submessages.push(submsg);
|
2021-02-28 00:46:27 +01:00
|
|
|
}
|
2018-05-30 22:52:11 +02:00
|
|
|
|
2021-02-28 00:46:27 +01:00
|
|
|
export function handle_event(submsg) {
|
2018-05-30 22:52:11 +02:00
|
|
|
// Update message.submessages in case we haven't actually
|
|
|
|
// activated the widget yet, so that when the message does
|
|
|
|
// come in view, the data will be complete.
|
2021-02-28 00:46:27 +01:00
|
|
|
update_message(submsg);
|
2018-05-30 22:52:11 +02:00
|
|
|
|
2018-02-23 16:20:33 +01:00
|
|
|
// Right now, our only use of submessages is widgets.
|
2019-11-02 00:06:25 +01:00
|
|
|
const msg_type = submsg.msg_type;
|
2018-02-23 16:20:33 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (msg_type !== "widget") {
|
|
|
|
blueslip.warn("unknown msg_type: " + msg_type);
|
2018-02-23 16:20:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let data;
|
2018-05-30 21:12:16 +02:00
|
|
|
|
|
|
|
try {
|
2018-05-30 22:29:56 +02:00
|
|
|
data = JSON.parse(submsg.content);
|
2020-10-07 10:18:48 +02:00
|
|
|
} catch {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("server sent us invalid json in handle_event: " + submsg.content);
|
2018-05-30 21:12:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-23 16:20:33 +01:00
|
|
|
widgetize.handle_event({
|
2018-05-30 22:29:56 +02:00
|
|
|
sender_id: submsg.sender_id,
|
|
|
|
message_id: submsg.message_id,
|
2020-07-20 22:18:43 +02:00
|
|
|
data,
|
2018-02-23 16:20:33 +01:00
|
|
|
});
|
2021-02-28 00:46:27 +01:00
|
|
|
}
|
2018-02-12 15:12:40 +01:00
|
|
|
|
2021-02-28 00:46:27 +01:00
|
|
|
export function make_server_callback(message_id) {
|
2018-02-12 15:12:40 +01:00
|
|
|
return function (opts) {
|
2020-07-15 01:29:15 +02:00
|
|
|
const url = "/json/submessage";
|
2018-02-12 15:12:40 +01:00
|
|
|
|
|
|
|
channel.post({
|
2020-07-20 22:18:43 +02:00
|
|
|
url,
|
2018-02-12 15:12:40 +01:00
|
|
|
data: {
|
2020-07-20 22:18:43 +02:00
|
|
|
message_id,
|
2018-02-12 15:12:40 +01:00
|
|
|
msg_type: opts.msg_type,
|
|
|
|
content: JSON.stringify(opts.data),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
2021-02-28 00:46:27 +01:00
|
|
|
}
|