mirror of
https://github.com/zulip/zulip.git
synced 2025-11-15 11:22:04 +00:00
refactor: Extract can_view_subscribers().
We also remove some needless uses of the calculated field in the node tests.
This commit is contained in:
@@ -1149,7 +1149,6 @@ test_ui("needs_subscribe_warning", () => {
|
||||
const sub = {
|
||||
stream_id: 110,
|
||||
name: "stream",
|
||||
can_access_subscribers: true,
|
||||
};
|
||||
|
||||
stream_data.add_sub(sub);
|
||||
|
||||
@@ -59,7 +59,6 @@ run_test("set_focused_recipient", () => {
|
||||
stream_id: 101,
|
||||
name: "social",
|
||||
subscribed: true,
|
||||
can_access_subscribers: true,
|
||||
};
|
||||
|
||||
compose_fade.set_focused_recipient("stream");
|
||||
|
||||
@@ -172,7 +172,6 @@ const sweden_stream = {
|
||||
description: "Cold, mountains and home decor.",
|
||||
stream_id: 1,
|
||||
subscribed: true,
|
||||
can_access_subscribers: true,
|
||||
};
|
||||
const denmark_stream = {
|
||||
name: "Denmark",
|
||||
|
||||
@@ -16,7 +16,6 @@ const {page_params} = require("../zjsunit/zpage_params");
|
||||
const peer_data = zrequire("peer_data");
|
||||
const people = zrequire("people");
|
||||
const stream_data = zrequire("stream_data");
|
||||
const stream_settings_data = zrequire("stream_settings_data");
|
||||
|
||||
page_params.is_admin = false;
|
||||
page_params.realm_users = [];
|
||||
@@ -114,7 +113,6 @@ test("subscribers", () => {
|
||||
]);
|
||||
|
||||
peer_data.set_subscribers(stream_id, [me.user_id, fred.user_id, george.user_id]);
|
||||
stream_settings_data.update_calculated_fields(sub);
|
||||
assert(stream_data.is_user_subscribed(stream_id, me.user_id));
|
||||
assert(stream_data.is_user_subscribed(stream_id, fred.user_id));
|
||||
assert(stream_data.is_user_subscribed(stream_id, george.user_id));
|
||||
@@ -182,7 +180,6 @@ test("subscribers", () => {
|
||||
|
||||
// Verify that we noop and don't crash when unsubscribed.
|
||||
sub.subscribed = false;
|
||||
stream_settings_data.update_calculated_fields(sub);
|
||||
peer_data.add_subscriber(stream_id, brutus.user_id);
|
||||
assert.equal(stream_data.is_user_subscribed(stream_id, brutus.user_id), true);
|
||||
peer_data.remove_subscriber(stream_id, brutus.user_id);
|
||||
@@ -196,7 +193,6 @@ test("subscribers", () => {
|
||||
2,
|
||||
);
|
||||
sub.invite_only = true;
|
||||
stream_settings_data.update_calculated_fields(sub);
|
||||
assert.equal(stream_data.is_user_subscribed(stream_id, brutus.user_id), undefined);
|
||||
peer_data.remove_subscriber(stream_id, brutus.user_id);
|
||||
assert.equal(stream_data.is_user_subscribed(stream_id, brutus.user_id), undefined);
|
||||
|
||||
@@ -402,8 +402,6 @@ test("remove_deactivated_user_from_all_streams", () => {
|
||||
const subs_stub = make_stub();
|
||||
subs.update_subscribers_ui = subs_stub.f;
|
||||
|
||||
dev_help.can_access_subscribers = true;
|
||||
|
||||
// assert starting state
|
||||
assert(!stream_data.is_user_subscribed(dev_help.stream_id, george.user_id));
|
||||
|
||||
|
||||
@@ -515,6 +515,11 @@ export function can_change_permissions(sub) {
|
||||
return page_params.is_admin && (!sub.invite_only || sub.subscribed);
|
||||
}
|
||||
|
||||
export function can_view_subscribers(sub) {
|
||||
// Guest users can't access subscribers of any(public or private) non-subscribed streams.
|
||||
return page_params.is_admin || sub.subscribed || (!page_params.is_guest && !sub.invite_only);
|
||||
}
|
||||
|
||||
export function is_subscribed(stream_name) {
|
||||
const sub = get_sub(stream_name);
|
||||
return sub !== undefined && sub.subscribed;
|
||||
@@ -592,7 +597,7 @@ export function maybe_get_stream_name(stream_id) {
|
||||
|
||||
export function is_user_subscribed(stream_id, user_id) {
|
||||
const sub = get_sub_by_id(stream_id);
|
||||
if (sub === undefined || !sub.can_access_subscribers) {
|
||||
if (sub === undefined || !can_view_subscribers(sub)) {
|
||||
// If we don't know about the stream, or we ourselves cannot access subscriber list,
|
||||
// so we return undefined (treated as falsy if not explicitly handled).
|
||||
blueslip.warn(
|
||||
|
||||
@@ -54,11 +54,10 @@ export function update_calculated_fields(sub) {
|
||||
sub.should_display_subscription_button = stream_data.can_toggle_subscription(sub);
|
||||
sub.should_display_preview_button = stream_data.can_preview(sub);
|
||||
sub.can_change_stream_permissions = stream_data.can_change_permissions(sub);
|
||||
// User can add other users to stream if stream is public or user is subscribed to stream.
|
||||
// Guest users can't access subscribers of any(public or private) non-subscribed streams.
|
||||
sub.can_access_subscribers =
|
||||
page_params.is_admin || sub.subscribed || (!page_params.is_guest && !sub.invite_only);
|
||||
sub.can_access_subscribers = stream_data.can_view_subscribers(sub);
|
||||
|
||||
sub.preview_url = hash_util.by_stream_uri(sub.stream_id);
|
||||
// User can add other users to stream if stream is public or user is subscribed to stream.
|
||||
sub.can_add_subscribers = !page_params.is_guest && (!sub.invite_only || sub.subscribed);
|
||||
sub.is_old_stream = sub.stream_weekly_traffic !== null;
|
||||
if (sub.rendered_description !== undefined) {
|
||||
|
||||
@@ -151,7 +151,7 @@ export function update_subscribers_list(sub) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!sub.can_access_subscribers) {
|
||||
if (!stream_data.can_view_subscribers(sub)) {
|
||||
$(".subscriber_list_settings_container").hide();
|
||||
} else {
|
||||
const subscribers = peer_data.get_subscribers(sub.stream_id);
|
||||
|
||||
Reference in New Issue
Block a user