Skip to content

Invites

Bases: BaseService

Typed Invites API.

Reads cover event listing, single-event lookup, RSVP listing, guest photo download (rsvp_image), and public share resolution (resolve / accept); writes cover rsvp(), cancel(), publish(), create_event(), and invite_via_link().

Source code in pyicloud/services/invites/service.py
 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
class InvitesService(BaseService):
    """Typed Invites API.

    Reads cover event listing, single-event lookup, RSVP listing, guest photo
    download (``rsvp_image``), and public share resolution (``resolve`` /
    ``accept``); writes cover ``rsvp()``, ``cancel()``, ``publish()``,
    ``create_event()``, and ``invite_via_link()``.
    """

    _CONTAINER = CONTAINER
    _ENV = ENV

    def __init__(
        self,
        service_root: str,
        session: Any,
        params: dict[str, str],
        *,
        cloudkit_validation_extra: CloudKitExtraMode | None = None,
    ) -> None:
        super().__init__(service_root=service_root, session=session, params=params)
        env_base_url = f"{self.service_root}/database/1/{self._CONTAINER}/{self._ENV}"
        base_params = {
            "remapEnums": True,
            "getCurrentSyncToken": True,
            **(params or {}),
        }
        self._raw = CloudKitInvitesClient(
            env_base_url,
            session,
            base_params,
            validation_extra=cloudkit_validation_extra,
        )
        self._feature_access: FeatureAccess | None = None

    @property
    def raw(self) -> CloudKitInvitesClient:
        """Escape hatch for advanced/unsupported CloudKit workflows."""
        return self._raw

    # ------------------------------------------------------------------
    # Public reads
    # ------------------------------------------------------------------

    def events(self) -> list[Event]:
        """Return all events visible to the current user (private + shared).

        Each event is populated only from its ``EventDetails`` record; the
        share and RSVPs are ``None`` / empty here. Call :meth:`event` for a
        fully-hydrated view.
        """
        out: list[Event] = []
        seen: set[tuple[EventScope, str]] = set()
        for scope in (EventScope.PRIVATE, EventScope.SHARED):
            for record in self._iter_event_details(scope):
                event_id = self._event_id_from_record_name(record.recordName)
                key = (scope, event_id)
                if key in seen:
                    continue
                seen.add(key)
                out.append(self._event_from_record(record, scope=scope))
        return out

    def event(self, event_id: str) -> Event:
        """Return a fully-hydrated event by ID (zoneName / UUID).

        Looks up the event in the private scope first, then shared. Raises
        :class:`EventNotFound` if no scope has the event.
        """
        for scope in (EventScope.PRIVATE, EventScope.SHARED):
            event = self._fetch_event_full(event_id, scope)
            if event is not None:
                return event
        raise EventNotFound(f"Event not found: {event_id!r}")

    def rsvps(self, event: Event) -> list[Rsvp]:
        """Return the list of RSVPs in an event's zone."""
        # A zone's changes feed can report a record more than once, or report a
        # tombstone for one it reported earlier; keep the surviving latest state.
        latest: dict[str, CKRecord] = {}
        for record in self._zone_records(
            self._scope_str(event.scope),
            self._zone_id_req(event.event_id, event.scope),
            InvitesRecordType.Rsvp.value,
        ):
            if isinstance(record, CKTombstoneRecord):
                latest.pop(record.recordName, None)
            else:
                latest[record.recordName] = record
        return [self._rsvp_from_record(record) for record in latest.values()]

    def _zone_records(
        self, scope_str: ScopeLiteral, zone_id: CKZoneIDReq, *record_types: str
    ) -> Iterator[CKRecord | CKTombstoneRecord]:
        """Yield a zone's changes for the given record types.

        Not by querying it: an event's own zone rejects ``records/query`` with
        "syncToken operations supported only in SyncZone", so a per-zone query
        works only for events shared with you. The changes feed reads any zone.

        ``desiredRecordTypes`` asks Apple for those records alone; the filter is
        kept because that projection is a request, not a guarantee. Tombstones
        are yielded too, unfiltered because they carry no ``recordType``, so
        callers reconcile by ``recordName``: keep the latest live record, and
        drop the name on a tombstone.
        """

        wanted = frozenset(record_types)
        zone_req = CKZoneChangesZoneReq(
            zoneID=CKZoneID(**zone_id.model_dump(exclude_none=True)),
            desiredRecordTypes=list(record_types),
        )
        for page in self._raw.iter_changes(scope_str, zone_req=zone_req):
            for record in page.records:
                if isinstance(record, CKTombstoneRecord) or (
                    isinstance(record, CKRecord) and record.recordType in wanted
                ):
                    yield record

    def _zone_rsvps_and_links(
        self, scope_str: ScopeLiteral, zone_id: CKZoneIDReq
    ) -> tuple[tuple[Rsvp, ...], tuple[OneTimeLinkGuest, ...]]:
        """Read a zone's RSVPs and one-time links in one changes-feed pass.

        Both record types live in the event's zone, and the changes feed is the
        only read an event you own supports. Records are reconciled by
        ``recordName`` (the feed can repeat one, and a tombstone deletes one),
        and records read before a mid-feed failure are kept: hydration is
        best-effort, and partial data beats none.
        """

        rsvps: dict[str, Rsvp] = {}
        links: dict[str, OneTimeLinkGuest] = {}
        try:
            for record in self._zone_records(
                scope_str,
                zone_id,
                InvitesRecordType.Rsvp.value,
                InvitesRecordType.OneTimeLinkGuestInfo.value,
            ):
                if isinstance(record, CKTombstoneRecord):
                    rsvps.pop(record.recordName, None)
                    links.pop(record.recordName, None)
                elif record.recordType == InvitesRecordType.Rsvp.value:
                    rsvps[record.recordName] = self._rsvp_from_record(record)
                else:
                    links[record.recordName] = self._one_time_link_from_record(record)
        except InvitesError:
            LOGGER.debug(
                "invites.event.zone_read_failed zone=%s",
                zone_id.zoneName,
                exc_info=True,
            )
        return tuple(rsvps.values()), tuple(links.values())

    def rsvp_image(self, rsvp: Rsvp) -> bytes | None:
        """Return the image a guest attached to their RSVP.

        ``None`` when they attached none, which is the common case: the field
        is optional and most responses carry only a monogram.

        The download goes through the private container's HTTP wrapper, which
        only supplies the session and timeouts -- the asset URL Apple returns
        is absolute and carries its own token, so an RSVP in a shared event
        downloads the same way as one in your own.
        """

        url = rsvp.image_download_url
        if not url:
            return None
        # The download uses the authenticated session, so an attacker-supplied
        # scheme would put iCloud credentials on the wire in the clear. Guarded
        # the same way PhotosUploader.send_bytes guards the upload URL Apple
        # echoes back to it, plus a real host check so a URL without a host,
        # such as ``https:example.com/a`` or ``https://@/a``, cannot slip
        # through.
        try:
            parsed = urlparse(url)
        except ValueError as exc:
            raise InvitesApiError(
                f"RSVP image URL is malformed; refusing to fetch it: {url!r}"
            ) from exc
        if parsed.scheme != "https" or not parsed.hostname:
            raise InvitesApiError(
                "RSVP image URL is not an absolute HTTPS URL; refusing to "
                f"fetch it: {url!r}"
            )
        return self._raw.download_asset_bytes(url)

    def resolve(self, short_guid: str) -> ResolvedShare:
        """Preview a share without joining it."""
        data = self._raw.resolve([short_guid])
        result = self._first_resolve_result(data)
        return self._resolved_share_from_result(result)

    def accept(self, short_guid: str) -> Event:
        """Accept a share invite. Returns the joined event (now in SHARED)."""
        data = self._raw.accept([short_guid])
        result = self._first_resolve_result(data)
        zone = result.get("zoneID") or {}
        event_id = zone.get("zoneName")
        if not isinstance(event_id, str):
            raise InvitesApiError(
                "Accept response missing zoneID.zoneName",
                payload=data,
            )
        scope = self._scope_from_db_scope(result.get("databaseScope"))
        event = self._fetch_event_full(event_id, scope)
        if event is None:
            # Fall back to the other scope in case of routing quirks.
            other = (
                EventScope.PRIVATE if scope == EventScope.SHARED else EventScope.SHARED
            )
            event = self._fetch_event_full(event_id, other)
        if event is None:
            raise EventNotFound(
                f"Accepted event {event_id!r} not visible in either scope"
            )
        return event

    # ------------------------------------------------------------------
    # Public writes
    # ------------------------------------------------------------------

    def create_event(
        self,
        title: str,
        time: EventTime,
        *,
        place: EventPlace | None = None,
        notes: str = "",
        max_attendees: int | None = None,
        max_additional_guests_per_rsvp: int = 0,
        is_private: bool = False,
    ) -> Event:
        """Create an unpublished hosted event with a share, but no guests.

        Dates must be timezone-aware. Entitlement is checked before creating a
        zone. After a confirmed zone creation, subsequent failures trigger a
        best-effort deletion of that new zone only. InvitesCreationError retains
        its ID, cleanup outcome, and the original failure as its cause. An
        unconfirmed zone creation is not automatically deleted or retried. The
        created share may not identify the current user as a participant yet;
        refetch via ``event()`` before ``rsvp()``.
        """
        fields, preview = creation_fields(
            title,
            time,
            place=place,
            notes=notes,
            max_attendees=max_attendees,
            max_additional_guests_per_rsvp=max_additional_guests_per_rsvp,
            is_private=is_private,
        )
        fields[EventDetailsField.HINT.value] = {
            "type": "STRING",
            "value": json.dumps({"subscriptionAccessToken": self._entitlement_token()}),
        }
        event_id = str(uuid4()).upper()
        zone_id = CKZoneIDReq(zoneName=event_id)
        zone_created = False
        try:
            self._modify_new_zone(event_id, "create")
            zone_created = True
            record_name = f"{EVENT_DETAILS_RECORD_NAME_PREFIX}{event_id}"
            response = self._raw.modify(
                "private",
                zone_id=zone_id,
                atomic=True,
                operations=[
                    CKModifyOperation(
                        operationType="create",
                        record=CKWriteRecord(
                            recordName=record_name,
                            recordType=InvitesRecordType.EventDetails.value,
                            fields=fields,
                        ),
                    )
                ],
            )
            event = self._event_from_modify_response(
                response, record_name, EventScope.PRIVATE
            )
            if not event.record_change_tag:
                raise InvitesApiError("Created event response has no change tag")
            share_response = self._raw.modify(
                "private",
                zone_id=zone_id,
                atomic=True,
                operations=[
                    CKModifyOperation(
                        operationType="create",
                        record=CKWriteRecord.model_validate({
                            "recordName": SHARE_RECORD_NAME,
                            "recordType": InvitesRecordType.Share.value,
                            "fields": preview,
                            "participants": [],
                            "requesters": [],
                            "blocked": [],
                            "publicPermission": "NONE",
                            "denyAccessRequests": True,
                        }),
                    )
                ],
            )
            for record in self._records_of(share_response):
                if (
                    record.recordName == SHARE_RECORD_NAME
                    and record.recordType == InvitesRecordType.Share.value
                ):
                    share = self._share_from_record(record)
                    if share.short_guid:
                        return event.model_copy(update={"share": share})
            raise InvitesApiError("Created share response has no usable shortGUID")
        except Exception as exc:
            cleanup: bool | None = None
            cleanup_error: Exception | None = None
            if zone_created:
                try:
                    self._modify_new_zone(event_id, "delete")
                    cleanup = True
                except Exception as cleanup_exc:
                    cleanup = False
                    cleanup_error = cleanup_exc
            raise InvitesCreationError(
                event_id, cleanup_succeeded=cleanup, cleanup_error=cleanup_error
            ) from exc

    def _modify_new_zone(
        self, event_id: str, operation: Literal["create", "delete"]
    ) -> None:
        response = self._raw.zones_modify([
            CKZoneModifyOperation(
                operationType=operation,
                zone=CKZoneListZone(zoneID=CKZoneID(zoneName=event_id)),
            )
        ])
        if not any(
            zone.zoneID.zoneName == event_id
            and bool(zone.deleted) == (operation == "delete")
            for zone in response.zones
        ):
            raise InvitesApiError("Zone modification was not confirmed")

    def invite_via_link(
        self,
        event: Event,
        *,
        emails: Sequence[str] = (),
        phone_numbers: Sequence[str] = (),
        name: str = "",
    ) -> OneTimeLinkInvitation:
        """Create a participant-specific invitation for an event you host.

        Experimental: verified for email invitations on a controlled account;
        push notifications occurred during live tests. Email delivery and
        phone-only invitations remain unverified.
        Contacts are guest metadata, not an account lookup or an email-send API.
        Dates and publication are not modified. The latest owner share is read
        before an atomic guest-info create/share update. There are no retries.

        A failure after submission raises InvitesInvitationError with the event
        and participant IDs and the original cause. Inspect those records before
        retrying: a missing or malformed response does not imply rollback.
        """
        if event.scope is not EventScope.PRIVATE:
            raise InvitesApiError("Only hosted events can receive guest invitations")
        contacts = invitation_contacts(name, emails, phone_numbers)
        zone_id = self._zone_id_req(event.event_id, event.scope)
        share = owner_share(
            self._raw.lookup("private", [SHARE_RECORD_NAME], zone_id=zone_id)
        )
        participant_id = str(uuid4())
        guest = OneTimeLinkGuest(
            record_name=f"{participant_id}{ONE_TIME_LINK_RECORD_NAME_SUFFIX}",
            participant_id=participant_id,
            name=contacts.name,
            emails=contacts.emails,
            phone_numbers=contacts.phone_numbers,
        )
        operations = invitation_operations(share, guest)
        try:
            response = self._raw.modify(
                "private", operations=operations, zone_id=zone_id, atomic=True
            )
            guest_record, updated_share, token = invitation_records(
                response, share, guest
            )
            return OneTimeLinkInvitation(
                **self._one_time_link_from_record(guest_record).model_dump(),
                short_guid=token,
                record_change_tag=cast(str, guest_record.recordChangeTag),
                share_record_change_tag=cast(str, updated_share.recordChangeTag),
            )
        except Exception as exc:
            raise InvitesInvitationError(event.event_id, participant_id) from exc

    def rsvp(
        self,
        event: Event,
        status: RsvpStatus,
        *,
        name: str | None = None,
        message: str | None = None,
        plus_one_adults: int = 0,
        plus_one_kids: int = 0,
    ) -> Rsvp:
        """Submit or update the current user's RSVP for ``event``.

        Dispatches to ``private`` if the current user owns the event, or
        ``shared`` if they're a guest (per ``event.scope``). Creates the RSVP
        record on first response and updates it on subsequent calls.

        For :attr:`RsvpStatus.NOT_GOING`, plus-one counts are zeroed to mirror
        the iCloud web UI behavior; the ``plus_one_*`` kwargs are ignored in
        that case.

        Returns the freshly written :class:`Rsvp` (including the new
        ``record_change_tag``).
        """
        if plus_one_adults < 0 or plus_one_kids < 0:
            raise InvitesApiError(
                "plus_one_adults and plus_one_kids must be non-negative; "
                f"got plus_one_adults={plus_one_adults!r}, "
                f"plus_one_kids={plus_one_kids!r}"
            )

        participant_id = self._current_participant_id(event)
        record_name = f"{participant_id}{RSVP_RECORD_NAME_SUFFIX}"
        existing = self._find_existing_rsvp(event, record_name)

        if status == RsvpStatus.NOT_GOING:
            plus_one_adults = 0
            plus_one_kids = 0

        fields: dict[str, Any] = {
            RsvpField.STATUS.value: {
                "type": "INT64",
                "value": status.value,
                "isEncrypted": True,
            },
            RsvpField.NUM_ADDITIONAL_ADULTS.value: {
                "type": "INT64",
                "value": plus_one_adults,
                "isEncrypted": True,
            },
            RsvpField.NUM_ADDITIONAL_KIDS.value: {
                "type": "INT64",
                "value": plus_one_kids,
                "isEncrypted": True,
            },
            RsvpField.NUM_ADDITIONAL_GUESTS.value: {
                "type": "INT64",
                "value": plus_one_adults + plus_one_kids,
                "isEncrypted": True,
            },
        }
        if name is not None:
            fields[RsvpField.NAME.value] = {
                "type": "STRING",
                "value": name,
                "isEncrypted": True,
            }
        if message is not None:
            fields[RsvpField.MESSAGE.value] = {
                "type": "STRING",
                "value": message,
                "isEncrypted": True,
            }

        record_change_tag = existing.record_change_tag if existing else None
        op = CKModifyOperation(
            operationType="update" if existing else "create",
            record=CKWriteRecord(
                recordName=record_name,
                recordType=InvitesRecordType.Rsvp.value,
                recordChangeTag=record_change_tag,
                fields=cast(CKWriteFields, fields),
            ),
        )

        scope_str = self._scope_str(event.scope)
        zone_id = self._zone_id_req(event.event_id, event.scope)
        response: CKModifyResponse = self._raw.modify(
            scope_str,
            operations=[op],
            zone_id=zone_id,
            atomic=True,
        )
        return self._rsvp_from_modify_response(response, record_name)

    def cancel(self, event: Event, *, cancelled: bool = True) -> Event:
        """Cancel an event you host, or reinstate one you cancelled.

        Apple accepts both directions, so this is a symmetric switch rather
        than a one-way verb -- an accidental cancel is recoverable.

        ``event`` must carry a current record change tag, so pass one from
        ``event()`` or from a previous write rather than reusing a stale
        object: Apple rejects a stale tag with an opaque ``502``.
        """

        return self._set_event_flag(event, EventDetailsField.IS_CANCELLED, cancelled)

    def publish(self, event: Event, *, published: bool = True) -> Event:
        """Publish an event you host, or return it to an unpublished state.

        Pass an event with a current record change tag from ``event()`` or a
        previous write. Both EventDetails and its share are updated atomically;
        no modify is sent unless the share's current tag could be read. A share
        with no readable tag raises, and a failed lookup surfaces its own error
        rather than a generic refusal. The returned event carries the new tag,
        but is not rehydrated with share/RSVP data.
        """
        if event.scope is not EventScope.PRIVATE:
            raise InvitesApiError("Only events you host can be published.")
        return self._set_event_flag(event, EventDetailsField.IS_PUBLISHED, published)

    # ------------------------------------------------------------------
    # Internal: write helpers
    # ------------------------------------------------------------------

    def _entitlement_token(self) -> str:
        """Return a token proving this account may edit events.

        Cached until shortly before Apple's own ``cacheTill``: the grant lasts
        a week, and fetching one per write would add a round trip for nothing.
        """

        now = datetime.now(timezone.utc)
        cached = self._feature_access
        if cached is not None and cached.usable_at(now):
            return cast(str, cached.access_token)

        access = self._raw.feature_access(CREATE_EVENT_FEATURE)
        if not access.can_use or not access.access_token:
            raise InvitesEntitlementError(
                "This account cannot edit events. Apple gates them behind a "
                f"subscription feature ({CREATE_EVENT_FEATURE}) and reports "
                "it as unavailable here."
            )
        self._feature_access = access
        return access.access_token

    def _set_event_flag(
        self, event: Event, field: EventDetailsField, value: bool
    ) -> Event:
        """Set one boolean flag on an event, and on its share.

        Two operations in one atomic modify, because the share record keeps its
        own copies of these flags and does **not** follow the event: cancelling
        only the event leaves the share reading as live to guests.

        The ``hint`` field carries the entitlement token. Without it Apple
        answers ``502 INTERNAL_ERROR`` with no explanation at all.

        With ``remapEnums=true``, publication uses an ``INT64`` carrying
        ``isEncrypted: true`` on EventDetails and a plain ``INT64`` on the share.
        Omitting the marker causes ``expected type ENCRYPTED_NUMBER_INT64``;
        client-side ciphertext is not required. Cancellation remains plain.

        Publication reads the share's tag strictly: a failed lookup propagates
        its own error, and a share carrying no tag refuses the write outright,
        whereas cancellation keeps its event-only fallback.
        """

        if not event.record_change_tag:
            raise InvitesApiError(
                f"Event {event.event_id!r} has no record change tag; fetch it "
                "via InvitesService.event(...) before writing to it."
            )

        hint = json.dumps({"subscriptionAccessToken": self._entitlement_token()})
        publishing = field is EventDetailsField.IS_PUBLISHED
        flag: dict[str, Any] = {"type": "INT64", "value": int(value)}
        event_flag = dict(flag)
        if publishing:
            event_flag["isEncrypted"] = True
        scope_str = self._scope_str(event.scope)
        zone_id = self._zone_id_req(event.event_id, event.scope)

        operations = [
            CKModifyOperation(
                operationType="update",
                record=CKWriteRecord(
                    recordName=(f"{EVENT_DETAILS_RECORD_NAME_PREFIX}{event.event_id}"),
                    recordType=InvitesRecordType.EventDetails.value,
                    recordChangeTag=event.record_change_tag,
                    fields=cast(
                        CKWriteFields,
                        {
                            field.value: event_flag,
                            EventDetailsField.HINT.value: {
                                "type": "STRING",
                                "value": hint,
                            },
                        },
                    ),
                ),
            )
        ]

        share_tag = self._share_change_tag(scope_str, zone_id, strict=publishing)
        if publishing and not share_tag:
            raise InvitesApiError(
                "Cannot publish an event without its share's current change tag; "
                "no changes were sent."
            )
        if share_tag is not None:
            operations.append(
                CKModifyOperation(
                    operationType="update",
                    record=CKWriteRecord(
                        recordName=SHARE_RECORD_NAME,
                        recordType=InvitesRecordType.Share.value,
                        recordChangeTag=share_tag,
                        fields=cast(CKWriteFields, {field.value: flag}),
                    ),
                )
            )

        response: CKModifyResponse = self._raw.modify(
            scope_str, operations=operations, zone_id=zone_id, atomic=True
        )
        return self._event_from_modify_response(
            response,
            f"{EVENT_DETAILS_RECORD_NAME_PREFIX}{event.event_id}",
            event.scope,
        )

    def _share_change_tag(
        self, scope_str: ScopeLiteral, zone_id: CKZoneIDReq, *, strict: bool = False
    ) -> str | None:
        """Return the share record's own change tag, or None if unreadable.

        ``EventShare`` does not carry it and an update needs it, so read it
        back from the same lookup ``event()`` already uses. An API error
        reading the share still gets its event updated rather than nothing;
        auth and rate-limit failures propagate instead, because the write that
        follows would hit the same wall.

        ``strict`` is for publication, which refuses to write without the tag:
        the lookup's own failure then propagates with its message and payload
        instead of degrading to ``None``, so callers can tell a failed read
        apart from a share that carries no tag. A per-record error item in the
        lookup answer (anything but ``NOT_FOUND``, which only means the share
        is absent) is treated the same way in strict mode; ``NOT_FOUND`` is
        reported by the caller's missing-tag refusal instead.
        """

        try:
            resp = self._raw.lookup(scope_str, [SHARE_RECORD_NAME], zone_id=zone_id)
        except InvitesApiError:
            if strict:
                raise
            LOGGER.debug("invites.share_change_tag_failed", exc_info=True)
            return None
        error = next(
            (
                item
                for item in resp.records
                if isinstance(item, CKErrorItem) and item.serverErrorCode != "NOT_FOUND"
            ),
            None,
        )
        if error is not None:
            message = f"Share lookup failed: {error.serverErrorCode}"
            if error.reason:
                message = f"{message} ({error.reason})"
            if strict:
                raise InvitesApiError(
                    message,
                    payload=error.model_dump(mode="json", exclude_none=True),
                )
            LOGGER.debug("invites.share_lookup_error code=%s", error.serverErrorCode)
            return None
        for record in self._records_of(resp):
            if record.recordName == SHARE_RECORD_NAME:
                return record.recordChangeTag
        return None

    def _event_from_modify_response(
        self, response: CKModifyResponse, record_name: str, scope: EventScope
    ) -> Event:
        """Pick the event record out of a modify response and convert it.

        Carries the new change tag so writes can be chained. ``share`` and
        ``rsvps`` are unset, as in ``events()``.
        """

        for record in response.records:
            if (
                isinstance(record, CKRecord)
                and record.recordName == record_name
                and record.recordType == InvitesRecordType.EventDetails.value
            ):
                return self._event_from_record(record, scope=scope)
        raise InvitesApiError(
            f"Modify response did not return {record_name!r}",
            payload=response.model_dump(mode="json"),
        )

    @staticmethod
    def _current_participant_id(event: Event) -> str:
        """Return the current user's participant_id on ``event``'s share."""
        if event.share is None:
            raise InvitesApiError(
                f"Event {event.event_id!r} has no loaded share; "
                "fetch the event via InvitesService.event(...) first."
            )
        participant_id = event.share.current_user_participant_id
        if not participant_id:
            raise InvitesApiError(
                f"Event {event.event_id!r} share has no currentUserParticipant; "
                "the user may not be a participant on this event."
            )
        return participant_id

    @staticmethod
    def _find_existing_rsvp(event: Event, record_name: str) -> Rsvp | None:
        """Find the user's RSVP in ``event.rsvps`` matching ``record_name``."""
        for rsvp in event.rsvps:  # noqa: S3862
            if rsvp.record_name == record_name:
                return rsvp
        return None

    def _rsvp_from_modify_response(
        self, response: CKModifyResponse, record_name: str
    ) -> Rsvp:
        """Pick the RSVP record from a modify response and convert to DTO."""
        for record in response.records:
            if (
                isinstance(record, CKRecord)
                and record.recordName == record_name
                and record.recordType == InvitesRecordType.Rsvp.value
            ):
                return self._rsvp_from_record(record)
        raise InvitesApiError(
            f"RSVP modify response missing record {record_name!r}",
            payload=response.model_dump(mode="json", exclude_none=True),
        )

    # ------------------------------------------------------------------
    # Internal: zone-level helpers
    # ------------------------------------------------------------------

    @staticmethod
    def _scope_str(scope: EventScope) -> ScopeLiteral:
        return "private" if scope == EventScope.PRIVATE else "shared"

    @staticmethod
    def _scope_from_db_scope(db_scope: str | None) -> EventScope:
        if isinstance(db_scope, str) and db_scope.upper() == "SHARED":
            return EventScope.SHARED
        return EventScope.PRIVATE

    @staticmethod
    def _event_id_from_record_name(record_name: str) -> str:
        if record_name.startswith(EVENT_DETAILS_RECORD_NAME_PREFIX):
            return record_name[len(EVENT_DETAILS_RECORD_NAME_PREFIX) :]
        return record_name

    def _zone_id_req(self, event_id: str, scope: EventScope) -> CKZoneIDReq:
        """Build the zone reference for an event.

        A shared zone is only addressable with its original owner, so without
        ownerRecordName a shared lookup finds nothing and the caller sees
        EventNotFound for an event ``events()`` just returned. The owner comes
        from the shared /zones/list entry whose zoneName is the event id.
        """

        owner = (
            self._shared_zone_owner(event_id) if scope is EventScope.SHARED else None
        )
        return CKZoneIDReq(
            zoneName=event_id,
            zoneType="REGULAR_CUSTOM_ZONE",
            ownerRecordName=owner,
        )

    def _shared_zone_owner(self, event_id: str) -> str | None:
        """Return the owner of the shared zone holding ``event_id``, if any."""

        try:
            zones = self._raw.zones_list(self._scope_str(EventScope.SHARED)).zones or []
        except InvitesError:
            LOGGER.debug("invites.shared_zone_owner_failed", exc_info=True)
            return None
        for zone in zones:
            if zone.zoneID.zoneName == event_id:
                return zone.zoneID.ownerRecordName
        return None

    @staticmethod
    def _records_of(
        resp: CKQueryResponse | CKLookupResponse | CKModifyResponse,
    ) -> list[CKRecord]:
        return [r for r in resp.records if isinstance(r, CKRecord)]

    def _iter_event_details(self, scope: EventScope) -> Iterable[CKRecord]:
        if scope is EventScope.SHARED:
            return self._shared_event_details()

        try:
            resp = self._raw.query(
                self._scope_str(scope),
                query=CKQueryObject(
                    recordType=InvitesRecordType.EventDetails.value,
                ),
                zone_wide=True,
            )
        except InvitesError:
            LOGGER.debug(
                "invites.events.query_failed scope=%s", scope.value, exc_info=True
            )
            return []
        return self._records_of(resp)

    def _shared_event_details(self) -> list[CKRecord]:
        """Enumerate shared events one zone at a time.

        CloudKit refuses a zone-wide query against the shared database --
        "SharedDB does not support Zone Wide queries" -- so the shared scope is
        read by listing the zones shared with this account and taking the
        records of each. A zone that fails is skipped rather than failing the
        whole call, since one unreadable share should not hide the others.
        """

        scope_str = self._scope_str(EventScope.SHARED)
        try:
            zones = self._raw.zones_list(scope_str).zones or []
        except InvitesError:
            LOGGER.debug("invites.events.zones_list_failed", exc_info=True)
            return []

        wanted = InvitesRecordType.EventDetails.value
        records: list[CKRecord] = []
        for zone in zones:
            try:
                # Every page, not just the first: a zone with more behind it
                # would otherwise drop events with no sign anything was lost.
                pages = list(
                    self._raw.iter_changes(
                        scope_str,
                        zone_req=CKZoneChangesZoneReq(zoneID=zone.zoneID),
                    )
                )
            except InvitesError:
                LOGGER.debug(
                    "invites.events.zone_changes_failed zone=%s",
                    zone.zoneID.zoneName,
                    exc_info=True,
                )
                continue
            records.extend(
                record
                for page in pages
                for record in page.records
                if isinstance(record, CKRecord) and record.recordType == wanted
            )
        return records

    def _fetch_event_full(self, event_id: str, scope: EventScope) -> Event | None:
        zone_id = self._zone_id_req(event_id, scope)
        scope_str = self._scope_str(scope)
        try:
            resp = self._raw.lookup(
                scope_str,
                [
                    f"{EVENT_DETAILS_RECORD_NAME_PREFIX}{event_id}",
                    SHARE_RECORD_NAME,
                ],
                zone_id=zone_id,
            )
        except InvitesError:
            LOGGER.debug(
                "invites.event.lookup_failed event_id=%s scope=%s",
                event_id,
                scope.value,
                exc_info=True,
            )
            return None

        event_record: CKRecord | None = None
        share_record: CKRecord | None = None
        for record in self._records_of(resp):
            if record.recordType == InvitesRecordType.EventDetails.value:
                event_record = record
            elif record.recordType == InvitesRecordType.Share.value:
                share_record = record
        if event_record is None:
            return None

        share = self._share_from_record(share_record) if share_record else None

        rsvps, otl_guests = self._zone_rsvps_and_links(scope_str, zone_id)

        if share is not None and otl_guests:
            share = share.model_copy(update={"one_time_links": otl_guests})

        return self._event_from_record(
            event_record, scope=scope, share=share, rsvps=rsvps
        )

    # ------------------------------------------------------------------
    # Internal: record -> DTO mapping
    # ------------------------------------------------------------------

    def _event_from_record(
        self,
        record: CKRecord,
        *,
        scope: EventScope,
        share: EventShare | None = None,
        rsvps: tuple[Rsvp, ...] = (),
    ) -> Event:
        event_id = self._event_id_from_record_name(record.recordName)
        fields = record.fields

        title = self._field_str(fields, EventDetailsField.TITLE.value, default="") or ""
        notes = self._field_str(fields, EventDetailsField.NOTES.value, default="") or ""
        host_display_name = (
            self._field_str(
                fields, EventDetailsField.HOST_DISPLAY_NAME.value, default=""
            )
            or ""
        )

        is_published = self._field_bool(fields, EventDetailsField.IS_PUBLISHED.value)
        is_private = self._field_bool(fields, EventDetailsField.IS_PRIVATE.value)
        is_cancelled = self._field_bool(fields, EventDetailsField.IS_CANCELLED.value)
        block_new_rsvps = self._field_bool(
            fields, EventDetailsField.BLOCK_NEW_RSVPS.value
        )

        max_attendees = self._field_int(fields, EventDetailsField.MAX_ATTENDEES.value)
        max_additional = (
            self._field_int(
                fields, EventDetailsField.MAX_ADDITIONAL_GUESTS_PER_RSVP.value
            )
            or 0
        )

        time_obj = self._decode_time(fields)
        place_obj = self._decode_place(fields)
        background = self._decode_json(fields, EventDetailsField.BACKGROUND.value) or {}
        style = self._decode_json(fields, EventDetailsField.STYLE.value) or {}
        integrations_blob = self._decode_json(
            fields, EventDetailsField.INTEGRATIONS.value
        )
        integrations = decode_integrations(integrations_blob)

        created_ts = record.created.timestamp if record.created else None
        modified_ts = record.modified.timestamp if record.modified else None

        return Event(
            event_id=event_id,
            scope=scope,
            record_change_tag=record.recordChangeTag,
            title=title,
            notes=notes,
            host_display_name=host_display_name,
            is_published=is_published,
            is_private=is_private,
            is_cancelled=is_cancelled,
            block_new_rsvps=block_new_rsvps,
            max_attendees=max_attendees,
            max_additional_guests_per_rsvp=max_additional,
            time=time_obj,
            place=place_obj,
            background=background if isinstance(background, dict) else {},
            style=style if isinstance(style, dict) else {},
            integrations=integrations,
            created_timestamp=created_ts,
            modified_timestamp=modified_ts,
            share=share,
            rsvps=rsvps,
        )

    def _share_from_record(self, record: CKRecord) -> EventShare:
        short_guid = record.shortGUID or ""
        public_permission = record.publicPermission or "NONE"
        participants = tuple(
            self._participant_from_ck(p) for p in (record.participants or [])
        )
        current_user_participant_id: str | None = None
        current = getattr(record, "currentUserParticipant", None)
        if current is not None:
            current_user_participant_id = getattr(current, "participantId", None)
        return EventShare(
            short_guid=short_guid,
            public_permission=public_permission,
            participants=participants,
            one_time_links=(),
            current_user_participant_id=current_user_participant_id,
        )

    @staticmethod
    def _participant_from_ck(p: Any) -> Participant:
        identity = getattr(p, "userIdentity", None)
        name_components = (
            getattr(identity, "nameComponents", None) if identity else None
        )
        lookup_info = getattr(identity, "lookupInfo", None) if identity else None
        return Participant(
            participant_id=getattr(p, "participantId", "") or "",
            user_record_name=(
                getattr(identity, "userRecordName", None) if identity else None
            ),
            given_name=(
                getattr(name_components, "givenName", None) if name_components else None
            ),
            family_name=(
                getattr(name_components, "familyName", None)
                if name_components
                else None
            ),
            email=(getattr(lookup_info, "emailAddress", None) if lookup_info else None),
            type=InvitesService._safe_participant_type(getattr(p, "type", None)),
            acceptance_status=InvitesService._safe_acceptance_status(
                getattr(p, "acceptanceStatus", None)
            ),
            permission=getattr(p, "permission", "READ_ONLY") or "READ_ONLY",
        )

    @staticmethod
    def _safe_participant_type(value: Any) -> ParticipantType:
        """Map a wire participant ``type`` to the enum, defaulting on unknowns."""
        try:
            return ParticipantType(str(value or "USER"))
        except ValueError:
            LOGGER.debug("invites.participant.unknown_type %r", value)
            return ParticipantType.USER

    @staticmethod
    def _safe_acceptance_status(value: Any) -> AcceptanceStatus:
        """Map a wire ``acceptanceStatus`` to the enum, defaulting on unknowns."""
        try:
            return AcceptanceStatus(str(value or "INVITED"))
        except ValueError:
            LOGGER.debug("invites.participant.unknown_acceptance %r", value)
            return AcceptanceStatus.INVITED

    def _rsvp_from_record(self, record: CKRecord) -> Rsvp:
        record_name = record.recordName
        suffix = "_rsvp"
        participant_id = (
            record_name[: -len(suffix)] if record_name.endswith(suffix) else record_name
        )
        fields = record.fields
        status_int = self._field_int(fields, RsvpField.STATUS.value) or 0
        try:
            status = RsvpStatus(status_int)
        except ValueError:
            status = RsvpStatus.NO_RESPONSE
        image_url = self._asset_download_url(fields, RsvpField.IMAGE.value)
        return Rsvp(
            record_name=record_name,
            participant_id=participant_id,
            name=self._field_str(fields, RsvpField.NAME.value, default="") or "",
            status=status,
            message=self._field_str(fields, RsvpField.MESSAGE.value, default=None),
            num_additional_adults=(
                self._field_int(fields, RsvpField.NUM_ADDITIONAL_ADULTS.value) or 0
            ),
            num_additional_kids=(
                self._field_int(fields, RsvpField.NUM_ADDITIONAL_KIDS.value) or 0
            ),
            image_download_url=image_url,
            record_change_tag=record.recordChangeTag,
        )

    def _one_time_link_from_record(self, record: CKRecord) -> OneTimeLinkGuest:
        record_name = record.recordName
        suffix = "_otl"
        participant_id = (
            record_name[: -len(suffix)] if record_name.endswith(suffix) else record_name
        )
        fields = record.fields
        emails_raw = self._field_value(fields, OneTimeLinkField.EMAILS.value)
        phones_raw = self._field_value(fields, OneTimeLinkField.PHONE_NUMBERS.value)
        return OneTimeLinkGuest(
            record_name=record_name,
            participant_id=participant_id,
            name=self._field_str(fields, OneTimeLinkField.NAME.value, default="") or "",
            emails=tuple(emails_raw) if isinstance(emails_raw, list) else (),
            phone_numbers=tuple(phones_raw) if isinstance(phones_raw, list) else (),
        )

    # ------------------------------------------------------------------
    # Internal: resolve/accept response parsing
    # ------------------------------------------------------------------

    @staticmethod
    def _first_resolve_result(data: Mapping[str, Any]) -> dict[str, Any]:
        results = data.get("results")
        if not isinstance(results, list) or not results:
            raise InvitesApiError(
                "Resolve/accept response missing results",
                payload=data,
            )
        first = results[0]
        if not isinstance(first, dict):
            raise InvitesApiError(
                "Resolve/accept response result not a dict",
                payload=data,
            )
        return first

    def _resolved_share_from_result(  # noqa: S3776
        self, result: Mapping[str, Any]
    ) -> ResolvedShare:
        short = result.get("shortGUID") or {}
        short_guid = short.get("value") if isinstance(short, dict) else None
        if not isinstance(short_guid, str):
            raise InvitesApiError(
                "Resolve response missing shortGUID.value", payload=dict(result)
            )
        zone = result.get("zoneID") or {}
        event_id = zone.get("zoneName") if isinstance(zone, dict) else None
        if not isinstance(event_id, str):
            raise InvitesApiError(
                "Resolve response missing zoneID.zoneName", payload=dict(result)
            )

        owner_identity = result.get("ownerIdentity") or {}
        owner_name_components = (
            owner_identity.get("nameComponents")
            if isinstance(owner_identity, dict)
            else None
        )
        owner_lookup_info = (
            owner_identity.get("lookupInfo")
            if isinstance(owner_identity, dict)
            else None
        )

        share_raw = result.get("share") or {}
        if isinstance(share_raw, dict):
            try:
                share_record = CKRecord.model_validate(share_raw)
                share = self._share_from_record(share_record)
            except Exception:
                share = EventShare(
                    short_guid=short_guid,
                    public_permission="NONE",
                )
        else:
            share = EventShare(short_guid=short_guid, public_permission="NONE")

        return ResolvedShare(
            short_guid=short_guid,
            event_id=event_id,
            owner_record_name=(
                owner_identity.get("userRecordName")
                if isinstance(owner_identity, dict)
                else None
            ),
            owner_email=(
                owner_lookup_info.get("emailAddress")
                if isinstance(owner_lookup_info, dict)
                else None
            ),
            owner_given_name=(
                owner_name_components.get("givenName")
                if isinstance(owner_name_components, dict)
                else None
            ),
            owner_family_name=(
                owner_name_components.get("familyName")
                if isinstance(owner_name_components, dict)
                else None
            ),
            participant_status=str(result.get("participantStatus") or ""),
            participant_type=str(result.get("participantType") or ""),
            participant_permission=str(result.get("participantPermission") or ""),
            share=share,
        )

    # ------------------------------------------------------------------
    # Internal: field-extraction helpers
    # ------------------------------------------------------------------

    @staticmethod
    def _field_value(fields: Any, key: str) -> Any:
        if hasattr(fields, "get_value"):
            return fields.get_value(key)
        if hasattr(fields, "get"):
            wrapper = fields.get(key)
            return getattr(wrapper, "value", None)
        return None

    def _field_str(self, fields: Any, key: str, *, default: str | None) -> str | None:
        value = self._field_value(fields, key)
        if isinstance(value, str):
            return value
        if isinstance(value, bytes):
            try:
                return value.decode("utf-8")
            except UnicodeDecodeError:
                return default
        return default

    def _field_int(self, fields: Any, key: str) -> int | None:
        value = self._field_value(fields, key)
        if isinstance(value, bool):
            return int(value)
        if isinstance(value, int):
            return value
        return None

    def _field_bool(self, fields: Any, key: str) -> bool:
        value = self._field_value(fields, key)
        if isinstance(value, bool):
            return value
        if isinstance(value, int):
            return value != 0
        return False

    def _decode_json(self, fields: Any, key: str) -> Any:
        value = self._field_value(fields, key)
        return decode_json_bytes(value)

    def _decode_time(self, fields: Any) -> EventTime | None:
        blob = self._decode_json(fields, EventDetailsField.TIME.value)
        if not isinstance(blob, Mapping):
            return None
        start_ms = blob.get("startSince1970")
        if not isinstance(start_ms, (int, float)):
            return None
        try:
            start = datetime.fromtimestamp(start_ms / 1000.0, tz=timezone.utc)
        except (OSError, ValueError, OverflowError):
            return None
        end: datetime | None = None
        end_ms = blob.get("endSince1970")
        if isinstance(end_ms, (int, float)):
            try:
                end = datetime.fromtimestamp(end_ms / 1000.0, tz=timezone.utc)
            except (OSError, ValueError, OverflowError):
                end = None
        return EventTime(
            start=start,
            end=end,
            is_all_day=bool(blob.get("isAllDay", False)),
            is_open_ended=bool(blob.get("isOpenEnded", False)),
        )

    def _decode_place(self, fields: Any) -> EventPlace | None:
        blob = self._decode_json(fields, EventDetailsField.PLACE.value)
        if not isinstance(blob, Mapping):
            return None
        return EventPlace(
            title=blob.get("title") if isinstance(blob.get("title"), str) else None,
            subtitle=(
                blob.get("subtitle") if isinstance(blob.get("subtitle"), str) else None
            ),
            city=blob.get("city") if isinstance(blob.get("city"), str) else None,
            time_zone_identifier=(
                blob.get("timeZoneIdentifier")
                if isinstance(blob.get("timeZoneIdentifier"), str)
                else None
            ),
            latitude=(
                float(blob["latitude"])
                if isinstance(blob.get("latitude"), (int, float))
                else None
            ),
            longitude=(
                float(blob["longitude"])
                if isinstance(blob.get("longitude"), (int, float))
                else None
            ),
            url=blob.get("url") if isinstance(blob.get("url"), str) else None,
        )

    @staticmethod
    def _asset_download_url(fields: Any, key: str) -> str | None:
        value = InvitesService._field_value(fields, key)
        if value is None:
            return None
        url = getattr(value, "downloadURL", None)
        if isinstance(url, str):
            return url
        if isinstance(value, Mapping):
            maybe = value.get("downloadURL")
            return maybe if isinstance(maybe, str) else None
        return None

