jmap-chat-client 0.1.1

JMAP Chat HTTP client — auth-agnostic, WebSocket and SSE support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
//! JMAP Chat — Space/* method implementations on SessionClient.
//!
//! Each method follows the standard five-step pattern:
//!   1. Validate arguments (defence-in-depth empty-state guards).
//!   2. Call `self.session_parts()?` → `(api_url, account_id)`.
//!   3. Build args JSON with `serde_json::json!({…})`.
//!   4. Call `build_request(method_name, args, USING_CHAT)`.
//!   5. Call `self.call_internal(api_url, &req).await?`.
//!   6. Call `jmap_base_client::extract_response(&resp, CALL_ID)?`.

use jmap_types::{Id, PatchObject, State};

use super::{
    ChangesResponse, GetResponse, QueryChangesResponse, QueryResponse, SetResponse,
    SpaceAddCategoryInput, SpaceAddChannelInput, SpaceAddMemberInput, SpaceAddRoleInput,
    SpaceCreateInput, SpaceJoinInput, SpaceJoinResponse, SpacePatch, SpaceQueryInput,
    SpaceUpdateCategoryInput, SpaceUpdateChannelInput, SpaceUpdateMemberInput,
    SpaceUpdateRoleInput,
};

impl super::SessionClient {
    /// Fetch Space objects by IDs (RFC 8620 §5.1 / JMAP Chat §Space/get).
    ///
    /// If `ids` is `None`, the server returns all Spaces for the account.
    /// Pass `properties: None` to return all fields.
    pub async fn space_get(
        &self,
        ids: Option<&[Id]>,
        properties: Option<&[&str]>,
    ) -> Result<GetResponse<jmap_chat_types::Space>, jmap_base_client::ClientError> {
        let (api_url, account_id) = self.session_parts()?;
        // Omit `ids` / `properties` when None — see the matching comment on
        // `chat_get` for the rationale (consistent with set/changes/query).
        let mut args = serde_json::json!({ "accountId": account_id });
        if let Some(id_slice) = ids {
            args["ids"] = serde_json::to_value(id_slice).expect("Id slice Serialize is infallible");
        }
        if let Some(props) = properties {
            args["properties"] = serde_json::Value::Array(
                props.iter().copied().map(serde_json::Value::from).collect(),
            );
        }
        let req = super::build_request("Space/get", args, super::USING_CHAT);
        let resp = self.call_internal(api_url, &req).await?;
        jmap_base_client::extract_response(&resp, super::CALL_ID)
    }

    /// Fetch changes to Space objects since `since_state` (RFC 8620 §5.2 / Space/changes).
    ///
    /// If `has_more_changes` is true in the response, call again with `new_state`
    /// as `since_state` until the flag is false.
    pub async fn space_changes(
        &self,
        since_state: &State,
        max_changes: Option<u64>,
    ) -> Result<ChangesResponse, jmap_base_client::ClientError> {
        // Defence-in-depth: see `chat_changes`.
        if since_state.as_ref().is_empty() {
            return Err(jmap_base_client::ClientError::InvalidArgument(
                "space_changes: since_state may not be empty".into(),
            ));
        }
        let (api_url, account_id) = self.session_parts()?;
        let mut args = serde_json::json!({
            "accountId": account_id,
            "sinceState": since_state,
        });
        if let Some(mc) = max_changes {
            args["maxChanges"] = mc.into();
        }
        let req = super::build_request("Space/changes", args, super::USING_CHAT);
        let resp = self.call_internal(api_url, &req).await?;
        jmap_base_client::extract_response(&resp, super::CALL_ID)
    }

    /// Destroy Space objects (RFC 8620 §5.3 / Space/set destroy).
    ///
    /// Permanently removes the listed Space IDs from the account.
    /// `ids` must be non-empty; the guard fires before any network call.
    pub async fn space_destroy(
        &self,
        ids: &[Id],
    ) -> Result<SetResponse, jmap_base_client::ClientError> {
        if ids.is_empty() {
            return Err(jmap_base_client::ClientError::InvalidArgument(
                "space_destroy: ids may not be empty".into(),
            ));
        }
        let (api_url, account_id) = self.session_parts()?;
        let args = serde_json::json!({
            "accountId": account_id,
            "destroy": ids,
        });
        let req = super::build_request("Space/set", args, super::USING_CHAT);
        let resp = self.call_internal(api_url, &req).await?;
        jmap_base_client::extract_response(&resp, super::CALL_ID)
    }

