jmap-chat-server 0.3.0

JMAP Chat extension method handlers — backend-agnostic, plugs into jmap-server Dispatcher
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
//! Chat/* method handlers (draft-atwood-jmap-chat-00 §Chat).
//!
//! # Wire-shape contract
//!
//! Every `handle_*` function in this module conforms to the canonical JMAP
//! method shape. The `args: serde_json::Value` parameter MUST be a JSON
//! Object whose fields match the corresponding RFC 8620 §5 method shape
//! (`/get` → §5.1, `/changes` → §5.2, `/set` → §5.3,
//! `/query` → §5.5, `/queryChanges` → §5.6), with the type-specific
//! arguments defined by draft-atwood-jmap-chat-00 §Chat. The returned
//! `Value` is the corresponding method-response object per the same
//! section refs. `Chat/typing` (draft §Chat) is a Chat-specific signal
//! method with its own request/response shape.
//!
//! The returned `HandlerOutput::extra_invocations` carries any invocations
//! that this handler injected into the request stream (RFC 8620 §6.3);
//! for the handlers in this module the vector is **always empty**.
//!
//! Each handler returns `Err(JmapError)` for method-level failures
//! (`accountNotFound`, `invalidArguments`, `stateMismatch`, `serverFail`,
//! `unsupportedFilter`, `unsupportedSort`, `cannotCalculateChanges` —
//! per RFC 8620 §3.6 and §5). Per-target failures inside `/set` surface
//! in the `notCreated` / `notUpdated` / `notDestroyed` maps within
//! `Ok((Value, ...))`, not as `Err`.

use std::collections::{HashMap, HashSet};

use jmap_chat_types::query::ChatComparator;
use jmap_chat_types::vocabulary::SPEC_CHAT_MEMBER_ROLES;
use jmap_chat_types::{Chat, ChatKind, ChatMember};
use jmap_types::{Id, JmapError, PatchObject, UTCDate};
use serde::Deserialize;
use serde_json::{json, Value};

use crate::backend::{BackendSetError, ChatBackend, SetError, SetErrorType};
use crate::helpers::{
    enforce_max_objects_in_set, extract_account_id, finalize_set_response, now_utc_string,
    reject_unsupported_sort, set_error_value, SetAccumulators,
};
use crate::helpers::{mistyped_create_props, PropType, PLACEHOLDER_ID};
use jmap_server::{server_fail_from_backend, server_fail_value_from_backend, HandlerOutput};

/// `Chat/set` update keys that change a group Chat's member list
/// (draft-atwood-jmap-chat-00 §Chat/set "Updating a Chat").
const MEMBER_PATCH_KEYS: &[&str] = &["addMembers", "removeMembers", "updateMemberRoles"];

/// `Chat/set` update keys, beyond [`MEMBER_PATCH_KEYS`], that a closed
/// group rejects (draft-atwood-jmap-chat-00 §Chat `closedAt`).
const SHARED_GROUP_KEYS: &[&str] = &["name", "description", "avatarBlobId", "pinnedMessageIds"];

/// One `addMembers` or `updateMemberRoles` entry.
#[derive(Deserialize)]
struct MemberRoleEntry {
    id: Id,
    role: Option<String>,
}

