2017-03-30 20:04:01 +02:00
|
|
|
// This module just manages data. See activity.js for
|
|
|
|
// the UI of our buddy list.
|
|
|
|
|
2020-02-07 17:19:03 +01:00
|
|
|
// The following Maps have user_id as the key. Some of the
|
|
|
|
// user_ids may not yet be registered in people.js.
|
|
|
|
// See the long comment in `set_info` below for details.
|
2017-03-30 20:04:01 +02:00
|
|
|
|
2020-02-07 17:19:03 +01:00
|
|
|
// In future commits we'll use raw_info to facilitate
|
|
|
|
// handling server events and/or timeout events.
|
|
|
|
const raw_info = new Map();
|
|
|
|
exports.presence_info = new Map();
|
2017-03-30 20:04:01 +02:00
|
|
|
|
|
|
|
/* Mark users as offline after 140 seconds since their last checkin,
|
|
|
|
* Keep in sync with zerver/tornado/event_queue.py:receiver_is_idle
|
|
|
|
*/
|
2019-11-02 00:06:25 +01:00
|
|
|
const OFFLINE_THRESHOLD_SECS = 140;
|
2017-03-30 20:04:01 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const BIG_REALM_COUNT = 250;
|
2017-03-31 00:18:04 +02:00
|
|
|
|
2018-04-24 18:12:09 +02:00
|
|
|
exports.is_active = function (user_id) {
|
2020-02-06 04:21:07 +01:00
|
|
|
if (exports.presence_info.has(user_id)) {
|
|
|
|
const status = exports.presence_info.get(user_id).status;
|
2020-02-07 17:19:03 +01:00
|
|
|
if (status === "active") {
|
2017-03-30 20:04:01 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_status = function (user_id) {
|
2019-01-03 16:44:06 +01:00
|
|
|
if (people.is_my_user_id(user_id)) {
|
2017-04-13 04:28:15 +02:00
|
|
|
return "active";
|
|
|
|
}
|
2020-02-06 04:21:07 +01:00
|
|
|
if (exports.presence_info.has(user_id)) {
|
|
|
|
return exports.presence_info.get(user_id).status;
|
2017-06-19 19:03:39 +02:00
|
|
|
}
|
2017-10-12 18:08:42 +02:00
|
|
|
return "offline";
|
2017-03-30 20:04:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_user_ids = function () {
|
2020-02-06 04:21:07 +01:00
|
|
|
return Array.from(exports.presence_info.keys());
|
2017-03-30 20:04:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function status_from_timestamp(baseline_time, info) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let status = 'offline';
|
|
|
|
let last_active = 0;
|
2020-02-05 15:08:51 +01:00
|
|
|
|
2020-02-06 03:58:06 +01:00
|
|
|
for (const [device, device_presence] of Object.entries(info)) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const age = baseline_time - device_presence.timestamp;
|
2017-03-30 20:04:01 +02:00
|
|
|
if (last_active < device_presence.timestamp) {
|
|
|
|
last_active = device_presence.timestamp;
|
|
|
|
}
|
|
|
|
if (age < OFFLINE_THRESHOLD_SECS) {
|
|
|
|
switch (device_presence.status) {
|
2018-05-07 01:28:37 +02:00
|
|
|
case 'active':
|
|
|
|
status = device_presence.status;
|
|
|
|
break;
|
|
|
|
case 'idle':
|
|
|
|
if (status !== 'active') {
|
2017-03-30 20:04:01 +02:00
|
|
|
status = device_presence.status;
|
2018-05-07 01:28:37 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'offline':
|
|
|
|
if (status !== 'active' && status !== 'idle') {
|
|
|
|
status = device_presence.status;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
blueslip.error('Unexpected status', {presence_object: device_presence, device: device}, undefined);
|
2017-03-30 20:04:01 +02:00
|
|
|
}
|
|
|
|
}
|
2020-02-06 03:58:06 +01:00
|
|
|
}
|
2017-03-30 20:04:01 +02:00
|
|
|
return {status: status,
|
|
|
|
last_active: last_active };
|
|
|
|
}
|
|
|
|
|
|
|
|
// For testing
|
|
|
|
exports._status_from_timestamp = status_from_timestamp;
|
|
|
|
|
2020-02-07 17:19:03 +01:00
|
|
|
exports.update_info_from_event = function (user_id, info, server_time) {
|
|
|
|
raw_info.set(user_id, {
|
|
|
|
info: info,
|
|
|
|
server_time: server_time,
|
|
|
|
});
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const status = status_from_timestamp(server_time, info);
|
2020-02-06 04:21:07 +01:00
|
|
|
exports.presence_info.set(user_id, status);
|
2017-03-30 20:04:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.set_info = function (presences, server_timestamp) {
|
2020-02-07 17:19:03 +01:00
|
|
|
raw_info.clear();
|
2020-02-06 04:21:07 +01:00
|
|
|
exports.presence_info.clear();
|
2020-02-06 03:58:06 +01:00
|
|
|
for (const [user_id_str, info] of Object.entries(presences)) {
|
2020-02-07 17:19:03 +01:00
|
|
|
const user_id = parseInt(user_id_str, 10);
|
|
|
|
|
2020-02-13 20:56:37 +01:00
|
|
|
// Note: In contrast with all other state updates received
|
|
|
|
// receive from the server, presence data is updated via a
|
|
|
|
// polling process rather than the events system
|
|
|
|
// (server_events_dispatch.js).
|
2020-02-04 21:35:51 +01:00
|
|
|
//
|
2020-02-13 20:56:37 +01:00
|
|
|
// This means that if we're coming back from being offline and
|
|
|
|
// new users were created in the meantime, we may see user IDs
|
|
|
|
// not yet present in people.js if server_events doesn't have
|
|
|
|
// current data (or we've been offline, our event queue was
|
|
|
|
// GC'ed, and we're about to reload). Such user_ids being
|
|
|
|
// present could, in turn, create spammy downstream exceptions
|
|
|
|
// when rendering the buddy list. To address this, we check
|
|
|
|
// if the user ID is not yet present in people.js, and if it
|
|
|
|
// is, we skip storing that user (we'll see them again in the
|
|
|
|
// next presence request in 1 minute anyway).
|
|
|
|
//
|
|
|
|
// It's important to check both suspect_offline and
|
|
|
|
// reload_state.is_in_progress, because races where presence
|
|
|
|
// returns data on users not yet received via the server_events
|
|
|
|
// system are common in both situations.
|
|
|
|
const person = people.get_by_user_id(user_id, true);
|
|
|
|
if (person === undefined) {
|
|
|
|
if (!(server_events.suspect_offline || reload_state.is_in_progress())) {
|
|
|
|
// If we're online, and we get a user who we don't
|
|
|
|
// know about in the presence data, throw an error.
|
|
|
|
blueslip.error('Unknown user ID in presence data: ' + user_id);
|
|
|
|
}
|
|
|
|
// Either way, we deal by skipping this user and
|
|
|
|
// continuing with processing everyone else.
|
|
|
|
continue;
|
|
|
|
}
|
2020-02-07 17:19:03 +01:00
|
|
|
|
|
|
|
raw_info.set(user_id, {
|
|
|
|
info: info,
|
|
|
|
server_time: server_timestamp,
|
|
|
|
});
|
|
|
|
|
2020-02-02 17:29:05 +01:00
|
|
|
const status = status_from_timestamp(server_timestamp,
|
|
|
|
info);
|
2018-09-08 14:25:06 +02:00
|
|
|
|
2020-02-06 04:21:07 +01:00
|
|
|
exports.presence_info.set(user_id, status);
|
2020-02-06 03:58:06 +01:00
|
|
|
}
|
2017-03-31 00:18:04 +02:00
|
|
|
exports.update_info_for_small_realm();
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.update_info_for_small_realm = function () {
|
|
|
|
if (people.get_realm_count() >= BIG_REALM_COUNT) {
|
|
|
|
// For big realms, we don't want to bloat our buddy
|
|
|
|
// lists with lots of long-time-inactive users.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For small realms, we create presence info for users
|
|
|
|
// that the server didn't include in its presence update.
|
2019-11-02 00:06:25 +01:00
|
|
|
const persons = people.get_realm_persons();
|
2017-03-31 00:18:04 +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
|
|
|
for (const person of persons) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_id = person.user_id;
|
|
|
|
let status = "offline";
|
2017-03-31 00:18:04 +02:00
|
|
|
|
2020-02-06 04:21:07 +01:00
|
|
|
if (exports.presence_info.has(user_id)) {
|
2017-03-31 00:18:04 +02:00
|
|
|
// this is normal, we have data for active
|
|
|
|
// users that we don't want to clobber.
|
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
|
|
|
continue;
|
2017-03-31 00:18:04 +02:00
|
|
|
}
|
|
|
|
|
2017-06-07 18:36:26 +02:00
|
|
|
if (person.is_bot) {
|
|
|
|
// we don't show presence for bots
|
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
|
|
|
continue;
|
2017-06-07 18:36:26 +02:00
|
|
|
}
|
|
|
|
|
2018-09-08 14:41:41 +02:00
|
|
|
if (people.is_my_user_id(user_id)) {
|
|
|
|
status = "active";
|
|
|
|
}
|
|
|
|
|
2020-02-06 04:21:07 +01:00
|
|
|
exports.presence_info.set(user_id, {
|
2018-09-08 14:41:41 +02:00
|
|
|
status: status,
|
2017-03-31 00:18:04 +02:00
|
|
|
last_active: undefined,
|
2020-02-06 04:21:07 +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
|
|
|
}
|
2017-03-30 20:04:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.last_active_date = function (user_id) {
|
2020-02-06 04:21:07 +01:00
|
|
|
const info = exports.presence_info.get(user_id);
|
2017-03-30 20:04:01 +02:00
|
|
|
|
|
|
|
if (!info || !info.last_active) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const date = new XDate(info.last_active * 1000);
|
2017-03-30 20:04:01 +02:00
|
|
|
return date;
|
|
|
|
};
|
|
|
|
|
2020-02-25 12:16:26 +01:00
|
|
|
exports.initialize = function (params) {
|
|
|
|
presence.set_info(params.presences,
|
|
|
|
params.initial_servertime);
|
2020-02-07 16:33:03 +01:00
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.presence = exports;
|