Skip to content

Commit b07cc15

Browse files
Merge pull request #47 from pycalendar/feat/scheduling
Scheduling: accept, decline, send invitations
2 parents 99593e4 + 301996c commit b07cc15

13 files changed

Lines changed: 1337 additions & 102 deletions

File tree

‎news/12.bugfix.1.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``ical_to_jscal`` no longer emits a ``sendTo`` property on participants, and now emits ``calendarAddress`` instead. Cyrus rejects any participant with ``sendTo`` present, on both create and update, and separately requires ``calendarAddress``, which is not one of RFC 8984's own Participant properties. Stalwart accepts either shape. Any event with ATTENDEE or ORGANIZER previously failed outright against Cyrus.

‎news/12.bugfix.2.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``ical_to_jscal`` and ``jscal_to_ical`` now use ``recurrenceRule``/``excludedRecurrenceRule`` (singular) instead of RFC 8984's actual ``recurrenceRules``/``excludedRecurrenceRules`` (an array). Neither Cyrus nor Stalwart accept the array shape; both use the older singular property on create, update, and read. Any recurring event previously failed outright against both servers. Only the first RRULE/EXRULE is kept when a VEVENT has more than one, a rare case in practice.

‎news/12.bugfix.3.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``ical_to_jscal`` no longer writes a non-mailto ORGANIZER/ATTENDEE URI (e.g. ``sip:alice@example.com``) into the Participant ``email`` property, which RFC 8984 defines as an RFC 5322 addr-spec, not an arbitrary URI. Such a participant now has ``calendarAddress`` only, no ``email``, and accepting/declining an invitation for it works correctly via a ``calendarAddress`` match. ``jscal_to_ical`` no longer double-wraps a non-mailto ``calendarAddress`` in a spurious ``mailto:`` prefix on the way back to iCalendar. ``_find_participant_id_by_email`` no longer crashes with an ``AttributeError`` when a participant has an explicitly null ``calendarAddress``.

‎news/12.feature.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added scheduling: ``accept_invitation``, ``decline_invitation``, ``tentatively_accept``, and ``send_invite`` on both ``JMAPClient`` and ``AsyncJMAPClient``. The three response methods take the event id and your own email address, find the matching participant on the live event, and patch just that participant's ``participationStatus``. ``send_invite`` is ``create_event`` with the server told to actually dispatch invitations. ``build_event_set_create``/``build_event_set_update``/``build_event_set_destroy`` gained a ``send_scheduling_messages`` argument for this; ``delete_event`` also gained it, so cancelling an event can send participants an iTIP CANCEL. ``get_event`` gained the same ``account_id`` parameter the other event methods already had, since responding to an invitation on a shared calendar needs it.