/// Apply the member patch keys in `patch` to `chat.members` on behalf of
/// `owner_id` and return the resulting member list, and whether the
/// patch closes the group.
///
/// Per draft-atwood-jmap-chat-00 §Chat/set "Updating a Chat":
/// - a malformed entry, or a `role` other than `"admin"` or `"member"`,
///   is `invalidProperties` naming the key; an `updateMemberRoles` entry
///   must carry a `role` and name a current member;
/// - "all require admin role, except leaving the group": a non-admin may
///   only send `removeMembers` listing just their own id, else
///   `forbidden`. `owner_id` is the accountId, which references the owner
///   (draft §Terminology), so a non-admin owner leaves with
///   `removeMembers: [accountId]`; the keys apply to group chats only;
/// - more members than `max_members` after `addMembers` is `overQuota`;
/// - "If the owner is the last admin and other members remain, leaving
///   closes the group": the caller was an admin, is removed, and members
///   but no admin remain.
///
/// Per the implementer guide's "Deployments SHOULD avoid admin-less open
/// groups by refusing to let the last admin demote themselves via
/// `updateMemberRoles`", an `updateMemberRoles` patch that leaves members
/// but no admin is `forbidden`.
#[allow(clippy::result_large_err)]
fn apply_member_patch_keys(
    chat: &Chat,
    owner_id: &Id,
    patch: &serde_json::Map<String, Value>,
    max_members: u32,
) -> Result<(Vec<ChatMember>, bool), SetError> {
    fn parse<T: serde::de::DeserializeOwned>(
        patch: &serde_json::Map<String, Value>,
        key: &str,
    ) -> Result<Option<T>, SetError> {
        patch
            .get(key)
            .map(|v| serde_json::from_value(v.clone()))
            .transpose()
            .map_err(|_| SetError::new(SetErrorType::InvalidProperties).with_properties([key]))
    }
    let valid_role = |role: &str| SPEC_CHAT_MEMBER_ROLES.contains(&role);
    let invalid = |key: &str| SetError::new(SetErrorType::InvalidProperties).with_properties([key]);
    let forbidden = |desc: &str| SetError::new(SetErrorType::Forbidden).with_description(desc);

    let add: Option<Vec<MemberRoleEntry>> = parse(patch, "addMembers")?;
    let remove: Option<Vec<Id>> = parse(patch, "removeMembers")?;
    let update: Option<Vec<MemberRoleEntry>> = parse(patch, "updateMemberRoles")?;
    if add
        .iter()
        .flatten()
        .any(|e| e.role.as_deref().is_some_and(|r| !valid_role(r)))
    {
        return Err(invalid("addMembers"));
    }
    if update
        .iter()
        .flatten()
        .any(|e| !e.role.as_deref().is_some_and(valid_role))
    {
        return Err(invalid("updateMemberRoles"));
    }

    if chat.kind != ChatKind::Group {
        return Err(forbidden("member patch keys apply to group chats only"));
    }
    let mut members = chat.members.clone().unwrap_or_default();
    let is_admin = members
        .iter()
        .any(|m| m.id == *owner_id && m.role == "admin");
    let self_leave = add.is_none()
        && update.is_none()
        && remove.as_deref() == Some(std::slice::from_ref(owner_id));
    if !is_admin && !self_leave {
        return Err(forbidden("only group chat admins may change members"));
    }

    if let Some(add) = &add {
        let now = UTCDate::from(now_utc_string().as_ref());
        for entry in add {
            if members.iter().all(|m| m.id != entry.id) {
                let role = entry.role.clone().unwrap_or_else(|| "member".to_owned());
                let mut m = ChatMember::new(entry.id.clone(), role, now.clone());
                m.invited_by = Some(owner_id.clone());
                members.push(m);
            }
        }
        if members.len() > max_members as usize {
            return Err(SetError::new(SetErrorType::OverQuota)
                .with_description("group chat member limit exceeded"));
        }
    }
    if let Some(remove) = &remove {
        members.retain(|m| !remove.contains(&m.id));
    }
    if let Some(update) = &update {
        for entry in update {
            let member = members
                .iter_mut()
                .find(|m| m.id == entry.id)
                .ok_or_else(|| invalid("updateMemberRoles"))?;
            member.role = entry.role.clone().expect("role checked above");
        }
        if !members.is_empty() && members.iter().all(|m| m.role != "admin") {
            return Err(forbidden("the last admin may not demote themselves"));
        }
    }
    let closes = is_admin
        && members.iter().all(|m| m.id != *owner_id)
        && !members.is_empty()
        && members.iter().all(|m| m.role != "admin");
    Ok((members, closes))
}

// ---------------------------------------------------------------------------
// Chat/get
// ---------------------------------------------------------------------------

/// Handle a `Chat/get` method call (draft-atwood-jmap-chat-00 §Chat).
///
/// `args` is the RFC 8620 §5.1 `/get` request shape (`accountId`, optional
/// `ids`, optional `properties`); the returned `Value` is the §5.1
/// `/get` response shape (`accountId`, `state`, `list`, `notFound`).
///
/// Returns `(response_args, extra_invocations)`. The extra list is always empty.
// NOTE: properties forwarded via handle_get
pub async fn handle_chat_get<B: ChatBackend>(
    backend: &B,
    caller: &B::CallerCtx,
    args: Value,
) -> Result<HandlerOutput, JmapError> {
    jmap_server::handlers::handle_get::<Chat, B>(backend, caller, args).await
}

// ---------------------------------------------------------------------------
// Chat/changes
// ---------------------------------------------------------------------------

/// Handle a `Chat/changes` method call (draft-atwood-jmap-chat-00 §Chat).
///
/// `args` is the RFC 8620 §5.2 `/changes` request shape (`accountId`,
/// `sinceState`, optional `maxChanges`); the returned `Value` is the
/// §5.2 `/changes` response shape (`accountId`, `oldState`, `newState`,
/// `hasMoreChanges`, `created`, `updated`, `destroyed`).
///
/// Returns `(response_args, extra_invocations)`. The extra list is always empty.
pub async fn handle_chat_changes<B: ChatBackend>(
    backend: &B,
    caller: &B::CallerCtx,
    args: Value,
) -> Result<HandlerOutput, JmapError> {
    jmap_server::handlers::handle_changes::<Chat, B>(backend, caller, args).await
}

// ---------------------------------------------------------------------------
// Chat/query
// ---------------------------------------------------------------------------

/// Handle a `Chat/query` method call (draft-atwood-jmap-chat-00 §Chat).
///
/// `args` is the RFC 8620 §5.5 `/query` request shape (`accountId`, optional
/// `filter`, optional `sort`, optional `position` / `anchor` /
/// `anchorOffset`, optional `limit`, optional `calculateTotal`); the
/// returned `Value` is the §5.5 `/query` response shape (`accountId`,
/// `queryState`, `canCalculateChanges`, `position`, `ids`, optional
/// `total`, optional `limit`).
///
/// Unknown filter clauses and sort properties are refused with
/// `unsupportedFilter` / `unsupportedSort` (RFC 8620 §5.5).
///
/// Returns `(response_args, extra_invocations)`. The extra list is always empty.
pub async fn handle_chat_query<B: ChatBackend>(
    backend: &B,
    caller: &B::CallerCtx,
    args: Value,
) -> Result<HandlerOutput, JmapError> {
    reject_unsupported_sort::<ChatComparator>(args.get("sort"))?;
    jmap_server::handlers::handle_query::<Chat, B>(backend, caller, args).await
}

