jmap-chat-server 0.4.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
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
//! 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 (RFC 8620
//! §3.6.2 and §5):
//!
//! - every handler: `accountNotFound`, `invalidArguments`, `serverFail`;
//! - `/get`, `/set`: `requestTooLarge` past `maxObjectsInGet` /
//!   `maxObjectsInSet`;
//! - `/set`: `stateMismatch`;
//! - `/changes`, `/queryChanges`: `cannotCalculateChanges`;
//! - `/queryChanges`: `tooManyChanges`;
//! - `/query`, `/queryChanges`: `unsupportedFilter`;
//! - `/query`: `unsupportedSort`, `anchorNotFound`.
//!
//! Per-target failures inside `/set` surface in the `notCreated` / `notUpdated`
//! / `notDestroyed` maps within `Ok((Value, ...))`, not as `Err`.

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

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

use crate::ChatBackend;
use crate::helpers::{PropType, check_metadata_entry, create_metadata, mistyped_create_props};
use crate::helpers::{
    SetAccumulators, default_sort, enforce_max_objects_in_set, extract_account_id,
    finalize_set_response, now_utc_string, reject_unsupported_sort,
};
use crate::helpers::{resolve_entry_refs, resolve_refs, resolved_create};
use crate::outbound::{GroupUpdateAction, OutboundEvent};
use jmap_server::{
    HandlerOutput, backend_set_error_value, is_client_updatable, reject_server_set,
    server_fail_from_backend, server_fail_value_from_backend,
};
use jmap_server::{SetError, SetErrorType};

/// `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"];

/// `Chat/set` update keys only a group Chat's admins may change
/// (draft-atwood-jmap-chat-00 §Chat/set "Updating a Chat": "For group
/// chats, admins additionally may update: `name`, `description`,
/// `avatarBlobId`.").
const GROUP_ADMIN_KEYS: &[&str] = &["name", "description", "avatarBlobId"];

/// Chat properties draft-atwood-jmap-chat-00 §Chat Object Fields scopes
/// to one kind ("**Direct chats only.**", "**Group chats only.**",
/// "**Channel Chats only.**"), with that kind. `name` is "**Group and
/// channel Chats.**", and `Chat/set` never creates a channel, so a group
/// is the only created kind that carries it. A `Chat/set` create of
/// another kind that carries one is `invalidProperties` (RFC 8620 §5.3).
const KIND_SCOPED_CREATE_PROPS: &[(&str, ChatKind)] = &[
    ("contactId", ChatKind::Direct),
    ("name", ChatKind::Group),
    ("description", ChatKind::Group),
    ("avatarBlobId", ChatKind::Group),
    ("spaceId", ChatKind::Channel),
];

/// Whether `account_id` holds the `"admin"` role in `chat.members`.
fn is_chat_admin(chat: &Chat, account_id: &Id) -> bool {
    chat.members
        .iter()
        .flatten()
        .any(|m| m.id == *account_id && m.role == ChatMemberRole::Admin)
}

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

