stream_data: Refactored function to return sub and can_sub streams.

This commit refactors the 'get_subscribed_streams_for_user' function
to return an object with two keys: one for the subscribed streams
and another for the streams the user can subscribe to. The node
tests have been updated accordingly.

Renamed 'get_subscribed_streams_for_user' to 'get_streams_for_user'.
This commit is contained in:
palashb01 2023-07-29 04:19:51 +05:30 committed by Tim Abbott
parent 94fe762ea5
commit 8198a04ce8
3 changed files with 30 additions and 6 deletions

View File

@ -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[] {

View File

@ -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,

View File

@ -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", () => {