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.

streams: Add function to get subscribed streams of a user.

m-e-l-u-h-a-n433452a9b38f
diff --git a/frontend_tests/node_tests/stream_data.js b/frontend_tests/node_tests/stream_data.js
index 7eb7645d36d..555515023dd 100644
--- a/frontend_tests/node_tests/stream_data.js
+++ b/frontend_tests/node_tests/stream_data.js
@@ -11,6 +11,7 @@ const {page_params} = require("../zjsunit/zpage_params");
const color_data = zrequire("color_data");
const stream_topic_history = zrequire("stream_topic_history");
+const peer_data = zrequire("peer_data");
const people = zrequire("people");
const sub_store = zrequire("sub_store");
const stream_data = zrequire("stream_data");
@@ -23,6 +24,12 @@ const me = {
user_id: 100,
};
+const test_user = {
+ email: "[email protected]",
+ full_name: "Test User",
+ user_id: 101,
+};
+
// set up user data
function contains_sub(subs, sub) {
return subs.some((s) => s.name === sub.name);
@@ -128,6 +135,54 @@ test("basics", () => {
assert.equal(stream_data.slug_to_name("99whatever"), "99whatever");
});
+test("get_subscribed_streams_for_user", () => {
+ const denmark = {
+ subscribed: true,
+ color: "blue",
+ name: "Denmark",
+ stream_id: 1,
+ is_muted: true,
+ invite_only: true,
+ history_public_to_subscribers: true,
+ };
+ const social = {
+ color: "red",
+ name: "social",
+ stream_id: 2,
+ is_muted: false,
+ invite_only: false,
+ history_public_to_subscribers: false,
+ stream_post_policy: stream_data.stream_post_policy_values.admins.code,
+ };
+ const test = {
+ color: "yellow",
+ name: "test",
+ stream_id: 3,
+ is_muted: true,
+ invite_only: true,
+ };
+ const subs = [denmark, social, test];
+ for (const sub of subs) {
+ stream_data.add_sub(sub);
+ }
+
+ 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]);
+
+ // 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.
+ // #social: current user is can get this as neither this is invite onyl nor current
+ // 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), [
+ denmark,
+ social,
+ ]);
+});
+
test("renames", () => {
const id = 42;
let sub = {
diff --git a/static/js/popovers.js b/static/js/popovers.js
index 4faffb1b2fe..8cf74760aef 100644
--- a/static/js/popovers.js
+++ b/static/js/popovers.js
@@ -433,7 +433,7 @@ export function show_user_profile(user) {
const profile_data = page_params.custom_profile_fields
.map((f) => get_custom_profile_field_data(user, f, field_types, dateFormat))
.filter((f) => f.name !== undefined);
- const user_streams = stream_data.subscribed_subs();
+ const user_streams = stream_data.get_subscribed_streams_for_user(user.user_id);
const groups_of_user = user_groups.get_user_groups_of_user(user.user_id);
const args = {
full_name: user.full_name,
diff --git a/static/js/stream_data.js b/static/js/stream_data.js
index 9ca05ed2c58..cdfcd7e6460 100644
--- a/static/js/stream_data.js
+++ b/static/js/stream_data.js
@@ -379,6 +379,26 @@ export function subscribed_stream_ids() {
return subscribed_subs().map((sub) => sub.stream_id);
}
+export function get_subscribed_streams_for_user(user_id) {
+ // Note that we only have access to subscribers of some streams
+ // depending on our role.
+ const all_subs = get_unsorted_subs();
+ const subscribed_subs = [];
+ for (const sub of all_subs) {
+ if (!can_view_subscribers(sub)) {
+ // Private streams that we have been removed from appear
+ // in get_unsorted_subs; we don't attempt to check their
+ // subscribers (which would trigger a warning).
+ continue;
+ }
+ if (is_user_subscribed(sub.stream_id, user_id)) {
+ subscribed_subs.push(sub);
+ }
+ }
+
+ return subscribed_subs;
+}
+
export function get_invite_stream_data() {
function get_data(sub) {
return {