Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

stream_data: Refactored function to return sub and can_sub streams.

palashb018198a04ce8e7
diff --git a/web/src/stream_data.ts b/web/src/stream_data.ts
index 16a0547fae4..e0ce1a9f9e0 100644
--- a/web/src/stream_data.ts
+++ b/web/src/stream_data.ts
@@ -388,11 +388,15 @@ export function muted_stream_ids(): number[] {
.map((sub) => sub.stream_id);
}
-export function get_subscribed_streams_for_user(user_id: number): StreamSubscription[] {
+export function get_streams_for_user(user_id: number): {
+ subscribed: StreamSubscription[];
+ can_subscribe: StreamSubscription[];
+} {
// Note that we only have access to subscribers of some streams
// depending on our role.
const all_subs = get_unsorted_subs();
const subscribed_subs = [];
+ const can_subscribe_subs = [];
for (const sub of all_subs) {
if (!can_view_subscribers(sub)) {
// Private streams that we have been removed from appear
@@ -402,10 +406,15 @@ export function get_subscribed_streams_for_user(user_id: number): StreamSubscrip
}
if (is_user_subscribed(sub.stream_id, user_id)) {
subscribed_subs.push(sub);
+ } else if (can_subscribe_others(sub)) {
+ can_subscribe_subs.push(sub);
}
}
- return subscribed_subs;
+ return {
+ subscribed: subscribed_subs,
+ can_subscribe: can_subscribe_subs,
+ };
}
export function get_invite_stream_data(): InviteStreamData[] {
diff --git a/web/src/user_profile.js b/web/src/user_profile.js
index a10870386f3..180e7d73942 100644
--- a/web/src/user_profile.js
+++ b/web/src/user_profile.js
@@ -197,7 +197,7 @@ export function show_user_profile(user, default_tab_key = "profile-tab") {
const profile_data = page_params.custom_profile_fields
.map((f) => get_custom_profile_field_data(user, f, field_types))
.filter((f) => f.name !== undefined);
- const user_streams = stream_data.get_subscribed_streams_for_user(user.user_id);
+ const user_streams = stream_data.get_streams_for_user(user.user_id).subscribed;
const groups_of_user = user_groups.get_user_groups_of_user(user.user_id);
const args = {
user_id: user.user_id,
diff --git a/web/tests/stream_data.test.js b/web/tests/stream_data.test.js
index 2032aae6810..3d931918bb8 100644
--- a/web/tests/stream_data.test.js
+++ b/web/tests/stream_data.test.js
@@ -201,7 +201,7 @@ test("basics", () => {
]);
});
-test("get_subscribed_streams_for_user", () => {
+test("get_streams_for_user", () => {
const denmark = {
subscribed: true,
color: "blue",
@@ -227,7 +227,16 @@ test("get_subscribed_streams_for_user", () => {
is_muted: true,
invite_only: true,
};
- const subs = [denmark, social, test];
+ const world = {
+ color: "blue",
+ name: "world",
+ stream_id: 4,
+ is_muted: false,
+ invite_only: false,
+ history_public_to_subscribers: false,
+ stream_post_policy: stream_data.stream_post_policy_values.admins.code,
+ };
+ const subs = [denmark, social, test, world];
for (const sub of subs) {
stream_data.add_sub(sub);
}
@@ -235,6 +244,7 @@ test("get_subscribed_streams_for_user", () => {
peer_data.set_subscribers(denmark.stream_id, [me.user_id, test_user.user_id]);
peer_data.set_subscribers(social.stream_id, [test_user.user_id]);
peer_data.set_subscribers(test.stream_id, [test_user.user_id]);
+ peer_data.set_subscribers(world.stream_id, [me.user_id]);
// test_user is subscribed to all three streams, but current user (me)
// gets only two because of subscriber visibility policy of stream:
@@ -243,10 +253,15 @@ test("get_subscribed_streams_for_user", () => {
// user is a guest.
// #test: current user is no longer subscribed to a private stream, so
// he can not see whether test_user is subscribed to it.
- assert.deepEqual(stream_data.get_subscribed_streams_for_user(test_user.user_id), [
+ assert.deepEqual(stream_data.get_streams_for_user(test_user.user_id).subscribed, [
denmark,
social,
]);
+ 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]);
+ page_params.is_admin = false;
});
test("renames", () => {