2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-03-30 06:23:09 +02:00
|
|
|
import {all_messages_data} from "./all_messages_data";
|
2021-03-16 23:38:59 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
2021-02-28 00:55:11 +01:00
|
|
|
import * as color_data from "./color_data";
|
2021-03-30 02:21:21 +02:00
|
|
|
import * as message_lists from "./message_lists";
|
2021-02-28 01:10:31 +01:00
|
|
|
import * as message_util from "./message_util";
|
2021-02-28 01:24:04 +01:00
|
|
|
import * as message_view_header from "./message_view_header";
|
2021-02-28 00:55:11 +01:00
|
|
|
import * as narrow_state from "./narrow_state";
|
2021-02-28 01:03:46 +01:00
|
|
|
import * as overlays from "./overlays";
|
2021-02-28 00:55:11 +01:00
|
|
|
import * as peer_data from "./peer_data";
|
2021-06-10 14:18:46 +02:00
|
|
|
import * as recent_topics_ui from "./recent_topics_ui";
|
2021-02-28 01:18:48 +01:00
|
|
|
import * as settings_notifications from "./settings_notifications";
|
2021-02-28 00:55:11 +01:00
|
|
|
import * as stream_color from "./stream_color";
|
|
|
|
import * as stream_data from "./stream_data";
|
2021-02-28 21:31:02 +01:00
|
|
|
import * as stream_list from "./stream_list";
|
2021-02-28 00:55:11 +01:00
|
|
|
import * as stream_muting from "./stream_muting";
|
2021-07-09 15:51:31 +02:00
|
|
|
import * as stream_settings_ui from "./stream_settings_ui";
|
2021-04-15 17:02:54 +02:00
|
|
|
import * as sub_store from "./sub_store";
|
2021-01-12 21:38:01 +01:00
|
|
|
|
2020-03-22 14:53:00 +01:00
|
|
|
// In theory, this function should apply the account-level defaults,
|
|
|
|
// however, they are only called after a manual override, so
|
2019-02-13 10:22:16 +01:00
|
|
|
// doing so is unnecessary with the current code. Ideally, we'd do a
|
|
|
|
// refactor to address that, however.
|
2019-06-14 19:29:48 +02:00
|
|
|
function update_stream_setting(sub, value, setting) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $setting_checkbox = $(`#${CSS.escape(setting)}_${CSS.escape(sub.stream_id)}`);
|
|
|
|
$setting_checkbox.prop("checked", value);
|
2019-06-14 19:29:48 +02:00
|
|
|
sub[setting] = value;
|
2017-03-19 16:03:07 +01:00
|
|
|
}
|
|
|
|
|
2021-02-28 00:55:11 +01:00
|
|
|
export function update_property(stream_id, property, value, other_values) {
|
2021-04-15 17:02:54 +02:00
|
|
|
const sub = sub_store.get(stream_id);
|
2017-03-19 16:03:07 +01:00
|
|
|
if (sub === undefined) {
|
|
|
|
// This isn't a stream we know about, so ignore it.
|
2020-07-15 00:34:28 +02:00
|
|
|
blueslip.warn("Update for an unknown subscription", {
|
2020-07-20 22:18:43 +02:00
|
|
|
stream_id,
|
|
|
|
property,
|
|
|
|
value,
|
2020-07-15 00:34:28 +02:00
|
|
|
});
|
2017-03-19 16:03:07 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (property) {
|
2020-07-15 02:14:03 +02:00
|
|
|
case "color":
|
|
|
|
stream_color.update_stream_color(sub, value, {update_historical: true});
|
|
|
|
break;
|
|
|
|
case "in_home_view":
|
|
|
|
stream_muting.update_is_muted(sub, !value);
|
2022-02-20 21:32:14 +01:00
|
|
|
stream_list.refresh_muted_or_unmuted_stream(sub);
|
2021-06-10 14:18:46 +02:00
|
|
|
recent_topics_ui.complete_rerender();
|
2020-07-15 02:14:03 +02:00
|
|
|
break;
|
|
|
|
case "desktop_notifications":
|
|
|
|
case "audible_notifications":
|
|
|
|
case "push_notifications":
|
|
|
|
case "email_notifications":
|
|
|
|
case "wildcard_mentions_notify":
|
|
|
|
update_stream_setting(sub, value, property);
|
2021-09-20 15:56:32 +02:00
|
|
|
settings_notifications.update_page(settings_notifications.user_settings_panel);
|
2020-07-15 02:14:03 +02:00
|
|
|
break;
|
|
|
|
case "name":
|
2021-07-09 15:51:31 +02:00
|
|
|
stream_settings_ui.update_stream_name(sub, value);
|
2020-07-15 02:14:03 +02:00
|
|
|
break;
|
|
|
|
case "description":
|
2021-07-09 15:51:31 +02:00
|
|
|
stream_settings_ui.update_stream_description(
|
|
|
|
sub,
|
|
|
|
value,
|
|
|
|
other_values.rendered_description,
|
|
|
|
);
|
2020-07-15 02:14:03 +02:00
|
|
|
break;
|
|
|
|
case "email_address":
|
|
|
|
sub.email_address = value;
|
|
|
|
break;
|
|
|
|
case "pin_to_top":
|
|
|
|
update_stream_setting(sub, value, property);
|
|
|
|
stream_list.refresh_pinned_or_unpinned_stream(sub);
|
|
|
|
break;
|
|
|
|
case "invite_only":
|
2021-07-09 15:51:31 +02:00
|
|
|
stream_settings_ui.update_stream_privacy(sub, {
|
2020-07-15 02:14:03 +02:00
|
|
|
invite_only: value,
|
|
|
|
history_public_to_subscribers: other_values.history_public_to_subscribers,
|
2020-11-10 15:57:14 +01:00
|
|
|
is_web_public: other_values.is_web_public,
|
2020-07-15 02:14:03 +02:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "stream_post_policy":
|
2021-07-09 15:51:31 +02:00
|
|
|
stream_settings_ui.update_stream_post_policy(sub, value);
|
2020-07-15 02:14:03 +02:00
|
|
|
break;
|
|
|
|
case "message_retention_days":
|
2021-07-09 15:51:31 +02:00
|
|
|
stream_settings_ui.update_message_retention_setting(sub, value);
|
2020-07-15 02:14:03 +02:00
|
|
|
break;
|
|
|
|
default:
|
2020-07-15 00:34:28 +02:00
|
|
|
blueslip.warn("Unexpected subscription property type", {
|
2020-07-20 22:18:43 +02:00
|
|
|
property,
|
|
|
|
value,
|
2020-07-15 00:34:28 +02:00
|
|
|
});
|
2017-03-19 16:03:07 +01:00
|
|
|
}
|
2021-02-28 00:55:11 +01:00
|
|
|
}
|
2017-03-19 16:03:07 +01:00
|
|
|
|
2017-03-22 07:31:58 +01:00
|
|
|
// Add yourself to a stream we already know about client-side.
|
|
|
|
// It's likely we should be passing in the full sub object from the caller/backend,
|
|
|
|
// but for now we just pass in the subscribers and color (things likely to be different).
|
2021-02-28 00:55:11 +01:00
|
|
|
export function mark_subscribed(sub, subscribers, color) {
|
2017-03-19 16:47:30 +01:00
|
|
|
if (sub === undefined) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("Undefined sub passed to mark_subscribed");
|
2017-03-19 16:47:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sub.subscribed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-22 07:31:58 +01:00
|
|
|
// If the backend sent us a color, use that
|
2018-11-10 01:57:10 +01:00
|
|
|
if (color !== undefined && sub.color !== color) {
|
2017-03-22 07:31:58 +01:00
|
|
|
sub.color = color;
|
2018-11-10 01:57:10 +01:00
|
|
|
stream_color.update_stream_color(sub, color, {update_historical: true});
|
2017-03-22 07:31:58 +01:00
|
|
|
} else if (sub.color === undefined) {
|
|
|
|
// If the backend didn't, and we have a color already, send
|
|
|
|
// the backend that color. It's not clear this code path is
|
|
|
|
// needed.
|
2017-03-22 07:36:45 +01:00
|
|
|
blueslip.warn("Frontend needed to pick a color in mark_subscribed");
|
2018-11-28 23:12:40 +01:00
|
|
|
color = color_data.pick_color();
|
2021-07-09 15:51:31 +02:00
|
|
|
stream_settings_ui.set_color(sub.stream_id, color);
|
2017-03-22 07:31:58 +01:00
|
|
|
}
|
2017-03-19 16:47:30 +01:00
|
|
|
stream_data.subscribe_myself(sub);
|
|
|
|
if (subscribers) {
|
2021-01-13 22:03:25 +01:00
|
|
|
peer_data.set_subscribers(sub.stream_id, subscribers);
|
2017-03-19 16:47:30 +01:00
|
|
|
}
|
|
|
|
|
2017-10-18 08:36:56 +02:00
|
|
|
if (overlays.streams_open()) {
|
2021-07-09 15:51:31 +02:00
|
|
|
stream_settings_ui.update_settings_for_subscribed(sub);
|
2017-10-18 08:36:56 +02:00
|
|
|
}
|
2017-03-19 16:47:30 +01:00
|
|
|
|
2020-06-22 23:17:23 +02:00
|
|
|
// update navbar if necessary
|
2020-07-08 23:44:01 +02:00
|
|
|
message_view_header.maybe_rerender_title_area_for_stream(sub);
|
2020-06-22 23:17:23 +02:00
|
|
|
|
2017-04-25 15:25:31 +02:00
|
|
|
if (narrow_state.is_for_stream_id(sub.stream_id)) {
|
2021-03-30 02:21:21 +02:00
|
|
|
message_lists.current.update_trailing_bookend();
|
2017-03-19 16:47:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update unread counts as the new stream in sidebar might
|
|
|
|
// need its unread counts re-calculated
|
2021-03-30 06:23:09 +02:00
|
|
|
message_util.do_unread_count_updates(all_messages_data.all_messages());
|
2017-03-19 16:47:30 +01:00
|
|
|
|
2020-06-18 13:53:44 +02:00
|
|
|
stream_list.add_sidebar_row(sub);
|
2021-02-28 00:55:11 +01:00
|
|
|
}
|
2017-03-19 16:47:30 +01:00
|
|
|
|
2021-02-28 00:55:11 +01:00
|
|
|
export function mark_unsubscribed(sub) {
|
2017-03-19 16:47:30 +01:00
|
|
|
if (sub === undefined) {
|
|
|
|
// We don't know about this stream
|
|
|
|
return;
|
|
|
|
} else if (sub.subscribed) {
|
|
|
|
stream_data.unsubscribe_myself(sub);
|
2017-10-24 07:43:00 +02:00
|
|
|
if (overlays.streams_open()) {
|
2021-07-09 15:51:31 +02:00
|
|
|
stream_settings_ui.update_settings_for_unsubscribed(sub);
|
2017-10-24 07:43:00 +02:00
|
|
|
}
|
2020-06-22 23:17:23 +02:00
|
|
|
// update navbar if necessary
|
2020-07-08 23:44:01 +02:00
|
|
|
message_view_header.maybe_rerender_title_area_for_stream(sub);
|
2017-03-19 16:47:30 +01:00
|
|
|
} else {
|
|
|
|
// Already unsubscribed
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-25 15:25:31 +02:00
|
|
|
if (narrow_state.is_for_stream_id(sub.stream_id)) {
|
2021-03-30 02:21:21 +02:00
|
|
|
message_lists.current.update_trailing_bookend();
|
2017-03-19 16:47:30 +01:00
|
|
|
}
|
|
|
|
|
2020-06-18 13:53:44 +02:00
|
|
|
stream_list.remove_sidebar_row(sub.stream_id);
|
2021-02-28 00:55:11 +01:00
|
|
|
}
|
2017-03-19 16:47:30 +01:00
|
|
|
|
2021-02-28 00:55:11 +01:00
|
|
|
export function remove_deactivated_user_from_all_streams(user_id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const all_subs = stream_data.get_unsorted_subs();
|
2018-08-04 19:44:31 +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 sub of all_subs) {
|
2020-07-08 22:28:35 +02:00
|
|
|
if (stream_data.is_user_subscribed(sub.stream_id, user_id)) {
|
2021-01-12 21:38:01 +01:00
|
|
|
peer_data.remove_subscriber(sub.stream_id, user_id);
|
2021-07-09 15:51:31 +02:00
|
|
|
stream_settings_ui.update_subscribers_ui(sub);
|
2018-08-04 19:44:31 +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
|
|
|
}
|
2021-02-28 00:55:11 +01:00
|
|
|
}
|