raw property

Escape hatch for advanced/unsupported CloudKit workflows.

events()

Return all events visible to the current user (private + shared).

Each event is populated only from its EventDetails record; the share and RSVPs are None / empty here. Call :meth:event for a fully-hydrated view.

Source code in pyicloud/services/invites/service.py
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
def events(self) -> list[Event]:
    """Return all events visible to the current user (private + shared).

    Each event is populated only from its ``EventDetails`` record; the
    share and RSVPs are ``None`` / empty here. Call :meth:`event` for a
    fully-hydrated view.
    """
    out: list[Event] = []
    seen: set[tuple[EventScope, str]] = set()
    for scope in (EventScope.PRIVATE, EventScope.SHARED):
        for record in self._iter_event_details(scope):
            event_id = self._event_id_from_record_name(record.recordName)
            key = (scope, event_id)
            if key in seen:
                continue
            seen.add(key)
            out.append(self._event_from_record(record, scope=scope))
    return out

event(event_id)

Return a fully-hydrated event by ID (zoneName / UUID).

Looks up the event in the private scope first, then shared. Raises :class:EventNotFound if no scope has the event.

Source code in pyicloud/services/invites/service.py
171
172
173
174
175
176
177
178
179
180
181
def event(self, event_id: str) -> Event:
    """Return a fully-hydrated event by ID (zoneName / UUID).

    Looks up the event in the private scope first, then shared. Raises
    :class:`EventNotFound` if no scope has the event.
    """
    for scope in (EventScope.PRIVATE, EventScope.SHARED):
        event = self._fetch_event_full(event_id, scope)
        if event is not None:
            return event
    raise EventNotFound(f"Event not found: {event_id!r}")

