Skip to main content

jmap_chat_client/methods/
space_ban.rs

1//! JMAP Chat — SpaceBan/* method implementations on SessionClient.
2//!
3//! Spec: JMAP Chat draft §4.18 (SpaceBan/get, /changes, /set).
4
5use jmap_types::{Id, State};
6
7use super::{ChangesResponse, GetResponse, SetResponse, SpaceBanCreateInput};
8
9impl super::SessionClient {
10    /// Fetch SpaceBan objects by IDs (JMAP Chat §4.18 SpaceBan/get).
11    ///
12    /// If `ids` is `None`, returns all SpaceBan objects for the account.
13    /// Pass `properties: None` to return all fields.
14    pub async fn space_ban_get(
15        &self,
16        ids: Option<&[Id]>,
17        properties: Option<&[&str]>,
18    ) -> Result<GetResponse<jmap_chat_types::SpaceBan>, 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("SpaceBan/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 SpaceBan objects since `since_state` (RFC 8620 §5.2 / SpaceBan/changes).
37    ///
38    /// Only members with `"ban"` permission in the Space see all changes;
39    /// other members see changes to their own bans only.
40    pub async fn space_ban_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_ban_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("SpaceBan/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 SpaceBan (RFC 8620 §5.3 / SpaceBan/set create).
65    ///
66    /// When `input.client_id` is `None`, a ULID is generated automatically.
67    pub async fn space_ban_create(
68        &self,
69        input: &SpaceBanCreateInput<'_>,
70    ) -> Result<SetResponse, jmap_base_client::ClientError> {
71        let (api_url, account_id) = self.session_parts()?;
72        let mut create_obj = serde_json::json!({
73            "spaceId": input.space_id,
74            "userId": input.user_id,
75        });
76        if let Some(r) = input.reason {
77            create_obj["reason"] = r.into();
78        }
79        if let Some(ea) = input.expires_at {
80            create_obj["expiresAt"] = ea.as_ref().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: create_obj },
86        });
87        let req = super::build_request("SpaceBan/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 SpaceBan objects (RFC 8620 §5.3 / SpaceBan/set destroy).
93    ///
94    /// `ids` must be non-empty; the guard fires before any network call.
95    pub async fn space_ban_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_ban_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("SpaceBan/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}