2021-02-28 00:49:06 +01:00
|
|
|
import _ from "lodash";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-02-28 01:13:13 +01:00
|
|
|
import {buddy_list} from "./buddy_list";
|
2021-02-28 00:50:52 +01:00
|
|
|
import * as compose_state from "./compose_state";
|
2021-02-28 00:58:04 +01:00
|
|
|
import * as floating_recipient_bar from "./floating_recipient_bar";
|
2021-02-28 00:49:06 +01:00
|
|
|
import * as message_viewport from "./message_viewport";
|
|
|
|
import * as people from "./people";
|
|
|
|
import * as rows from "./rows";
|
2021-02-28 00:53:59 +01:00
|
|
|
import * as stream_data from "./stream_data";
|
2021-02-28 00:49:06 +01:00
|
|
|
import * as util from "./util";
|
2020-07-24 06:02:07 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let focused_recipient;
|
|
|
|
let normal_display = false;
|
2013-08-11 21:21:47 +02:00
|
|
|
|
2021-02-28 00:49:06 +01:00
|
|
|
export function should_fade_message(message) {
|
2017-02-24 01:20:49 +01:00
|
|
|
return !util.same_recipient(focused_recipient, message);
|
2021-02-28 00:49:06 +01:00
|
|
|
}
|
2014-02-12 21:07:22 +01:00
|
|
|
|
2021-02-28 00:49:06 +01:00
|
|
|
export function set_focused_recipient(msg_type) {
|
2013-08-12 17:40:55 +02:00
|
|
|
if (msg_type === undefined) {
|
|
|
|
focused_recipient = undefined;
|
|
|
|
}
|
|
|
|
|
2013-08-11 23:54:50 +02:00
|
|
|
// Construct focused_recipient as a mocked up element which has all the
|
|
|
|
// fields of a message used by util.same_recipient()
|
|
|
|
focused_recipient = {
|
2017-01-12 00:17:43 +01:00
|
|
|
type: msg_type,
|
2013-08-11 23:54:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (focused_recipient.type === "stream") {
|
2020-07-15 01:29:15 +02:00
|
|
|
const stream_name = $("#stream_message_recipient_stream").val();
|
|
|
|
focused_recipient.topic = $("#stream_message_recipient_topic").val();
|
2017-02-23 03:53:02 +01:00
|
|
|
focused_recipient.stream = stream_name;
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = stream_data.get_sub(stream_name);
|
2017-02-23 03:53:02 +01:00
|
|
|
if (sub) {
|
|
|
|
focused_recipient.stream_id = sub.stream_id;
|
|
|
|
}
|
2013-08-11 23:54:50 +02:00
|
|
|
} else {
|
|
|
|
// Normalize the recipient list so it matches the one used when
|
2014-01-31 16:27:24 +01:00
|
|
|
// adding the message (see message_store.add_message_metadata()).
|
2019-12-02 17:53:55 +01:00
|
|
|
const reply_to = util.normalize_recipients(compose_state.private_message_recipient());
|
2017-02-25 00:30:51 +01:00
|
|
|
focused_recipient.reply_to = reply_to;
|
|
|
|
focused_recipient.to_user_ids = people.reply_to_to_user_ids_string(reply_to);
|
2013-08-11 23:54:50 +02:00
|
|
|
}
|
2021-02-28 00:49:06 +01:00
|
|
|
}
|
2013-08-11 21:21:47 +02:00
|
|
|
|
2018-04-22 16:30:58 +02:00
|
|
|
function display_messages_normally() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const table = rows.get_table(current_msg_list.table_name);
|
2020-07-15 01:29:15 +02:00
|
|
|
table.find(".recipient_row").removeClass("message-fade");
|
2013-08-11 21:34:05 +02:00
|
|
|
|
2013-08-12 17:40:55 +02:00
|
|
|
normal_display = true;
|
2014-03-13 21:14:33 +01:00
|
|
|
floating_recipient_bar.update();
|
2013-08-12 17:40:55 +02:00
|
|
|
}
|
2013-08-11 21:21:47 +02:00
|
|
|
|
2014-02-26 17:22:21 +01:00
|
|
|
function change_fade_state(elt, should_fade_group) {
|
|
|
|
if (should_fade_group) {
|
2017-03-16 19:26:26 +01:00
|
|
|
elt.addClass("message-fade");
|
2013-10-30 19:53:17 +01:00
|
|
|
} else {
|
2017-03-16 19:26:26 +01:00
|
|
|
elt.removeClass("message-fade");
|
2013-10-30 19:53:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-22 16:30:58 +02:00
|
|
|
function fade_messages() {
|
2019-11-02 00:06:25 +01:00
|
|
|
let i;
|
|
|
|
let first_message;
|
|
|
|
let first_row;
|
|
|
|
let should_fade_group = false;
|
|
|
|
const visible_groups = message_viewport.visible_groups(false);
|
2013-08-12 17:40:55 +02:00
|
|
|
|
|
|
|
normal_display = false;
|
|
|
|
|
2013-10-30 19:53:17 +01:00
|
|
|
// Update the visible messages first, before the compose box opens
|
2016-11-30 19:05:04 +01:00
|
|
|
for (i = 0; i < visible_groups.length; i += 1) {
|
2014-02-26 17:22:21 +01:00
|
|
|
first_row = rows.first_message_in_group(visible_groups[i]);
|
|
|
|
first_message = current_msg_list.get(rows.id(first_row));
|
2021-02-28 00:49:06 +01:00
|
|
|
should_fade_group = should_fade_message(first_message);
|
2013-08-11 21:21:47 +02:00
|
|
|
|
2014-02-26 17:22:21 +01:00
|
|
|
change_fade_state($(visible_groups[i]), should_fade_group);
|
2013-08-11 21:21:47 +02:00
|
|
|
}
|
2013-08-16 00:04:12 +02:00
|
|
|
|
2014-02-26 17:22:21 +01:00
|
|
|
// Defer updating all message groups so that the compose box can open sooner
|
2020-07-15 00:34:28 +02:00
|
|
|
setTimeout(
|
|
|
|
(expected_msg_list, expected_recipient) => {
|
|
|
|
const all_groups = rows.get_table(current_msg_list.table_name).find(".recipient_row");
|
|
|
|
|
|
|
|
if (
|
|
|
|
current_msg_list !== expected_msg_list ||
|
|
|
|
!compose_state.composing() ||
|
|
|
|
compose_state.private_message_recipient() !== expected_recipient
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
should_fade_group = false;
|
|
|
|
|
|
|
|
// Note: The below algorithm relies on the fact that all_elts is
|
|
|
|
// sorted as it would be displayed in the message view
|
|
|
|
for (i = 0; i < all_groups.length; i += 1) {
|
|
|
|
const group_elt = $(all_groups[i]);
|
2021-02-28 00:49:06 +01:00
|
|
|
should_fade_group = should_fade_message(rows.recipient_from_group(group_elt));
|
2020-07-15 00:34:28 +02:00
|
|
|
change_fade_state(group_elt, should_fade_group);
|
|
|
|
}
|
|
|
|
|
|
|
|
floating_recipient_bar.update();
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
current_msg_list,
|
|
|
|
compose_state.private_message_recipient(),
|
|
|
|
);
|
2013-08-11 21:21:47 +02:00
|
|
|
}
|
|
|
|
|
2021-02-28 00:49:06 +01:00
|
|
|
export function would_receive_message(user_id) {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (focused_recipient.type === "stream") {
|
2020-06-20 23:38:59 +02:00
|
|
|
const sub = stream_data.get_sub_by_id(focused_recipient.stream_id);
|
2020-06-05 19:49:16 +02:00
|
|
|
if (!sub) {
|
|
|
|
// If the stream isn't valid, there is no risk of a mix
|
2018-04-25 01:05:57 +02:00
|
|
|
// yet, so we sort of "lie" and say they would receive a
|
|
|
|
// message.
|
|
|
|
return true;
|
2017-03-04 22:43:15 +01:00
|
|
|
}
|
|
|
|
|
2020-07-08 22:28:35 +02:00
|
|
|
return stream_data.is_user_subscribed(focused_recipient.stream_id, user_id);
|
2013-09-07 04:22:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PM, so check if the given email is in the recipients list.
|
2020-06-05 19:49:16 +02:00
|
|
|
return util.is_pm_recipient(user_id, focused_recipient);
|
2021-02-28 00:49:06 +01:00
|
|
|
}
|
2013-09-07 04:22:18 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_fade_config = {
|
2020-07-20 22:18:43 +02:00
|
|
|
get_user_id(li) {
|
|
|
|
return buddy_list.get_key_from_li({li});
|
2018-04-22 14:12:08 +02:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
fade(li) {
|
2020-07-15 01:29:15 +02:00
|
|
|
return li.addClass("user-fade");
|
2018-04-22 14:12:08 +02:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
unfade(li) {
|
2020-07-15 01:29:15 +02:00
|
|
|
return li.removeClass("user-fade");
|
2018-04-22 14:12:08 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
function update_user_row_when_fading(li, conf) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_id = conf.get_user_id(li);
|
2021-02-28 00:49:06 +01:00
|
|
|
const would_receive = would_receive_message(user_id);
|
2018-04-22 14:12:08 +02:00
|
|
|
|
2019-07-18 05:23:05 +02:00
|
|
|
if (would_receive || people.is_my_user_id(user_id)) {
|
2018-04-22 14:12:08 +02:00
|
|
|
conf.unfade(li);
|
2018-04-25 01:05:57 +02:00
|
|
|
} else {
|
|
|
|
conf.fade(li);
|
2017-02-26 23:40:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-22 14:12:08 +02:00
|
|
|
function display_users_normally(items, conf) {
|
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 li of items) {
|
2018-04-22 14:12:08 +02:00
|
|
|
conf.unfade(li);
|
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
|
|
|
}
|
2018-04-22 14:12:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function fade_users(items, conf) {
|
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 li of items) {
|
2018-04-22 14:12:08 +02:00
|
|
|
update_user_row_when_fading(li, conf);
|
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
|
|
|
}
|
2013-09-07 04:22:18 +02:00
|
|
|
}
|
|
|
|
|
2018-04-22 16:30:58 +02:00
|
|
|
function want_normal_display() {
|
2013-08-12 17:40:55 +02:00
|
|
|
// If we're not composing show a normal display.
|
|
|
|
if (focused_recipient === undefined) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the user really hasn't specified anything let, then we want a normal display
|
2013-09-24 17:16:47 +02:00
|
|
|
if (focused_recipient.type === "stream") {
|
|
|
|
// If a stream doesn't exist, there is no real chance of a mix, so fading
|
|
|
|
// is just noise to the user.
|
2020-06-20 23:38:59 +02:00
|
|
|
if (!stream_data.get_sub_by_id(focused_recipient.stream_id)) {
|
2013-09-24 17:16:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is kind of debatable. If the topic is empty, it could be that
|
|
|
|
// the user simply hasn't started typing it yet, but disabling fading here
|
|
|
|
// means the feature doesn't help realms where topics aren't mandatory
|
|
|
|
// (which is most realms as of this writing).
|
2018-12-22 16:10:31 +01:00
|
|
|
if (focused_recipient.topic === "") {
|
2013-09-24 17:16:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 17:35:24 +01:00
|
|
|
return focused_recipient.type === "private" && focused_recipient.reply_to === "";
|
2013-08-12 17:40:55 +02:00
|
|
|
}
|
|
|
|
|
2018-04-22 16:25:46 +02:00
|
|
|
function do_update_all() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_items = buddy_list.get_items();
|
2018-04-22 14:12:08 +02:00
|
|
|
|
2018-04-22 16:30:58 +02:00
|
|
|
if (want_normal_display()) {
|
2013-08-12 17:40:55 +02:00
|
|
|
if (!normal_display) {
|
2018-04-22 16:30:58 +02:00
|
|
|
display_messages_normally();
|
2018-04-22 14:12:08 +02:00
|
|
|
display_users_normally(user_items, user_fade_config);
|
2013-08-12 17:40:55 +02:00
|
|
|
}
|
2016-06-09 23:02:49 +02:00
|
|
|
} else {
|
2018-04-22 16:30:58 +02:00
|
|
|
fade_messages();
|
2018-04-22 14:12:08 +02:00
|
|
|
fade_users(user_items, user_fade_config);
|
2013-08-12 17:40:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-07 04:22:18 +02:00
|
|
|
// This one only updates the users, not both, like update_faded_messages.
|
|
|
|
// This is for when new presence information comes in, redrawing the presence
|
|
|
|
// list.
|
2021-02-28 00:49:06 +01:00
|
|
|
export function update_faded_users() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_items = buddy_list.get_items();
|
2018-04-22 14:12:08 +02:00
|
|
|
|
2021-02-28 00:49:06 +01:00
|
|
|
update_user_info(user_items, user_fade_config);
|
|
|
|
}
|
2018-04-22 17:46:20 +02:00
|
|
|
|
2021-02-28 00:49:06 +01:00
|
|
|
export function update_user_info(items, conf) {
|
2018-04-22 16:30:58 +02:00
|
|
|
if (want_normal_display()) {
|
2018-04-22 17:46:20 +02:00
|
|
|
display_users_normally(items, conf);
|
2013-09-07 04:22:18 +02:00
|
|
|
} else {
|
2018-04-22 17:46:20 +02:00
|
|
|
fade_users(items, conf);
|
2013-09-07 04:22:18 +02:00
|
|
|
}
|
2021-02-28 00:49:06 +01:00
|
|
|
}
|
2013-09-07 04:22:18 +02:00
|
|
|
|
2018-04-22 16:25:46 +02:00
|
|
|
// This gets called on keyup events, hence the throttling.
|
2021-02-28 00:49:06 +01:00
|
|
|
export const update_all = _.debounce(do_update_all, 50);
|
2013-08-11 21:21:47 +02:00
|
|
|
|
2021-02-28 00:49:06 +01:00
|
|
|
export function start_compose(msg_type) {
|
|
|
|
set_focused_recipient(msg_type);
|
2018-04-22 16:25:46 +02:00
|
|
|
do_update_all();
|
2021-02-28 00:49:06 +01:00
|
|
|
}
|
2013-08-12 00:08:36 +02:00
|
|
|
|
2021-02-28 00:49:06 +01:00
|
|
|
export function clear_compose() {
|
2013-08-12 17:40:55 +02:00
|
|
|
focused_recipient = undefined;
|
2018-04-22 16:30:58 +02:00
|
|
|
display_messages_normally();
|
2021-02-28 00:49:06 +01:00
|
|
|
update_faded_users();
|
|
|
|
}
|
2013-08-12 17:40:55 +02:00
|
|
|
|
2021-02-28 00:49:06 +01:00
|
|
|
export function update_message_list() {
|
2018-04-22 16:30:58 +02:00
|
|
|
if (want_normal_display()) {
|
2018-05-06 21:43:17 +02:00
|
|
|
display_messages_normally();
|
2016-06-09 23:02:49 +02:00
|
|
|
} else {
|
2018-04-22 16:30:58 +02:00
|
|
|
fade_messages();
|
2013-08-12 17:40:55 +02:00
|
|
|
}
|
2021-02-28 00:49:06 +01:00
|
|
|
}
|
2013-08-12 17:40:55 +02:00
|
|
|
|
2021-02-28 00:49:06 +01:00
|
|
|
export function update_rendered_message_groups(message_groups, get_element) {
|
2018-04-22 16:30:58 +02:00
|
|
|
if (want_normal_display()) {
|
2013-08-13 17:18:28 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-22 16:30:58 +02:00
|
|
|
// This loop is superficially similar to some code in fade_messages, but an
|
2013-08-13 17:18:28 +02:00
|
|
|
// important difference here is that we look at each message individually, whereas
|
|
|
|
// the other code takes advantage of blocks beneath recipient bars.
|
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_group of message_groups) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const elt = get_element(message_group);
|
|
|
|
const first_message = message_group.message_containers[0].msg;
|
2021-02-28 00:49:06 +01:00
|
|
|
const should_fade = should_fade_message(first_message);
|
2014-02-21 21:10:09 +01:00
|
|
|
change_fade_state(elt, should_fade);
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
2021-02-28 00:49:06 +01:00
|
|
|
}
|