2020-02-13 22:34:29 +01:00
|
|
|
const util = require("./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();
|
|
|
|
|
|
|
|
exports.user_ids = function () {
|
|
|
|
return Array.from(message_user_ids);
|
|
|
|
};
|
|
|
|
|
2014-01-31 22:02:57 +01:00
|
|
|
exports.get = 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-04-09 23:12:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
message_id = parseFloat(message_id);
|
|
|
|
}
|
|
|
|
|
2020-02-06 02:19:58 +01:00
|
|
|
return stored_messages.get(message_id);
|
2014-01-31 22:02:57 +01:00
|
|
|
};
|
|
|
|
|
2019-02-25 19:01:23 +01:00
|
|
|
exports.each = function (f) {
|
2020-02-06 02:19:58 +01:00
|
|
|
stored_messages.forEach(f);
|
2019-02-25 19:01:23 +01:00
|
|
|
};
|
|
|
|
|
2017-01-25 00:38:19 +01:00
|
|
|
exports.get_pm_emails = function (message) {
|
2017-04-17 23:28:59 +02:00
|
|
|
function email(user_id) {
|
2020-02-05 14:30:59 +01:00
|
|
|
const person = people.get_by_user_id(user_id);
|
2017-04-17 23:28:59 +02:00
|
|
|
if (!person) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("Unknown user id " + user_id);
|
|
|
|
return "?";
|
2017-04-17 23:28:59 +02:00
|
|
|
}
|
|
|
|
return person.email;
|
2016-05-25 13:53:23 +02:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids = people.pm_with_user_ids(message);
|
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
|
|
|
const emails = user_ids.map(email).sort();
|
2017-01-25 00:38:19 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
return emails.join(", ");
|
2016-05-25 13:53:23 +02:00
|
|
|
};
|
|
|
|
|
2017-01-25 00:38:19 +01:00
|
|
|
exports.get_pm_full_names = function (message) {
|
2017-04-17 23:08:38 +02:00
|
|
|
function name(user_id) {
|
2020-02-05 14:30:59 +01:00
|
|
|
const person = people.get_by_user_id(user_id);
|
2017-04-17 23:08:38 +02:00
|
|
|
if (!person) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("Unknown user id " + user_id);
|
|
|
|
return "?";
|
2017-04-17 23:08:38 +02:00
|
|
|
}
|
|
|
|
return person.full_name;
|
2017-01-25 00:38:19 +01:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids = people.pm_with_user_ids(message);
|
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
|
|
|
const names = user_ids.map(name).sort();
|
2017-01-25 00:38:19 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
return names.join(", ");
|
2017-01-25 00:38:19 +01:00
|
|
|
};
|
|
|
|
|
2017-02-09 01:34:54 +01:00
|
|
|
exports.process_message_for_recent_private_messages = function (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);
|
2017-04-13 20:34:21 +02:00
|
|
|
};
|
2016-11-18 15:48:53 +01:00
|
|
|
|
2017-12-16 22:40:43 +01:00
|
|
|
exports.set_message_booleans = function (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;
|
2017-08-04 14:14:09 +02:00
|
|
|
};
|
|
|
|
|
2017-12-16 23:25:31 +01:00
|
|
|
exports.init_booleans = function (message) {
|
|
|
|
// 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;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.update_booleans = function (message, flags) {
|
|
|
|
// 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");
|
2017-12-16 23:25:31 +01:00
|
|
|
};
|
|
|
|
|
2017-03-19 18:19:48 +01:00
|
|
|
exports.add_message_metadata = function (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;
|
2020-07-15 00:34:28 +02:00
|
|
|
message.reply_to = util.normalize_recipients(exports.get_pm_emails(message));
|
2020-07-15 02:14:03 +02:00
|
|
|
message.display_reply_to = exports.get_pm_full_names(message);
|
|
|
|
message.pm_with_url = people.pm_with_url(message);
|
|
|
|
message.to_user_ids = people.pm_reply_user_string(message);
|
|
|
|
|
|
|
|
exports.process_message_for_recent_private_messages(message);
|
|
|
|
|
|
|
|
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;
|
2017-03-19 18:19:48 +01:00
|
|
|
};
|
2014-01-31 16:27:24 +01:00
|
|
|
|
2020-07-26 20:28:40 +02:00
|
|
|
exports.update_property = function (property, value, info) {
|
|
|
|
switch (property) {
|
|
|
|
case "sender_full_name":
|
2020-07-26 23:17:30 +02:00
|
|
|
case "small_avatar_url":
|
2020-07-26 20:28:40 +02:00
|
|
|
exports.each((msg) => {
|
|
|
|
if (msg.sender_id && msg.sender_id === info.user_id) {
|
|
|
|
msg[property] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
2020-07-26 23:39:32 +02:00
|
|
|
case "stream_name":
|
|
|
|
exports.each((msg) => {
|
|
|
|
if (msg.stream_id && msg.stream_id === info.stream_id) {
|
|
|
|
msg.display_recipient = value;
|
|
|
|
msg.stream = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
2020-07-26 20:28:40 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-19 12:49:49 +02:00
|
|
|
exports.reify_message_id = function (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
|
|
|
}
|
2017-07-04 02:09:30 +02:00
|
|
|
};
|
2014-01-31 16:27:24 +01:00
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.message_store = exports;
|