2022-11-16 10:50:35 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
|
|
|
import * as blueslip from "./blueslip";
|
2021-02-28 21:30:08 +01:00
|
|
|
import * as channel from "./channel";
|
2022-11-16 10:50:35 +01:00
|
|
|
import {$t_html} from "./i18n";
|
|
|
|
import * as loading from "./loading";
|
2021-02-28 21:30:08 +01:00
|
|
|
import * as message_flags from "./message_flags";
|
2021-02-28 21:31:33 +01:00
|
|
|
import * as message_list from "./message_list";
|
2021-03-30 02:21:21 +02:00
|
|
|
import * as message_lists from "./message_lists";
|
2021-06-09 13:31:39 +02:00
|
|
|
import * as message_live_update from "./message_live_update";
|
2021-02-28 21:30:08 +01:00
|
|
|
import * as message_store from "./message_store";
|
|
|
|
import * as message_viewport from "./message_viewport";
|
|
|
|
import * as notifications from "./notifications";
|
2021-06-09 13:31:39 +02:00
|
|
|
import * as people from "./people";
|
2021-06-10 14:18:46 +02:00
|
|
|
import * as recent_topics_ui from "./recent_topics_ui";
|
2022-08-25 00:28:44 +02:00
|
|
|
import * as recent_topics_util from "./recent_topics_util";
|
2022-11-16 10:50:35 +01:00
|
|
|
import * as ui_report from "./ui_report";
|
2021-02-28 21:30:38 +01:00
|
|
|
import * as unread from "./unread";
|
2021-02-28 21:30:08 +01:00
|
|
|
import * as unread_ui from "./unread_ui";
|
|
|
|
|
2022-11-16 10:50:35 +01:00
|
|
|
let loading_indicator_displayed = false;
|
|
|
|
|
2022-11-17 19:20:17 +01:00
|
|
|
// We might want to use a slightly smaller batch for the first
|
|
|
|
// request, because empirically, the first request can be
|
|
|
|
// significantly slower, likely due to the database warming up its
|
|
|
|
// cache with your UserMessage rows. We don't do that, just because
|
|
|
|
// the progress indicator experience of 1000, 3000, etc. feels weird.
|
|
|
|
const INITIAL_BATCH_SIZE = 1000;
|
|
|
|
const FOLLOWUP_BATCH_SIZE = 1000;
|
|
|
|
|
2022-11-16 10:53:04 +01:00
|
|
|
export function mark_all_as_read(args = {}) {
|
|
|
|
args = {
|
2022-11-17 19:09:10 +01:00
|
|
|
// We use an anchor of "oldest", not "first_unread", because
|
|
|
|
// "first_unread" will be the oldest non-muted unread message,
|
|
|
|
// which would result in muted unreads older than the first
|
|
|
|
// unread not being processed.
|
|
|
|
anchor: "oldest",
|
2022-11-16 10:53:04 +01:00
|
|
|
messages_read_till_now: 0,
|
2022-11-17 19:20:17 +01:00
|
|
|
num_after: INITIAL_BATCH_SIZE,
|
2022-11-16 10:53:04 +01:00
|
|
|
...args,
|
|
|
|
};
|
|
|
|
const request = {
|
|
|
|
anchor: args.anchor,
|
2022-11-17 19:15:16 +01:00
|
|
|
// anchor="oldest" is an anchor ID lower than any valid
|
|
|
|
// message ID; and follow-up requests will have already
|
|
|
|
// processed the anchor ID, so we just want this to be
|
|
|
|
// unconditionally false.
|
|
|
|
include_anchor: false,
|
2022-11-16 10:53:04 +01:00
|
|
|
num_before: 0,
|
2022-11-17 19:20:17 +01:00
|
|
|
num_after: args.num_after,
|
2022-11-16 10:53:04 +01:00
|
|
|
op: "add",
|
|
|
|
flag: "read",
|
|
|
|
// Since there's a database index on is:unread, it's a fast
|
|
|
|
// search query and thus worth including here as an optimization.
|
|
|
|
narrow: JSON.stringify([{operator: "is", operand: "unread", negated: false}]),
|
|
|
|
};
|
2017-03-18 01:41:56 +01:00
|
|
|
channel.post({
|
2022-11-16 10:53:04 +01:00
|
|
|
url: "/json/messages/flags/narrow",
|
|
|
|
data: request,
|
|
|
|
success(data) {
|
|
|
|
const messages_read_till_now = args.messages_read_till_now + data.updated_count;
|
|
|
|
|
|
|
|
if (!data.found_newest) {
|
|
|
|
// If we weren't able to make everything as read in a
|
|
|
|
// single API request, then show a loading indicator.
|
|
|
|
ui_report.loading(
|
|
|
|
$t_html(
|
2022-11-17 08:08:39 +01:00
|
|
|
{
|
|
|
|
defaultMessage:
|
|
|
|
"{N, plural, one {Working… {N} message marked as read so far.} other {Working… {N} messages marked as read so far.}}",
|
|
|
|
},
|
2022-11-16 10:53:04 +01:00
|
|
|
{N: messages_read_till_now},
|
|
|
|
),
|
|
|
|
$("#request-progress-status-banner"),
|
|
|
|
);
|
|
|
|
if (!loading_indicator_displayed) {
|
|
|
|
loading.make_indicator(
|
|
|
|
$("#request-progress-status-banner .loading-indicator"),
|
|
|
|
{abs_positioned: true},
|
|
|
|
);
|
|
|
|
loading_indicator_displayed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
mark_all_as_read({
|
|
|
|
anchor: data.last_processed_id,
|
|
|
|
messages_read_till_now,
|
2022-11-17 19:20:17 +01:00
|
|
|
num_after: FOLLOWUP_BATCH_SIZE,
|
2022-11-16 10:53:04 +01:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (loading_indicator_displayed) {
|
|
|
|
// Only show the success message if a progress banner was displayed.
|
|
|
|
ui_report.loading(
|
|
|
|
$t_html(
|
2022-11-17 08:08:39 +01:00
|
|
|
{
|
|
|
|
defaultMessage:
|
|
|
|
"{N, plural, one {Done! {N} message marked as read.} other {Done! {N} messages marked as read.}}",
|
|
|
|
},
|
2022-11-16 10:53:04 +01:00
|
|
|
{N: messages_read_till_now},
|
|
|
|
),
|
|
|
|
$("#request-progress-status-banner"),
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
loading_indicator_displayed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unread.old_unreads_missing) {
|
|
|
|
// In the rare case that the user had more than
|
|
|
|
// 50K total unreads on the server, the client
|
|
|
|
// won't have known about all of them; this was
|
|
|
|
// communicated to the client via
|
|
|
|
// unread.old_unreads_missing.
|
|
|
|
//
|
|
|
|
// However, since we know we just marked
|
|
|
|
// **everything** as read, we know that we now
|
|
|
|
// have a correct data set of unreads.
|
|
|
|
unread.clear_old_unreads_missing();
|
|
|
|
blueslip.log("Cleared old_unreads_missing after bankruptcy.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error(xhr) {
|
|
|
|
// If we hit the rate limit, just continue without showing any error.
|
|
|
|
if (xhr.responseJSON.code === "RATE_LIMIT_HIT") {
|
|
|
|
const milliseconds_to_wait = 1000 * xhr.responseJSON["retry-after"];
|
|
|
|
setTimeout(() => mark_all_as_read(args), milliseconds_to_wait);
|
|
|
|
} else {
|
|
|
|
// TODO: Ideally this would be a ui_report.error();
|
|
|
|
// the user needs to know that our operation failed.
|
|
|
|
blueslip.error("Failed to mark messages as read: " + xhr.responseText);
|
|
|
|
}
|
2020-06-18 08:11:41 +02:00
|
|
|
},
|
|
|
|
});
|
2021-02-28 21:30:08 +01:00
|
|
|
}
|
2017-03-18 01:41:56 +01:00
|
|
|
|
2017-08-03 00:14:36 +02:00
|
|
|
function process_newly_read_message(message, options) {
|
2022-08-17 07:10:19 +02:00
|
|
|
for (const msg_list of message_lists.all_rendered_message_lists()) {
|
|
|
|
msg_list.show_message_as_read(message, options);
|
2017-08-03 00:14:36 +02:00
|
|
|
}
|
|
|
|
notifications.close_notification(message);
|
2021-06-10 14:18:46 +02:00
|
|
|
recent_topics_ui.update_topic_unread_count(message);
|
2017-08-03 00:14:36 +02:00
|
|
|
}
|
|
|
|
|
2022-11-16 10:50:35 +01:00
|
|
|
export function mark_as_unread_from_here(
|
|
|
|
message_id,
|
2022-11-17 19:20:17 +01:00
|
|
|
include_anchor = true,
|
2022-11-16 10:50:35 +01:00
|
|
|
messages_marked_unread_till_now = 0,
|
2022-11-17 19:20:17 +01:00
|
|
|
num_after = INITIAL_BATCH_SIZE - 1,
|
2022-11-16 10:50:35 +01:00
|
|
|
narrow,
|
|
|
|
) {
|
|
|
|
if (narrow === undefined) {
|
|
|
|
narrow = JSON.stringify(message_lists.current.data.filter.operators());
|
|
|
|
}
|
2021-08-05 00:59:03 +02:00
|
|
|
message_lists.current.prevent_reading();
|
2022-11-16 10:50:35 +01:00
|
|
|
const opts = {
|
|
|
|
anchor: message_id,
|
2022-11-17 19:20:17 +01:00
|
|
|
include_anchor,
|
2022-11-16 10:50:35 +01:00
|
|
|
num_before: 0,
|
2022-11-17 19:20:17 +01:00
|
|
|
num_after,
|
2022-11-16 10:50:35 +01:00
|
|
|
narrow,
|
|
|
|
op: "remove",
|
|
|
|
flag: "read",
|
|
|
|
};
|
|
|
|
channel.post({
|
|
|
|
url: "/json/messages/flags/narrow",
|
|
|
|
data: opts,
|
|
|
|
success(data) {
|
|
|
|
messages_marked_unread_till_now += data.updated_count;
|
|
|
|
|
|
|
|
if (!data.found_newest) {
|
|
|
|
// If we weren't able to complete the request fully in
|
|
|
|
// the current batch, show a progress indicator.
|
|
|
|
ui_report.loading(
|
|
|
|
$t_html(
|
|
|
|
{
|
2022-11-17 08:08:39 +01:00
|
|
|
defaultMessage:
|
|
|
|
"{N, plural, one {Working… {N} message marked as unread so far.} other {Working… {N} messages marked as unread so far.}}",
|
2022-11-16 10:50:35 +01:00
|
|
|
},
|
|
|
|
{N: messages_marked_unread_till_now},
|
|
|
|
),
|
|
|
|
$("#request-progress-status-banner"),
|
|
|
|
);
|
|
|
|
if (!loading_indicator_displayed) {
|
|
|
|
loading.make_indicator(
|
|
|
|
$("#request-progress-status-banner .loading-indicator"),
|
|
|
|
{abs_positioned: true},
|
|
|
|
);
|
|
|
|
loading_indicator_displayed = true;
|
|
|
|
}
|
|
|
|
mark_as_unread_from_here(
|
|
|
|
data.last_processed_id,
|
|
|
|
false,
|
|
|
|
messages_marked_unread_till_now,
|
2022-11-17 19:20:17 +01:00
|
|
|
FOLLOWUP_BATCH_SIZE,
|
2022-11-16 10:50:35 +01:00
|
|
|
narrow,
|
|
|
|
);
|
|
|
|
} else if (loading_indicator_displayed) {
|
|
|
|
// If we were showing a loading indicator, then
|
|
|
|
// display that we finished. For the common case where
|
|
|
|
// the operation succeeds in a single batch, we don't
|
|
|
|
// bother distracting the user with the indication;
|
|
|
|
// the success will be obvious from the UI updating.
|
|
|
|
loading_indicator_displayed = false;
|
|
|
|
ui_report.loading(
|
|
|
|
$t_html(
|
|
|
|
{
|
|
|
|
defaultMessage:
|
2022-11-17 08:08:39 +01:00
|
|
|
"{N, plural, one {Done! {N} message marked as unread.} other {Done! {N} messages marked as unread.}}",
|
2022-11-16 10:50:35 +01:00
|
|
|
},
|
2022-11-17 08:08:39 +01:00
|
|
|
{N: messages_marked_unread_till_now},
|
2022-11-16 10:50:35 +01:00
|
|
|
),
|
|
|
|
$("#request-progress-status-banner"),
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error(xhr) {
|
|
|
|
// If we hit the rate limit, just continue without showing any error.
|
|
|
|
if (xhr.responseJSON.code === "RATE_LIMIT_HIT") {
|
|
|
|
const milliseconds_to_wait = 1000 * xhr.responseJSON["retry-after"];
|
|
|
|
setTimeout(
|
|
|
|
() =>
|
|
|
|
mark_as_unread_from_here(
|
|
|
|
message_id,
|
|
|
|
false,
|
|
|
|
messages_marked_unread_till_now,
|
|
|
|
narrow,
|
|
|
|
),
|
|
|
|
milliseconds_to_wait,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// TODO: Ideally, this case would communicate the
|
|
|
|
// failure to the user, with some manual retry
|
|
|
|
// offered, since the most likely cause is a 502.
|
|
|
|
blueslip.error("Unexpected error marking messages as unread: " + xhr.responseText);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2021-08-05 00:59:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function resume_reading() {
|
|
|
|
message_lists.current.resume_reading();
|
|
|
|
}
|
|
|
|
|
2021-02-28 21:30:08 +01:00
|
|
|
export function process_read_messages_event(message_ids) {
|
2017-08-03 00:28:16 +02:00
|
|
|
/*
|
2018-04-04 21:32:45 +02:00
|
|
|
This code has a lot in common with notify_server_messages_read,
|
2017-08-03 00:28:16 +02:00
|
|
|
but there are subtle differences due to the fact that the
|
|
|
|
server can tell us about unread messages that we didn't
|
|
|
|
actually read locally (and which we may not have even
|
|
|
|
loaded locally).
|
|
|
|
*/
|
2020-07-15 01:29:15 +02:00
|
|
|
const options = {from: "server"};
|
2017-08-03 00:28:16 +02:00
|
|
|
|
2017-12-16 16:53:27 +01:00
|
|
|
message_ids = unread.get_unread_message_ids(message_ids);
|
|
|
|
if (message_ids.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-03 00:28:16 +02:00
|
|
|
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
for (const message_id of message_ids) {
|
2021-03-30 02:21:21 +02:00
|
|
|
if (message_lists.current === message_list.narrowed) {
|
2017-08-03 00:28:16 +02:00
|
|
|
// I'm not sure this entirely makes sense for all server
|
|
|
|
// notifications.
|
2018-08-04 08:42:57 +02:00
|
|
|
unread.set_messages_read_in_narrow(true);
|
2017-08-03 00:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unread.mark_as_read(message_id);
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = message_store.get(message_id);
|
2017-08-03 00:28:16 +02:00
|
|
|
|
2022-11-17 18:01:15 +01:00
|
|
|
// TODO: This ends up doing one in-place rerender operation on
|
|
|
|
// recent conversations per message, not a single global
|
|
|
|
// rerender or one per conversation.
|
2017-08-03 00:28:16 +02:00
|
|
|
if (message) {
|
|
|
|
process_newly_read_message(message, options);
|
|
|
|
}
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
2017-08-03 00:28:16 +02:00
|
|
|
|
2017-12-16 16:53:27 +01:00
|
|
|
unread_ui.update_unread_counts();
|
2021-02-28 21:30:08 +01:00
|
|
|
}
|
2017-08-03 00:28:16 +02:00
|
|
|
|
2021-06-09 13:31:39 +02:00
|
|
|
export function process_unread_messages_event({message_ids, message_details}) {
|
|
|
|
// This is the reverse of process_unread_messages_event.
|
|
|
|
message_ids = unread.get_read_message_ids(message_ids);
|
|
|
|
if (message_ids.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-05 00:59:03 +02:00
|
|
|
if (message_lists.current === message_list.narrowed) {
|
|
|
|
unread.set_messages_read_in_narrow(false);
|
|
|
|
}
|
|
|
|
|
2021-06-09 13:31:39 +02:00
|
|
|
for (const message_id of message_ids) {
|
|
|
|
const message = message_store.get(message_id);
|
|
|
|
|
|
|
|
if (message) {
|
|
|
|
message.unread = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const message_info = message_details[message_id];
|
|
|
|
|
|
|
|
let user_ids_string;
|
|
|
|
|
|
|
|
if (message_info.type === "private") {
|
|
|
|
user_ids_string = people.pm_lookup_key_from_user_ids(message_info.user_ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
unread.process_unread_message({
|
|
|
|
id: message_id,
|
|
|
|
mentioned: message_info.mentioned,
|
|
|
|
stream_id: message_info.stream_id,
|
|
|
|
topic: message_info.topic,
|
|
|
|
type: message_info.type,
|
|
|
|
unread: true,
|
|
|
|
user_ids_string,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-11-17 18:01:15 +01:00
|
|
|
A batch of messages marked as unread can be 1000+ messages, so
|
|
|
|
we do want to do a bulk operation for these UI updates.
|
|
|
|
|
2021-06-09 13:31:39 +02:00
|
|
|
We use a big-hammer approach now to updating the message view.
|
|
|
|
This is relatively harmless, since the only time we are called is
|
|
|
|
when the user herself marks her message as unread. But we
|
|
|
|
do eventually want to be more surgical here, especially once we
|
|
|
|
have a final scheme for how best to structure the HTML within
|
|
|
|
the message to indicate read-vs.-unread. Currently we use a
|
|
|
|
green border, but that may change.
|
2022-11-17 18:01:15 +01:00
|
|
|
|
|
|
|
The main downside of doing a full rerender is that it can be
|
|
|
|
user-visible in the form of users' avatars flickering.
|
2021-06-09 13:31:39 +02:00
|
|
|
*/
|
|
|
|
message_live_update.rerender_messages_view();
|
2022-11-17 18:01:15 +01:00
|
|
|
recent_topics_ui.complete_rerender();
|
2021-06-09 13:31:39 +02:00
|
|
|
|
2022-11-10 16:55:35 +01:00
|
|
|
if (
|
|
|
|
!message_lists.current.can_mark_messages_read() &&
|
|
|
|
message_lists.current.has_unread_messages()
|
|
|
|
) {
|
|
|
|
unread_ui.notify_messages_remain_unread();
|
|
|
|
}
|
|
|
|
|
2021-06-09 13:31:39 +02:00
|
|
|
unread_ui.update_unread_counts();
|
|
|
|
}
|
|
|
|
|
2018-02-16 20:44:12 +01:00
|
|
|
// Takes a list of messages and marks them as read.
|
|
|
|
// Skips any messages that are already marked as read.
|
2021-06-25 22:08:05 +02:00
|
|
|
export function notify_server_messages_read(messages, options = {}) {
|
2017-12-16 16:53:27 +01:00
|
|
|
messages = unread.get_unread_messages(messages);
|
|
|
|
if (messages.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-18 01:41:56 +01:00
|
|
|
|
2017-12-21 17:51:52 +01:00
|
|
|
message_flags.send_read(messages);
|
|
|
|
|
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-03-30 02:21:21 +02:00
|
|
|
if (message_lists.current === message_list.narrowed) {
|
2018-08-04 08:42:57 +02:00
|
|
|
unread.set_messages_read_in_narrow(true);
|
2017-03-18 01:41:56 +01:00
|
|
|
}
|
|
|
|
|
2017-08-02 21:40:01 +02:00
|
|
|
unread.mark_as_read(message.id);
|
2017-08-03 00:14:36 +02:00
|
|
|
process_newly_read_message(message, options);
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
2017-03-18 01:41:56 +01:00
|
|
|
|
2017-12-16 16:53:27 +01:00
|
|
|
unread_ui.update_unread_counts();
|
2021-02-28 21:30:08 +01:00
|
|
|
}
|
2017-03-18 01:41:56 +01:00
|
|
|
|
2021-02-28 21:30:08 +01:00
|
|
|
export function notify_server_message_read(message, options) {
|
|
|
|
notify_server_messages_read([message], options);
|
|
|
|
}
|
2017-03-18 01:41:56 +01:00
|
|
|
|
2022-02-15 02:23:46 +01:00
|
|
|
export function process_scrolled_to_bottom() {
|
2022-08-25 00:28:44 +02:00
|
|
|
if (recent_topics_util.is_visible()) {
|
|
|
|
// First, verify the current message list is visible.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-15 02:23:46 +01:00
|
|
|
if (message_lists.current.can_mark_messages_read()) {
|
|
|
|
mark_current_list_as_read();
|
2021-08-02 20:09:56 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For message lists that don't support marking messages as read
|
|
|
|
// automatically, we display a banner offering to let you mark
|
|
|
|
// them as read manually, only if there are unreads present.
|
|
|
|
if (message_lists.current.has_unread_messages()) {
|
|
|
|
unread_ui.notify_messages_remain_unread();
|
2022-02-15 02:23:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 01:41:56 +01:00
|
|
|
// If we ever materially change the algorithm for this function, we
|
|
|
|
// may need to update notifications.received_messages as well.
|
2021-02-28 21:30:08 +01:00
|
|
|
export function process_visible() {
|
2022-02-15 02:23:46 +01:00
|
|
|
if (message_viewport.is_visible_and_focused() && message_viewport.bottom_message_visible()) {
|
|
|
|
process_scrolled_to_bottom();
|
2017-03-18 01:41:56 +01:00
|
|
|
}
|
2021-02-28 21:30:08 +01:00
|
|
|
}
|
2017-03-18 01:41:56 +01:00
|
|
|
|
2021-02-28 21:30:08 +01:00
|
|
|
export function mark_current_list_as_read(options) {
|
2021-03-30 02:21:21 +02:00
|
|
|
notify_server_messages_read(message_lists.current.all_messages(), options);
|
2021-02-28 21:30:08 +01:00
|
|
|
}
|
2017-03-18 01:41:56 +01:00
|
|
|
|
2021-02-28 21:30:08 +01:00
|
|
|
export function mark_stream_as_read(stream_id, cont) {
|
2017-03-18 01:41:56 +01:00
|
|
|
channel.post({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/mark_stream_as_read",
|
2020-07-20 22:18:43 +02:00
|
|
|
data: {stream_id},
|
2018-05-06 21:43:17 +02:00
|
|
|
success: cont,
|
|
|
|
});
|
2021-02-28 21:30:08 +01:00
|
|
|
}
|
2017-03-18 01:41:56 +01:00
|
|
|
|
2021-02-28 21:30:08 +01:00
|
|
|
export function mark_topic_as_read(stream_id, topic, cont) {
|
2017-03-18 01:41:56 +01:00
|
|
|
channel.post({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/mark_topic_as_read",
|
2020-07-20 22:18:43 +02:00
|
|
|
data: {stream_id, topic_name: topic},
|
2018-05-06 21:43:17 +02:00
|
|
|
success: cont,
|
|
|
|
});
|
2021-02-28 21:30:08 +01:00
|
|
|
}
|
2022-10-21 11:52:47 +02:00
|
|
|
|
|
|
|
export function mark_pm_as_read(user_ids_string) {
|
|
|
|
// user_ids_string is a stringified list of user ids which are
|
|
|
|
// participants in the conversation other than the current
|
|
|
|
// user. Eg: "123,124" or "123"
|
|
|
|
const unread_msg_ids = unread.get_msg_ids_for_user_ids_string(user_ids_string);
|
|
|
|
message_flags.mark_as_read(unread_msg_ids);
|
|
|
|
}
|