2021-03-16 23:38:59 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
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-05-10 15:35:02 +02:00
|
|
|
const names = people.get_display_full_names(user_ids).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-03-28 19:08:25 +02:00
|
|
|
export function reify_message_id({old_id, 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
|
|
|
}
|
2021-02-28 01:10:03 +01:00
|
|
|
}
|