mirror of
https://github.com/zulip/zulip.git
synced 2025-11-02 21:13:36 +00:00
refactor: Remove stream_data.update_subscribers_count.
This removes a bit of complexity. If a piece of settings code needs to render a stream with subscribers, it just asks for it. We no longer have the brittle, action-at-a-distance mechanism of mutating the subscriber count on to the stream_data version of a sub. Stream subs are pretty small, so making copies of them is cheap, and the blueslip timings from the previous commit can help confirm that. There is some discussion of putting `subscriber_count` on the Stream model, which may eventually get us away from tracking it in `peer_data.js`, but we will cross that bridge when we get there. See https://github.com/zulip/zulip/issues/17101 for more details.
This commit is contained in:
@@ -352,6 +352,25 @@ exports.get_unsorted_subs = function () {
|
||||
return Array.from(stream_info.values());
|
||||
};
|
||||
|
||||
exports.get_sub_for_settings = (sub) => {
|
||||
// Since we make a copy of the sub here, it may eventually
|
||||
// make sense to get the other calculated fields here as
|
||||
// well, instead of using update_calculated_fields everywhere.
|
||||
const sub_count = peer_data.get_subscriber_count(sub.stream_id);
|
||||
return {
|
||||
...sub,
|
||||
subscriber_count: sub_count,
|
||||
};
|
||||
};
|
||||
|
||||
function get_subs_for_settings(subs) {
|
||||
// We may eventually add subscribers to the subs here, rather than
|
||||
// delegating, so that we can more efficiently compute subscriber counts
|
||||
// (in bulk). If that plan appears to have been aborted, feel free to
|
||||
// inline this.
|
||||
return subs.map((sub) => exports.get_sub_for_settings(sub));
|
||||
}
|
||||
|
||||
exports.get_updated_unsorted_subs = function () {
|
||||
// This function is expensive in terms of calculating
|
||||
// some values (particularly stream counts) but avoids
|
||||
@@ -368,7 +387,7 @@ exports.get_updated_unsorted_subs = function () {
|
||||
all_subs = all_subs.filter((sub) => sub.subscribed);
|
||||
}
|
||||
|
||||
return all_subs;
|
||||
return get_subs_for_settings(all_subs);
|
||||
};
|
||||
|
||||
exports.num_subscribed_subs = function () {
|
||||
@@ -423,13 +442,6 @@ exports.get_colors = function () {
|
||||
return exports.subscribed_subs().map((sub) => sub.color);
|
||||
};
|
||||
|
||||
exports.update_subscribers_count = function (sub) {
|
||||
// This is part of an unfortunate legacy hack, where we
|
||||
// put calculated fields onto the sub object instead of
|
||||
// letting callers build their own objects.
|
||||
sub.subscriber_count = peer_data.get_subscriber_count(sub.stream_id);
|
||||
};
|
||||
|
||||
exports.update_stream_email_address = function (sub, email) {
|
||||
sub.email_address = email;
|
||||
};
|
||||
@@ -462,6 +474,8 @@ exports.receives_notifications = function (stream_id, notification_name) {
|
||||
};
|
||||
|
||||
exports.update_calculated_fields = function (sub) {
|
||||
// Note that we don't calculate subscriber counts here.
|
||||
|
||||
sub.is_realm_admin = page_params.is_admin;
|
||||
// Admin can change any stream's name & description either stream is public or
|
||||
// private, subscribed or unsubscribed.
|
||||
@@ -485,7 +499,6 @@ exports.update_calculated_fields = function (sub) {
|
||||
if (sub.rendered_description !== undefined) {
|
||||
sub.rendered_description = sub.rendered_description.replace("<p>", "").replace("</p>", "");
|
||||
}
|
||||
exports.update_subscribers_count(sub);
|
||||
|
||||
// Apply the defaults for our notification settings for rendering.
|
||||
for (const setting of settings_config.stream_specific_notification_settings) {
|
||||
@@ -786,7 +799,7 @@ exports.get_streams_for_settings_page = function () {
|
||||
exports.update_calculated_fields(sub);
|
||||
}
|
||||
|
||||
return all_subs;
|
||||
return get_subs_for_settings(all_subs);
|
||||
};
|
||||
|
||||
exports.sort_for_stream_settings = function (stream_ids, order) {
|
||||
|
||||
Reference in New Issue
Block a user