diff --git a/web/src/stream_data.ts b/web/src/stream_data.ts index 186a8e1cc8..3cd3f8f3c2 100644 --- a/web/src/stream_data.ts +++ b/web/src/stream_data.ts @@ -347,7 +347,7 @@ export function get_streams_for_user(user_id: number): { } if (is_user_subscribed(sub.stream_id, user_id)) { subscribed_subs.push(sub); - } else if (can_subscribe_others(sub)) { + } else if (can_subscribe_user(sub, user_id)) { can_subscribe_subs.push(sub); } } @@ -532,6 +532,14 @@ export function can_subscribe_others(sub: StreamSubscription): boolean { ); } +export function can_subscribe_user(sub: StreamSubscription, user_id: number): boolean { + if (people.is_my_user_id(user_id)) { + return can_toggle_subscription(sub); + } + + return can_subscribe_others(sub); +} + export function can_unsubscribe_others(sub: StreamSubscription): boolean { // Whether the current user has permission to remove other users // from the stream. Organization administrators can remove users diff --git a/web/tests/stream_data.test.js b/web/tests/stream_data.test.js index 7a9956ccab..217fd13333 100644 --- a/web/tests/stream_data.test.js +++ b/web/tests/stream_data.test.js @@ -237,7 +237,16 @@ test("get_streams_for_user", () => { history_public_to_subscribers: false, stream_post_policy: settings_config.stream_post_policy_values.admins.code, }; - const subs = [denmark, social, test, world]; + const errors = { + color: "green", + name: "errors", + stream_id: 5, + is_muted: false, + invite_only: false, + history_public_to_subscribers: false, + stream_post_policy: settings_config.stream_post_policy_values.admins.code, + }; + const subs = [denmark, social, test, world, errors]; for (const sub of subs) { stream_data.add_sub(sub); } @@ -247,6 +256,10 @@ test("get_streams_for_user", () => { peer_data.set_subscribers(test.stream_id, [test_user.user_id]); peer_data.set_subscribers(world.stream_id, [me.user_id]); + page_params.realm_invite_to_stream_policy = + settings_config.common_policy_values.by_admins_only.code; + assert.deepEqual(stream_data.get_streams_for_user(me.user_id).can_subscribe, [social, errors]); + // test_user is subscribed to all three streams, but current user (me) // gets only two because of subscriber visibility policy of stream: // #denmark: current user is subscribed to it so he can see its subscribers. @@ -261,8 +274,18 @@ test("get_streams_for_user", () => { assert.deepEqual(stream_data.get_streams_for_user(test_user.user_id).can_subscribe, []); // Verify can subscribe if we're an administrator. page_params.is_admin = true; - assert.deepEqual(stream_data.get_streams_for_user(test_user.user_id).can_subscribe, [world]); + assert.deepEqual(stream_data.get_streams_for_user(test_user.user_id).can_subscribe, [ + world, + errors, + ]); page_params.is_admin = false; + + page_params.realm_invite_to_stream_policy = + settings_config.common_policy_values.by_members.code; + assert.deepEqual(stream_data.get_streams_for_user(test_user.user_id).can_subscribe, [ + world, + errors, + ]); }); test("renames", () => {