rsvps(event)

Return the list of RSVPs in an event's zone.

Source code in pyicloud/services/invites/service.py
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
def rsvps(self, event: Event) -> list[Rsvp]:
    """Return the list of RSVPs in an event's zone."""
    # A zone's changes feed can report a record more than once, or report a
    # tombstone for one it reported earlier; keep the surviving latest state.
    latest: dict[str, CKRecord] = {}
    for record in self._zone_records(
        self._scope_str(event.scope),
        self._zone_id_req(event.event_id, event.scope),
        InvitesRecordType.Rsvp.value,
    ):
        if isinstance(record, CKTombstoneRecord):
            latest.pop(record.recordName, None)
        else:
            latest[record.recordName] = record
    return [self._rsvp_from_record(record) for record in latest.values()]

rsvp_image(rsvp)

Return the image a guest attached to their RSVP.

None when they attached none, which is the common case: the field is optional and most responses carry only a monogram.

The download goes through the private container's HTTP wrapper, which only supplies the session and timeouts -- the asset URL Apple returns is absolute and carries its own token, so an RSVP in a shared event downloads the same way as one in your own.

Source code in pyicloud/services/invites/service.py
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
def rsvp_image(self, rsvp: Rsvp) -> bytes | None:
    """Return the image a guest attached to their RSVP.

    ``None`` when they attached none, which is the common case: the field
    is optional and most responses carry only a monogram.

    The download goes through the private container's HTTP wrapper, which
    only supplies the session and timeouts -- the asset URL Apple returns
    is absolute and carries its own token, so an RSVP in a shared event
    downloads the same way as one in your own.
    """

    url = rsvp.image_download_url
    if not url:
        return None
    # The download uses the authenticated session, so an attacker-supplied
    # scheme would put iCloud credentials on the wire in the clear. Guarded
    # the same way PhotosUploader.send_bytes guards the upload URL Apple
    # echoes back to it, plus a real host check so a URL without a host,
    # such as ``https:example.com/a`` or ``https://@/a``, cannot slip
    # through.
    try:
        parsed = urlparse(url)
    except ValueError as exc:
        raise InvitesApiError(
            f"RSVP image URL is malformed; refusing to fetch it: {url!r}"
        ) from exc
    if parsed.scheme != "https" or not parsed.hostname:
        raise InvitesApiError(
            "RSVP image URL is not an absolute HTTPS URL; refusing to "
            f"fetch it: {url!r}"
        )
    return self._raw.download_asset_bytes(url)

