zulip/static/js/stream_events.js

145 lines
5.0 KiB
JavaScript
Raw Normal View History

// In theory, this function should apply the account-level defaults,
// however, they are only called after a manual override, so
// doing so is unnecessary with the current code. Ideally, we'd do a
// refactor to address that, however.
function update_stream_setting(sub, value, setting) {
const setting_checkbox = $("#" + setting + "_" + sub.stream_id);
setting_checkbox.prop("checked", value);
sub[setting] = value;
2017-03-19 16:03:07 +01:00
}
exports.update_property = function (stream_id, property, value, other_values) {
const sub = stream_data.get_sub_by_id(stream_id);
2017-03-19 16:03:07 +01:00
if (sub === undefined) {
// This isn't a stream we know about, so ignore it.
blueslip.warn("Update for an unknown subscription", {stream_id: stream_id,
property: property,
value: value});
2017-03-19 16:03:07 +01:00
return;
}
switch (property) {
case 'color':
stream_color.update_stream_color(sub, value, {update_historical: true});
break;
case 'in_home_view':
stream_muting.update_is_muted(sub, !value);
2017-03-19 16:03:07 +01:00
break;
case 'desktop_notifications':
case 'audible_notifications':
case 'push_notifications':
case 'email_notifications':
case 'wildcard_mentions_notify':
update_stream_setting(sub, value, property);
settings_notifications.update_page();
break;
2017-03-19 16:03:07 +01:00
case 'name':
subs.update_stream_name(sub, value);
break;
case 'description':
subs.update_stream_description(sub, value, other_values.rendered_description);
2017-03-19 16:03:07 +01:00
break;
case 'email_address':
sub.email_address = value;
break;
case 'pin_to_top':
update_stream_setting(sub, value, property);
2017-03-19 16:03:07 +01:00
stream_list.refresh_pinned_or_unpinned_stream(sub);
break;
case 'invite_only':
subs.update_stream_privacy(sub, {
invite_only: value,
history_public_to_subscribers: other_values.history_public_to_subscribers,
});
break;
case 'stream_post_policy':
subs.update_stream_post_policy(sub, value);
break;
2017-03-19 16:03:07 +01:00
default:
blueslip.warn("Unexpected subscription property type", {property: property,
value: value});
}
};
// 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).
exports.mark_subscribed = function (sub, subscribers, color) {
if (sub === undefined) {
blueslip.error('Undefined sub passed to mark_subscribed');
return;
}
if (sub.subscribed) {
return;
}
// If the backend sent us a color, use that
if (color !== undefined && sub.color !== color) {
sub.color = color;
stream_color.update_stream_color(sub, color, {update_historical: true});
} 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.
blueslip.warn("Frontend needed to pick a color in mark_subscribed");
color = color_data.pick_color();
subs.set_color(sub.stream_id, color);
}
stream_data.subscribe_myself(sub);
if (subscribers) {
stream_data.set_subscribers(sub, subscribers);
}
stream_data.update_calculated_fields(sub);
if (overlays.streams_open()) {
subs.update_settings_for_subscribed(sub);
}
if (narrow_state.is_for_stream_id(sub.stream_id)) {
current_msg_list.update_trailing_bookend();
}
// Update unread counts as the new stream in sidebar might
// need its unread counts re-calculated
2017-03-19 22:43:38 +01:00
message_util.do_unread_count_updates(message_list.all.all_messages());
$(document).trigger($.Event('subscription_add_done.zulip', {sub: sub}));
};
exports.mark_unsubscribed = function (sub) {
if (sub === undefined) {
// We don't know about this stream
return;
} else if (sub.subscribed) {
stream_data.unsubscribe_myself(sub);
stream_data.update_calculated_fields(sub);
if (overlays.streams_open()) {
subs.update_settings_for_unsubscribed(sub);
}
} else {
// Already unsubscribed
return;
}
if (narrow_state.is_for_stream_id(sub.stream_id)) {
current_msg_list.update_trailing_bookend();
}
$(document).trigger($.Event('subscription_remove_done.zulip', {sub: sub}));
};
exports.remove_deactivated_user_from_all_streams = function (user_id) {
const all_subs = stream_data.get_unsorted_subs();
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) {
if (stream_data.is_user_subscribed(sub.name, user_id)) {
stream_data.remove_subscriber(sub.name, user_id);
subs.rerender_subscriptions_settings(sub);
}
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
}
};
window.stream_events = exports;