// ---------------------------------------------------------------------------
// Chat/queryChanges
// ---------------------------------------------------------------------------

/// Handle a `Chat/queryChanges` method call (draft-atwood-jmap-chat-00 §Chat).
///
/// `args` is the RFC 8620 §5.6 `/queryChanges` request shape (`accountId`,
/// optional `filter`, optional `sort`, `sinceQueryState`, optional
/// `maxChanges`, optional `upToId`, optional `calculateTotal`); the
/// returned `Value` is the §5.6 `/queryChanges` response shape
/// (`accountId`, `oldQueryState`, `newQueryState`, optional `total`,
/// `removed`, `added`).
///
/// Returns `(response_args, extra_invocations)`. The extra list is always empty.
pub async fn handle_chat_query_changes<B: ChatBackend>(
    backend: &B,
    caller: &B::CallerCtx,
    args: Value,
) -> Result<HandlerOutput, JmapError> {
    jmap_server::handlers::handle_query_changes::<Chat, B>(backend, caller, args).await
}

// ---------------------------------------------------------------------------
// Chat/set
// ---------------------------------------------------------------------------

/// Handle a `Chat/set` method call (draft-atwood-jmap-chat-00 §Chat).
///
/// `args` is the RFC 8620 §5.3 `/set` request shape (`accountId`, optional
/// `ifInState`, optional `create` / `update` / `destroy` maps); the
/// returned `Value` is the §5.3 `/set` response shape (`accountId`,
/// `oldState`, `newState`, plus the per-operation `created` /
/// `notCreated` / `updated` / `notUpdated` / `destroyed` / `notDestroyed`
/// maps).
///
/// Validation enforced here (not in the backend):
/// - `kind` is required on create.
/// - `direct` chats require `contactId`.
/// - `channel` chats are `forbidden` (created via `Space/set` `addChannels`).
/// - `id`, `createdAt`, `unreadCount`, `closedAt`, `activeCallId` are
///   server-set and rejected in updates.
/// - `pinnedMessageIds` updates are gated by
///   [`ChatBackend::may_set_pinned_messages`].
///
/// Returns `(response_args, extra_invocations)`. The extra list is always empty.
pub async fn handle_chat_set<B: ChatBackend>(
    backend: &B,
    caller: &B::CallerCtx,
    args: Value,
) -> Result<HandlerOutput, JmapError> {
    let (account_id, mut args) = extract_account_id(args)?;

    // RFC 8620 §5.3 maxObjectsInSet (bd:JMAP-ayoz.41.3). Reject
    // unbounded /set batches before touching the storage layer.
    enforce_max_objects_in_set(&args, backend.max_objects_in_set(caller, &account_id))?;

    let old_state = backend
        .get_state::<Chat>(caller, &account_id)
        .await
        .map_err(|e| server_fail_from_backend(&e))?;

    if let Some(if_in_state) = args.get("ifInState").and_then(|v| v.as_str()) {
        if if_in_state != old_state.as_ref() {
            return Err(JmapError::state_mismatch());
        }
    }

    let mut created = serde_json::Map::new();
    let mut not_created = serde_json::Map::new();
    let mut updated = serde_json::Map::new();
    let mut not_updated = serde_json::Map::new();
    let mut destroyed_list: Vec<Value> = Vec::new();
    let mut not_destroyed = serde_json::Map::new();
    let mut mutated = false;

    // -----------------------------------------------------------------------
    // create
    // -----------------------------------------------------------------------
    if let Some(create_map) = args.get("create").and_then(|v| v.as_object()) {
        // Only pay the cost of a full get_objects fetch when the batch contains
        // at least one Direct create (JMAP-63k.4).
        let has_direct_create = create_map.values().any(|v| {
            v.get("kind")
                .and_then(|k| k.as_str())
                .is_some_and(|s| s.eq_ignore_ascii_case("direct"))
        });

        // Fetch all existing chats once before the loop. For a batch of N
        // creates against an account with K existing chats this is O(K + N)
        // reads (one full-account scan + a HashSet lookup per create) rather
        // than the naive O(N * K) of one fetch per create. The per-batch fetch
        // still scales linearly in the account's chat count K, so production
        // backends serving accounts with large K should push the
        // contact-id-uniqueness check into a typed query method on
        // ChatBackend rather than rely on this hoisted scan (tracked by
        // JMAP-63k.9). Skipped entirely for non-Direct batches.
        let (existing_chats, mut known_direct_contact_ids): (Vec<Chat>, HashSet<String>) =
            if has_direct_create {
                let (chats, _) = backend
                    .get_objects::<Chat>(caller, &account_id, None, None)
                    .await
                    .map_err(|e| server_fail_from_backend(&e))?;
                let known = chats
                    .iter()
                    .filter(|c| c.kind == ChatKind::Direct)
                    .filter_map(|c| c.contact_id.as_ref().map(|id| id.as_ref().to_owned()))
                    .collect();
                (chats, known)
            } else {
                (Vec::new(), HashSet::new())
            };

        // Maps contactId -> assigned new_id for Direct chats successfully
        // created earlier in this batch.  Used to resolve intra-batch duplicates
        // without a re-fetch (JMAP-63k.12).
        let mut batch_direct_ids: HashMap<String, Id> = HashMap::new();

        for (create_id, obj_val) in create_map {
            // Optional properties: a wrongly-typed value is invalidProperties
            // (RFC 8620 §5.3), not a silent absence.
            if let Some(err) = mistyped_create_props(
                obj_val,
                &[
                    ("contactId", PropType::Str),
                    ("name", PropType::Str),
                    ("description", PropType::Str),
                    ("spaceId", PropType::Str),
                    ("muted", PropType::Bool),
                    ("receiveTypingIndicators", PropType::Bool),
                ],
            ) {
                not_created.insert(create_id.clone(), err);
                continue;
            }
            // kind is required.
            let Some(kind_str) = obj_val
                .get("kind")
                .and_then(|v| v.as_str())
                .map(str::to_owned)
            else {
                not_created.insert(
                    create_id.clone(),
                    json!({ "type": "invalidProperties", "properties": ["kind"] }),
                );
                continue;
            };

            // ChatKind::Other(_) is the deserialize-from-wire forward-compat
            // catch-all: it preserves round-trip fidelity when reading data
            // produced by a future server that knows a new spec variant. It
            // is NOT a legitimate value on /set create — a client supplying
            // an unrecognised kind must be rejected with invalidProperties,
            // otherwise junk Chats end up in storage and break every
            // downstream kind-dispatched invariant (kind-specific required
            // field checks, /get response shape, Message/set chatId
            // resolution, etc.).
            let kind = match serde_json::from_value::<ChatKind>(Value::String(kind_str)) {
                Ok(k) if !matches!(k, ChatKind::Other(_)) => k,
                _ => {
                    not_created.insert(
                        create_id.clone(),
                        json!({ "type": "invalidProperties", "properties": ["kind"] }),
                    );
                    continue;
                }
            };

            // Validate kind-specific required fields and extract per-kind state.
            // `direct_contact_id_str` is Some(id) for Direct chats and None for
            // all other kinds — it simultaneously encodes the "is direct" flag and
            // the contact ID, avoiding a bool+Option pair whose invariant (Some iff
            // direct) would otherwise be implicit.
            let direct_contact_id_str: Option<String> = match &kind {
                ChatKind::Direct => {
                    let Some(contact_id_str) = obj_val
                        .get("contactId")
                        .and_then(|v| v.as_str())
                        .map(str::to_owned)
                    else {
                        not_created.insert(
                            create_id.clone(),
                            json!({ "type": "invalidProperties", "properties": ["contactId"] }),
                        );
                        continue;
                    };

                    // Pre-check: reject if a direct chat with this contactId is
                    // already known from the hoisted fetch.
                    if let Some(dup) = existing_chats.iter().find(|c| {
                        c.kind == ChatKind::Direct
                            && c.contact_id.as_ref().map(|id| id.as_ref())
                                == Some(contact_id_str.as_str())
                    }) {
                        not_created.insert(
                            create_id.clone(),
                            serde_json::to_value(
                                SetError::new(SetErrorType::AlreadyExists)
                                    .with_existing_id(dup.id.clone()),
                            )
                            .expect("derive(Serialize) on plain data is infallible"),
                        );
                        continue;
                    }
                    // Also check contactIds created earlier in this same batch.
                    // Resolve the canonical id from the hoisted pre-fetch data or
                    // from the batch map — no re-fetch required (JMAP-63k.12).
                    if known_direct_contact_ids.contains(&contact_id_str) {
                        // Try the pre-fetch snapshot first (pre-existing chat).
                        let canonical_id = if let Some(c) = existing_chats.iter().find(|c| {
                            c.kind == ChatKind::Direct
                                && c.contact_id.as_ref().map(|id| id.as_ref())
                                    == Some(contact_id_str.as_str())
                        }) {
                            c.id.clone()
                        } else if let Some(id) = batch_direct_ids.get(&contact_id_str) {
                            // Created earlier in this batch.
                            id.clone()
                        } else {
                            // Should not happen: known_direct_contact_ids is only
                            // populated from existing_chats and batch_direct_ids.
                            not_created.insert(
                                create_id.clone(),
                                json!({
                                    "type": "serverFail",
                                    "description": "direct chat for contact not found after concurrent operation; retry"
                                }),
                            );
                            continue;
                        };
                        not_created.insert(
                            create_id.clone(),
                            serde_json::to_value(
                                SetError::new(SetErrorType::AlreadyExists)
                                    .with_existing_id(canonical_id),
                            )
                            .expect("derive(Serialize) on plain data is infallible"),
                        );
                        continue;
                    }
                    Some(contact_id_str)
                }
                // §Chat/set: "Creating a Chat with `kind: "channel"` via
                // `Chat/set` is not supported; the server MUST return a
                // SetError of type `forbidden`."
                ChatKind::Channel => {
                    not_created.insert(create_id.clone(), json!({ "type": "forbidden" }));
                    continue;
                }
                _ => None,
            };

            // Group create: "`memberIds` (String[], required): ChatContact.ids
            // of additional initial members." Absent is tolerated for
            // backward compatibility; a non-String[] value is rejected.
            let member_ids: Option<Vec<Id>> = if kind == ChatKind::Group {
                match obj_val.get("memberIds") {
                    None => Some(Vec::new()),
                    Some(v) => match serde_json::from_value::<Vec<String>>(v.clone()) {
                        Ok(ids) => Some(ids.into_iter().map(Id::from).collect()),
                        Err(_) => {
                            not_created.insert(
                                create_id.clone(),
                                json!({ "type": "invalidProperties", "properties": ["memberIds"] }),
                            );
                            continue;
                        }
                    },
                }
            } else {
                None
            };

            let now_str = now_utc_string();
            let now: UTCDate = UTCDate::from(now_str.as_ref());

            // "The server [...] adds the creator to `members` with at least
            // one role granting permissions sufficient to administer the
            // chat": the creator is `"admin"`, each distinct memberIds entry
            // is `"member"` invited by the creator. The creator is the owner,
            // referenced by the accountId (§Terminology).
            let members: Option<Vec<ChatMember>> = member_ids.map(|ids| {
                let creator: Id = account_id.clone();
                let mut members = vec![ChatMember::new(
                    creator.clone(),
                    "admin".to_owned(),
                    now.clone(),
                )];
                for id in ids {
                    if members.iter().all(|m| m.id != id) {
                        let mut m = ChatMember::new(id, "member".to_owned(), now.clone());
                        m.invited_by = Some(creator.clone());
                        members.push(m);
                    }
                }
                members
            });

            let contact_id: Option<Id> = obj_val
                .get("contactId")
                .and_then(|v| v.as_str())
                .map(Id::from);
            let name: Option<String> = obj_val
                .get("name")
                .and_then(|v| v.as_str())
                .map(str::to_owned);
            let description: Option<String> = obj_val
                .get("description")
                .and_then(|v| v.as_str())
                .map(str::to_owned);
            let space_id: Option<Id> = obj_val
                .get("spaceId")
                .and_then(|v| v.as_str())
                .map(Id::from);
            let muted: bool = obj_val
                .get("muted")
                .and_then(|v| v.as_bool())
                .unwrap_or(false);
            let receive_typing_indicators: bool = obj_val
                .get("receiveTypingIndicators")
                .and_then(|v| v.as_bool())
                .unwrap_or(true);

            let mut chat = Chat::new(
                // Id::from: wire-boundary validation deferred to JMAP-k9va; backend rejects unknown IDs.
                Id::from(PLACEHOLDER_ID),
                kind,
                now,
                0,
                vec![],
                muted,
                receive_typing_indicators,
            );
            chat.contact_id = contact_id;
            chat.name = name;
            chat.description = description;
            chat.space_id = space_id;
            chat.members = members;

            match backend
                .create_object::<Chat>(caller, &account_id, create_id, chat)
                .await
            {
                Ok((new_id, created_obj)) => {
                    // For Direct chats: re-fetch to detect a concurrent duplicate
                    // (optimistic create-then-validate, required for JMAP-0c9).
                    // We fetch all chats because the backend does not currently
                    // expose a filter-by-kind query; a tighter fetch (Direct only)
                    // would be preferable but requires backend support (JMAP-63k.9).
                    if let Some(contact_id_str) = direct_contact_id_str.as_deref() {
                        let (current_chats, _) = backend
                            .get_objects::<Chat>(caller, &account_id, None, None)
                            .await
                            .map_err(|e| server_fail_from_backend(&e))?;
                        let duplicates: Vec<&Chat> = current_chats
                            .iter()
                            .filter(|c| {
                                c.kind == ChatKind::Direct
                                    && c.contact_id.as_ref().map(|id| id.as_ref())
                                        == Some(contact_id_str)
                            })
                            .collect();
                        if duplicates.len() > 1 {
                            // Race occurred: pick lexicographically smallest id
                            // as the canonical winner.
                            let canonical_id: Id = duplicates
                                .iter()
                                .map(|c| c.id.as_ref())
                                .min()
                                .map(Id::from)
                                .unwrap_or_else(|| new_id.clone());
                            if new_id != canonical_id {
                                // We lost the race: destroy our copy.
                                if let Err(e) = backend
                                    .destroy_object::<Chat>(caller, &account_id, &new_id)
                                    .await
                                {
                                    // Cleanup failed — the duplicate is still
                                    // live. Return a retryable server error
                                    // rather than alreadyExists with a
                                    // potentially inconsistent state.
                                    //
                                    // Route through server_fail_value_from_backend
                                    // to redact backend Display text from the
                                    // wire description per workspace redaction
                                    // discipline.
                                    not_created.insert(
                                        create_id.clone(),
                                        server_fail_value_from_backend(&e),
                                    );
                                    continue;
                                }
                                // Cleanup succeeded: report alreadyExists
                                // pointing to the canonical winner.
                                not_created.insert(
                                    create_id.clone(),
                                    serde_json::to_value(
                                        SetError::new(SetErrorType::AlreadyExists)
                                            .with_existing_id(canonical_id),
                                    )
                                    .unwrap_or_else(|e| server_fail_value_from_backend(&e)),
                                );
                                continue;
                            }
                            // We won the race (our id is canonical): fall through
                            // to success path below.
                        }
                        // Exactly one (or we won): record contactId as known so
                        // subsequent creates in this batch are pre-checked.
                        known_direct_contact_ids.insert(contact_id_str.to_owned());
                        batch_direct_ids.insert(contact_id_str.to_owned(), new_id.clone());
                    }
                    mutated = true;
                    created.insert(
                        create_id.clone(),
                        serde_json::to_value(&created_obj)
                            .expect("derive(Serialize) on plain data is infallible"),
                    );
                }
                Err(BackendSetError::SetError(set_err)) => {
                    not_created.insert(create_id.clone(), set_error_value(&set_err));
                }
                Err(BackendSetError::Other(e)) => {
                    not_created.insert(create_id.clone(), server_fail_value_from_backend(&e));
                }
                Err(_) => {
                    not_created.insert(
                        create_id.clone(),
                        json!({
                            "type": "serverFail",
                            "description": "unhandled backend error variant",
                        }),
                    );
                }
            }
        }
    }

    // -----------------------------------------------------------------------
    // update
    // -----------------------------------------------------------------------
    if let Some(Value::Object(update_map)) = args.remove("update") {
        for (id_str, patch_val) in update_map {
            let id = Id::from(id_str.as_str());

            // Reject patches that include server-set fields.
            // Server-set fields that clients may not patch via Chat/set.
            // INVARIANT: this list must include every field on jmap_chat_types::Chat that
            // is set by the server rather than the client. Add new server-set fields here
            // at the same time as adding them to the Chat struct.
            const CHAT_READONLY: &[&str] =
                &["id", "createdAt", "unreadCount", "closedAt", "activeCallId"];
            let bad_props: Vec<&str> = CHAT_READONLY
                .iter()
                .copied()
                .filter(|&field| patch_val.get(field).is_some())
                .collect();
            if !bad_props.is_empty() {
                not_updated.insert(
                    id_str,
                    json!({ "type": "invalidProperties", "properties": bad_props }),
                );
                continue;
            }

            // Convert wire-format Value into a typed PatchObject. RFC 8620
            // §5.3 mandates a PatchObject is a JSON Object; non-object
            // values produce an `invalidPatch` SetError.
            let mut patch = match serde_json::from_value::<PatchObject>(patch_val) {
                Ok(p) => p,
                Err(e) => {
                    not_updated.insert(
                        id_str,
                        json!({ "type": "invalidPatch", "description": e.to_string() }),
                    );
                    continue;
                }
            };

            // draft-atwood-jmap-chat-00 §Chat `closedAt`: "After it is
            // set, the server MUST reject with a `forbidden` SetError ...
            // every `Chat/set` update that changes membership ... or
            // shared group settings". Pointer paths beneath them count.
            let touches_closed_keys = patch.as_map().keys().any(|k| {
                let top = k.split('/').next().unwrap_or(k);
                MEMBER_PATCH_KEYS.contains(&top) || SHARED_GROUP_KEYS.contains(&top)
            });
            if touches_closed_keys {
                match backend
                    .get_objects::<Chat>(caller, &account_id, Some(std::slice::from_ref(&id)), None)
                    .await
                {
                    Ok((chats, _)) if chats.first().is_some_and(|c| c.closed_at.is_some()) => {
                        not_updated.insert(
                            id_str,
                            json!({ "type": "forbidden", "description": "the group is closed" }),
                        );
                        continue;
                    }
                    Ok(_) => {}
                    Err(e) => {
                        not_updated.insert(id_str, server_fail_value_from_backend(&e));
                        continue;
                    }
                }
            }

            // draft-atwood-jmap-chat-00 §Chat/set "Updating a Chat": the
            // member patch keys are operations on `members`, not
            // properties. They are applied to the stored list here and
            // replaced by the resulting `members`, so the backend never
            // stores them. A Chat that does not resolve is left for
            // update_object to surface `notFound`.
            // ponytail: check-then-update is not atomic; two concurrent
            // member patches can lose one. A backend needing strictness
            // re-checks inside its update transaction.
            if MEMBER_PATCH_KEYS
                .iter()
                .any(|k| patch.as_map().contains_key(*k))
            {
                let stored = match backend
                    .get_objects::<Chat>(caller, &account_id, Some(std::slice::from_ref(&id)), None)
                    .await
                {
                    Ok((chats, _)) => chats.into_iter().next(),
                    Err(e) => {
                        not_updated.insert(id_str, server_fail_value_from_backend(&e));
                        continue;
                    }
                };
                if let Some(stored) = stored {
                    let max_members = backend.limits(caller, &account_id).max_group_chat_members;
                    match apply_member_patch_keys(&stored, &account_id, patch.as_map(), max_members)
                    {
                        Ok((members, closes)) => {
                            let map = patch.as_map_mut();
                            for key in MEMBER_PATCH_KEYS {
                                map.remove(*key);
                            }
                            map.insert(
                                "members".to_owned(),
                                serde_json::to_value(members)
                                    .expect("derive(Serialize) on plain data is infallible"),
                            );
                            // "the server MUST set `closedAt` ({{chat}}) in
                            // the same operation".
                            if closes {
                                map.insert("closedAt".to_owned(), json!(now_utc_string()));
                            }
                        }
                        Err(set_err) => {
                            not_updated.insert(id_str, set_error_value(&set_err));
                            continue;
                        }
                    }
                }
            }

            // draft-atwood-jmap-chat-00 §Chat `pinnedMessageIds`: who may
            // modify the list depends on the chat kind; the backend decides.
            // Pointer paths beneath the property are gated too.
            let touches_pins = patch
                .as_map()
                .keys()
                .any(|k| k == "pinnedMessageIds" || k.starts_with("pinnedMessageIds/"));
            if touches_pins {
                match backend
                    .may_set_pinned_messages(caller, &account_id, &id)
                    .await
                {
                    Ok(Ok(())) => {}
                    Ok(Err(set_err)) => {
                        not_updated.insert(id_str, set_error_value(&set_err));
                        continue;
                    }
                    Err(e) => {
                        not_updated.insert(id_str, server_fail_value_from_backend(&e));
                        continue;
                    }
                }
            }

            // draft-atwood-jmap-chat-00 §Chat `pinnedMessageIds`: an update
            // that would add an id not naming a Message in this Chat is
            // `invalidProperties`. RFC 8620 §5.3 forbids pointers into
            // arrays (update_object rejects them as invalidPatch), so only
            // the whole-property key can add ids, and its value is the
            // post-patch list. Only ids absent from the stored list count
            // as added; see ChatBackend::validate_pinned_message_ids.
            // ponytail: check-then-update is not atomic; a Message
            // hard-deleted between the two leaves a stale pin. A backend
            // needing strictness re-checks inside its update transaction.
            if let Some(Value::Array(new_pins)) = patch.as_map().get("pinnedMessageIds") {
                let stored = match backend
                    .get_objects::<Chat>(caller, &account_id, Some(std::slice::from_ref(&id)), None)
                    .await
                {
                    Ok((chats, _)) => chats.into_iter().next(),
                    Err(e) => {
                        not_updated.insert(id_str, server_fail_value_from_backend(&e));
                        continue;
                    }
                };
                if let Some(stored) = stored {
                    let added: Vec<Id> = new_pins
                        .iter()
                        .filter_map(Value::as_str)
                        .map(Id::from)
                        .filter(|pin| !stored.pinned_message_ids.contains(pin))
                        .collect();
                    if !added.is_empty() {
                        match backend
                            .validate_pinned_message_ids(caller, &account_id, &id, &added)
                            .await
                        {
                            Ok(Ok(())) => {}
                            Ok(Err(set_err)) => {
                                not_updated.insert(id_str, set_error_value(&set_err));
                                continue;
                            }
                            Err(e) => {
                                not_updated.insert(id_str, server_fail_value_from_backend(&e));
                                continue;
                            }
                        }
                    }
                }
            }

            match backend
                .update_object::<Chat>(caller, &account_id, &id, patch)
                .await
            {
                Ok(Some(obj)) => {
                    mutated = true;
                    updated.insert(
                        id_str,
                        serde_json::to_value(&obj)
                            .expect("derive(Serialize) on plain data is infallible"),
                    );
                }
                Ok(None) => {
                    mutated = true;
                    updated.insert(id_str, Value::Null);
                }
                Err(BackendSetError::SetError(set_err)) => {
                    not_updated.insert(id_str, set_error_value(&set_err));
                }
                Err(BackendSetError::Other(e)) => {
                    not_updated.insert(id_str, server_fail_value_from_backend(&e));
                }
                Err(_) => {
                    not_updated.insert(
                        id_str,
                        json!({
                            "type": "serverFail",
                            "description": "unhandled backend error variant",
                        }),
                    );
                }
            }
        }
    }

    // -----------------------------------------------------------------------
    // destroy
    // -----------------------------------------------------------------------
    if let Some(destroy_arr) = args.get("destroy").and_then(|v| v.as_array()) {
        // RFC 8620 §5.3: every element of the destroy array MUST be a string Id.
        // Reject the whole request if any element is non-string rather than
        // silently skipping it, which would produce a misleading response.
        if let Some(bad) = destroy_arr.iter().find(|v| !v.is_string()) {
            return Err(JmapError::invalid_arguments(format!(
                "destroy: every element must be a string Id; got {bad}"
            )));
        }
        for id_val in destroy_arr {
            let Some(id_str) = id_val.as_str() else {
                continue; // unreachable: validated above
            };
            let id = Id::from(id_str);

            match backend
                .destroy_object::<Chat>(caller, &account_id, &id)
                .await
            {
                Ok(()) => {
                    mutated = true;
                    destroyed_list.push(Value::String(id_str.to_owned()));
                }
                Err(BackendSetError::SetError(set_err)) => {
                    not_destroyed.insert(id_str.to_owned(), set_error_value(&set_err));
                }
                Err(BackendSetError::Other(e)) => {
                    not_destroyed.insert(id_str.to_owned(), server_fail_value_from_backend(&e));
                }
                Err(_) => {
                    not_destroyed.insert(
                        id_str.to_owned(),
                        json!({
                            "type": "serverFail",
                            "description": "unhandled backend error variant",
                        }),
                    );
                }
            }
        }
    }

    finalize_set_response::<B, Chat>(
        backend,
        caller,
        &account_id,
        old_state,
        mutated,
        SetAccumulators {
            created,
            updated,
            destroyed: destroyed_list,
            not_created,
            not_updated,
            not_destroyed,
        },
    )
    .await
}

