2022-10-25 00:34:47 +02:00
|
|
|
import * as blueslip from "./blueslip";
|
2021-02-28 21:30:38 +01:00
|
|
|
import {FoldDict} from "./fold_dict";
|
|
|
|
import * as message_store from "./message_store";
|
2023-12-13 02:00:54 +01:00
|
|
|
import type {Message} from "./message_store";
|
2021-02-28 21:30:38 +01:00
|
|
|
import * as people from "./people";
|
2023-09-06 23:21:13 +02:00
|
|
|
import * as recent_view_util from "./recent_view_util";
|
2021-03-22 13:14:33 +01:00
|
|
|
import * as settings_config from "./settings_config";
|
2021-02-28 21:30:38 +01:00
|
|
|
import * as stream_data from "./stream_data";
|
2024-01-02 07:03:01 +01:00
|
|
|
import type {TopicHistoryEntry} from "./stream_topic_history";
|
2021-04-15 17:02:54 +02:00
|
|
|
import * as sub_store from "./sub_store";
|
2023-12-13 02:00:54 +01:00
|
|
|
import type {UpdateMessageEvent} from "./types";
|
2021-07-28 16:00:58 +02:00
|
|
|
import {user_settings} from "./user_settings";
|
2022-08-14 15:33:13 +02:00
|
|
|
import * as user_topics from "./user_topics";
|
2021-02-28 21:30:38 +01:00
|
|
|
import * as util from "./util";
|
2019-02-08 11:56:33 +01:00
|
|
|
|
2020-02-04 21:39:58 +01:00
|
|
|
// The unread module tracks the message IDs and locations of the
|
|
|
|
// user's unread messages. The tracking is initialized with
|
|
|
|
// server-provided data of the total set of unread messages in the
|
|
|
|
// user's history via page_params.unread_msgs (well, it cuts off at
|
|
|
|
// MAX_UNREAD_MESSAGES unreads for performance reasons). As a result,
|
|
|
|
// it can contain many thousands of messages that we don't have full
|
|
|
|
// data for in `message_store`, so we cannot in general look these
|
|
|
|
// messages up there.
|
|
|
|
|
|
|
|
// See https://zulip.readthedocs.io/en/latest/subsystems/pointer.html
|
|
|
|
// for more details on how this system is designed.
|
2016-08-17 01:19:14 +02:00
|
|
|
|
2021-02-28 21:30:38 +01:00
|
|
|
export let messages_read_in_narrow = false;
|
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function set_messages_read_in_narrow(value: boolean): void {
|
2021-02-28 21:30:38 +01:00
|
|
|
messages_read_in_narrow = value;
|
|
|
|
}
|
2013-05-17 21:32:26 +02:00
|
|
|
|
2022-11-16 10:53:04 +01:00
|
|
|
export let old_unreads_missing = false;
|
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function clear_old_unreads_missing(): void {
|
2022-11-16 10:53:04 +01:00
|
|
|
old_unreads_missing = false;
|
|
|
|
}
|
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export const unread_mentions_counter = new Set<number>();
|
2023-09-12 20:30:02 +02:00
|
|
|
export const direct_message_with_mention_count = new Set();
|
2023-12-13 02:00:54 +01:00
|
|
|
const unread_messages = new Set<number>();
|
2017-08-15 19:54:38 +02:00
|
|
|
|
2022-10-14 17:37:47 +02:00
|
|
|
// Map with keys of the form "{stream_id}:{topic.toLowerCase()}" and
|
|
|
|
// values being Sets of message IDs for unread messages mentioning the
|
2023-09-06 23:21:13 +02:00
|
|
|
// user within that topic. Use `recent_view_util.get_topic_key` to
|
2022-10-14 17:37:47 +02:00
|
|
|
// calculate keys.
|
|
|
|
//
|
|
|
|
// Functionally a cache; see clear_and_populate_unread_mention_topics
|
|
|
|
// for how we can refresh it efficiently.
|
|
|
|
export const unread_mention_topics = new Map();
|
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
type StreamCountInfo = {
|
|
|
|
unmuted_count: number;
|
|
|
|
muted_count: number;
|
|
|
|
followed_count: number;
|
|
|
|
stream_is_muted: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
type DirectMessageCountInfo = {
|
|
|
|
total_count: number;
|
|
|
|
pm_dict: Map<string, number>;
|
|
|
|
};
|
|
|
|
|
2023-06-16 12:24:46 +02:00
|
|
|
class UnreadDirectMessageCounter {
|
2023-12-13 02:00:54 +01:00
|
|
|
bucketer = new Map<string, Set<number>>();
|
2023-12-21 23:23:35 +01:00
|
|
|
// Maps direct message id to the user id string that's the key for bucketer
|
2023-12-13 02:00:54 +01:00
|
|
|
reverse_lookup = new Map<number, string>();
|
2017-07-31 14:59:18 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
clear(): void {
|
2020-07-23 03:58:17 +02:00
|
|
|
this.bucketer.clear();
|
2023-12-21 23:23:35 +01:00
|
|
|
this.reverse_lookup.clear();
|
2020-07-23 03:58:17 +02:00
|
|
|
}
|
2017-07-31 14:59:18 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
set_pms(pms: UnreadDirectMessageInfo[]): void {
|
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 obj of pms) {
|
2022-03-07 16:47:49 +01:00
|
|
|
const user_ids_string = obj.other_user_id.toString();
|
2020-07-23 03:58:17 +02:00
|
|
|
this.set_message_ids(user_ids_string, obj.unread_message_ids);
|
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-23 03:58:17 +02:00
|
|
|
}
|
2017-08-01 14:50:40 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
set_huddles(huddles: UnreadHuddleInfo[]): void {
|
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 obj of huddles) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids_string = people.pm_lookup_key(obj.user_ids_string);
|
2020-07-23 03:58:17 +02:00
|
|
|
this.set_message_ids(user_ids_string, obj.unread_message_ids);
|
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-23 03:58:17 +02:00
|
|
|
}
|
2017-08-01 14:50:40 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
set_message_ids(user_ids_string: string, unread_message_ids: number[]): void {
|
2023-12-21 23:23:35 +01:00
|
|
|
for (const message_id of unread_message_ids) {
|
|
|
|
this.add({message_id, user_ids_string});
|
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-23 03:58:17 +02:00
|
|
|
}
|
2017-08-01 14:50:40 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
add({message_id, user_ids_string}: {message_id: number; user_ids_string: string}): void {
|
2017-07-31 14:59:18 +02:00
|
|
|
if (user_ids_string) {
|
2023-12-21 23:23:35 +01:00
|
|
|
let bucket = this.bucketer.get(user_ids_string);
|
|
|
|
if (bucket === undefined) {
|
|
|
|
bucket = new Set();
|
|
|
|
this.bucketer.set(user_ids_string, bucket);
|
|
|
|
}
|
|
|
|
bucket.add(message_id);
|
|
|
|
this.reverse_lookup.set(message_id, user_ids_string);
|
2017-07-31 14:59:18 +02:00
|
|
|
}
|
2020-07-23 03:58:17 +02:00
|
|
|
}
|
2017-07-31 14:59:18 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
delete(message_id: number): void {
|
2023-12-21 23:23:35 +01:00
|
|
|
const user_ids_string = this.reverse_lookup.get(message_id);
|
|
|
|
if (user_ids_string === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.bucketer.get(user_ids_string)?.delete(message_id);
|
|
|
|
this.reverse_lookup.delete(message_id);
|
2020-07-23 03:58:17 +02:00
|
|
|
}
|
2017-07-31 14:59:18 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
get_counts(include_latest_msg_id = false): DirectMessageCountInfo {
|
2023-10-05 12:49:42 +02:00
|
|
|
const pm_dict = new Map(); // Hash by user_ids_string -> count Optional[, max_id]
|
2019-11-02 00:06:25 +01:00
|
|
|
let total_count = 0;
|
2020-07-23 03:58:17 +02:00
|
|
|
for (const [user_ids_string, id_set] of this.bucketer) {
|
2020-02-01 03:28:35 +01:00
|
|
|
const count = id_set.size;
|
2023-10-05 12:49:42 +02:00
|
|
|
if (include_latest_msg_id) {
|
|
|
|
const latest_msg_id = Math.max(...id_set);
|
|
|
|
pm_dict.set(user_ids_string, {
|
|
|
|
count,
|
|
|
|
latest_msg_id,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
pm_dict.set(user_ids_string, count);
|
|
|
|
}
|
2017-07-31 14:59:18 +02:00
|
|
|
total_count += count;
|
2020-02-03 09:26:53 +01:00
|
|
|
}
|
2017-07-31 14:59:18 +02:00
|
|
|
return {
|
2020-07-20 22:18:43 +02:00
|
|
|
total_count,
|
|
|
|
pm_dict,
|
2017-07-31 14:59:18 +02:00
|
|
|
};
|
2020-07-23 03:58:17 +02:00
|
|
|
}
|
2017-07-31 14:59:18 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
num_unread(user_ids_string: string): number {
|
2017-07-31 14:59:18 +02:00
|
|
|
if (!user_ids_string) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-12-21 23:23:35 +01:00
|
|
|
const bucket = this.bucketer.get(user_ids_string);
|
2017-08-03 04:32:21 +02:00
|
|
|
|
|
|
|
if (!bucket) {
|
2017-07-31 14:59:18 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2020-02-01 03:28:35 +01:00
|
|
|
return bucket.size;
|
2020-07-23 03:58:17 +02:00
|
|
|
}
|
2017-07-31 14:59:18 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
get_msg_ids(): number[] {
|
2020-02-12 01:35:16 +01:00
|
|
|
const ids = [];
|
2018-05-02 19:50:25 +02:00
|
|
|
|
2020-07-23 03:58:17 +02:00
|
|
|
for (const id_set of this.bucketer.values()) {
|
2020-02-26 09:08:35 +01:00
|
|
|
for (const id of id_set) {
|
|
|
|
ids.push(id);
|
|
|
|
}
|
2020-02-03 09:26:53 +01:00
|
|
|
}
|
2018-05-02 19:50:25 +02:00
|
|
|
|
|
|
|
return util.sorted_ids(ids);
|
2020-07-23 03:58:17 +02:00
|
|
|
}
|
2018-05-02 19:50:25 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
get_msg_ids_for_user_ids_string(user_ids_string: string): number[] {
|
2018-05-02 13:02:32 +02:00
|
|
|
if (!user_ids_string) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2023-12-21 23:23:35 +01:00
|
|
|
const bucket = this.bucketer.get(user_ids_string);
|
2018-05-02 13:02:32 +02:00
|
|
|
|
|
|
|
if (!bucket) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2023-03-02 01:58:25 +01:00
|
|
|
const ids = [...bucket];
|
2018-05-02 13:02:32 +02:00
|
|
|
return util.sorted_ids(ids);
|
2020-07-23 03:58:17 +02:00
|
|
|
}
|
|
|
|
}
|
2023-06-16 12:24:46 +02:00
|
|
|
const unread_direct_message_counter = new UnreadDirectMessageCounter();
|
2017-07-31 14:59:18 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
type UnreadTopicCounts = {
|
|
|
|
stream_unread_messages: number;
|
|
|
|
followed_topic_unread_messages: number;
|
|
|
|
stream_count: Map<number, number>;
|
|
|
|
};
|
|
|
|
|
2020-07-23 04:01:19 +02:00
|
|
|
class UnreadTopicCounter {
|
2023-12-13 02:00:54 +01:00
|
|
|
bucketer = new Map<number, FoldDict<Set<number>>>();
|
2023-12-21 23:23:35 +01:00
|
|
|
// Maps the message id to the (stream, topic) we stored it under in the bucketer.
|
2023-12-13 02:00:54 +01:00
|
|
|
reverse_lookup = new Map<number, {stream_id: number; topic: string}>();
|
2016-11-30 03:20:29 +01:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
clear(): void {
|
2020-07-23 04:01:19 +02:00
|
|
|
this.bucketer.clear();
|
2023-12-21 23:23:35 +01:00
|
|
|
this.reverse_lookup.clear();
|
2020-07-23 04:01:19 +02:00
|
|
|
}
|
2016-11-30 03:20:29 +01:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
set_streams(objs: {stream_id: number; topic: string; unread_message_ids: number[]}[]): void {
|
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 obj of objs) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = obj.stream_id;
|
|
|
|
const topic = obj.topic;
|
|
|
|
const unread_message_ids = obj.unread_message_ids;
|
2017-08-01 14:50:40 +02:00
|
|
|
|
2021-08-04 19:27:20 +02:00
|
|
|
for (const message_id of unread_message_ids) {
|
|
|
|
this.add({message_id, stream_id, topic});
|
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-23 04:01:19 +02:00
|
|
|
}
|
2017-08-01 14:50:40 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
add({
|
|
|
|
message_id,
|
|
|
|
stream_id,
|
|
|
|
topic,
|
|
|
|
}: {
|
|
|
|
message_id: number;
|
|
|
|
stream_id: number;
|
|
|
|
topic: string;
|
|
|
|
}): void {
|
2023-12-21 23:23:35 +01:00
|
|
|
let per_stream_bucketer = this.bucketer.get(stream_id);
|
|
|
|
if (per_stream_bucketer === undefined) {
|
|
|
|
per_stream_bucketer = new FoldDict();
|
|
|
|
this.bucketer.set(stream_id, per_stream_bucketer);
|
|
|
|
}
|
|
|
|
let bucket = per_stream_bucketer.get(topic);
|
|
|
|
if (bucket === undefined) {
|
|
|
|
bucket = new Set();
|
|
|
|
per_stream_bucketer.set(topic, bucket);
|
|
|
|
}
|
|
|
|
this.reverse_lookup.set(message_id, {stream_id, topic});
|
|
|
|
bucket.add(message_id);
|
2020-07-23 04:01:19 +02:00
|
|
|
}
|
2016-11-30 03:20:29 +01:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
delete(message_id: number): void {
|
2023-12-21 23:23:35 +01:00
|
|
|
const stream_topic = this.reverse_lookup.get(message_id);
|
|
|
|
if (stream_topic === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const {stream_id, topic} = stream_topic;
|
|
|
|
this.bucketer.get(stream_id)?.get(topic)?.delete(message_id);
|
|
|
|
this.reverse_lookup.delete(message_id);
|
2020-07-23 04:01:19 +02:00
|
|
|
}
|
2016-11-30 03:20:29 +01:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
get_counts(
|
|
|
|
include_per_topic_count = false,
|
|
|
|
include_per_topic_latest_msg_id = false,
|
|
|
|
): UnreadTopicCounts {
|
|
|
|
let stream_unread_messages = 0;
|
|
|
|
let followed_topic_unread_messages = 0;
|
|
|
|
const stream_counts_by_id = new Map(); // hash by stream_id -> count
|
2023-08-09 07:23:30 +02:00
|
|
|
for (const [stream_id, per_stream_bucketer] of this.bucketer) {
|
2016-12-15 01:59:08 +01:00
|
|
|
// We track unread counts for streams that may be currently
|
|
|
|
// unsubscribed. Since users may re-subscribe, we don't
|
|
|
|
// completely throw away the data. But we do ignore it here,
|
|
|
|
// so that callers have a view of the **current** world.
|
2021-04-15 17:02:54 +02:00
|
|
|
const sub = sub_store.get(stream_id);
|
2021-11-23 00:41:09 +01:00
|
|
|
if (!sub || !stream_data.is_subscribed(stream_id)) {
|
2020-02-03 09:26:53 +01:00
|
|
|
continue;
|
2016-11-30 03:20:29 +01:00
|
|
|
}
|
|
|
|
|
2023-08-09 07:23:30 +02:00
|
|
|
if (include_per_topic_count) {
|
|
|
|
const topic_unread = new Map();
|
|
|
|
let stream_count = 0;
|
|
|
|
for (const [topic, msgs] of per_stream_bucketer) {
|
|
|
|
const topic_count = msgs.size;
|
2023-10-05 12:49:42 +02:00
|
|
|
if (include_per_topic_latest_msg_id) {
|
|
|
|
const latest_msg_id = Math.max(...msgs);
|
|
|
|
topic_unread.set(topic, {
|
|
|
|
topic_count,
|
|
|
|
latest_msg_id,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
topic_unread.set(topic, topic_count);
|
|
|
|
}
|
2023-08-09 07:23:30 +02:00
|
|
|
stream_count += topic_count;
|
|
|
|
}
|
|
|
|
|
2023-09-14 09:21:46 +02:00
|
|
|
// Note: The format of res.stream_count is completely
|
|
|
|
// different from the else clause; with objects
|
|
|
|
// containing data for individual topics rather than
|
|
|
|
// just muted/unmuted totals, and all muted
|
|
|
|
// streams/topics included so that the inbox view can
|
|
|
|
// easily show/hide topics when its filters are
|
|
|
|
// adjusted. This should be refactored before the
|
|
|
|
// TypeScript migration for this file.
|
2023-12-13 02:00:54 +01:00
|
|
|
stream_counts_by_id.set(stream_id, topic_unread);
|
|
|
|
stream_unread_messages += stream_count;
|
2023-08-09 07:23:30 +02:00
|
|
|
} else {
|
2023-12-13 02:00:54 +01:00
|
|
|
// get_stream_count_info calculates both the number of
|
2023-09-14 09:21:46 +02:00
|
|
|
// unmuted unread as well as the number of muted
|
|
|
|
// unreads.
|
2023-12-13 02:00:54 +01:00
|
|
|
stream_counts_by_id.set(stream_id, this.get_stream_count_info(stream_id));
|
|
|
|
stream_unread_messages += stream_counts_by_id.get(stream_id).unmuted_count;
|
|
|
|
followed_topic_unread_messages += stream_counts_by_id.get(stream_id).followed_count;
|
2023-08-09 07:23:30 +02:00
|
|
|
}
|
2020-02-03 09:26:53 +01:00
|
|
|
}
|
2016-12-15 01:53:33 +01:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
return {
|
|
|
|
stream_unread_messages,
|
|
|
|
followed_topic_unread_messages,
|
|
|
|
stream_count: stream_counts_by_id,
|
|
|
|
};
|
2020-07-23 04:01:19 +02:00
|
|
|
}
|
2016-11-30 03:20:29 +01:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
get_missing_topics(opts: {
|
|
|
|
stream_id: number;
|
2024-01-02 07:03:01 +01:00
|
|
|
topic_dict: FoldDict<TopicHistoryEntry>;
|
2023-12-13 02:00:54 +01:00
|
|
|
}): {pretty_name: string; message_id: number}[] {
|
2021-06-29 18:43:52 +02:00
|
|
|
/* Clients have essentially complete unread data, but
|
|
|
|
* stream_topic_history.is_complete_for_stream_id() can be
|
|
|
|
* false. In that situation, this function helps ensure that
|
|
|
|
* we include all topics with unread messages in data that.
|
|
|
|
*
|
|
|
|
* It will return all topics in the provided stream with a
|
|
|
|
* nonzero unread count that are not already present in the
|
|
|
|
* topic_dict parameter.
|
|
|
|
*/
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = opts.stream_id;
|
|
|
|
const topic_dict = opts.topic_dict;
|
2018-05-13 11:38:40 +02:00
|
|
|
|
2023-12-21 23:23:35 +01:00
|
|
|
const per_stream_bucketer = this.bucketer.get(stream_id);
|
2018-05-13 11:38:40 +02:00
|
|
|
if (!per_stream_bucketer) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2023-12-23 21:45:51 +01:00
|
|
|
let topics_and_message_ids = [...per_stream_bucketer];
|
2018-05-13 11:38:40 +02:00
|
|
|
|
2021-06-29 18:43:52 +02:00
|
|
|
/* Include topics that have at least one unread. It would likely
|
|
|
|
* be better design for buckets to be deleted when emptied. */
|
2023-12-23 21:45:51 +01:00
|
|
|
topics_and_message_ids = topics_and_message_ids.filter(
|
|
|
|
([_, message_ids]) => message_ids.size > 0,
|
|
|
|
);
|
2021-06-29 18:43:52 +02:00
|
|
|
/* And aren't already present in topic_dict. */
|
2023-12-23 21:45:51 +01:00
|
|
|
topics_and_message_ids = topics_and_message_ids.filter(
|
|
|
|
([topic_name, _]) => !topic_dict.has(topic_name),
|
|
|
|
);
|
|
|
|
|
|
|
|
const result = topics_and_message_ids.map(([topic_name, message_ids]) => ({
|
|
|
|
pretty_name: topic_name,
|
|
|
|
message_id: Math.max(...message_ids),
|
|
|
|
}));
|
2018-05-13 11:38:40 +02:00
|
|
|
|
|
|
|
return result;
|
2020-07-23 04:01:19 +02:00
|
|
|
}
|
2018-05-13 11:38:40 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
get_stream_count_info(stream_id: number): StreamCountInfo {
|
2023-12-21 23:23:35 +01:00
|
|
|
const per_stream_bucketer = this.bucketer.get(stream_id);
|
2017-08-03 05:01:47 +02:00
|
|
|
|
|
|
|
if (!per_stream_bucketer) {
|
2023-12-21 23:36:42 +01:00
|
|
|
return {
|
|
|
|
unmuted_count: 0,
|
|
|
|
muted_count: 0,
|
|
|
|
followed_count: 0,
|
|
|
|
stream_is_muted: false,
|
|
|
|
};
|
2017-01-15 16:44:33 +01:00
|
|
|
}
|
|
|
|
|
2021-04-15 17:02:54 +02:00
|
|
|
const sub = sub_store.get(stream_id);
|
2023-04-22 18:24:59 +02:00
|
|
|
let unmuted_count = 0;
|
|
|
|
let muted_count = 0;
|
2023-11-16 09:45:19 +01:00
|
|
|
let followed_count = 0;
|
2020-02-03 09:26:53 +01:00
|
|
|
for (const [topic, msgs] of per_stream_bucketer) {
|
2023-04-22 18:24:59 +02:00
|
|
|
const topic_count = msgs.size;
|
|
|
|
|
2023-11-16 09:45:19 +01:00
|
|
|
if (user_topics.is_topic_followed(stream_id, topic)) {
|
|
|
|
followed_count += topic_count;
|
|
|
|
}
|
|
|
|
|
2023-09-25 15:08:07 +02:00
|
|
|
if (user_topics.is_topic_unmuted_or_followed(stream_id, topic)) {
|
2023-04-22 18:24:59 +02:00
|
|
|
unmuted_count += topic_count;
|
|
|
|
} else if (user_topics.is_topic_muted(stream_id, topic)) {
|
|
|
|
muted_count += topic_count;
|
2023-12-13 02:00:54 +01:00
|
|
|
} else if (sub?.is_muted) {
|
2023-04-22 18:24:59 +02:00
|
|
|
muted_count += topic_count;
|
|
|
|
} else {
|
|
|
|
unmuted_count += topic_count;
|
2017-01-15 16:44:33 +01:00
|
|
|
}
|
2020-02-03 09:26:53 +01:00
|
|
|
}
|
2023-04-22 18:24:59 +02:00
|
|
|
const stream_count = {
|
|
|
|
unmuted_count,
|
|
|
|
muted_count,
|
2023-11-16 09:45:19 +01:00
|
|
|
followed_count,
|
2023-12-13 02:00:54 +01:00
|
|
|
stream_is_muted: sub?.is_muted ?? false,
|
2023-04-22 18:24:59 +02:00
|
|
|
};
|
2017-01-15 16:44:33 +01:00
|
|
|
return stream_count;
|
2020-07-23 04:01:19 +02:00
|
|
|
}
|
2017-01-15 16:44:33 +01:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
get(stream_id: number, topic: string): number {
|
2023-12-21 23:23:35 +01:00
|
|
|
const per_stream_bucketer = this.bucketer.get(stream_id);
|
2017-08-03 05:01:47 +02:00
|
|
|
if (!per_stream_bucketer) {
|
|
|
|
return 0;
|
2016-11-30 03:20:29 +01:00
|
|
|
}
|
2017-08-03 05:01:47 +02:00
|
|
|
|
2023-12-21 23:23:35 +01:00
|
|
|
const topic_bucket = per_stream_bucketer.get(topic);
|
2017-08-03 05:01:47 +02:00
|
|
|
if (!topic_bucket) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-01 03:28:35 +01:00
|
|
|
return topic_bucket.size;
|
2020-07-23 04:01:19 +02:00
|
|
|
}
|
2016-11-30 03:20:29 +01:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
get_msg_ids_for_stream(stream_id: number): number[] {
|
2023-12-21 23:23:35 +01:00
|
|
|
const per_stream_bucketer = this.bucketer.get(stream_id);
|
2018-05-01 21:22:49 +02:00
|
|
|
|
|
|
|
if (!per_stream_bucketer) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:35:16 +01:00
|
|
|
const ids = [];
|
2021-04-15 17:02:54 +02:00
|
|
|
const sub = sub_store.get(stream_id);
|
2020-02-26 09:08:35 +01:00
|
|
|
for (const [topic, id_set] of per_stream_bucketer) {
|
2022-08-14 15:33:13 +02:00
|
|
|
if (sub && !user_topics.is_topic_muted(stream_id, topic)) {
|
2020-02-26 09:08:35 +01:00
|
|
|
for (const id of id_set) {
|
|
|
|
ids.push(id);
|
|
|
|
}
|
2018-05-01 21:22:49 +02:00
|
|
|
}
|
2020-02-03 09:26:53 +01:00
|
|
|
}
|
2018-05-01 21:22:49 +02:00
|
|
|
|
|
|
|
return util.sorted_ids(ids);
|
2020-07-23 04:01:19 +02:00
|
|
|
}
|
2018-05-01 21:22:49 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
get_msg_ids_for_topic(stream_id: number, topic: string): number[] {
|
2023-12-21 23:23:35 +01:00
|
|
|
const per_stream_bucketer = this.bucketer.get(stream_id);
|
2018-04-25 22:48:51 +02:00
|
|
|
if (!per_stream_bucketer) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2023-12-21 23:23:35 +01:00
|
|
|
const topic_bucket = per_stream_bucketer.get(topic);
|
2018-04-25 22:48:51 +02:00
|
|
|
if (!topic_bucket) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2023-03-02 01:58:25 +01:00
|
|
|
const ids = [...topic_bucket];
|
2018-04-25 22:48:51 +02:00
|
|
|
return util.sorted_ids(ids);
|
2020-07-23 04:01:19 +02:00
|
|
|
}
|
2018-04-25 22:48:51 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
get_streams_with_unread_mentions(): Set<number> {
|
2023-05-19 22:31:46 +02:00
|
|
|
// Collect the set of streams containing at least one unread
|
|
|
|
// mention, without considering muting.
|
|
|
|
|
2022-08-05 22:45:20 +02:00
|
|
|
// We can do this efficiently, since unread_mentions_counter
|
|
|
|
// contains all unread message IDs, and we use stream_ids as
|
|
|
|
// bucket keys in our outer bucketer.
|
2023-12-13 02:00:54 +01:00
|
|
|
const streams_with_mentions = new Set<number>();
|
2022-08-05 22:45:20 +02:00
|
|
|
|
|
|
|
for (const message_id of unread_mentions_counter) {
|
2023-12-21 23:23:35 +01:00
|
|
|
const stream_id = this.reverse_lookup.get(message_id)?.stream_id;
|
2023-05-03 21:44:52 +02:00
|
|
|
if (stream_id === undefined) {
|
2023-06-16 13:23:39 +02:00
|
|
|
// This is a direct message containing a mention.
|
2023-05-03 21:44:52 +02:00
|
|
|
continue;
|
|
|
|
}
|
2022-08-05 22:45:20 +02:00
|
|
|
streams_with_mentions.add(stream_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return streams_with_mentions;
|
|
|
|
}
|
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
get_streams_with_unmuted_mentions(): Set<number> {
|
2023-04-22 18:58:54 +02:00
|
|
|
// Collect the set of streams containing at least one mention
|
2023-05-19 22:31:46 +02:00
|
|
|
// that is not in a muted topic or non-unmuted topic in a
|
|
|
|
// muted stream.
|
2023-12-13 02:00:54 +01:00
|
|
|
const streams_with_unmuted_mentions = new Set<number>();
|
2023-04-22 18:58:54 +02:00
|
|
|
for (const message_id of unread_mentions_counter) {
|
2023-12-21 23:23:35 +01:00
|
|
|
const stream_topic = this.reverse_lookup.get(message_id);
|
|
|
|
if (stream_topic === undefined) {
|
2023-06-16 13:23:39 +02:00
|
|
|
// This is a direct message containing a mention.
|
2023-05-03 21:44:52 +02:00
|
|
|
continue;
|
|
|
|
}
|
2023-12-21 23:23:35 +01:00
|
|
|
const {stream_id, topic} = stream_topic;
|
2023-05-03 21:44:52 +02:00
|
|
|
|
2023-05-19 22:31:46 +02:00
|
|
|
const stream_is_muted = sub_store.get(stream_id)?.is_muted;
|
|
|
|
if (stream_is_muted) {
|
2023-09-25 15:08:07 +02:00
|
|
|
if (user_topics.is_topic_unmuted_or_followed(stream_id, topic)) {
|
2023-05-19 22:31:46 +02:00
|
|
|
streams_with_unmuted_mentions.add(stream_id);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!user_topics.is_topic_muted(stream_id, topic)) {
|
|
|
|
streams_with_unmuted_mentions.add(stream_id);
|
|
|
|
}
|
2023-04-22 18:58:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return streams_with_unmuted_mentions;
|
|
|
|
}
|
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
get_followed_topic_unread_mentions(): number {
|
2023-11-16 09:45:19 +01:00
|
|
|
let followed_topic_unread_mentions = 0;
|
|
|
|
for (const message_id of unread_mentions_counter) {
|
2023-12-21 23:23:35 +01:00
|
|
|
const stream_topic = this.reverse_lookup.get(message_id);
|
|
|
|
if (stream_topic === undefined) {
|
2023-11-16 09:45:19 +01:00
|
|
|
// This is a direct message containing a mention.
|
|
|
|
continue;
|
|
|
|
}
|
2023-12-21 23:23:35 +01:00
|
|
|
const {stream_id, topic} = stream_topic;
|
2023-11-16 09:45:19 +01:00
|
|
|
|
|
|
|
if (user_topics.is_topic_followed(stream_id, topic)) {
|
|
|
|
followed_topic_unread_mentions += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return followed_topic_unread_mentions;
|
|
|
|
}
|
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
topic_has_any_unread(stream_id: number, topic: string): boolean {
|
2023-12-21 23:23:35 +01:00
|
|
|
const per_stream_bucketer = this.bucketer.get(stream_id);
|
2017-04-21 18:00:19 +02:00
|
|
|
|
2017-08-03 05:01:47 +02:00
|
|
|
if (!per_stream_bucketer) {
|
2017-04-21 18:00:19 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-12-21 23:23:35 +01:00
|
|
|
const id_set = per_stream_bucketer.get(topic);
|
2017-08-03 05:01:47 +02:00
|
|
|
if (!id_set) {
|
2017-04-21 18:00:19 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-01 03:28:35 +01:00
|
|
|
return id_set.size !== 0;
|
2020-07-23 04:01:19 +02:00
|
|
|
}
|
2022-08-21 23:05:56 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
get_topics_with_unread_mentions(stream_id: number): Set<string> {
|
2022-08-21 23:05:56 +02:00
|
|
|
// Returns the set of lower cased topics with unread mentions
|
|
|
|
// in the given stream.
|
2023-12-13 02:00:54 +01:00
|
|
|
const result = new Set<string>();
|
2023-12-21 23:23:35 +01:00
|
|
|
const per_stream_bucketer = this.bucketer.get(stream_id);
|
2022-08-21 23:05:56 +02:00
|
|
|
|
|
|
|
if (!per_stream_bucketer) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const message_id of unread_mentions_counter) {
|
|
|
|
// Because bucket keys in per_stream_bucketer are topics,
|
|
|
|
// we can just directly use reverse_lookup to find the
|
|
|
|
// topic in this stream containing a given unread message
|
|
|
|
// ID. If it's not in this stream, we'll get undefined.
|
2023-12-21 23:23:35 +01:00
|
|
|
const stream_topic = this.reverse_lookup.get(message_id);
|
|
|
|
if (stream_topic !== undefined && stream_topic.stream_id === stream_id) {
|
2022-08-21 23:05:56 +02:00
|
|
|
// Important: We lower-case topics here before adding them
|
|
|
|
// to this set, to support case-insensitive checks.
|
2023-12-21 23:23:35 +01:00
|
|
|
result.add(stream_topic.topic.toLowerCase());
|
2022-08-21 23:05:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2020-07-23 04:01:19 +02:00
|
|
|
}
|
2021-03-09 14:51:01 +01:00
|
|
|
const unread_topic_counter = new UnreadTopicCounter();
|
2017-08-01 14:04:48 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
function add_message_to_unread_mention_topics(message_id: number): void {
|
2022-11-01 17:50:52 +01:00
|
|
|
const message = message_store.get(message_id);
|
2023-12-13 02:00:54 +01:00
|
|
|
if (message?.type !== "stream") {
|
2022-11-01 17:50:52 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-09-06 23:21:13 +02:00
|
|
|
const topic_key = recent_view_util.get_topic_key(message.stream_id, message.topic);
|
2022-11-01 17:50:52 +01:00
|
|
|
if (unread_mention_topics.has(topic_key)) {
|
|
|
|
unread_mention_topics.get(topic_key).add(message_id);
|
|
|
|
}
|
|
|
|
unread_mention_topics.set(topic_key, new Set([message_id]));
|
|
|
|
}
|
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
function remove_message_from_unread_mention_topics(message_id: number): void {
|
2023-12-21 23:23:35 +01:00
|
|
|
const stream_topic = unread_topic_counter.reverse_lookup.get(message_id);
|
|
|
|
if (!stream_topic) {
|
2023-06-16 13:23:39 +02:00
|
|
|
// Direct messages and messages that were already not unread
|
2022-11-01 18:14:09 +01:00
|
|
|
// exit here.
|
2022-11-01 17:50:52 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-12-21 23:23:35 +01:00
|
|
|
const {stream_id, topic} = stream_topic;
|
2023-09-06 23:21:13 +02:00
|
|
|
const topic_key = recent_view_util.get_topic_key(stream_id, topic);
|
2022-11-01 17:50:52 +01:00
|
|
|
if (unread_mention_topics.has(topic_key)) {
|
|
|
|
unread_mention_topics.get(topic_key).delete(message_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function clear_and_populate_unread_mention_topics(): void {
|
2022-10-14 17:37:47 +02:00
|
|
|
// The unread_mention_topics is an important data structure for
|
|
|
|
// efficiently querying whether a given stream/topic pair contains
|
|
|
|
// unread mentions.
|
|
|
|
//
|
|
|
|
// It is effectively a cache, since it can be reconstructed from
|
|
|
|
// unread_mentions_counter (IDs for all unread mentions) and
|
|
|
|
// unread_topic_counter (Streams/topics for all unread messages).
|
|
|
|
//
|
|
|
|
// Since this function runs in O(unread mentions) time, we can use
|
|
|
|
// it in topic editing code paths where it might be onerous to
|
|
|
|
// write custom live-update code; but we should avoid calling it
|
|
|
|
// in loops.
|
|
|
|
unread_mention_topics.clear();
|
|
|
|
|
|
|
|
for (const message_id of unread_mentions_counter) {
|
2023-12-21 23:23:35 +01:00
|
|
|
const stream_topic = unread_topic_counter.reverse_lookup.get(message_id);
|
|
|
|
if (!stream_topic) {
|
|
|
|
// Direct messages and messages that were already not unread
|
|
|
|
// exit here.
|
2022-10-14 17:37:47 +02:00
|
|
|
continue;
|
|
|
|
}
|
2023-12-21 23:23:35 +01:00
|
|
|
const {stream_id, topic} = stream_topic;
|
|
|
|
|
2023-09-06 23:21:13 +02:00
|
|
|
const topic_key = recent_view_util.get_topic_key(stream_id, topic);
|
2022-10-14 17:37:47 +02:00
|
|
|
if (unread_mention_topics.has(topic_key)) {
|
|
|
|
unread_mention_topics.get(topic_key).add(message_id);
|
|
|
|
}
|
|
|
|
unread_mention_topics.set(topic_key, new Set([message_id]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function message_unread(message: Message): boolean {
|
2013-05-17 21:32:26 +02:00
|
|
|
if (message === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-16 21:42:41 +01:00
|
|
|
return message.unread;
|
2021-02-28 21:30:38 +01:00
|
|
|
}
|
2013-05-17 21:32:26 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function get_read_message_ids(message_ids: number[]): number[] {
|
2021-08-04 19:47:51 +02:00
|
|
|
return message_ids.filter((message_id) => !unread_messages.has(message_id));
|
|
|
|
}
|
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function get_unread_message_ids(message_ids: number[]): number[] {
|
2020-07-02 01:39:34 +02:00
|
|
|
return message_ids.filter((message_id) => unread_messages.has(message_id));
|
2021-02-28 21:30:38 +01:00
|
|
|
}
|
2017-12-16 16:53:27 +01:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function get_unread_messages(messages: Message[]): Message[] {
|
2020-07-02 01:39:34 +02:00
|
|
|
return messages.filter((message) => unread_messages.has(message.id));
|
2021-02-28 21:30:38 +01:00
|
|
|
}
|
2017-08-03 22:01:21 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function get_unread_message_count(): number {
|
2022-10-27 05:27:56 +02:00
|
|
|
return unread_messages.size;
|
|
|
|
}
|
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function update_unread_topics(
|
|
|
|
msg: Message & {type: "stream"},
|
|
|
|
event: UpdateMessageEvent,
|
|
|
|
): void {
|
2019-11-02 00:06:25 +01:00
|
|
|
const new_topic = util.get_edit_event_topic(event);
|
2020-04-07 22:29:22 +02:00
|
|
|
const {new_stream_id} = event;
|
2018-12-22 17:45:18 +01:00
|
|
|
|
2020-04-07 22:29:22 +02:00
|
|
|
if (new_topic === undefined && new_stream_id === undefined) {
|
2017-08-10 20:27:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!unread_messages.has(msg.id)) {
|
|
|
|
return;
|
2013-05-17 21:32:26 +02:00
|
|
|
}
|
2017-08-10 20:27:23 +02:00
|
|
|
|
2021-02-28 21:30:38 +01:00
|
|
|
unread_topic_counter.delete(msg.id);
|
2017-08-10 20:27:23 +02:00
|
|
|
|
2021-08-04 19:27:20 +02:00
|
|
|
unread_topic_counter.add({
|
|
|
|
message_id: msg.id,
|
2023-12-13 02:00:54 +01:00
|
|
|
stream_id: new_stream_id ?? msg.stream_id,
|
|
|
|
topic: new_topic ?? msg.topic,
|
2021-08-04 19:27:20 +02:00
|
|
|
});
|
2021-02-28 21:30:38 +01:00
|
|
|
}
|
2013-05-17 21:32:26 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function process_loaded_messages(
|
|
|
|
messages: Message[],
|
|
|
|
expect_no_new_unreads = false,
|
|
|
|
): boolean {
|
2022-10-25 00:34:47 +02:00
|
|
|
// Process a set of messages that we have full copies of from the
|
|
|
|
// server for whether any are unread but not tracked as such by
|
|
|
|
// our data structures. This can occur due to old_unreads_missing,
|
|
|
|
// changes in muting configuration, innocent races, or potentially bugs.
|
|
|
|
//
|
|
|
|
// Returns whether there were any new unread messages; in that
|
|
|
|
// case, the caller will need to trigger a rerender of UI
|
|
|
|
// displaying unread counts.
|
|
|
|
|
|
|
|
let any_untracked_unread_messages = false;
|
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 message of messages) {
|
2021-08-04 18:53:49 +02:00
|
|
|
if (message.unread) {
|
2022-10-25 00:34:47 +02:00
|
|
|
if (unread_messages.has(message.id)) {
|
|
|
|
// If we're already tracking this message as unread, there's nothing to do.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-10-27 22:13:43 +02:00
|
|
|
if (expect_no_new_unreads && !old_unreads_missing) {
|
2022-10-25 00:34:47 +02:00
|
|
|
// This may happen due to races, where someone narrows
|
|
|
|
// to a view and the message_fetch request returns
|
|
|
|
// before server_events system delivers the message to
|
|
|
|
// the client.
|
2022-11-02 06:46:24 +01:00
|
|
|
blueslip.log(`New unread ${message.id} discovered in process_loaded_messages.`);
|
2022-10-25 00:34:47 +02:00
|
|
|
}
|
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
if (message.type === "private") {
|
|
|
|
process_unread_message({
|
|
|
|
id: message.id,
|
|
|
|
mentioned: message.mentioned,
|
|
|
|
mentioned_me_directly: message.mentioned_me_directly,
|
|
|
|
type: message.type,
|
|
|
|
user_ids_string: people.pm_reply_user_string(message) ?? "",
|
|
|
|
unread: true,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
process_unread_message({
|
|
|
|
id: message.id,
|
|
|
|
mentioned: message.mentioned,
|
|
|
|
mentioned_me_directly: message.mentioned_me_directly,
|
|
|
|
type: message.type,
|
|
|
|
stream_id: message.stream_id,
|
|
|
|
topic: message.topic,
|
|
|
|
unread: true,
|
|
|
|
});
|
|
|
|
}
|
2022-10-25 00:34:47 +02:00
|
|
|
any_untracked_unread_messages = true;
|
2013-05-17 21:32:26 +02:00
|
|
|
}
|
2021-08-04 18:53:49 +02:00
|
|
|
}
|
2022-10-25 00:34:47 +02:00
|
|
|
|
|
|
|
return any_untracked_unread_messages;
|
2021-08-04 18:53:49 +02:00
|
|
|
}
|
2013-05-17 21:32:26 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
type UnreadMessageData = {
|
|
|
|
id: number;
|
|
|
|
mentioned: boolean;
|
|
|
|
mentioned_me_directly: boolean;
|
|
|
|
unread: boolean;
|
|
|
|
} & (
|
|
|
|
| {
|
|
|
|
type: "private";
|
|
|
|
user_ids_string: string;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: "stream";
|
|
|
|
stream_id: number;
|
|
|
|
topic: string;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
export function process_unread_message(message: UnreadMessageData): void {
|
2021-08-04 18:53:49 +02:00
|
|
|
// The `message` here just needs to require certain fields. For example,
|
|
|
|
// the "message" may actually be constructed from a Zulip event that doesn't
|
|
|
|
// include fields like "content". The caller must verify that the message
|
|
|
|
// is actually unread--we don't defend against that.
|
|
|
|
unread_messages.add(message.id);
|
2013-05-17 21:32:26 +02:00
|
|
|
|
2021-08-04 18:53:49 +02:00
|
|
|
if (message.type === "private") {
|
2023-06-16 12:24:46 +02:00
|
|
|
unread_direct_message_counter.add({
|
2021-08-04 19:16:50 +02:00
|
|
|
message_id: message.id,
|
|
|
|
user_ids_string: message.user_ids_string,
|
|
|
|
});
|
2021-08-04 18:53:49 +02:00
|
|
|
}
|
2013-05-30 22:15:59 +02:00
|
|
|
|
2021-08-04 18:53:49 +02:00
|
|
|
if (message.type === "stream") {
|
2021-08-04 19:27:20 +02:00
|
|
|
unread_topic_counter.add({
|
|
|
|
message_id: message.id,
|
|
|
|
stream_id: message.stream_id,
|
|
|
|
topic: message.topic,
|
|
|
|
});
|
2020-04-11 02:51:45 +02:00
|
|
|
}
|
2021-08-04 18:53:49 +02:00
|
|
|
|
|
|
|
update_message_for_mention(message);
|
2021-02-28 21:30:38 +01:00
|
|
|
}
|
2020-04-11 02:51:45 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function update_message_for_mention(
|
|
|
|
message: UnreadMessageData,
|
|
|
|
content_edited = false,
|
|
|
|
): boolean {
|
2023-03-11 17:37:17 +01:00
|
|
|
// Returns true if this is a stream message whose content was
|
|
|
|
// changed, and thus the caller might need to trigger a rerender
|
|
|
|
// of UI elements displaying whether the message's topic contains
|
|
|
|
// an unread mention of the user.
|
2020-04-11 02:51:45 +02:00
|
|
|
if (!message.unread) {
|
2021-02-28 21:30:38 +01:00
|
|
|
unread_mentions_counter.delete(message.id);
|
2023-09-12 20:30:02 +02:00
|
|
|
direct_message_with_mention_count.delete(message.id);
|
2022-10-14 17:37:47 +02:00
|
|
|
remove_message_from_unread_mention_topics(message.id);
|
2023-03-11 17:37:17 +01:00
|
|
|
return false;
|
2020-04-11 02:51:45 +02:00
|
|
|
}
|
|
|
|
|
2023-09-20 13:25:50 +02:00
|
|
|
// A message is said to have an unmuted mention if message contains a mention and
|
|
|
|
// if the message is a direct message or
|
|
|
|
// if the message is in a non muted topic in an unmuted stream or
|
|
|
|
// if the message is in a followed or an unmuted topic in a muted stream.
|
2020-04-11 02:51:45 +02:00
|
|
|
const is_unmuted_mention =
|
|
|
|
message.mentioned &&
|
2023-09-20 13:25:50 +02:00
|
|
|
(message.type === "private" ||
|
|
|
|
(!stream_data.is_muted(message.stream_id) &&
|
|
|
|
!user_topics.is_topic_muted(message.stream_id, message.topic)) ||
|
|
|
|
(stream_data.is_muted(message.stream_id) &&
|
|
|
|
user_topics.is_topic_unmuted_or_followed(message.stream_id, message.topic)));
|
2020-04-11 02:51:45 +02:00
|
|
|
|
|
|
|
if (is_unmuted_mention || message.mentioned_me_directly) {
|
2021-02-28 21:30:38 +01:00
|
|
|
unread_mentions_counter.add(message.id);
|
2022-10-14 17:37:47 +02:00
|
|
|
add_message_to_unread_mention_topics(message.id);
|
2023-12-13 07:52:09 +01:00
|
|
|
if (message.type === "private") {
|
2023-09-12 20:30:02 +02:00
|
|
|
direct_message_with_mention_count.add(message.id);
|
|
|
|
}
|
2020-04-11 02:51:45 +02:00
|
|
|
} else {
|
2021-02-28 21:30:38 +01:00
|
|
|
unread_mentions_counter.delete(message.id);
|
2023-09-12 20:30:02 +02:00
|
|
|
direct_message_with_mention_count.delete(message.id);
|
2022-10-14 17:37:47 +02:00
|
|
|
remove_message_from_unread_mention_topics(message.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (content_edited && message.type === "stream") {
|
2023-03-11 17:37:17 +01:00
|
|
|
return true;
|
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
|
|
|
}
|
2023-03-11 17:37:17 +01:00
|
|
|
return false;
|
2021-02-28 21:30:38 +01:00
|
|
|
}
|
2013-05-17 21:32:26 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function mark_as_read(message_id: number): void {
|
2017-08-02 21:40:01 +02:00
|
|
|
// We don't need to check anything about the message, since all
|
|
|
|
// the following methods are cheap and work fine even if message_id
|
|
|
|
// was never set to unread.
|
2023-06-16 12:24:46 +02:00
|
|
|
unread_direct_message_counter.delete(message_id);
|
2022-11-01 18:14:09 +01:00
|
|
|
|
|
|
|
// Important: This function uses `unread_topic_counter` to look up
|
|
|
|
// the stream/topic for this previously unread message, so much
|
|
|
|
// happen before the message is removed from that data structure.
|
|
|
|
remove_message_from_unread_mention_topics(message_id);
|
2021-02-28 21:30:38 +01:00
|
|
|
unread_topic_counter.delete(message_id);
|
|
|
|
unread_mentions_counter.delete(message_id);
|
2023-09-12 20:30:02 +02:00
|
|
|
direct_message_with_mention_count.delete(message_id);
|
2020-02-01 03:17:09 +01:00
|
|
|
unread_messages.delete(message_id);
|
2017-12-16 21:42:41 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = message_store.get(message_id);
|
2017-12-16 21:42:41 +01:00
|
|
|
if (message) {
|
|
|
|
message.unread = false;
|
|
|
|
}
|
2021-02-28 21:30:38 +01:00
|
|
|
}
|
2013-05-17 21:32:26 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function declare_bankruptcy(): void {
|
2022-11-16 10:53:04 +01:00
|
|
|
// Only used in tests.
|
2023-06-16 12:24:46 +02:00
|
|
|
unread_direct_message_counter.clear();
|
2021-02-28 21:30:38 +01:00
|
|
|
unread_topic_counter.clear();
|
|
|
|
unread_mentions_counter.clear();
|
2023-09-12 20:30:02 +02:00
|
|
|
direct_message_with_mention_count.clear();
|
2017-08-03 22:01:21 +02:00
|
|
|
unread_messages.clear();
|
2022-10-14 17:37:47 +02:00
|
|
|
unread_mention_topics.clear();
|
2021-02-28 21:30:38 +01:00
|
|
|
}
|
2013-05-17 21:32:26 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function get_unread_pm(include_per_bucket_latest_msg_id = false): DirectMessageCountInfo {
|
2023-12-13 07:54:11 +01:00
|
|
|
return unread_direct_message_counter.get_counts(include_per_bucket_latest_msg_id);
|
2023-08-09 07:23:30 +02:00
|
|
|
}
|
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function get_unread_topics(include_per_topic_latest_msg_id = false): UnreadTopicCounts {
|
2023-08-09 07:23:30 +02:00
|
|
|
const include_per_topic_count = true;
|
2023-12-13 07:54:11 +01:00
|
|
|
return unread_topic_counter.get_counts(
|
2023-10-05 12:49:42 +02:00
|
|
|
include_per_topic_count,
|
|
|
|
include_per_topic_latest_msg_id,
|
|
|
|
);
|
2023-08-09 07:23:30 +02:00
|
|
|
}
|
|
|
|
|
2023-12-22 06:02:47 +01:00
|
|
|
export type FullUnreadCountsData = {
|
2023-12-13 02:00:54 +01:00
|
|
|
direct_message_count: number;
|
|
|
|
mentioned_message_count: number;
|
|
|
|
direct_message_with_mention_count: number;
|
|
|
|
stream_unread_messages: number;
|
|
|
|
followed_topic_unread_messages_count: number;
|
|
|
|
followed_topic_unread_messages_with_mention_count: number;
|
|
|
|
stream_count: Map<number, number>;
|
|
|
|
streams_with_mentions: number[];
|
|
|
|
streams_with_unmuted_mentions: number[];
|
|
|
|
pm_count: Map<string, number>;
|
|
|
|
home_unread_messages: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Return a data structure with various counts. This function should be
|
|
|
|
// pretty cheap, even if you don't care about all the counts, and you
|
|
|
|
// should strive to keep it free of side effects on globals or DOM.
|
|
|
|
export function get_counts(): FullUnreadCountsData {
|
2021-02-28 21:30:38 +01:00
|
|
|
const topic_res = unread_topic_counter.get_counts();
|
2023-06-16 12:24:46 +02:00
|
|
|
const pm_res = unread_direct_message_counter.get_counts();
|
2013-05-17 21:32:26 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
return {
|
|
|
|
direct_message_count: pm_res.total_count,
|
|
|
|
mentioned_message_count: unread_mentions_counter.size,
|
|
|
|
direct_message_with_mention_count: direct_message_with_mention_count.size,
|
|
|
|
stream_unread_messages: topic_res.stream_unread_messages,
|
|
|
|
followed_topic_unread_messages_count: topic_res.followed_topic_unread_messages,
|
|
|
|
followed_topic_unread_messages_with_mention_count:
|
|
|
|
unread_topic_counter.get_followed_topic_unread_mentions(),
|
|
|
|
stream_count: topic_res.stream_count,
|
|
|
|
streams_with_mentions: [...unread_topic_counter.get_streams_with_unread_mentions()],
|
|
|
|
streams_with_unmuted_mentions: [
|
|
|
|
...unread_topic_counter.get_streams_with_unmuted_mentions(),
|
|
|
|
],
|
|
|
|
pm_count: pm_res.pm_dict,
|
|
|
|
home_unread_messages: topic_res.stream_unread_messages + pm_res.total_count,
|
|
|
|
};
|
2021-02-28 21:30:38 +01:00
|
|
|
}
|
2013-05-17 21:32:26 +02:00
|
|
|
|
2019-07-20 14:45:56 +02:00
|
|
|
// Saves us from calling to get_counts() when we can avoid it.
|
2023-12-13 02:00:54 +01:00
|
|
|
export function calculate_notifiable_count(res: FullUnreadCountsData): number {
|
2019-11-02 00:06:25 +01:00
|
|
|
let new_message_count = 0;
|
2019-07-20 14:45:56 +02:00
|
|
|
|
2023-11-16 09:45:19 +01:00
|
|
|
const only_show_dm_mention =
|
2021-07-28 16:00:58 +02:00
|
|
|
user_settings.desktop_icon_count_display ===
|
2023-11-16 09:45:19 +01:00
|
|
|
settings_config.desktop_icon_count_display_values.dm_mention.code;
|
|
|
|
const only_show_dm_mention_followed_topic =
|
|
|
|
user_settings.desktop_icon_count_display ===
|
|
|
|
settings_config.desktop_icon_count_display_values.dm_mention_followed_topic.code;
|
2020-07-15 00:34:28 +02:00
|
|
|
const no_notifications =
|
2021-07-28 16:00:58 +02:00
|
|
|
user_settings.desktop_icon_count_display ===
|
2021-03-22 13:14:33 +01:00
|
|
|
settings_config.desktop_icon_count_display_values.none.code;
|
2023-11-16 09:45:19 +01:00
|
|
|
|
|
|
|
if (only_show_dm_mention || only_show_dm_mention_followed_topic) {
|
|
|
|
// DESKTOP_ICON_COUNT_DISPLAY_DM_MENTION
|
|
|
|
const dm_mention_count =
|
2023-09-12 20:30:02 +02:00
|
|
|
res.mentioned_message_count +
|
2023-09-12 20:58:40 +02:00
|
|
|
res.direct_message_count -
|
2023-09-12 20:30:02 +02:00
|
|
|
// Avoid double-counting direct messages containing mentions
|
|
|
|
res.direct_message_with_mention_count;
|
2023-11-16 09:45:19 +01:00
|
|
|
if (only_show_dm_mention_followed_topic) {
|
|
|
|
// DESKTOP_ICON_COUNT_DISPLAY_DM_MENTION_FOLLOWED_TOPIC
|
|
|
|
// Avoid double-counting followed topic messages containing mentions
|
|
|
|
new_message_count =
|
|
|
|
dm_mention_count +
|
|
|
|
res.followed_topic_unread_messages_count -
|
|
|
|
res.followed_topic_unread_messages_with_mention_count;
|
|
|
|
} else {
|
|
|
|
new_message_count = dm_mention_count;
|
|
|
|
}
|
2019-08-19 23:33:11 +02:00
|
|
|
} else if (no_notifications) {
|
|
|
|
// DESKTOP_ICON_COUNT_DISPLAY_NONE
|
|
|
|
new_message_count = 0;
|
2019-07-20 14:45:56 +02:00
|
|
|
} else {
|
|
|
|
// DESKTOP_ICON_COUNT_DISPLAY_MESSAGES
|
|
|
|
new_message_count = res.home_unread_messages;
|
|
|
|
}
|
|
|
|
return new_message_count;
|
2021-02-28 21:30:38 +01:00
|
|
|
}
|
2019-07-20 14:45:56 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function get_notifiable_count(): number {
|
2021-02-28 21:30:38 +01:00
|
|
|
const res = get_counts();
|
|
|
|
return calculate_notifiable_count(res);
|
|
|
|
}
|
2019-07-20 14:45:56 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function unread_count_info_for_stream(stream_id: number): StreamCountInfo {
|
2023-12-13 01:45:18 +01:00
|
|
|
return unread_topic_counter.get_stream_count_info(stream_id);
|
2021-02-28 21:30:38 +01:00
|
|
|
}
|
2017-01-15 16:44:33 +01:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function num_unread_for_topic(stream_id: number, topic_name: string): number {
|
2021-02-28 21:30:38 +01:00
|
|
|
return unread_topic_counter.get(stream_id, topic_name);
|
|
|
|
}
|
2013-05-17 21:32:26 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function stream_has_any_unread_mentions(stream_id: number): boolean {
|
2022-10-14 17:37:47 +02:00
|
|
|
// This function is somewhat inefficient and thus should not be
|
|
|
|
// called in loops, since runs in O(total unread mentions) time.
|
2022-08-05 22:45:20 +02:00
|
|
|
const streams_with_mentions = unread_topic_counter.get_streams_with_unread_mentions();
|
|
|
|
return streams_with_mentions.has(stream_id);
|
|
|
|
}
|
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function stream_has_any_unmuted_mentions(stream_id: number): boolean {
|
2023-04-22 18:58:54 +02:00
|
|
|
// This function is somewhat inefficient and thus should not be
|
|
|
|
// called in loops, since runs in O(total unread mentions) time.
|
|
|
|
const streams_with_mentions = unread_topic_counter.get_streams_with_unmuted_mentions();
|
|
|
|
return streams_with_mentions.has(stream_id);
|
|
|
|
}
|
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function topic_has_any_unread_mentions(stream_id: number, topic: string): boolean {
|
2022-10-14 17:37:47 +02:00
|
|
|
// Because this function is called in a loop for every displayed
|
2023-09-06 23:04:07 +02:00
|
|
|
// Recent Conversations row, it's important for it to run in O(1) time.
|
2022-10-14 17:37:47 +02:00
|
|
|
const topic_key = stream_id + ":" + topic.toLowerCase();
|
|
|
|
return unread_mention_topics.get(topic_key) && unread_mention_topics.get(topic_key).size > 0;
|
|
|
|
}
|
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function topic_has_any_unread(stream_id: number, topic: string): boolean {
|
2021-02-28 21:30:38 +01:00
|
|
|
return unread_topic_counter.topic_has_any_unread(stream_id, topic);
|
|
|
|
}
|
2017-04-21 18:00:19 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function get_topics_with_unread_mentions(stream_id: number): Set<string> {
|
2022-08-21 23:05:56 +02:00
|
|
|
return unread_topic_counter.get_topics_with_unread_mentions(stream_id);
|
|
|
|
}
|
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function num_unread_for_user_ids_string(user_ids_string: string): number {
|
2023-06-16 12:24:46 +02:00
|
|
|
return unread_direct_message_counter.num_unread(user_ids_string);
|
2021-02-28 21:30:38 +01:00
|
|
|
}
|
2013-08-22 19:06:04 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function get_msg_ids_for_stream(stream_id: number): number[] {
|
2021-02-28 21:30:38 +01:00
|
|
|
return unread_topic_counter.get_msg_ids_for_stream(stream_id);
|
|
|
|
}
|
2018-05-01 21:22:49 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function get_msg_ids_for_topic(stream_id: number, topic_name: string): number[] {
|
2021-02-28 21:30:38 +01:00
|
|
|
return unread_topic_counter.get_msg_ids_for_topic(stream_id, topic_name);
|
|
|
|
}
|
2018-04-25 22:48:51 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function get_msg_ids_for_user_ids_string(user_ids_string: string): number[] {
|
2023-06-16 12:24:46 +02:00
|
|
|
return unread_direct_message_counter.get_msg_ids_for_user_ids_string(user_ids_string);
|
2021-02-28 21:30:38 +01:00
|
|
|
}
|
2018-05-02 19:50:25 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function get_msg_ids_for_private(): number[] {
|
2023-06-16 12:24:46 +02:00
|
|
|
return unread_direct_message_counter.get_msg_ids();
|
2021-02-28 21:30:38 +01:00
|
|
|
}
|
2018-05-02 13:02:32 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function get_msg_ids_for_mentions(): number[] {
|
2023-03-02 01:58:25 +01:00
|
|
|
const ids = [...unread_mentions_counter];
|
2018-05-02 19:54:36 +02:00
|
|
|
|
|
|
|
return util.sorted_ids(ids);
|
2021-02-28 21:30:38 +01:00
|
|
|
}
|
2018-05-02 19:54:36 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function get_all_msg_ids(): number[] {
|
2023-03-02 01:58:25 +01:00
|
|
|
const ids = [...unread_messages];
|
2018-05-10 13:11:53 +02:00
|
|
|
|
|
|
|
return util.sorted_ids(ids);
|
2021-02-28 21:30:38 +01:00
|
|
|
}
|
2018-05-10 13:11:53 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function get_missing_topics(opts: {
|
|
|
|
stream_id: number;
|
2024-01-02 07:03:01 +01:00
|
|
|
topic_dict: FoldDict<TopicHistoryEntry>;
|
2023-12-13 02:00:54 +01:00
|
|
|
}): {pretty_name: string; message_id: number}[] {
|
2021-02-28 21:30:38 +01:00
|
|
|
return unread_topic_counter.get_missing_topics(opts);
|
|
|
|
}
|
2018-05-13 11:38:40 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
export function get_msg_ids_for_starred(): number[] {
|
2018-05-03 22:57:07 +02:00
|
|
|
// This is here for API consistency sake--we never
|
|
|
|
// have unread starred messages. (Some day we may ironically
|
|
|
|
// want to make starring the same as mark-as-unread, but
|
|
|
|
// for now starring === reading.)
|
|
|
|
return [];
|
2021-02-28 21:30:38 +01:00
|
|
|
}
|
2018-05-03 22:57:07 +02:00
|
|
|
|
2023-12-13 02:00:54 +01:00
|
|
|
type UnreadStreamInfo = {
|
|
|
|
stream_id: number;
|
|
|
|
topic: string;
|
|
|
|
unread_message_ids: number[];
|
|
|
|
};
|
|
|
|
|
|
|
|
type UnreadDirectMessageInfo = {
|
|
|
|
other_user_id: number;
|
|
|
|
// Deprecated and misleading synonym for other_user_id
|
|
|
|
sender_id: number;
|
|
|
|
unread_message_ids: number[];
|
|
|
|
};
|
|
|
|
|
|
|
|
type UnreadHuddleInfo = {
|
|
|
|
user_ids_string: string;
|
|
|
|
unread_message_ids: number[];
|
|
|
|
};
|
|
|
|
|
|
|
|
type UnreadMessagesParams = {
|
|
|
|
unread_msgs: {
|
|
|
|
pms: UnreadDirectMessageInfo[];
|
|
|
|
streams: UnreadStreamInfo[];
|
|
|
|
huddles: UnreadHuddleInfo[];
|
|
|
|
mentions: number[];
|
|
|
|
count: number;
|
|
|
|
old_unreads_missing: boolean;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export function initialize(params: UnreadMessagesParams): void {
|
2022-10-27 06:15:43 +02:00
|
|
|
const unread_msgs = params.unread_msgs;
|
2017-08-01 14:50:40 +02:00
|
|
|
|
2022-10-27 22:13:43 +02:00
|
|
|
old_unreads_missing = unread_msgs.old_unreads_missing;
|
2023-06-16 12:24:46 +02:00
|
|
|
unread_direct_message_counter.set_huddles(unread_msgs.huddles);
|
|
|
|
unread_direct_message_counter.set_pms(unread_msgs.pms);
|
2021-02-28 21:30:38 +01:00
|
|
|
unread_topic_counter.set_streams(unread_msgs.streams);
|
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 message_id of unread_msgs.mentions) {
|
2021-02-28 21:30:38 +01:00
|
|
|
unread_mentions_counter.add(message_id);
|
2023-09-12 20:30:02 +02:00
|
|
|
if (unread_direct_message_counter.get_msg_ids().includes(message_id)) {
|
|
|
|
direct_message_with_mention_count.add(message_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
|
|
|
}
|
2022-10-14 17:37:47 +02:00
|
|
|
clear_and_populate_unread_mention_topics();
|
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 obj of unread_msgs.huddles) {
|
2020-07-15 00:34:28 +02:00
|
|
|
for (const message_id of obj.unread_message_ids) {
|
|
|
|
unread_messages.add(message_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
|
|
|
}
|
|
|
|
|
|
|
|
for (const obj of unread_msgs.pms) {
|
2020-07-15 00:34:28 +02:00
|
|
|
for (const message_id of obj.unread_message_ids) {
|
|
|
|
unread_messages.add(message_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
|
|
|
}
|
|
|
|
|
|
|
|
for (const obj of unread_msgs.streams) {
|
2020-07-15 00:34:28 +02:00
|
|
|
for (const message_id of obj.unread_message_ids) {
|
|
|
|
unread_messages.add(message_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 00:34:28 +02:00
|
|
|
for (const message_id of unread_msgs.mentions) {
|
|
|
|
unread_messages.add(message_id);
|
|
|
|
}
|
2021-02-28 21:30:38 +01:00
|
|
|
}
|