2021-02-28 01:10:03 +01:00
|
|
|
import * as alert_words from "./alert_words";
|
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";
|
|
|
|
import * as pm_conversations from "./pm_conversations";
|
|
|
|
import * as recent_senders from "./recent_senders";
|
|
|
|
import * as stream_topic_history from "./stream_topic_history";
|
|
|
|
import * as util from "./util";
|
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
|
|
|
|
2020-01-02 14:42:55 +01:00
|
|
|
/*
|
|
|
|
We keep a set of user_ids for all people
|
|
|
|
who have sent stream messages or who have
|
|
|
|
been on PMs sent by the user.
|
|
|
|
|
|
|
|
We will use this in search to prevent really
|
|
|
|
large result sets for realms that have lots
|
|
|
|
of users who haven't sent messages recently.
|
|
|
|
|
|
|
|
We'll likely eventually want to replace this with
|
|
|
|
accessing some combination of data from recent_senders
|
|
|
|
and pm_conversations for better accuracy.
|
|
|
|
*/
|
|
|
|
const message_user_ids = new Set();
|
|
|
|
|
2021-03-09 13:51:07 +01:00
|
|
|
export function clear_for_testing() {
|
|
|
|
stored_messages.clear();
|
|
|
|
message_user_ids.clear();
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:10:03 +01:00
|
|
|
export function user_ids() {
|
2020-01-02 14:42:55 +01:00
|
|
|
return Array.from(message_user_ids);
|
2021-02-28 01:10:03 +01:00
|
|
|
}
|
2020-01-02 14:42:55 +01:00
|
|
|
|
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
|
|
|
export function create_mock_message(message) {
|
|
|
|
// For use in tests only. `id` is a required field,
|
|
|
|
// everything else is optional, as required in the test.
|
|
|
|
stored_messages.set(message.id, message);
|
|
|
|
}
|
|
|
|
|
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 process_message_for_recent_private_messages(message) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids = people.pm_with_user_ids(message);
|
2017-02-09 01:34:54 +01:00
|
|
|
if (!user_ids) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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 user_id of user_ids) {
|
2017-06-01 07:46:23 +02:00
|
|
|
pm_conversations.set_partner(user_id);
|
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
|
|
|
}
|
2017-06-01 07:46:23 +02:00
|
|
|
|
2020-01-01 15:42:46 +01:00
|
|
|
pm_conversations.recent.insert(user_ids, message.id);
|
2021-02-28 01:10:03 +01:00
|
|
|
}
|
2016-11-18 15:48:53 +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 add_message_metadata(message) {
|
2020-02-06 02:19:58 +01:00
|
|
|
const cached_msg = stored_messages.get(message.id);
|
2014-01-31 16:27:24 +01:00
|
|
|
if (cached_msg !== undefined) {
|
2018-12-22 15:22:07 +01:00
|
|
|
// Copy the match topic and content over if they exist on
|
2014-01-31 16:27:24 +01:00
|
|
|
// the new message
|
2018-11-15 16:59:41 +01:00
|
|
|
if (util.get_match_topic(message) !== undefined) {
|
|
|
|
util.set_match_data(cached_msg, message);
|
2014-01-31 16:27:24 +01:00
|
|
|
}
|
|
|
|
return cached_msg;
|
|
|
|
}
|
|
|
|
|
2017-01-19 20:18:03 +01:00
|
|
|
message.sent_by_me = people.is_current_user(message.sender_email);
|
2014-01-31 16:27:24 +01:00
|
|
|
|
2016-12-15 23:33:36 +01:00
|
|
|
people.extract_people_from_message(message);
|
2017-11-06 15:48:44 +01:00
|
|
|
people.maybe_incr_recipient_count(message);
|
2016-12-15 23:33:36 +01:00
|
|
|
|
2020-02-05 14:30:59 +01:00
|
|
|
const sender = people.get_by_user_id(message.sender_id);
|
2017-01-24 23:10:01 +01:00
|
|
|
if (sender) {
|
|
|
|
message.sender_full_name = sender.full_name;
|
2017-02-04 19:12:25 +01:00
|
|
|
message.sender_email = sender.email;
|
2017-01-24 23:10:01 +01:00
|
|
|
}
|
|
|
|
|
2018-12-23 16:49:14 +01:00
|
|
|
// Convert topic even for PMs, as legacy code
|
|
|
|
// wants the empty field.
|
|
|
|
util.convert_message_topic(message);
|
|
|
|
|
2014-01-31 16:27:24 +01:00
|
|
|
switch (message.type) {
|
2020-07-15 02:14:03 +02:00
|
|
|
case "stream":
|
|
|
|
message.is_stream = true;
|
|
|
|
message.stream = message.display_recipient;
|
|
|
|
message.reply_to = message.sender_email;
|
|
|
|
|
|
|
|
stream_topic_history.add_message({
|
|
|
|
stream_id: message.stream_id,
|
|
|
|
topic_name: message.topic,
|
|
|
|
message_id: message.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
recent_senders.process_message_for_senders(message);
|
|
|
|
message_user_ids.add(message.sender_id);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "private":
|
|
|
|
message.is_private = true;
|
2021-02-28 01:10:03 +01:00
|
|
|
message.reply_to = util.normalize_recipients(get_pm_emails(message));
|
|
|
|
message.display_reply_to = get_pm_full_names(message);
|
2020-07-15 02:14:03 +02:00
|
|
|
message.pm_with_url = people.pm_with_url(message);
|
|
|
|
message.to_user_ids = people.pm_reply_user_string(message);
|
|
|
|
|
2021-02-28 01:10:03 +01:00
|
|
|
process_message_for_recent_private_messages(message);
|
2020-07-15 02:14:03 +02:00
|
|
|
|
|
|
|
if (people.is_my_user_id(message.sender_id)) {
|
|
|
|
for (const recip of message.display_recipient) {
|
|
|
|
message_user_ids.add(recip.id);
|
|
|
|
}
|
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
|
|
|
}
|
2020-07-15 02:14:03 +02:00
|
|
|
break;
|
2014-01-31 16:27:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
alert_words.process_message(message);
|
2016-12-02 13:23:23 +01:00
|
|
|
if (!message.reactions) {
|
|
|
|
message.reactions = [];
|
|
|
|
}
|
2020-02-06 02:19:58 +01:00
|
|
|
stored_messages.set(message.id, message);
|
2014-01-31 16:27:24 +01:00
|
|
|
return message;
|
2021-02-28 01:10:03 +01:00
|
|
|
}
|
2014-01-31 16:27:24 +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
|
|
|
}
|