Skip to content

Commit

Permalink
zulip: check if stream is public
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtwco committed Nov 13, 2024
1 parent 6d11429 commit 4dd146d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
34 changes: 28 additions & 6 deletions src/zulip/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,21 @@ impl ZulipApi {
Ok(response)
}

/// Is a Zulip stream private?
pub(crate) fn is_stream_private(&self, stream_id: u64) -> anyhow::Result<bool> {
#[derive(Deserialize)]
struct OneZulipStream {
stream: ZulipStream,
}

Ok(self
.req(reqwest::Method::GET, &format!("/streams/{stream_id}"), None)?
.error_for_status()?
.json::<OneZulipStream>()?
.stream
.invite_only)
}

pub(crate) fn update_user_group_members(
&self,
user_group_id: u64,
Expand Down Expand Up @@ -224,13 +239,19 @@ impl ZulipApi {
Ok(())
};

let subscriptions = format!("{{\"name\": \"{stream_name}\", \"description\": \"\"}}");
let add_ids = serialize_as_array(add_ids);
submit(reqwest::Method::POST, subscriptions, add_ids)?;
if !add_ids.is_empty() {
let subscriptions = format!("{{\"name\": \"{stream_name}\", \"description\": \"\"}}");
let add_ids = serialize_as_array(add_ids);
submit(reqwest::Method::POST, subscriptions, add_ids)?;
}

let subscriptions = format!("[{stream_name}]");
let remove_ids = serialize_as_array(remove_ids);
submit(reqwest::Method::DELETE, subscriptions, remove_ids)
if !remove_ids.is_empty() {
let subscriptions = format!("[{stream_name}]");
let remove_ids = serialize_as_array(remove_ids);
submit(reqwest::Method::DELETE, subscriptions, remove_ids)?;
}

Ok(())
}

/// Perform a request against the Zulip API
Expand Down Expand Up @@ -302,6 +323,7 @@ struct ZulipStreams {
pub(crate) struct ZulipStream {
pub(crate) stream_id: u64,
pub(crate) name: String,
pub(crate) invite_only: bool,
}

/// Membership of a Zulip stream
Expand Down
19 changes: 14 additions & 5 deletions src/zulip/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ impl SyncZulip {
));
}
};
let is_stream_private = self.zulip_controller.is_stream_private(stream_id)?;

let existing_members = self.zulip_controller.stream_members_from_id(stream_id)?;
log::debug!(
Expand All @@ -136,11 +137,15 @@ impl SyncZulip {
.filter(|i| !existing_members.contains(i))
.copied()
.collect::<Vec<_>>();
let remove_ids = existing_members
.iter()
.filter(|i| !member_ids.contains(i))
.copied()
.collect::<Vec<_>>();
let remove_ids = if is_stream_private {
existing_members
.iter()
.filter(|i| !member_ids.contains(i))
.copied()
.collect::<Vec<_>>()
} else {
vec![]
};
if add_ids.is_empty() && remove_ids.is_empty() {
log::debug!("'{stream_name}' stream ({stream_id}) does not need to be updated");
Ok(None)
Expand Down Expand Up @@ -463,4 +468,8 @@ impl ZulipController {
fn stream_members_from_id(&self, stream_id: u64) -> anyhow::Result<Vec<u64>> {
self.zulip_api.get_stream_members(stream_id)
}

fn is_stream_private(&self, stream_id: u64) -> anyhow::Result<bool> {
self.zulip_api.is_stream_private(stream_id)
}
}

0 comments on commit 4dd146d

Please sign in to comment.