/// 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`.
fn apply_member_patch_keys(
    chat: &Chat,
    owner_id: &Id,
    patch: &serde_json::Map<String, Value>,
    max_members: u32,
) -> Result<(Vec<ChatMember>, bool), Box<SetError>> {
    fn parse<T: serde::de::DeserializeOwned>(
        patch: &serde_json::Map<String, Value>,
        key: &str,
    ) -> Result<Option<T>, Box<SetError>> {
        patch
            .get(key)
            .map(|v| serde_json::from_value(v.clone()))
            .transpose()
            .map_err(|_| {
                Box::new(SetError::new(SetErrorType::InvalidProperties).with_properties([key]))
            })
    }
    let valid_role = |role: &ChatMemberRole| SPEC_CHAT_MEMBER_ROLES.contains(&role.as_str());
    let invalid =
        |key: &str| Box::new(SetError::new(SetErrorType::InvalidProperties).with_properties([key]));
    let forbidden =
        |desc: &str| Box::new(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_ref().is_some_and(|r| !valid_role(r)))
    {
        return Err(invalid("addMembers"));
    }
    if update
        .iter()
        .flatten()
        .any(|e| !e.role.as_ref().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 is_admin = is_chat_admin(chat, owner_id);
    let mut members = chat.members.clone().unwrap_or_default();
    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(ChatMemberRole::Member);
                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(Box::new(
                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 != ChatMemberRole::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 != ChatMemberRole::Admin);
    Ok((members, closes))
}

/// Ids of `members` other than the owner, who is referenced by the
/// accountId (draft-atwood-jmap-chat-00 §Terminology).
fn other_member_ids<'a>(
    members: impl IntoIterator<Item = &'a ChatMember>,
    owner_id: &Id,
) -> Vec<Id> {
    members
        .into_iter()
        .filter(|m| m.id != *owner_id)
        .map(|m| m.id.clone())
        .collect()
}

/// The `Peer/groupUpdate` events for a successful `Chat/set` update of
/// the group `before`, in [`GroupUpdateAction`] order.
///
/// `after` is the member list the update stored (`None` when it did not
/// touch members) and `closes` whether it closed the group;
/// `metadata_changed` is whether it patched `name`, `description` or
/// `avatarBlobId`. The member actions are the difference between the two
/// lists, so a no-op entry (adding a current member, re-assigning a
/// member's role) reports nothing. The owner's own removal is `"leave"`
/// or `"close"`, not `"removeMembers"`
/// (draft-atwood-jmap-chat-federation-00 §Peer/groupUpdate: `"leave"` "is
/// equivalent to `"removeMembers"` with `removedMemberIds:
/// [senderUserId]`"). Recipients follow draft-atwood-jmap-chat-00
/// §Chat/set "Updating a Chat".
fn group_update_events(
    chat_id: &Id,
    owner_id: &Id,
    before: &[ChatMember],
    after: Option<&[ChatMember]>,
    closes: bool,
    metadata_changed: bool,
) -> Vec<OutboundEvent> {
    let after = after.unwrap_or(before);
    let find = |list: &[ChatMember], id: &Id| list.iter().find(|m| m.id == *id).cloned();
    let everyone = other_member_ids(after, owner_id);
    let mut actions = Vec::new();
    if metadata_changed {
        actions.push((GroupUpdateAction::UpdateMetadata, everyone.clone()));
    }
    let added: Vec<ChatMember> = after
        .iter()
        .filter(|m| find(before, &m.id).is_none())
        .cloned()
        .collect();
    if !added.is_empty() {
        actions.push((GroupUpdateAction::AddMembers { added }, everyone.clone()));
    }
    let removed_ids = other_member_ids(
        before.iter().filter(|m| find(after, &m.id).is_none()),
        owner_id,
    );
    if !removed_ids.is_empty() {
        let mut recipients = everyone.clone();
        recipients.extend(removed_ids.iter().cloned());
        actions.push((GroupUpdateAction::RemoveMembers { removed_ids }, recipients));
    }
    let updated: Vec<ChatMember> = after
        .iter()
        .filter(|m| find(before, &m.id).is_some_and(|old| old.role != m.role))
        .cloned()
        .collect();
    if !updated.is_empty() {
        actions.push((GroupUpdateAction::UpdateRoles { updated }, everyone.clone()));
    }
    if find(before, owner_id).is_some() && find(after, owner_id).is_none() {
        let action = if closes {
            GroupUpdateAction::Close
        } else {
            GroupUpdateAction::Leave
        };
        actions.push((action, everyone));
    }
    actions
        .into_iter()
        .map(|(action, recipients)| OutboundEvent::GroupUpdate {
            chat_id: chat_id.clone(),
            action,
            recipients,
        })
        .collect()
}

// ---------------------------------------------------------------------------
// 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`).
///
/// `unreadCount` is computed on every call (§Chat, jmap-chat-spec 33702a5
/// L629): "The number of Messages in this Chat whose `id` ULID is
/// lexicographically greater than `ReadPosition.lastReadMessageId`,
/// derived server-side from the owner's ReadPosition record. If
/// `lastReadMessageId` is absent, all messages are counted as unread." A
/// Chat with no ReadPosition counts every Message. A backend whose
/// [`ChatBackend::maintains_unread_count`] is `true` gets its stored
/// value back instead.
///
/// `using` is the request's capability list (pass
/// [`jmap_server::CallContext::using`]). When the backend lists Chat for
/// draft-ietf-jmap-metadata-04 ([`JmapBackend::metadata_info`](jmap_server::JmapBackend::metadata_info)),
/// `metadata` is returned as an object, by default only with
/// `urn:ietf:params:jmap:metadata` in `using`, and with that capability
/// `properties` may hold `metadata/<namespace>` subselectors (-04 §2,
/// §3.1); see [`jmap_server::handle_get_with_metadata`].
///
/// 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,
    using: &[String],
) -> Result<HandlerOutput, JmapError> {
    let mut out =
        jmap_server::handle_get_with_metadata::<Chat, B>(backend, caller, args, using).await?;
    fill_unread_counts(backend, caller, &mut out.primary).await?;
    Ok(out)
}