resolve(short_guid)

Preview a share without joining it.

Source code in pyicloud/services/invites/service.py
297
298
299
300
301
def resolve(self, short_guid: str) -> ResolvedShare:
    """Preview a share without joining it."""
    data = self._raw.resolve([short_guid])
    result = self._first_resolve_result(data)
    return self._resolved_share_from_result(result)

accept(short_guid)

Accept a share invite. Returns the joined event (now in SHARED).

Source code in pyicloud/services/invites/service.py
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
def accept(self, short_guid: str) -> Event:
    """Accept a share invite. Returns the joined event (now in SHARED)."""
    data = self._raw.accept([short_guid])
    result = self._first_resolve_result(data)
    zone = result.get("zoneID") or {}
    event_id = zone.get("zoneName")
    if not isinstance(event_id, str):
        raise InvitesApiError(
            "Accept response missing zoneID.zoneName",
            payload=data,
        )
    scope = self._scope_from_db_scope(result.get("databaseScope"))
    event = self._fetch_event_full(event_id, scope)
    if event is None:
        # Fall back to the other scope in case of routing quirks.
        other = (
            EventScope.PRIVATE if scope == EventScope.SHARED else EventScope.SHARED
        )
        event = self._fetch_event_full(event_id, other)
    if event is None:
        raise EventNotFound(
            f"Accepted event {event_id!r} not visible in either scope"
        )
    return event