    /// Query Space IDs with optional filter (RFC 8620 §5.5 / JMAP Chat §Space/query).
    ///
    /// Only keys that are `Some` in `input` are included in the filter object;
    /// an empty filter is sent as JSON `null`.
    pub async fn space_query(
        &self,
        input: &SpaceQueryInput<'_>,
    ) -> Result<QueryResponse, jmap_base_client::ClientError> {
        let (api_url, account_id) = self.session_parts()?;
        let mut filter = serde_json::Map::new();
        if let Some(n) = input.filter_name {
            filter.insert("name".into(), n.into());
        }
        if let Some(p) = input.filter_is_public {
            filter.insert("isPublic".into(), p.into());
        }
        let filter_val = if filter.is_empty() {
            serde_json::Value::Null
        } else {
            serde_json::Value::Object(filter)
        };
        let mut args = serde_json::json!({
            "accountId": account_id,
            "filter": filter_val,
        });
        if let Some(p) = input.position {
            args["position"] = p.into();
        }
        if let Some(l) = input.limit {
            args["limit"] = l.into();
        }
        let req = super::build_request("Space/query", args, super::USING_CHAT);
        let resp = self.call_internal(api_url, &req).await?;
        jmap_base_client::extract_response(&resp, super::CALL_ID)
    }

    /// Fetch query-result changes for Space since `since_query_state`
    /// (RFC 8620 §5.6 / Space/queryChanges).
    ///
    /// Returns which Space IDs were removed from or added to the query result set
    /// since the given state. `max_changes` may be `None`.
    pub async fn space_query_changes(
        &self,
        since_query_state: &State,
        max_changes: Option<u64>,
    ) -> Result<QueryChangesResponse, jmap_base_client::ClientError> {
        // Defence-in-depth: see `chat_changes`.
        if since_query_state.as_ref().is_empty() {
            return Err(jmap_base_client::ClientError::InvalidArgument(
                "space_query_changes: since_query_state may not be empty".into(),
            ));
        }
        let (api_url, account_id) = self.session_parts()?;
        let mut args = serde_json::json!({
            "accountId": account_id,
            "sinceQueryState": since_query_state,
        });
        if let Some(mc) = max_changes {
            args["maxChanges"] = mc.into();
        }
        let req = super::build_request("Space/queryChanges", args, super::USING_CHAT);
        let resp = self.call_internal(api_url, &req).await?;
        jmap_base_client::extract_response(&resp, super::CALL_ID)
    }

    /// Create a new Space (JMAP Chat §Space/set create).
    ///
    /// When `input.client_id` is `None`, a ULID is generated automatically.
    /// The server maps the creation key to the server-assigned Space id in
    /// `SetResponse.created`.
    pub async fn space_create(
        &self,
        input: &SpaceCreateInput<'_>,
    ) -> Result<SetResponse, jmap_base_client::ClientError> {
        if input.name.is_empty() {
            return Err(jmap_base_client::ClientError::InvalidArgument(
                "space_create: name may not be empty".into(),
            ));
        }
        let (api_url, account_id) = self.session_parts()?;
        let client_id = super::resolve_client_id(input.client_id);
        let mut create_obj = serde_json::json!({ "name": input.name });
        if let Some(d) = input.description {
            create_obj["description"] = d.into();
        }
        if let Some(b) = input.icon_blob_id {
            create_obj["iconBlobId"] = b.as_ref().into();
        }
        let args = serde_json::json!({
            "accountId": account_id,
            "create": { client_id: create_obj },
        });
        let req = super::build_request("Space/set", args, super::USING_CHAT);
        let resp = self.call_internal(api_url, &req).await?;
        jmap_base_client::extract_response(&resp, super::CALL_ID)
    }