/// Overwrite `unreadCount` on each Chat in a `Chat/get` response `list`
/// that carries the property (it is absent when `properties` leaves it
/// out), unless the backend maintains the count itself
/// ([`ChatBackend::maintains_unread_count`]).
// ponytail: scans every Message and ReadPosition in the account per call
// (O(messages) per Chat/get); a backend with many messages keeps its own
// count via ChatBackend::maintains_unread_count.
async fn fill_unread_counts<B: ChatBackend>(
    backend: &B,
    caller: &B::CallerCtx,
    response: &mut Value,
) -> Result<(), JmapError> {
    if backend.maintains_unread_count() {
        return Ok(());
    }
    // Id::from: the accountId this handler already validated and echoed.
    let account_id = Id::from(response["accountId"].as_str().unwrap_or_default());
    let Some(list) = response["list"].as_array_mut() else {
        return Ok(());
    };
    if !list.iter().any(|c| c.get("unreadCount").is_some()) {
        return Ok(());
    }
    let (messages, positions) = messages_and_positions(backend, caller, &account_id).await?;
    for chat in list.iter_mut() {
        let Some(chat) = chat.as_object_mut() else {
            continue;
        };
        if !chat.contains_key("unreadCount") {
            continue;
        }
        let Some(chat_id) = chat.get("id").and_then(Value::as_str) else {
            continue;
        };
        let unread = unread_count(&messages, &positions, chat_id);
        chat.insert("unreadCount".to_owned(), Value::from(unread));
    }
    Ok(())
}

async fn messages_and_positions<B: ChatBackend>(
    backend: &B,
    caller: &B::CallerCtx,
    account_id: &Id,
) -> Result<(Vec<Message>, Vec<ReadPosition>), JmapError> {
    let (messages, _) = backend
        .get_objects::<Message>(caller, account_id, None, None)
        .await
        .map_err(|e| server_fail_from_backend(&e))?;
    let (positions, _) = backend
        .get_objects::<ReadPosition>(caller, account_id, None, None)
        .await
        .map_err(|e| server_fail_from_backend(&e))?;
    Ok((messages, positions))
}

/// §Chat `unreadCount` for `chat_id`: Messages in the Chat whose `id`
/// sorts after the owner's `lastReadMessageId`, or all of them when it
/// is absent.
pub(crate) fn unread_count(messages: &[Message], positions: &[ReadPosition], chat_id: &str) -> u64 {
    let last_read = positions
        .iter()
        .find(|p| p.chat_id.as_ref() == chat_id)
        .and_then(|p| p.last_read_message_id.as_ref());
    messages
        .iter()
        .filter(|m| m.chat_id.as_ref() == chat_id)
        .filter(|m| last_read.is_none_or(|l| m.id.as_ref() > l.as_ref()))
        .count() as u64
}