create_event(title, time, *, place=None, notes='', max_attendees=None, max_additional_guests_per_rsvp=0, is_private=False)

Create an unpublished hosted event with a share, but no guests.

Dates must be timezone-aware. Entitlement is checked before creating a zone. After a confirmed zone creation, subsequent failures trigger a best-effort deletion of that new zone only. InvitesCreationError retains its ID, cleanup outcome, and the original failure as its cause. An unconfirmed zone creation is not automatically deleted or retried. The created share may not identify the current user as a participant yet; refetch via event() before rsvp().

Source code in pyicloud/services/invites/service.py
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
def create_event(
    self,
    title: str,
    time: EventTime,
    *,
    place: EventPlace | None = None,
    notes: str = "",
    max_attendees: int | None = None,
    max_additional_guests_per_rsvp: int = 0,
    is_private: bool = False,
) -> Event:
    """Create an unpublished hosted event with a share, but no guests.

    Dates must be timezone-aware. Entitlement is checked before creating a
    zone. After a confirmed zone creation, subsequent failures trigger a
    best-effort deletion of that new zone only. InvitesCreationError retains
    its ID, cleanup outcome, and the original failure as its cause. An
    unconfirmed zone creation is not automatically deleted or retried. The
    created share may not identify the current user as a participant yet;
    refetch via ``event()`` before ``rsvp()``.
    """
    fields, preview = creation_fields(
        title,
        time,
        place=place,
        notes=notes,
        max_attendees=max_attendees,
        max_additional_guests_per_rsvp=max_additional_guests_per_rsvp,
        is_private=is_private,
    )
    fields[EventDetailsField.HINT.value] = {
        "type": "STRING",
        "value": json.dumps({"subscriptionAccessToken": self._entitlement_token()}),
    }
    event_id = str(uuid4()).upper()
    zone_id = CKZoneIDReq(zoneName=event_id)
    zone_created = False
    try:
        self._modify_new_zone(event_id, "create")
        zone_created = True
        record_name = f"{EVENT_DETAILS_RECORD_NAME_PREFIX}{event_id}"
        response = self._raw.modify(
            "private",
            zone_id=zone_id,
            atomic=True,
            operations=[
                CKModifyOperation(
                    operationType="create",
                    record=CKWriteRecord(
                        recordName=record_name,
                        recordType=InvitesRecordType.EventDetails.value,
                        fields=fields,
                    ),
                )
            ],
        )
        event = self._event_from_modify_response(
            response, record_name, EventScope.PRIVATE
        )
        if not event.record_change_tag:
            raise InvitesApiError("Created event response has no change tag")
        share_response = self._raw.modify(
            "private",
            zone_id=zone_id,
            atomic=True,
            operations=[
                CKModifyOperation(
                    operationType="create",
                    record=CKWriteRecord.model_validate({
                        "recordName": SHARE_RECORD_NAME,
                        "recordType": InvitesRecordType.Share.value,
                        "fields": preview,
                        "participants": [],
                        "requesters": [],
                        "blocked": [],
                        "publicPermission": "NONE",
                        "denyAccessRequests": True,
                    }),
                )
            ],
        )
        for record in self._records_of(share_response):
            if (
                record.recordName == SHARE_RECORD_NAME
                and record.recordType == InvitesRecordType.Share.value
            ):
                share = self._share_from_record(record)
                if share.short_guid:
                    return event.model_copy(update={"share": share})
        raise InvitesApiError("Created share response has no usable shortGUID")
    except Exception as exc:
        cleanup: bool | None = None
        cleanup_error: Exception | None = None
        if zone_created:
            try:
                self._modify_new_zone(event_id, "delete")
                cleanup = True
            except Exception as cleanup_exc:
                cleanup = False
                cleanup_error = cleanup_exc
        raise InvitesCreationError(
            event_id, cleanup_succeeded=cleanup, cleanup_error=cleanup_error
        ) from exc