    /// Join a Space via invite code or direct ID (JMAP Chat §Space/join).
    ///
    /// `input` selects exactly one join path; the enum makes invalid inputs
    /// unrepresentable at the type level.
    pub async fn space_join(
        &self,
        input: &SpaceJoinInput<'_>,
    ) -> Result<SpaceJoinResponse, jmap_base_client::ClientError> {
        let (api_url, account_id) = self.session_parts()?;
        let mut args = serde_json::json!({ "accountId": account_id });
        match input {
            SpaceJoinInput::InviteCode(ic) => {
                if ic.is_empty() {
                    return Err(jmap_base_client::ClientError::InvalidArgument(
                        "space_join: invite_code may not be empty".into(),
                    ));
                }
                args["inviteCode"] = (*ic).into();
            }
            SpaceJoinInput::SpaceId(sid) => {
                args["spaceId"] = sid.as_ref().into();
            }
        }
        let req = super::build_request("Space/join", args, super::USING_CHAT);
        let resp = self.call_internal(api_url, &req).await?;
        jmap_base_client::extract_response(&resp, super::CALL_ID)
    }

    /// Update Space properties (JMAP Chat §Space/set update).
    ///
    /// Issues an `update` operation patching only the fields present in `patch`.
    /// Use `Patch::Set(v)` to set nullable fields, `Patch::Clear` to null-clear
    /// them, and `Patch::Keep` (default) to leave them unchanged. Slice fields
    /// default to `None` for no-change.
    ///
    /// Supports the full set of semantic mutation keys from JMAP Chat §Space/set:
    /// metadata (`name`, `description`, `iconBlobId`, `isPublic`,
    /// `isPubliclyPreviewable`); members (`addMembers`, `removeMembers`,
    /// `updateMembers`, `manage_members`); channels (`addChannels`,
    /// `removeChannels`, `updateChannels`, `manage_channels`); roles
    /// (`addRoles`, `removeRoles`, `updateRoles`, `manage_roles`); and
    /// categories (`addCategories`, `removeCategories`, `updateCategories`,
    /// `manage_channels`).
    pub async fn space_update(
        &self,
        id: &Id,
        patch: &SpacePatch<'_>,
    ) -> Result<SetResponse, jmap_base_client::ClientError> {
        let (api_url, account_id) = self.session_parts()?;
        let mut patch_map = serde_json::Map::new();

        if let Some(n) = patch.name {
            patch_map.insert("name".into(), n.into());
        }
        if let Some(entry) = patch
            .description
            .map_entry()
            .map_err(jmap_base_client::ClientError::Parse)?
        {
            patch_map.insert("description".into(), entry);
        }
        if let Some(entry) = patch
            .icon_blob_id
            .map_entry()
            .map_err(jmap_base_client::ClientError::Parse)?
        {
            patch_map.insert("iconBlobId".into(), entry);
        }
        if let Some(ip) = patch.is_public {
            patch_map.insert("isPublic".into(), ip.into());
        }
        if let Some(ipp) = patch.is_publicly_previewable {
            patch_map.insert("isPubliclyPreviewable".into(), ipp.into());
        }
        if let Some(members) = patch.add_members {
            if !members.is_empty() {
                let arr: Vec<serde_json::Value> = members
                    .iter()
                    .map(
                        |m: &SpaceAddMemberInput<'_>| -> Result<
                            serde_json::Value,
                            jmap_base_client::ClientError,
                        > {
                            let mut obj = serde_json::json!({ "id": m.id });
                            if let Some(role_ids) = m.role_ids {
                                obj["roleIds"] = serde_json::to_value(role_ids)
                                    .expect("Id slice Serialize is infallible");
                            }
                            Ok(obj)
                        },
                    )
                    .collect::<Result<Vec<_>, _>>()?;
                patch_map.insert("addMembers".into(), serde_json::Value::Array(arr));
            }
        }
        if let Some(rm) = patch.remove_members {
            if !rm.is_empty() {
                patch_map.insert(
                    "removeMembers".into(),
                    serde_json::to_value(rm).expect("Id slice Serialize is infallible"),
                );
            }
        }
        if let Some(um) = patch.update_members {
            if !um.is_empty() {
                let arr: Vec<serde_json::Value> = um
                    .iter()
                    .map(
                        |u: &SpaceUpdateMemberInput<'_>| -> Result<
                            serde_json::Value,
                            jmap_base_client::ClientError,
                        > {
                            let mut obj = serde_json::json!({ "id": u.id });
                            if let Some(role_ids) = u.role_ids {
                                obj["roleIds"] = serde_json::to_value(role_ids)
                                    .expect("Id slice Serialize is infallible");
                            }
                            if let Some(entry) = u
                                .nick
                                .map_entry()
                                .map_err(jmap_base_client::ClientError::Parse)?
                            {
                                obj["nick"] = entry;
                            }
                            Ok(obj)
                        },
                    )
                    .collect::<Result<Vec<_>, _>>()?;
                patch_map.insert("updateMembers".into(), serde_json::Value::Array(arr));
            }
        }
        if let Some(ac) = patch.add_channels {
            if !ac.is_empty() {
                let arr = ac
                    .iter()
                    .map(|c: &SpaceAddChannelInput<'_>| {
                        if c.name.is_empty() {
                            return Err(jmap_base_client::ClientError::InvalidArgument(
                                "space_update: channel name may not be empty".into(),
                            ));
                        }
                        let mut obj = serde_json::json!({ "name": c.name });
                        if let Some(cat) = c.category_id {
                            obj["categoryId"] = cat.as_ref().into();
                        }
                        if let Some(pos) = c.position {
                            obj["position"] = pos.into();
                        }
                        if let Some(t) = c.topic {
                            obj["topic"] = t.into();
                        }
                        Ok(obj)
                    })
                    .collect::<Result<Vec<serde_json::Value>, jmap_base_client::ClientError>>()?;
                patch_map.insert("addChannels".into(), serde_json::Value::Array(arr));
            }
        }
        if let Some(rc) = patch.remove_channels {
            if !rc.is_empty() {
                patch_map.insert(
                    "removeChannels".into(),
                    serde_json::to_value(rc).expect("Id slice Serialize is infallible"),
                );
            }
        }
        if let Some(uc) = patch.update_channels {
            if !uc.is_empty() {
                let arr = uc
                    .iter()
                    .map(|c: &SpaceUpdateChannelInput<'_>| {
                        let mut obj = serde_json::json!({ "id": c.id });
                        if let Some(n) = c.name {
                            obj["name"] = n.into();
                        }
                        if let Some(entry) = c
                            .topic
                            .map_entry()
                            .map_err(jmap_base_client::ClientError::Parse)?
                        {
                            obj["topic"] = entry;
                        }
                        if let Some(entry) = c
                            .category_id
                            .map_entry()
                            .map_err(jmap_base_client::ClientError::Parse)?
                        {
                            obj["categoryId"] = entry;
                        }
                        if let Some(p) = c.position {
                            obj["position"] = p.into();
                        }
                        if let Some(s) = c.slow_mode_seconds {
                            obj["slowModeSeconds"] = s.into();
                        }
                        if let Some(po) = c.permission_overrides {
                            obj["permissionOverrides"] = serde_json::to_value(po)
                                .map_err(jmap_base_client::ClientError::Parse)?;
                        }
                        Ok(obj)
                    })
                    .collect::<Result<Vec<serde_json::Value>, jmap_base_client::ClientError>>()?;
                patch_map.insert("updateChannels".into(), serde_json::Value::Array(arr));
            }
        }
        if let Some(ar) = patch.add_roles {
            if !ar.is_empty() {
                let arr = ar
                    .iter()
                    .map(|r: &SpaceAddRoleInput<'_>| {
                        if r.name.is_empty() {
                            return Err(jmap_base_client::ClientError::InvalidArgument(
                                "space_update: role name may not be empty".into(),
                            ));
                        }
                        let mut obj = serde_json::json!({
                            "name": r.name,
                            "permissions": r.permissions,
                            "position": r.position,
                        });
                        if let Some(c) = r.color {
                            obj["color"] = c.into();
                        }
                        Ok(obj)
                    })
                    .collect::<Result<Vec<serde_json::Value>, jmap_base_client::ClientError>>()?;
                patch_map.insert("addRoles".into(), serde_json::Value::Array(arr));
            }
        }
        if let Some(rr) = patch.remove_roles {
            if !rr.is_empty() {
                patch_map.insert(
                    "removeRoles".into(),
                    serde_json::to_value(rr).expect("Id slice Serialize is infallible"),
                );
            }
        }
        if let Some(ur) = patch.update_roles {
            if !ur.is_empty() {
                let arr = ur
                    .iter()
                    .map(|r: &SpaceUpdateRoleInput<'_>| {
                        let mut obj = serde_json::json!({ "id": r.id });
                        if let Some(n) = r.name {
                            obj["name"] = n.into();
                        }
                        if let Some(entry) = r
                            .color
                            .map_entry()
                            .map_err(jmap_base_client::ClientError::Parse)?
                        {
                            obj["color"] = entry;
                        }
                        if let Some(perms) = r.permissions {
                            obj["permissions"] = serde_json::Value::Array(
                                perms.iter().copied().map(serde_json::Value::from).collect(),
                            );
                        }
                        if let Some(p) = r.position {
                            obj["position"] = p.into();
                        }
                        Ok(obj)
                    })
                    .collect::<Result<Vec<serde_json::Value>, jmap_base_client::ClientError>>()?;
                patch_map.insert("updateRoles".into(), serde_json::Value::Array(arr));
            }
        }
        if let Some(ac) = patch.add_categories {
            if !ac.is_empty() {
                let arr = ac
                    .iter()
                    .map(|c: &SpaceAddCategoryInput<'_>| {
                        if c.name.is_empty() {
                            return Err(jmap_base_client::ClientError::InvalidArgument(
                                "space_update: category name may not be empty".into(),
                            ));
                        }
                        let mut obj = serde_json::json!({ "name": c.name });
                        if let Some(p) = c.position {
                            obj["position"] = p.into();
                        }
                        if let Some(cids) = c.channel_ids {
                            obj["channelIds"] = serde_json::to_value(cids)
                                .expect("Id slice Serialize is infallible");
                        }
                        Ok(obj)
                    })
                    .collect::<Result<Vec<serde_json::Value>, jmap_base_client::ClientError>>()?;
                patch_map.insert("addCategories".into(), serde_json::Value::Array(arr));
            }
        }
        if let Some(rc) = patch.remove_categories {
            if !rc.is_empty() {
                patch_map.insert(
                    "removeCategories".into(),
                    serde_json::to_value(rc).expect("Id slice Serialize is infallible"),
                );
            }
        }
        if let Some(uc) = patch.update_categories {
            if !uc.is_empty() {
                let arr = uc
                    .iter()
                    .map(|c: &SpaceUpdateCategoryInput<'_>| {
                        let mut obj = serde_json::json!({ "id": c.id });
                        if let Some(n) = c.name {
                            obj["name"] = n.into();
                        }
                        if let Some(p) = c.position {
                            obj["position"] = p.into();
                        }
                        if let Some(cids) = c.channel_ids {
                            obj["channelIds"] = serde_json::to_value(cids)
                                .expect("Id slice Serialize is infallible");
                        }
                        obj
                    })
                    .collect::<Vec<serde_json::Value>>();
                patch_map.insert("updateCategories".into(), serde_json::Value::Array(arr));
            }
        }

        // Wrap the constructed map in a PatchObject (RFC 8620 §5.3) before
        // serializing. Wire bytes are unchanged because PatchObject is
        // #[serde(transparent)]; the typed boundary documents the contract.
        let patch_value = serde_json::Value::Object(PatchObject::from_map(patch_map).into_inner());
        let args = serde_json::json!({
            "accountId": account_id,
            "update": { id.as_ref(): patch_value },
        });
        let req = super::build_request("Space/set", args, super::USING_CHAT);
        let resp = self.call_internal(api_url, &req).await?;
        jmap_base_client::extract_response(&resp, super::CALL_ID)
    }
}