/// Write the recomputed `unreadCount` to each Chat in `chat_ids` so the
/// `Chat` state advances and `Chat/changes` reports the Chat as updated.
///
/// RFC 8620 §5.1: "If the data changes, this string MUST change", and
/// §ReadPosition/set (jmap-chat-spec 33702a5 L1434) "updates
/// `Chat.unreadCount` for the affected Chat, possibly later". `Chat/get`
/// derives the count on every call, so a ReadPosition update or a Message
/// create/destroy changes Chat data; this records that change through
/// [`ChatBackend::update_object`], whose contract bumps the type state
/// and logs the id as updated. Does nothing when the backend maintains
/// the count itself ([`ChatBackend::maintains_unread_count`]).
///
/// Best-effort, like [`crate::message::ensure_read_positions`]: the
/// caller's own write already succeeded and its response must report
/// it, so a failure here (including a Chat that no longer exists) is
/// not surfaced.
pub(crate) async fn record_unread_count_changes<B: ChatBackend>(
    backend: &B,
    caller: &B::CallerCtx,
    account_id: &Id,
    mut chat_ids: Vec<Id>,
) {
    if chat_ids.is_empty() || backend.maintains_unread_count() {
        return;
    }
    let Ok((messages, positions)) = messages_and_positions(backend, caller, account_id).await
    else {
        return;
    };
    chat_ids.sort_by(|a, b| a.as_ref().cmp(b.as_ref()));
    chat_ids.dedup();
    for chat_id in chat_ids {
        let unread = unread_count(&messages, &positions, chat_id.as_ref());
        let mut patch = PatchObject::new();
        patch
            .as_map_mut()
            .insert("unreadCount".to_owned(), Value::from(unread));
        let _ = backend
            .update_object::<Chat>(caller, account_id, &chat_id, patch)
            .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`).
///
/// `using` is the request's capability list (pass
/// [`jmap_server::CallContext::using`]); with
/// `urn:ietf:params:jmap:metadata` the request may set
/// draft-ietf-jmap-metadata-04 §3.3 `ignoreMetadataOnlyChanges`, and
/// without it metadata-only changes are left out (-04 §3).
///
/// 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,
    using: &[String],
) -> Result<HandlerOutput, JmapError> {
    jmap_server::handle_changes::<Chat, B>(backend, caller, args, using).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).
///
/// `using` is the request's capability list (pass
/// [`jmap_server::CallContext::using`]); without
/// `urn:ietf:params:jmap:metadata` a draft-ietf-jmap-metadata-04 §3.6
/// filter condition is `unsupportedFilter` (-04 §3).
///
/// Returns `(response_args, extra_invocations)`. The extra list is always empty.
pub async fn handle_chat_query<B: ChatBackend>(
    backend: &B,
    caller: &B::CallerCtx,
    mut args: Value,
    using: &[String],
) -> Result<HandlerOutput, JmapError> {
    jmap_server::reject_metadata_filter_arg(
        &args,
        jmap_chat_types::ChatFilterCondition::has_metadata_condition,
        using,
    )?;
    // §Chat/query: "Default sort: `lastMessageAt` descending; chats
    // without messages sort last."
    if let Value::Object(map) = &mut args {
        default_sort(
            map,
            json!([{ "property": "lastMessageAt", "isAscending": false }]),
        );
    }
    reject_unsupported_sort::<ChatComparator>(args.get("sort"))?;
    jmap_server::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`).
///
/// `using` is the request's capability list (pass
/// [`jmap_server::CallContext::using`]); without
/// `urn:ietf:params:jmap:metadata` a draft-ietf-jmap-metadata-04 §3.6
/// filter condition is `unsupportedFilter` (-04 §3).
///
/// 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,
    mut args: Value,
    using: &[String],
) -> Result<HandlerOutput, JmapError> {
    jmap_server::reject_metadata_filter_arg(
        &args,
        jmap_chat_types::ChatFilterCondition::has_metadata_condition,
        using,
    )?;
    // RFC 8620 §5.6: the `filter` and `sort` are those of the original
    // query, so the §Chat/query default sort applies here too.
    if let Value::Object(map) = &mut args {
        default_sort(
            map,
            json!([{ "property": "lastMessageAt", "isAscending": false }]),
        );
    }
    reject_unsupported_sort::<ChatComparator>(args.get("sort"))?;
    jmap_server::handle_query_changes::<Chat, B>(backend, caller, args).await
}

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

/// The Chat create and update properties that hold the id of another record, which a
/// client may set to a `#creationId` reference (RFC 8620 §5.3).
const CHAT_ID_PROPERTIES: &[&str] = &[
    "contactId",
    "memberIds",
    "pinnedMessageIds",
    "removeMembers",
];

/// The Chat update entry fields that hold a ChatContact id, as
/// `(patch key, entry field, property named on failure)`: draft-atwood-jmap-chat-00
/// §Chat/set "Updating a Chat" gives each `addMembers` and
/// `updateMemberRoles` entry an `id`, a ChatContact id.
const CHAT_ENTRY_ID_PROPERTIES: &[(&str, &str, &str)] = &[
    ("addMembers", "id", "addMembers"),
    ("updateMemberRoles", "id", "updateMemberRoles"),
];