Create a participant-specific invitation for an event you host.

Experimental: verified for email invitations on a controlled account; push notifications occurred during live tests. Email delivery and phone-only invitations remain unverified. Contacts are guest metadata, not an account lookup or an email-send API. Dates and publication are not modified. The latest owner share is read before an atomic guest-info create/share update. There are no retries.

A failure after submission raises InvitesInvitationError with the event and participant IDs and the original cause. Inspect those records before retrying: a missing or malformed response does not imply rollback.

Source code in pyicloud/services/invites/service.py
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
def invite_via_link(
    self,
    event: Event,
    *,
    emails: Sequence[str] = (),
    phone_numbers: Sequence[str] = (),
    name: str = "",
) -> OneTimeLinkInvitation:
    """Create a participant-specific invitation for an event you host.

    Experimental: verified for email invitations on a controlled account;
    push notifications occurred during live tests. Email delivery and
    phone-only invitations remain unverified.
    Contacts are guest metadata, not an account lookup or an email-send API.
    Dates and publication are not modified. The latest owner share is read
    before an atomic guest-info create/share update. There are no retries.

    A failure after submission raises InvitesInvitationError with the event
    and participant IDs and the original cause. Inspect those records before
    retrying: a missing or malformed response does not imply rollback.
    """
    if event.scope is not EventScope.PRIVATE:
        raise InvitesApiError("Only hosted events can receive guest invitations")
    contacts = invitation_contacts(name, emails, phone_numbers)
    zone_id = self._zone_id_req(event.event_id, event.scope)
    share = owner_share(
        self._raw.lookup("private", [SHARE_RECORD_NAME], zone_id=zone_id)
    )
    participant_id = str(uuid4())
    guest = OneTimeLinkGuest(
        record_name=f"{participant_id}{ONE_TIME_LINK_RECORD_NAME_SUFFIX}",
        participant_id=participant_id,
        name=contacts.name,
        emails=contacts.emails,
        phone_numbers=contacts.phone_numbers,
    )
    operations = invitation_operations(share, guest)
    try:
        response = self._raw.modify(
            "private", operations=operations, zone_id=zone_id, atomic=True
        )
        guest_record, updated_share, token = invitation_records(
            response, share, guest
        )
        return OneTimeLinkInvitation(
            **self._one_time_link_from_record(guest_record).model_dump(),
            short_guid=token,
            record_change_tag=cast(str, guest_record.recordChangeTag),
            share_record_change_tag=cast(str, updated_share.recordChangeTag),
        )
    except Exception as exc:
        raise InvitesInvitationError(event.event_id, participant_id) from exc