// ---------------------------------------------------------------------------
// Chat/typing
// ---------------------------------------------------------------------------

/// Handle a `Chat/typing` method call (draft-atwood-jmap-chat-00 §Chat).
///
/// `args` is the draft §Chat `Chat/typing` request shape (`accountId`,
/// `chatId`); the returned `Value` is the §Chat response shape
/// (`accountId` echo). No persistent state is changed.
///
/// This method is ephemeral — it signals the user is typing in a chat.
/// No state is persisted. In a full implementation, the server would
/// fan out a typing event to chat participants; this handler validates
/// and returns. An unknown `chatId` and a Chat the account does not
/// participate in both yield the same `invalidArguments` error. The
/// sender identity is always derived server-side from `accountId` —
/// clients MUST NOT supply a `senderId` field.
///
/// # Blocked-sender suppression
///
/// Per draft-atwood-jmap-chat-00 commit `d68b4e3` ("close blocked-sender
/// suppression gaps for typing/presence"): when the requesting account
/// corresponds to a [`ChatContact`] whose `blocked` is `true` on the
/// recipient's contact list, the server MUST silently suppress the
/// typing event for that recipient. The sender is NOT informed.
///
/// [`ChatContact`]: jmap_chat_types::ChatContact
///
/// The kit consults [`ChatBackend::is_contact_blocked`] on the
/// direct-chat path for observability, but does NOT itself perform
/// transport-layer fan-out — push delivery (SSE / WS) is the
/// consumer's responsibility. The consumer's typing-event publisher
/// MUST consult this predicate per recipient before fanning out the
/// event. The kit's handler returns the same success response in
/// either case (echo `accountId`); the wire shape does not depend on
/// the predicate's result.
///
/// Group / channel chats (n recipients) are skipped on the kit side:
/// the handler has no way to enumerate fan-out recipients. That work
/// belongs entirely to the consumer's transport layer.
///
/// Returns `(response_args, extra_invocations)`. The extra list is always empty.
pub async fn handle_chat_typing<B: ChatBackend>(
    backend: &B,
    caller: &B::CallerCtx,
    args: Value,
) -> Result<HandlerOutput, JmapError> {
    let (account_id, args) = extract_account_id(args)?;

    let chat_id: Id = match args.get("chatId").and_then(|v| v.as_str()) {
        Some(s) if !s.is_empty() => Id::from(s),
        _ => return Err(JmapError::invalid_arguments("chatId is required")),
    };

    let _typing: bool = match args.get("typing") {
        Some(Value::Bool(b)) => *b,
        None => return Err(JmapError::invalid_arguments("typing is required")),
        Some(_) => return Err(JmapError::invalid_arguments("typing must be a boolean")),
    };

    // draft-atwood-jmap-chat-00 §Chat/typing `chatId`: "The server MUST
    // return an `invalidArguments` method error if `chatId` does not
    // exist or the account is not a participant in that Chat, and MUST
    // NOT distinguish the two cases." A group chat's participants are its
    // `members`, where the owner is referenced by the accountId
    // (§Terminology).
    let (found, _not_found) = backend
        .get_objects::<Chat>(
            caller,
            &account_id,
            Some(std::slice::from_ref(&chat_id)),
            None,
        )
        .await
        .map_err(|e| server_fail_from_backend(&e))?;
    let chat = found
        .into_iter()
        .next()
        .filter(|c| {
            c.kind != ChatKind::Group || c.members.iter().flatten().any(|m| m.id == account_id)
        })
        .ok_or_else(|| {
            JmapError::invalid_arguments("chatId is not a Chat the account participates in")
        })?;

    // Blocked-sender suppression integration point (draft-atwood-
    // jmap-chat-00 commit `d68b4e3`). For direct chats, consult the
    // predicate so a production-grade backend override can observe /
    // record the consultation. The kit does not gate fan-out on the
    // result because the kit has no fan-out path; this site exists
    // as the documented hook for consumer transport layers.
    if let Some(contact_id) = chat.contact_id.as_ref() {
        // Direct chat — there is exactly one recipient.
        let _blocked = backend
            .is_contact_blocked(caller, &account_id, contact_id)
            .await
            .unwrap_or(false);
        // Result intentionally observed but not gated on
        // here — see the doc-comment on this function.
        // A consumer transport layer reads
        // `is_contact_blocked` itself and suppresses
        // accordingly.
    }

    Ok(HandlerOutput::new(json!({
        "accountId": account_id.as_ref(),
    })))
}