/// 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` on create and destroy (managed via
///   `Space/set` `addChannels` / `removeChannels`).
/// - a group create whose membership exceeds
///   [`ChatLimits::max_group_chat_members`](crate::ChatLimits::max_group_chat_members)
///   is `overQuota`.
/// - an update key outside §Chat/set "Updating a Chat" is
///   `invalidProperties`; `name`, `description` and `avatarBlobId` updates
///   are `forbidden` unless the Chat is a group and the caller is an admin.
/// - `pinnedMessageIds` updates are gated by
///   [`ChatBackend::may_set_pinned_messages`].
///
/// Returns `(response_args, extra_invocations)`. The extra list is always empty.
///
/// A `contactId`, a `memberIds`, `pinnedMessageIds` or `removeMembers`
/// element, or the `id` of an `addMembers` or `updateMemberRoles` entry,
/// that is a `#creationId` reference names a record created earlier in the request and resolves against
/// `created_ids` (pass [`jmap_server::CallContext::created_ids`]; RFC 8620
/// §5.3); an unknown reference is `invalidProperties` naming the property.
///
/// `using` is the request's capability list (pass
/// [`jmap_server::CallContext::using`]): a write to `metadata` or
/// `privateMetadata` without `urn:ietf:params:jmap:metadata` is
/// `invalidProperties` (draft-ietf-jmap-metadata-04 §3), as is `null` for
/// either in a create (-04 §3.2). The backend validates the rest.
pub async fn handle_chat_set<B: ChatBackend>(
    backend: &B,
    caller: &B::CallerCtx,
    created_ids: &BTreeMap<Id, Id>,
    args: Value,
    using: &[String],
) -> Result<HandlerOutput, JmapError> {
    let (account_id, mut args) = extract_account_id(args)?;

    // RFC 8620 §3.6.2 accountNotFound (bd:JMAP-fo38).
    if !backend
        .account_exists(caller, &account_id)
        .await
        .map_err(|e| server_fail_from_backend(&e))?
    {
        return Err(JmapError::account_not_found());
    }

    // 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_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 {
            let resolved = match resolved_create(obj_val, CHAT_ID_PROPERTIES, created_ids) {
                Ok(v) => v,
                Err(err) => {
                    not_created.insert(create_id.clone(), serde_json::Value::from(&*err));
                    continue;
                }
            };
            let obj_val = &resolved;
            if let Some(Err(err)) = obj_val
                .as_object()
                .map(|o| check_metadata_entry(o, true, using))
            {
                not_created.insert(create_id.clone(), serde_json::Value::from(&*err));
                continue;
            }
            // draft-ietf-jmap-metadata-04 §3.2: the create sets the
            // metadata properties; the backend validates them.
            let (metadata, private_metadata) = match create_metadata(obj_val) {
                Ok(maps) => maps,
                Err(err) => {
                    not_created.insert(create_id.clone(), err);
                    continue;
                }
            };
            // 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),
                    ("avatarBlobId", PropType::Str),
                    ("messageExpirySeconds", PropType::UnsignedInt),
                    ("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;
                }
            };

            // A channel create is `forbidden` below whatever it carries.
            // `null` is read as absent, as in `mistyped_create_props`.
            if kind != ChatKind::Channel {
                let foreign: Vec<&str> = KIND_SCOPED_CREATE_PROPS
                    .iter()
                    .filter(|(prop, scope)| {
                        *scope != kind && obj_val.get(*prop).is_some_and(|v| !v.is_null())
                    })
                    .map(|(prop, _)| *prop)
                    .collect();
                if !foreign.is_empty() {
                    not_created.insert(
                        create_id.clone(),
                        json!({ "type": "invalidProperties", "properties": foreign }),
                    );
                    continue;
                }
                if let Some(err) = obj_val.as_object().and_then(reject_server_set::<Chat>) {
                    not_created.insert(create_id.clone(), serde_json::Value::from(&err));
                    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::Value::from(
                                SetError::new(SetErrorType::AlreadyExists)
                                    .with_existing_id(dup.id.clone()),
                            ),
                        );
                        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(),
                            jmap_server::serialize_value(
                                SetError::new(SetErrorType::AlreadyExists)
                                    .with_existing_id(canonical_id),
                            )?,
                        );
                        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) => {
                            match ids
                                .iter()
                                .map(|s| jmap_server::parse_wire_id(s, "memberIds"))
                                .collect::<Result<Vec<Id>, _>>()
                            {
                                Ok(ids) => Some(ids),
                                Err(err) => {
                                    not_created
                                        .insert(create_id.clone(), serde_json::Value::from(&*err));
                                    continue;
                                }
                            }
                        }
                        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(),
                    ChatMemberRole::Admin,
                    now.clone(),
                )];
                for id in ids {
                    if members.iter().all(|m| m.id != id) {
                        let mut m = ChatMember::new(id, ChatMemberRole::Member, now.clone());
                        m.invited_by = Some(creator.clone());
                        members.push(m);
                    }
                }
                members
            });

            // Group create `memberIds`: "If the resulting membership would
            // exceed a server-defined limit on the number of members per
            // group Chat, the server MUST return an `overQuota` SetError".
            let max_members = backend.limits(caller, &account_id).max_group_chat_members;
            if members
                .as_ref()
                .is_some_and(|m| m.len() > max_members as usize)
            {
                not_created.insert(
                    create_id.clone(),
                    json!({
                        "type": "overQuota",
                        "description": "group chat member limit exceeded",
                    }),
                );
                continue;
            }

            let contact_id: Option<Id> = match obj_val.get("contactId").and_then(|v| v.as_str()) {
                Some(s) => match jmap_server::parse_wire_id(s, "contactId") {
                    Ok(id) => Some(id),
                    Err(err) => {
                        not_created.insert(create_id.clone(), serde_json::Value::from(&*err));
                        continue;
                    }
                },
                None => None,
            };
            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 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);

            // "Optional at creation: `description` (String), `avatarBlobId`
            // (String), `messageExpirySeconds` (UnsignedInt)."
            let avatar_blob_id = match obj_val.get("avatarBlobId").and_then(Value::as_str) {
                Some(s) => match jmap_server::parse_wire_id(s, "avatarBlobId") {
                    Ok(id) => Some(id),
                    Err(err) => {
                        not_created.insert(create_id.clone(), serde_json::Value::from(&*err));
                        continue;
                    }
                },
                None => None,
            };
            let message_expiry_seconds =
                obj_val.get("messageExpirySeconds").and_then(Value::as_u64);
            let build = |id: &Id| {
                let mut chat = Chat::new(
                    id.clone(),
                    kind,
                    now,
                    0,
                    vec![],
                    muted,
                    receive_typing_indicators,
                );
                chat.contact_id = contact_id;
                chat.name = name;
                chat.description = description;
                chat.avatar_blob_id = avatar_blob_id;
                chat.message_expiry_seconds = message_expiry_seconds;
                chat.members = members;
                chat.metadata = metadata;
                chat.private_metadata = private_metadata;
                chat
            };

            match backend
                .create_object::<Chat, _>(caller, &account_id, create_id, build)
                .await
            {
                Ok((new_id, created_obj)) => {
                    // The create is committed; every branch below (including
                    // rollback and cleanup failure) has moved backend state,
                    // so newState must be re-read (RFC 8620 §5.3).
                    mutated = true;
                    // 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 = match backend
                            .get_objects::<Chat>(caller, &account_id, None, None)
                            .await
                        {
                            Ok((chats, _)) => chats,
                            Err(read_err) => {
                                // The Chat (and earlier creates in this
                                // batch) are already committed, so a
                                // method-level serverFail would break
                                // RFC 8620 §3.6.2 ("The method call made
                                // no changes to the server's state").
                                // Without the re-check the draft's
                                // alreadyExists rule cannot be enforced,
                                // so roll this create back and fail it
                                // per id; the batch continues.
                                let value = match backend
                                    .destroy_object::<Chat>(caller, &account_id, &new_id)
                                    .await
                                {
                                    Ok(()) => server_fail_value_from_backend(&read_err),
                                    Err(e) => server_fail_value_from_backend(&e),
                                };
                                not_created.insert(create_id.clone(), value);
                                continue;
                            }
                        };
                        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());
                    }
                    // §Creating a Chat: the server "MUST send
                    // `Peer/groupUpdate` to each initial member".
                    if created_obj.kind == ChatKind::Group {
                        let event = OutboundEvent::GroupUpdate {
                            chat_id: new_id.clone(),
                            action: GroupUpdateAction::Create,
                            recipients: other_member_ids(
                                created_obj.members.iter().flatten(),
                                &account_id,
                            ),
                        };
                        backend.on_outbound_event(caller, &account_id, &event).await;
                    }
                    created.insert(
                        create_id.clone(),
                        jmap_server::serialize_value(&created_obj)?,
                    );
                }
                Err(e) => {
                    not_created.insert(create_id.clone(), backend_set_error_value(e));
                }
            }
        }
    }

    // -----------------------------------------------------------------------
    // update
    // -----------------------------------------------------------------------
    if let Some(Value::Object(update_map)) = args.remove("update") {
        for (id_str, mut patch_val) in update_map {
            let id = match jmap_server::parse_wire_id(&id_str, "id") {
                Ok(id) => id,
                Err(err) => {
                    not_updated.insert(id_str, serde_json::Value::from(&*err));
                    continue;
                }
            };
            if let Err(err) = resolve_refs(&mut patch_val, CHAT_ID_PROPERTIES, created_ids)
                .and_then(|()| {
                    resolve_entry_refs(&mut patch_val, CHAT_ENTRY_ID_PROPERTIES, created_ids)
                })
            {
                not_updated.insert(id_str, serde_json::Value::from(&*err));
                continue;
            }

            // Only the keys §Chat/set "Updating a Chat" defines may be
            // patched: the client-settable Chat properties that are not
            // immutable, and [`MEMBER_PATCH_KEYS`]. Any other key names a
            // server-set, immutable or `Space/set`-managed property and is
            // `invalidProperties` (RFC 8620 §5.3). JSON Pointer paths count
            // by their first segment.
            let bad_props: Vec<&str> = patch_val
                .as_object()
                .map(|obj| {
                    obj.keys()
                        .map(String::as_str)
                        .filter(|k| {
                            let top = k.split('/').next().unwrap_or(k);
                            !(is_client_updatable::<Chat>(top) || MEMBER_PATCH_KEYS.contains(&top))
                        })
                        .collect()
                })
                .unwrap_or_default();
            if !bad_props.is_empty() {
                not_updated.insert(
                    id_str,
                    json!({ "type": "invalidProperties", "properties": bad_props }),
                );
                continue;
            }
            if let Some(Err(err)) = patch_val
                .as_object()
                .map(|o| check_metadata_entry(o, false, using))
            {
                not_updated.insert(id_str, serde_json::Value::from(&*err));
                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;
                }
            };

            // The stored Chat, read once for every check below. A Chat
            // that does not resolve is left for update_object to surface
            // `notFound`.
            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;
                }
            };
            // Per-Chat restrictions of extension-bound Chats; see
            // `ChatBackend::may_update_chat`.
            if let Some(chat) = &stored {
                let patch_keys: Vec<&str> = patch.as_map().keys().map(String::as_str).collect();
                match backend
                    .may_update_chat(caller, &account_id, chat, &patch_keys)
                    .await
                {
                    Ok(Ok(())) => {}
                    Ok(Err(set_err)) => {
                        not_updated.insert(id_str, serde_json::Value::from(&set_err));
                        continue;
                    }
                    Err(e) => {
                        not_updated.insert(id_str, server_fail_value_from_backend(&e));
                        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.
            // The stored group and the member list the update stores, for
            // `group_update_events` once the update succeeds.
            let mut group_before: Option<Vec<ChatMember>> = None;
            let mut member_change: Option<(Vec<ChatMember>, bool)> = None;
            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 && let Some(chat) = &stored {
                if chat.closed_at.is_some() {
                    not_updated.insert(
                        id_str,
                        json!({ "type": "forbidden", "description": "the group is closed" }),
                    );
                    continue;
                }
                // "For group chats, admins additionally may update:
                // `name`, `description`, `avatarBlobId`."
                let touches_admin_keys = patch
                    .as_map()
                    .keys()
                    .any(|k| GROUP_ADMIN_KEYS.contains(&k.split('/').next().unwrap_or(k)));
                if touches_admin_keys
                    && !(chat.kind == ChatKind::Group && is_chat_admin(chat, &account_id))
                {
                    not_updated.insert(
                            id_str,
                            json!({
                                "type": "forbidden",
                                "description": "only group chat admins may update name, description and avatarBlobId",
                            }),
                        );
                    continue;
                }
                if chat.kind == ChatKind::Group {
                    group_before = Some(chat.members.clone().unwrap_or_default());
                }
            }

            // 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.
            // 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 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)) => {
                        member_change = Some((members.clone(), closes));
                        let map = patch.as_map_mut();
                        for key in MEMBER_PATCH_KEYS {
                            map.remove(*key);
                        }
                        map.insert("members".to_owned(), jmap_server::serialize_value(members)?);
                        // "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, serde_json::Value::from(&*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, serde_json::Value::from(&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 Some(stored) = &stored
            {
                let pins = match new_pins
                    .iter()
                    .filter_map(Value::as_str)
                    .map(|s| jmap_server::parse_wire_id(s, "pinnedMessageIds"))
                    .collect::<Result<Vec<Id>, _>>()
                {
                    Ok(pins) => pins,
                    Err(err) => {
                        not_updated.insert(id_str, serde_json::Value::from(&*err));
                        continue;
                    }
                };
                let added: Vec<Id> = pins
                    .into_iter()
                    .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, serde_json::Value::from(&set_err));
                            continue;
                        }
                        Err(e) => {
                            not_updated.insert(id_str, server_fail_value_from_backend(&e));
                            continue;
                        }
                    }
                }
            }

            let metadata_changed = patch
                .as_map()
                .keys()
                .any(|k| GROUP_ADMIN_KEYS.contains(&k.split('/').next().unwrap_or(k)));
            let outcome = backend
                .update_object::<Chat>(caller, &account_id, &id, patch)
                .await;
            if let (Ok(_), Some(before)) = (&outcome, &group_before) {
                let (after, closes) = match &member_change {
                    Some((members, closes)) => (Some(members.as_slice()), *closes),
                    None => (None, false),
                };
                for event in
                    group_update_events(&id, &account_id, before, after, closes, metadata_changed)
                {
                    backend.on_outbound_event(caller, &account_id, &event).await;
                }
            }
            match outcome {
                Ok(Some(obj)) => {
                    mutated = true;
                    updated.insert(id_str, jmap_server::serialize_value(&obj)?);
                }
                Ok(None) => {
                    mutated = true;
                    updated.insert(id_str, Value::Null);
                }
                Err(e) => {
                    not_updated.insert(id_str, backend_set_error_value(e));
                }
            }
        }
    }

    // -----------------------------------------------------------------------
    // 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.
        let id_strs: Vec<&str> = destroy_arr
            .iter()
            .map(|v| {
                v.as_str().ok_or_else(|| {
                    JmapError::invalid_arguments(format!(
                        "destroy: every element must be a string Id; got {}",
                        jmap_server::json_type_name(v)
                    ))
                })
            })
            .collect::<Result<_, _>>()?;
        // §Destroying a Chat: "For channel Chats, destroy via `Chat/set` is
        // not supported; the server MUST return a SetError of type
        // `forbidden`." An id that does not resolve is left for
        // destroy_object to surface `notFound`.
        let mut ids: Vec<Id> = Vec::new();
        for raw in id_strs {
            match jmap_server::parse_wire_id(raw, "id") {
                Ok(id) => ids.push(id),
                Err(err) => {
                    not_destroyed.insert(raw.to_owned(), serde_json::Value::from(&*err));
                }
            }
        }
        // A failed read is per-id `serverFail`, not a method error: the
        // creates and updates above are already committed, and a method
        // `serverFail` means "The method call made no changes to the
        // server's state" (RFC 8620 §3.6.2). Without the kinds no id can
        // be checked, so none is destroyed.
        let channel_ids: HashSet<Id> = if ids.is_empty() {
            HashSet::new()
        } else {
            match backend
                .get_objects::<Chat>(caller, &account_id, Some(&ids), None)
                .await
            {
                Ok((chats, _)) => chats
                    .into_iter()
                    .filter(|c| c.kind == ChatKind::Channel)
                    .map(|c| c.id)
                    .collect(),
                Err(e) => {
                    let err = server_fail_value_from_backend(&e);
                    for id in ids.drain(..) {
                        not_destroyed.insert(id.as_ref().to_owned(), err.clone());
                    }
                    HashSet::new()
                }
            }
        };
        for id in ids {
            let id_str = id.as_ref();
            if channel_ids.contains(&id) {
                not_destroyed.insert(
                    id_str.to_owned(),
                    json!({
                        "type": "forbidden",
                        "description": "channel Chats are removed via Space/set removeChannels",
                    }),
                );
                continue;
            }

            match backend
                .destroy_object::<Chat>(caller, &account_id, &id)
                .await
            {
                Ok(()) => {
                    mutated = true;
                    destroyed_list.push(Value::String(id_str.to_owned()));
                    backend.on_chat_destroyed(caller, &account_id, &id).await;
                }
                Err(e) => {
                    not_destroyed.insert(id_str.to_owned(), backend_set_error_value(e));
                }
            }
        }
    }

    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