rsvp(event, status, *, name=None, message=None, plus_one_adults=0, plus_one_kids=0)

Submit or update the current user's RSVP for event.

Dispatches to private if the current user owns the event, or shared if they're a guest (per event.scope). Creates the RSVP record on first response and updates it on subsequent calls.

For :attr:RsvpStatus.NOT_GOING, plus-one counts are zeroed to mirror the iCloud web UI behavior; the plus_one_* kwargs are ignored in that case.

Returns the freshly written :class:Rsvp (including the new record_change_tag).

Source code in pyicloud/services/invites/service.py
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
def rsvp(
    self,
    event: Event,
    status: RsvpStatus,
    *,
    name: str | None = None,
    message: str | None = None,
    plus_one_adults: int = 0,
    plus_one_kids: int = 0,
) -> Rsvp:
    """Submit or update the current user's RSVP for ``event``.

    Dispatches to ``private`` if the current user owns the event, or
    ``shared`` if they're a guest (per ``event.scope``). Creates the RSVP
    record on first response and updates it on subsequent calls.

    For :attr:`RsvpStatus.NOT_GOING`, plus-one counts are zeroed to mirror
    the iCloud web UI behavior; the ``plus_one_*`` kwargs are ignored in
    that case.

    Returns the freshly written :class:`Rsvp` (including the new
    ``record_change_tag``).
    """
    if plus_one_adults < 0 or plus_one_kids < 0:
        raise InvitesApiError(
            "plus_one_adults and plus_one_kids must be non-negative; "
            f"got plus_one_adults={plus_one_adults!r}, "
            f"plus_one_kids={plus_one_kids!r}"
        )

    participant_id = self._current_participant_id(event)
    record_name = f"{participant_id}{RSVP_RECORD_NAME_SUFFIX}"
    existing = self._find_existing_rsvp(event, record_name)

    if status == RsvpStatus.NOT_GOING:
        plus_one_adults = 0
        plus_one_kids = 0

    fields: dict[str, Any] = {
        RsvpField.STATUS.value: {
            "type": "INT64",
            "value": status.value,
            "isEncrypted": True,
        },
        RsvpField.NUM_ADDITIONAL_ADULTS.value: {
            "type": "INT64",
            "value": plus_one_adults,
            "isEncrypted": True,
        },
        RsvpField.NUM_ADDITIONAL_KIDS.value: {
            "type": "INT64",
            "value": plus_one_kids,
            "isEncrypted": True,
        },
        RsvpField.NUM_ADDITIONAL_GUESTS.value: {
            "type": "INT64",
            "value": plus_one_adults + plus_one_kids,
            "isEncrypted": True,
        },
    }
    if name is not None:
        fields[RsvpField.NAME.value] = {
            "type": "STRING",
            "value": name,
            "isEncrypted": True,
        }
    if message is not None:
        fields[RsvpField.MESSAGE.value] = {
            "type": "STRING",
            "value": message,
            "isEncrypted": True,
        }

    record_change_tag = existing.record_change_tag if existing else None
    op = CKModifyOperation(
        operationType="update" if existing else "create",
        record=CKWriteRecord(
            recordName=record_name,
            recordType=InvitesRecordType.Rsvp.value,
            recordChangeTag=record_change_tag,
            fields=cast(CKWriteFields, fields),
        ),
    )

    scope_str = self._scope_str(event.scope)
    zone_id = self._zone_id_req(event.event_id, event.scope)
    response: CKModifyResponse = self._raw.modify(
        scope_str,
        operations=[op],
        zone_id=zone_id,
        atomic=True,
    )
    return self._rsvp_from_modify_response(response, record_name)

