Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
stream_data: Add has_metadata_access function.
Shubham Padia29a1a013254d
diff --git a/web/src/stream_data.ts b/web/src/stream_data.tsindex d08e65f3e34..45e25cb81b0 100644--- a/web/src/stream_data.ts+++ b/web/src/stream_data.ts@@ -477,6 +477,59 @@ export function is_new_stream_announcements_stream_muted(): boolean {return is_muted(realm.realm_new_stream_announcements_stream_id);}+// This function will be true for every case since the server should be+// preventing a StreamSubscription from reaching clients without+// metadata access.+// This function can be used to allow callers to log blueslip errors+// when the client seems to have a group it shouldn't have access to,+// in order to find server bugs.+export function has_metadata_access(sub: StreamSubscription): boolean {+ if (sub.is_web_public) {+ return true;+ }++ if (page_params.is_spectator) {+ return false;+ }++ if (!current_user.is_guest && !sub.invite_only) {+ return true;+ }++ if (sub.subscribed) {+ return true;+ }++ if (can_administer_accessible_channel(sub)) {+ return true;+ }++ // Users that can add other subscribers to a private channel+ // have content access to that channel. Having content access+ // should give them metadata access to that private channel even+ // when unsubscribed.+ const can_add_subscribers = user_groups.is_user_in_setting_group(+ sub.can_add_subscribers_group,+ people.my_current_user_id(),+ );+ if (can_add_subscribers) {+ return true;+ }++ return false;+}++function can_administer_accessible_channel(sub: StreamSubscription): boolean {+ if (current_user.is_admin) {+ return true;+ }++ return user_groups.is_user_in_setting_group(+ sub.can_administer_channel_group,+ people.my_current_user_id(),+ );+}+export function can_toggle_subscription(sub: StreamSubscription): boolean {// You can always remove your subscription if you're subscribed.//diff --git a/web/tests/stream_data.test.cjs b/web/tests/stream_data.test.cjsindex 7aa8881e008..06d704bec74 100644--- a/web/tests/stream_data.test.cjs+++ b/web/tests/stream_data.test.cjs@@ -92,6 +92,14 @@ const students = {direct_subgroup_ids: new Set([]),};+const me_group = {+ name: "Me Group",+ id: 6,+ members: new Set([me.user_id]),+ is_system_group: false,+ direct_subgroup_ids: new Set([]),+};+function test(label, f) {run_test(label, (helpers) => {helpers.override(current_user, "is_admin", false);@@ -108,6 +116,7 @@ function test(label, f) {everyone_group,nobody_group,students,+ me_group,],});f(helpers);@@ -1459,3 +1468,77 @@ test("can_access_stream_email", ({override}) => {page_params.is_spectator = true;assert.equal(stream_data.can_access_stream_email(social), false);});++test("has_metadata_access", ({override}) => {+ const social = {+ subscribed: true,+ color: "red",+ name: "social",+ stream_id: 2,+ is_muted: false,+ invite_only: true,+ history_public_to_subscribers: false,+ can_add_subscribers_group: nobody_group.id,+ can_administer_channel_group: nobody_group.id,+ };++ assert.equal(stream_data.has_metadata_access(social), true);++ social.is_web_public = true;+ assert.equal(stream_data.has_metadata_access(social), true);+ page_params.is_spectator = true;+ assert.equal(stream_data.has_metadata_access(social), true);++ social.is_web_public = false;+ page_params.is_spectator = true;+ assert.equal(stream_data.has_metadata_access(social), false);+ page_params.is_spectator = false;++ override(current_user, "is_admin", false);+ assert.equal(stream_data.has_metadata_access(social), true);++ // A user without the permission to administer a channel should not+ // have metadata access to a private channel when unsubscribed.+ assert.equal(stream_data.has_metadata_access(social), true);+ social.subscribed = false;+ assert.equal(stream_data.has_metadata_access(social), false);++ // Realm admins should have metadata access to a private channel+ // when unsubscribed.+ assert.equal(stream_data.has_metadata_access(social), false);+ override(current_user, "is_admin", true);+ assert.equal(stream_data.has_metadata_access(social), true);+ override(current_user, "is_admin", false);++ // Channel admins should have metadata access to a private channel+ // when unsubscribed.+ assert.equal(stream_data.has_metadata_access(social), false);+ social.can_administer_channel_group = me_group.id;+ assert.equal(stream_data.has_metadata_access(social), true);+ social.can_administer_channel_group = nobody_group.id;++ // Users that can add other subscribers to a private channel+ // have content access to that channel. Having content access+ // should give them metadata access to that private channel even+ // when unsubscribed.+ assert.equal(stream_data.has_metadata_access(social), false);+ social.can_add_subscribers_group = me_group.id;+ assert.equal(stream_data.has_metadata_access(social), true);+ social.can_add_subscribers_group = nobody_group.id;++ // Non-admin and non-guest user should have access to public+ // channel.+ assert.equal(stream_data.has_metadata_access(social), false);+ social.invite_only = false;+ override(current_user, "is_admin", false);+ assert.equal(stream_data.has_metadata_access(social), true);++ // Guest should not have metadata access to a channel they are not+ // subscribed to.+ assert.equal(stream_data.has_metadata_access(social), true);+ override(current_user, "is_guest", true);+ social.subscribed = false;+ assert.equal(stream_data.has_metadata_access(social), false);+ social.subscribed = true;+ assert.equal(stream_data.has_metadata_access(social), true);+});