2021-03-16 23:38:59 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
2021-02-28 21:31:33 +01:00
|
|
|
import * as message_list from "./message_list";
|
2021-02-28 01:10:03 +01:00
|
|
|
import * as people from "./people";
|
2020-07-24 06:02:07 +02:00
|
|
|
|
2020-02-06 02:19:58 +01:00
|
|
|
const stored_messages = new Map();
|
2014-01-31 16:27:24 +01:00
|
|
|
|
2021-03-28 17:57:53 +02:00
|
|
|
export function update_message_cache(message) {
|
|
|
|
// You should only call this from message_helper (or in tests).
|
|
|
|
stored_messages.set(message.id, message);
|
2021-03-09 13:51:07 +01:00
|
|
|
}
|
|
|
|
|
2021-03-28 17:57:53 +02:00
|
|
|
export function get_cached_message(message_id) {
|
|
|
|
// You should only call this from message_helper.
|
|
|
|
// Use the get() wrapper below for most other use cases.
|
|
|
|
return stored_messages.get(message_id);
|
2021-02-28 01:10:03 +01:00
|
|
|
}
|
2020-01-02 14:42:55 +01:00
|
|
|
|
2021-03-28 17:57:53 +02:00
|
|
|
export function clear_for_testing() {
|
|
|
|
stored_messages.clear();
|
node tests: Introduce message_store.create_mock_message() helper.
Previously, it was tedious to create actual message
objects in message_store for use in node tests.
This was mainly because, `add_message_metadata`
in message_store has many dependencies and
validation checks. Since it was difficult to create
actual message objects, many tests just mocked
the `message_store.get()` method to return the desired
message.
This commit adds a new helper method (`create_mock_message`)
to message_store, for use in node tests. This just stores
the object passed to it in the `stores_messages` map,
without any validation. We do not add any
default fields to the message object before saving
it from this helper, because doing so would decrease
the utility of this helper, and, if a test
depends on some field having a particular value,
then it would be better to just pass the field: value
pair from the test itself, for readability, rather
than relying on the helper to add the field for us.
This helper allows us to write deeper tests.
This commit also replaces some instances of mocking
`message_store.get()` to use this new helper method.
2021-03-05 06:34:11 +01:00
|
|
|
}
|
|
|
|
|
2021-02-28 01:10:03 +01:00
|
|
|
export function get(message_id) {
|
2020-04-09 23:12:03 +02:00
|
|
|
if (message_id === undefined || message_id === null) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("message_store.get got bad value: " + message_id);
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2020-04-09 23:12:03 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (typeof message_id !== "number") {
|
|
|
|
blueslip.error("message_store got non-number: " + message_id);
|
2020-04-09 23:12:03 +02:00
|
|
|
|
2020-07-12 23:21:05 +02:00
|
|
|
// Try to soldier on, assuming the caller treats message
|
2020-04-09 23:12:03 +02:00
|
|
|
// ids as strings.
|
2020-10-07 09:17:30 +02:00
|
|
|
message_id = Number.parseFloat(message_id);
|
2020-04-09 23:12:03 +02:00
|
|
|
}
|
|
|
|
|
2020-02-06 02:19:58 +01:00
|
|
|
return stored_messages.get(message_id);
|
2021-02-28 01:10:03 +01:00
|
|
|
}
|
2014-01-31 22:02:57 +01:00
|
|
|
|
2021-02-28 01:10:03 +01:00
|
|
|
export function get_pm_emails(message) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids = people.pm_with_user_ids(message);
|
2021-01-23 02:36:54 +01:00
|
|
|
const emails = user_ids
|
|
|
|
.map((user_id) => {
|
|
|
|
const person = people.get_by_user_id(user_id);
|
|
|
|
if (!person) {
|
|
|
|
blueslip.error("Unknown user id " + user_id);
|
|
|
|
return "?";
|
|
|
|
}
|
|
|
|
return person.email;
|
|
|
|
})
|
|
|
|
.sort();
|
2017-01-25 00:38:19 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
return emails.join(", ");
|
2021-02-28 01:10:03 +01:00
|
|
|
}
|
2016-05-25 13:53:23 +02:00
|
|
|
|
2021-02-28 01:10:03 +01:00
|
|
|
export function get_pm_full_names(message) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids = people.pm_with_user_ids(message);
|
2021-01-23 02:36:54 +01:00
|
|
|
const names = user_ids
|
|
|
|
.map((user_id) => {
|
|
|
|
const person = people.get_by_user_id(user_id);
|
|
|
|
if (!person) {
|
|
|
|
blueslip.error("Unknown user id " + user_id);
|
|
|
|
return "?";
|
|
|
|
}
|
|
|
|
return person.full_name;
|
|
|
|
})
|
|
|
|
.sort();
|
2017-01-25 00:38:19 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
return names.join(", ");
|
2021-02-28 01:10:03 +01:00
|
|
|
}
|
2017-01-25 00:38:19 +01:00
|
|
|
|
2021-02-28 01:10:03 +01:00
|
|
|
export function set_message_booleans(message) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const flags = message.flags || [];
|
2017-12-16 22:40:43 +01:00
|
|
|
|
2017-08-04 14:14:09 +02:00
|
|
|
function convert_flag(flag_name) {
|
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
|
|
|
return flags.includes(flag_name);
|
2017-08-04 14:14:09 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
message.unread = !convert_flag("read");
|
|
|
|
message.historical = convert_flag("historical");
|
|
|
|
message.starred = convert_flag("starred");
|
|
|
|
message.mentioned = convert_flag("mentioned") || convert_flag("wildcard_mentioned");
|
2020-07-16 23:29:01 +02:00
|
|
|
message.mentioned_me_directly = convert_flag("mentioned");
|
2020-07-15 01:29:15 +02:00
|
|
|
message.collapsed = convert_flag("collapsed");
|
|
|
|
message.alerted = convert_flag("has_alert_word");
|
2017-12-21 16:08:16 +01:00
|
|
|
|
|
|
|
// Once we have set boolean flags here, the `flags` attribute is
|
|
|
|
// just a distraction, so we delete it. (All the downstream code
|
|
|
|
// uses booleans.)
|
|
|
|
delete message.flags;
|
2021-02-28 01:10:03 +01:00
|
|
|
}
|
2017-08-04 14:14:09 +02:00
|
|
|
|
2021-02-28 01:10:03 +01:00
|
|
|
export function init_booleans(message) {
|
2017-12-16 23:25:31 +01:00
|
|
|
// This initializes booleans for the local-echo path where
|
|
|
|
// we don't have flags from the server yet. (We want to
|
|
|
|
// explicitly set flags to false to be consistent with other
|
|
|
|
// codepaths.)
|
|
|
|
message.unread = false;
|
|
|
|
message.historical = false;
|
|
|
|
message.starred = false;
|
|
|
|
message.mentioned = false;
|
|
|
|
message.mentioned_me_directly = false;
|
|
|
|
message.collapsed = false;
|
|
|
|
message.alerted = false;
|
2021-02-28 01:10:03 +01:00
|
|
|
}
|
2017-12-16 23:25:31 +01:00
|
|
|
|
2021-02-28 01:10:03 +01:00
|
|
|
export function update_booleans(message, flags) {
|
2017-12-16 23:25:31 +01:00
|
|
|
// When we get server flags for local echo or message edits,
|
|
|
|
// we are vulnerable to race conditions, so only update flags
|
|
|
|
// that are driven by message content.
|
|
|
|
function convert_flag(flag_name) {
|
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
|
|
|
return flags.includes(flag_name);
|
2017-12-16 23:25:31 +01:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
message.mentioned = convert_flag("mentioned") || convert_flag("wildcard_mentioned");
|
2020-07-16 23:29:01 +02:00
|
|
|
message.mentioned_me_directly = convert_flag("mentioned");
|
2020-07-15 01:29:15 +02:00
|
|
|
message.alerted = convert_flag("has_alert_word");
|
2021-02-28 01:10:03 +01:00
|
|
|
}
|
2017-12-16 23:25:31 +01:00
|
|
|
|
2021-02-28 01:10:03 +01:00
|
|
|
export function update_property(property, value, info) {
|
2020-07-26 20:28:40 +02:00
|
|
|
switch (property) {
|
|
|
|
case "sender_full_name":
|
2020-07-26 23:17:30 +02:00
|
|
|
case "small_avatar_url":
|
2021-03-09 13:42:43 +01:00
|
|
|
for (const msg of stored_messages.values()) {
|
2020-07-26 20:28:40 +02:00
|
|
|
if (msg.sender_id && msg.sender_id === info.user_id) {
|
|
|
|
msg[property] = value;
|
|
|
|
}
|
2021-03-09 13:42:43 +01:00
|
|
|
}
|
2020-07-26 20:28:40 +02:00
|
|
|
break;
|
2020-07-26 23:39:32 +02:00
|
|
|
case "stream_name":
|
2021-03-09 13:42:43 +01:00
|
|
|
for (const msg of stored_messages.values()) {
|
2020-07-26 23:39:32 +02:00
|
|
|
if (msg.stream_id && msg.stream_id === info.stream_id) {
|
|
|
|
msg.display_recipient = value;
|
|
|
|
msg.stream = value;
|
|
|
|
}
|
2021-03-09 13:42:43 +01:00
|
|
|
}
|
2020-07-26 23:39:32 +02:00
|
|
|
break;
|
2020-07-26 20:28:40 +02:00
|
|
|
}
|
2021-02-28 01:10:03 +01:00
|
|
|
}
|
2020-07-26 20:28:40 +02:00
|
|
|
|
2021-02-28 01:10:03 +01:00
|
|
|
export function reify_message_id(opts) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const old_id = opts.old_id;
|
|
|
|
const new_id = opts.new_id;
|
2020-02-06 02:19:58 +01:00
|
|
|
if (stored_messages.has(old_id)) {
|
|
|
|
stored_messages.set(new_id, stored_messages.get(old_id));
|
|
|
|
stored_messages.delete(old_id);
|
2017-07-19 12:49:49 +02:00
|
|
|
}
|
2014-01-31 16:27:24 +01: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 msg_list of [message_list.all, home_msg_list, message_list.narrowed]) {
|
2017-07-19 12:49:49 +02:00
|
|
|
if (msg_list !== undefined) {
|
|
|
|
msg_list.change_message_id(old_id, new_id);
|
|
|
|
|
|
|
|
if (msg_list.view !== undefined) {
|
|
|
|
msg_list.view.change_message_id(old_id, new_id);
|
2014-01-31 16:27:24 +01:00
|
|
|
}
|
2017-07-19 12:49:49 +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
|
|
|
}
|
2021-02-28 01:10:03 +01:00
|
|
|
}
|