cancel(event, *, cancelled=True)

Cancel an event you host, or reinstate one you cancelled.

Apple accepts both directions, so this is a symmetric switch rather than a one-way verb -- an accidental cancel is recoverable.

event must carry a current record change tag, so pass one from event() or from a previous write rather than reusing a stale object: Apple rejects a stale tag with an opaque 502.

Source code in pyicloud/services/invites/service.py
599
600
601
602
603
604
605
606
607
608
609
610
def cancel(self, event: Event, *, cancelled: bool = True) -> Event:
    """Cancel an event you host, or reinstate one you cancelled.

    Apple accepts both directions, so this is a symmetric switch rather
    than a one-way verb -- an accidental cancel is recoverable.

    ``event`` must carry a current record change tag, so pass one from
    ``event()`` or from a previous write rather than reusing a stale
    object: Apple rejects a stale tag with an opaque ``502``.
    """

    return self._set_event_flag(event, EventDetailsField.IS_CANCELLED, cancelled)

publish(event, *, published=True)

Publish an event you host, or return it to an unpublished state.

Pass an event with a current record change tag from event() or a previous write. Both EventDetails and its share are updated atomically; no modify is sent unless the share's current tag could be read. A share with no readable tag raises, and a failed lookup surfaces its own error rather than a generic refusal. The returned event carries the new tag, but is not rehydrated with share/RSVP data.

Source code in pyicloud/services/invites/service.py
612
613
614
615
616
617
618
619
620
621
622
623
624
def publish(self, event: Event, *, published: bool = True) -> Event:
    """Publish an event you host, or return it to an unpublished state.

    Pass an event with a current record change tag from ``event()`` or a
    previous write. Both EventDetails and its share are updated atomically;
    no modify is sent unless the share's current tag could be read. A share
    with no readable tag raises, and a failed lookup surfaces its own error
    rather than a generic refusal. The returned event carries the new tag,
    but is not rehydrated with share/RSVP data.
    """
    if event.scope is not EventScope.PRIVATE:
        raise InvitesApiError("Only events you host can be published.")
    return self._set_event_flag(event, EventDetailsField.IS_PUBLISHED, published)