‎src/calendaring_jmap/_methods/event.py‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ def build_event_query_changes(
194194
def build_event_set_create(
195195
account_id: str,
196196
events: dict[str, dict],
197+
send_scheduling_messages: bool = False,
197198
) -> tuple:
198199
"""Build a ``CalendarEvent/set`` method call for creating events.
199200
@@ -203,6 +204,8 @@ def build_event_set_create(
203204
The creation IDs are ephemeral — they are used to correlate
204205
server responses with individual creation requests within the
205206
same batch call.
207+
send_scheduling_messages: If true, the server sends iTIP scheduling
208+
messages to the event's participants (JMAP Calendars §5.9).
206209
207210
Returns:
208211
A 3-tuple ``("CalendarEvent/set", arguments_dict, call_id)``.
@@ -212,6 +215,7 @@ def build_event_set_create(
212215
{
213216
"accountId": account_id,
214217
"create": dict(events),
218+
"sendSchedulingMessages": send_scheduling_messages,
215219
},
216220
"ev-set-create-0",
217221
)
@@ -220,6 +224,7 @@ def build_event_set_create(
220224
def build_event_set_update(
221225
account_id: str,
222226
updates: dict[str, dict],
227+
send_scheduling_messages: bool = False,
223228
) -> tuple:
224229
"""Build a ``CalendarEvent/set`` method call for updating events.
225230
@@ -229,33 +234,48 @@ def build_event_set_update(
229234
names (or JSON Pointer paths for nested properties); values are
230235
the new values. Use ``None`` as a value to reset a property to
231236
its server default.
237+
send_scheduling_messages: If true, the server sends iTIP scheduling
238+
messages to the event's participants, or back to the organizer
239+
if this account isn't the event's origin (JMAP Calendars §5.9).
232240
233241
Returns:
234242
A 3-tuple ``("CalendarEvent/set", arguments_dict, call_id)``.
235243
"""
236244
return (
237245
"CalendarEvent/set",
238-
{"accountId": account_id, "update": updates},
246+
{
247+
"accountId": account_id,
248+
"update": updates,
249+
"sendSchedulingMessages": send_scheduling_messages,
250+
},
239251
"ev-set-update-0",
240252
)
241253

242254

243255
def build_event_set_destroy(
244256
account_id: str,
245257
ids: list[str],
258+
send_scheduling_messages: bool = False,
246259
) -> tuple:
247260
"""Build a ``CalendarEvent/set`` method call for destroying events.
248261
249262
Args:
250263
account_id: The JMAP accountId.
251264
ids: List of event IDs to destroy.
265+
send_scheduling_messages: If true, and this account is the event's
266+
origin, the server sends an iTIP CANCEL to the event's
267+
participants (JMAP Calendars §5.9.2.2).
252268
253269
Returns:
254270
A 3-tuple ``("CalendarEvent/set", arguments_dict, call_id)``.
255271
"""
256272
return (
257273
"CalendarEvent/set",
258-
{"accountId": account_id, "destroy": ids},
274+
{
275+
"accountId": account_id,
276+
"destroy": ids,
277+
"sendSchedulingMessages": send_scheduling_messages,
278+
},
259279
"ev-set-destroy-0",
260280
)
261281

‎src/calendaring_jmap/async_client.py‎

Lines changed: 153 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@
5555
parse_task_set,
5656
)
5757
from calendaring_jmap.client import _DEFAULT_USING, _TASK_USING, _JMAPClientBase
58+
from calendaring_jmap.constants import (
59+
PARTICIPATION_STATUS_ACCEPTED,
60+
PARTICIPATION_STATUS_DECLINED,
61+
PARTICIPATION_STATUS_TENTATIVE,
62+
)
5863
from calendaring_jmap.convert import ical_to_jscal
5964
from calendaring_jmap.error import JMAPAuthError, JMAPMethodError
6065
from calendaring_jmap.objects.calendar import JMAPCalendar
@@ -219,7 +224,7 @@ async def get_calendars(
219224
List of :class:`~calendaring_jmap.objects.calendar.JMAPCalendar` objects.
220225
"""
221226
session = await self._get_session()
222-
target_account = account_id if account_id is not None else session.account_id
227+
target_account = self._resolve_account(session, account_id)
223228
responses = await self._request([build_calendar_get(target_account)])
224229
return self._parse_get_calendars(responses, self, True, account_id=target_account)
225230

@@ -253,7 +258,7 @@ async def _update_calendar_patch(
253258
self, calendar_id: str, patch: dict, account_id: str | None = None
254259
) -> None:
255260
session = await self._get_session()
256-
target_account = account_id if account_id is not None else session.account_id
261+
target_account = self._resolve_account(session, account_id)
257262
call = build_calendar_set_update(target_account, {calendar_id: patch})
258263
responses = await self._request([call])
259264
self._parse_update_response(
@@ -318,7 +323,7 @@ async def delete_calendar(
318323
and ``on_destroy_remove_events`` was left ``False``.
319324
"""
320325
session = await self._get_session()
321-
target_account = account_id if account_id is not None else session.account_id
326+
target_account = self._resolve_account(session, account_id)
322327
call = build_calendar_set_destroy(target_account, [calendar_id], on_destroy_remove_events)
323328
responses = await self._request([call])
324329
self._parse_delete_response(
@@ -407,6 +412,24 @@ async def set_default_alerts(
407412
patch["defaultAlertsWithoutTime"] = alerts_without_time
408413
await self._update_calendar_patch(calendar_id, patch, account_id=account_id)
409414

415+
async def _create_event_impl(
416+
self,
417+
calendar_id: str,
418+
ical_str: str,
419+
account_id: str | None,
420+
send_scheduling_messages: bool,
421+
) -> str:
422+
session = await self._get_session()
423+
jscal = ical_to_jscal(ical_str, calendar_id=calendar_id)
424+
target_account = self._resolve_account(session, account_id)
425+
call = build_event_set_create(
426+
target_account, {"new-0": jscal}, send_scheduling_messages=send_scheduling_messages
427+
)
428+
responses = await self._request([call])
429+
return self._parse_create_response(
430+
responses, session.api_url, "CalendarEvent/set", parse_event_set
431+
)
432+
410433
async def create_event(
411434
self, calendar_id: str, ical_str: str, account_id: str | None = None
412435
) -> str:
@@ -426,20 +449,48 @@ async def create_event(
426449
Raises:
427450
JMAPMethodError: If the server rejects the create request.
428451
"""
429-
session = await self._get_session()
430-
jscal = ical_to_jscal(ical_str, calendar_id=calendar_id)
431-
target_account = account_id if account_id is not None else session.account_id
432-
call = build_event_set_create(target_account, {"new-0": jscal})
433-
responses = await self._request([call])
434-
return self._parse_create_response(
435-
responses, session.api_url, "CalendarEvent/set", parse_event_set
452+
return await self._create_event_impl(
453+
calendar_id, ical_str, account_id, send_scheduling_messages=False
436454
)
437455

438-
async def get_event(self, event_id: str) -> JMAPCalendarObject:
456+
async def send_invite(
457+
self, calendar_id: str, ical_str: str, account_id: str | None = None
458+
) -> str:
459+
"""Create a calendar event and notify its participants.
460+
461+
Identical to :meth:`create_event`, except the server actually
462+
dispatches iTIP invitations to the event's participants (an iCalendar
463+
string with ATTENDEE/ORGANIZER properties). ``create_event`` itself
464+
never sends scheduling messages; use this instead when the event has
465+
participants who need to be notified.
466+
467+
Args:
468+
calendar_id: The JMAP calendar ID to create the event in.
469+
ical_str: A VCALENDAR string representing the event.
470+
account_id: The JMAP account owning ``calendar_id``. Defaults to
471+
the authenticated user's own primary account.
472+
473+
Returns:
474+
The server-assigned JMAP event ID.
475+
476+
Raises:
477+
JMAPMethodError: If the server rejects the create request, or
478+
with ``error_type == "noSupportedScheduleMethods"`` if a
479+
participant has no usable delivery method.
480+
"""
481+
return await self._create_event_impl(
482+
calendar_id, ical_str, account_id, send_scheduling_messages=True
483+
)
484+
485+
async def get_event(self, event_id: str, account_id: str | None = None) -> JMAPCalendarObject:
439486
"""Fetch a calendar event as an iCalendar string.
440487
441488
Args:
442489
event_id: The JMAP event ID to retrieve.
490+
account_id: The JMAP account owning ``event_id``. Defaults to
491+
the authenticated user's own primary account; pass the
492+
owner's account here to fetch an event on a calendar shared
493+
with you.
443494
444495
Returns:
445496
A :class:`~calendaring_jmap.objects.calendar_object.JMAPCalendarObject`
@@ -451,9 +502,84 @@ async def get_event(self, event_id: str) -> JMAPCalendarObject:
451502
JMAPMethodError: If the event is not found.
452503
"""
453504
session = await self._get_session()
454-
responses = await self._request([build_event_get(session.account_id, ids=[event_id])])
505+
target_account = self._resolve_account(session, account_id)
506+
responses = await self._request([build_event_get(target_account, ids=[event_id])])
455507
return self._parse_get_event_response(responses, session.api_url, event_id)
456508

509+
async def _find_own_participant_id(
510+
self, event_id: str, own_email: str, account_id: str | None = None
511+
) -> str:
512+
"""Return the participant id in ``event_id`` whose email is ``own_email``.
513+
514+
Only the ``participants`` property is fetched, not the whole event.
515+
See :meth:`JMAPClient._find_participant_id_by_email` for the matching rules.
516+
"""
517+
session = await self._get_session()
518+
target_account = self._resolve_account(session, account_id)
519+
responses = await self._request(
520+
[build_event_get(target_account, ids=[event_id], properties=["participants"])]
521+
)
522+
event = self._parse_get_event_response(responses, session.api_url, event_id)
523+
return self._find_participant_id_by_email(
524+
event.data.get("participants", {}), own_email, session.api_url, event_id
525+
)
526+
527+
async def _respond_to_invitation(
528+
self,
529+
event_id: str,
530+
own_email: str,
531+
participation_status: str,
532+
account_id: str | None = None,
533+
) -> None:
534+
session = await self._get_session()
535+
target_account = self._resolve_account(session, account_id)
536+
participant_id = await self._find_own_participant_id(
537+
event_id, own_email, account_id=account_id
538+
)
539+
patch = {f"participants/{participant_id}/participationStatus": participation_status}
540+
call = build_event_set_update(
541+
target_account, {event_id: patch}, send_scheduling_messages=True
542+
)
543+
responses = await self._request([call])
544+
self._parse_update_response(
545+
responses, session.api_url, "CalendarEvent/set", parse_event_set, event_id
546+
)
547+
548+
async def accept_invitation(
549+
self, event_id: str, own_email: str, account_id: str | None = None
550+
) -> None:
551+
"""Accept a meeting invitation, notifying the organizer.
552+
553+
See :meth:`JMAPClient.accept_invitation` for the full semantics.
554+
"""
555+
await self._respond_to_invitation(
556+
event_id, own_email, PARTICIPATION_STATUS_ACCEPTED, account_id=account_id
557+
)
558+
559+
async def decline_invitation(
560+
self, event_id: str, own_email: str, account_id: str | None = None
561+
) -> None:
562+
"""Decline a meeting invitation, notifying the organizer.
563+
564+
Same as :meth:`accept_invitation`, setting ``participationStatus``
565+
to declined instead.
566+
"""
567+
await self._respond_to_invitation(
568+
event_id, own_email, PARTICIPATION_STATUS_DECLINED, account_id=account_id
569+
)
570+
571+
async def tentatively_accept(
572+
self, event_id: str, own_email: str, account_id: str | None = None
573+
) -> None:
574+
"""Tentatively accept a meeting invitation, notifying the organizer.
575+
576+
Same as :meth:`accept_invitation`, setting ``participationStatus``
577+
to tentative instead.
578+
"""
579+
await self._respond_to_invitation(
580+
event_id, own_email, PARTICIPATION_STATUS_TENTATIVE, account_id=account_id
581+
)
582+
457583
async def update_event(
458584
self, event_id: str, ical_str: str, account_id: str | None = None
459585
) -> None:
@@ -471,7 +597,7 @@ async def update_event(
471597
JMAPMethodError: If the server rejects the update.
472598
"""
473599
session = await self._get_session()
474-
target_account = account_id if account_id is not None else session.account_id
600+
target_account = self._resolve_account(session, account_id)
475601
patch, nulled = self._build_event_update_patch(ical_str)
476602
while True:
477603
responses = await self._request(
@@ -497,7 +623,7 @@ async def _search(
497623
) -> list[JMAPCalendarObject]:
498624
session = await self._get_session()
499625
calls = self._build_event_search_calls(
500-
account_id if account_id is not None else session.account_id,
626+
self._resolve_account(session, account_id),
501627
calendar_id,
502628
start,
503629
end,
@@ -591,7 +717,12 @@ async def get_objects_by_sync_token(
591717
get_responses, created_ids, updated_ids, destroyed, new_sync_token
592718
)
593719

594-
async def delete_event(self, event_id: str, account_id: str | None = None) -> None:
720+
async def delete_event(
721+
self,
722+
event_id: str,
723+
account_id: str | None = None,
724+
send_scheduling_messages: bool = False,
725+
) -> None:
595726
"""Delete a calendar event.
596727
597728
Args:
@@ -600,13 +731,18 @@ async def delete_event(self, event_id: str, account_id: str | None = None) -> No
600731
the authenticated user's own primary account; pass the
601732
owner's account here to delete an event on a calendar
602733
shared with you.
734+
send_scheduling_messages: If true, and this account is the
735+
event's origin, the server sends an iTIP CANCEL to the
736+
event's participants (JMAP Calendars §5.9.2.2).
603737
604738
Raises:
605739
JMAPMethodError: If the server rejects the delete.
606740
"""
607741
session = await self._get_session()
608-
target_account = account_id if account_id is not None else session.account_id
609-
responses = await self._request([build_event_set_destroy(target_account, [event_id])])
742+
target_account = self._resolve_account(session, account_id)
743+
responses = await self._request(
744+
[build_event_set_destroy(target_account, [event_id], send_scheduling_messages)]
745+
)
610746
self._parse_delete_response(
611747
responses, session.api_url, "CalendarEvent/set", parse_event_set, event_id
612748
)

0 commit comments

Comments
 (0)