///
/// This handler does not call [`ChatBackend::is_contact_blocked`]: that
/// check reads the recipient's contact list, and the request names only
/// the sender's `accountId`. The transport that fans the event out knows
/// each recipient's account and MUST call [`crate::should_deliver_typing`]
/// per recipient, which applies this check and the
/// `receiveTypingIndicators` and membership checks.
/// The wire response (echo `accountId`) is the same either way.
///
/// 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)?;

    // RFC 8620 §3.6.2 accountNotFound (bd:JMAP-dtpe).
    if !backend
        .account_exists(caller, &account_id)
        .await
        .map_err(|e| server_fail_from_backend(&e))?
    {
        return Err(JmapError::account_not_found());
    }

    let chat_id: Id = match args.get("chatId").and_then(|v| v.as_str()) {
        Some(s) => Id::new_validated(s)
            .map_err(|e| JmapError::invalid_arguments(format!("chatId: {e}")))?,
        None => 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))?;
    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")
        })?;

    // §Chat/typing Server Behavior: deliver a `typing` push event to the
    // other local participants and, "For federated Chats, the server SHOULD
    // invoke `Peer/typing`". The backend does both from this event.
    backend
        .on_outbound_event(
            caller,
            &account_id,
            &OutboundEvent::Typing { chat_id, typing },
        )
        .await;

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