Skip to main content

jmap_chat_client/methods/
space_invite.rs

1//! JMAP Chat — SpaceInvite/* method implementations on SessionClient.
2//!
3//! Spec: JMAP Chat draft §4.17 (SpaceInvite/get, /changes, /set).
4
5use jmap_types::{Id, State};
6
7use super::{ChangesResponse, GetResponse, SetResponse, SpaceInviteCreateInput};
8
9impl super::SessionClient {
10    /// Fetch SpaceInvite objects by IDs (JMAP Chat §4.17 SpaceInvite/get).
11    ///
12    /// If `ids` is `None`, returns all SpaceInvite objects for the account.
13    /// Pass `properties: None` to return all fields.
14    pub async fn space_invite_get(
15        &self,
16        ids: Option<&[Id]>,
17        properties: Option<&[&str]>,
18    ) -> Result<GetResponse<jmap_chat_types::SpaceInvite>, jmap_base_client::ClientError> {
19        let (api_url, account_id) = self.session_parts()?;
20        // Omit `ids` / `properties` when None — see the matching comment on
21        // `chat_get` for the rationale (consistent with set/changes/query).
22        let mut args = serde_json::json!({ "accountId": account_id });
23        if let Some(id_slice) = ids {
24            args["ids"] = serde_json::to_value(id_slice).expect("Id slice Serialize is infallible");
25        }
26        if let Some(props) = properties {
27            args["properties"] = serde_json::Value::Array(
28                props.iter().copied().map(serde_json::Value::from).collect(),
29            );
30        }
31        let req = super::build_request("SpaceInvite/get", args, super::USING_CHAT);
32        let resp = self.call_internal(api_url, &req).await?;
33        jmap_base_client::extract_response(&resp, super::CALL_ID)
34    }
35
36    /// Fetch changes to SpaceInvite objects since `since_state` (RFC 8620 §5.2 / SpaceInvite/changes).
37    ///
38    /// If `has_more_changes` is true in the response, call again with `new_state`
39    /// as `since_state` until the flag is false.
40    pub async fn space_invite_changes(
41        &self,
42        since_state: &State,
43        max_changes: Option<u64>,
44    ) -> Result<ChangesResponse, jmap_base_client::ClientError> {
45        // Defence-in-depth: see `chat_changes`.
46        if since_state.as_ref().is_empty() {
47            return Err(jmap_base_client::ClientError::InvalidArgument(
48                "space_invite_changes: since_state may not be empty".into(),
49            ));
50        }
51        let (api_url, account_id) = self.session_parts()?;
52        let mut args = serde_json::json!({
53            "accountId": account_id,
54            "sinceState": since_state,
55        });
56        if let Some(mc) = max_changes {
57            args["maxChanges"] = mc.into();
58        }
59        let req = super::build_request("SpaceInvite/changes", args, super::USING_CHAT);
60        let resp = self.call_internal(api_url, &req).await?;
61        jmap_base_client::extract_response(&resp, super::CALL_ID)
62    }
63
64    /// Create a SpaceInvite (RFC 8620 §5.3 / SpaceInvite/set create).
65    ///
66    /// When `input.client_id` is `None`, a ULID is generated automatically.
67    pub async fn space_invite_create(
68        &self,
69        input: &SpaceInviteCreateInput<'_>,
70    ) -> Result<SetResponse, jmap_base_client::ClientError> {
71        let (api_url, account_id) = self.session_parts()?;
72        let mut obj = serde_json::json!({ "spaceId": input.space_id });
73        if let Some(ch) = input.default_channel_id {
74            obj["defaultChannelId"] = ch.as_ref().into();
75        }
76        if let Some(ea) = input.expires_at {
77            obj["expiresAt"] = ea.as_ref().into();
78        }
79        if let Some(mu) = input.max_uses {
80            obj["maxUses"] = mu.into();
81        }
82        let client_id = super::resolve_client_id(input.client_id);
83        let args = serde_json::json!({
84            "accountId": account_id,
85            "create": { client_id: obj },
86        });
87        let req = super::build_request("SpaceInvite/set", args, super::USING_CHAT);
88        let resp = self.call_internal(api_url, &req).await?;
89        jmap_base_client::extract_response(&resp, super::CALL_ID)
90    }
91
92    /// Destroy SpaceInvite objects (RFC 8620 §5.3 / SpaceInvite/set destroy).
93    ///
94    /// `ids` must be non-empty; the guard fires before any network call.
95    pub async fn space_invite_destroy(
96        &self,
97        ids: &[Id],
98    ) -> Result<SetResponse, jmap_base_client::ClientError> {
99        if ids.is_empty() {
100            return Err(jmap_base_client::ClientError::InvalidArgument(
101                "space_invite_destroy: ids may not be empty".into(),
102            ));
103        }
104        let (api_url, account_id) = self.session_parts()?;
105        let args = serde_json::json!({
106            "accountId": account_id,
107            "destroy": ids,
108        });
109        let req = super::build_request("SpaceInvite/set", args, super::USING_CHAT);
110        let resp = self.call_internal(api_url, &req).await?;
111        jmap_base_client::extract_response(&resp, super::CALL_ID)
112    }
113}