From 180ed2e50ff8ccd52074ed5d65e17a9e7e32a6fa Mon Sep 17 00:00:00 2001 From: Sahil Batra Date: Wed, 18 Oct 2023 13:34:05 +0530 Subject: [PATCH] user_profile: Show streams to subscribe for user's own profile. We did not show the streams to subscribe in dropdown in user profile modal when user is not allowed to subscribe others even in the user's own profile modal which is not correct. This commit fixes it to show the streams in dropdown for user's own profile irrespective of whether user is allowed to subscribe others or not. --- web/src/stream_data.ts | 10 +++++++++- web/tests/stream_data.test.js | 27 +++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) 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", () => {