@wtfalch/calendar 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/calendar/calendar-client.js +424 -18
- package/dist/calendar/calendar.css +13 -0
- package/dist/calendar/client.d.ts +27 -0
- package/dist/calendar/client.js +101 -0
- package/dist/calendar/fake/sample.d.ts +2 -1
- package/dist/calendar/fake/store.d.ts +31 -7
- package/dist/calendar/fake/store.js +300 -29
- package/dist/calendar/find-time.d.ts +17 -0
- package/dist/calendar/find-time.js +198 -0
- package/dist/calendar/ics.d.ts +47 -0
- package/dist/calendar/ics.js +247 -0
- package/dist/calendar/index.d.ts +3 -1
- package/dist/calendar/index.js +1 -0
- package/dist/calendar/react/Calendar.js +36 -27
- package/dist/calendar/react/EventDetail.js +22 -1
- package/dist/calendar/react/EventEditor.js +65 -3
- package/dist/calendar/types.d.ts +361 -11
- package/dist/calendar/wire.d.ts +121 -3
- package/dist/calendar/wire.js +248 -2
- package/package.json +1 -1
|
@@ -5,9 +5,10 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { CalendarConnection } from "./client.js";
|
|
7
7
|
import { guard } from "./errors.js";
|
|
8
|
+
import { findFreeSlots } from "./find-time.js";
|
|
8
9
|
import { push } from "./push.js";
|
|
9
10
|
import { CalendarError } from "./types.js";
|
|
10
|
-
import { CALENDAR_GET_PROPERTIES, EVENT_GET_PROPERTIES, availabilityFromWire, calendarFromWire, draftToWire, eventFromWire, patchToWire, principalFromWire, rightsToWire, } from "./wire.js";
|
|
11
|
+
import { CALENDAR_GET_PROPERTIES, EVENT_GET_PROPERTIES, availabilityFromWire, calendarFromWire, draftToWire, eventFromWire, patchToWire, principalFromWire, recurrenceRuleFromWire, rightsToWire, } from "./wire.js";
|
|
11
12
|
/** The first refusal in a `notCreated`/`notUpdated`/`notDestroyed` map, or
|
|
12
13
|
* `undefined` when the operation actually succeeded. */
|
|
13
14
|
function firstRefusal(errors) {
|
|
@@ -52,6 +53,43 @@ async function listEvents(connection, range) {
|
|
|
52
53
|
return result.list.map((wire) => eventFromWire(wire));
|
|
53
54
|
});
|
|
54
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* One `query` across every calendar this account holds, `options.range`
|
|
58
|
+
* optional -- the fix for Agora issue #216 (one search box over Mail, Chat
|
|
59
|
+
* and Calendar; https://github.com/wtfalch/agora/issues/216), which
|
|
60
|
+
* `listEvents(range)` alone could not answer without the caller guessing a
|
|
61
|
+
* range and filtering client-side.
|
|
62
|
+
*
|
|
63
|
+
* Sends a single `filter: {text: query}` -- live-confirmed against Stalwart
|
|
64
|
+
* v0.16.20 (2026-09-21) to match a case-insensitive substring of either
|
|
65
|
+
* `title` or `description` in one pass, so this never has to OR two filters
|
|
66
|
+
* together. `after`/`before` are added, with `expandRecurrences: true`,
|
|
67
|
+
* only when the caller names `options.range` -- mirroring `listEvents`
|
|
68
|
+
* exactly, because the server refuses to expand recurrences with no bounds
|
|
69
|
+
* at all (`invalidArguments`, live-confirmed). With no range, a matching
|
|
70
|
+
* recurring event's own master id comes back unexpanded, the same fallback
|
|
71
|
+
* `CalendarEvent/query` uses for any other unexpanded query -- there is no
|
|
72
|
+
* bound to expand against, so this is the only answer the server can give.
|
|
73
|
+
*/
|
|
74
|
+
async function searchEvents(connection, query, options) {
|
|
75
|
+
return guard('events.search', async () => {
|
|
76
|
+
const accountId = await connection.accountId();
|
|
77
|
+
const range = options?.range;
|
|
78
|
+
const args = {
|
|
79
|
+
accountId,
|
|
80
|
+
filter: range ? { text: query, after: range.after, before: range.before } : { text: query },
|
|
81
|
+
};
|
|
82
|
+
if (range)
|
|
83
|
+
args.expandRecurrences = true;
|
|
84
|
+
if (options?.limit !== undefined)
|
|
85
|
+
args.limit = options.limit;
|
|
86
|
+
const queryResult = await connection.request('CalendarEvent/query', args);
|
|
87
|
+
if (queryResult.ids.length === 0)
|
|
88
|
+
return [];
|
|
89
|
+
const result = await connection.request('CalendarEvent/get', { accountId, ids: queryResult.ids, properties: EVENT_GET_PROPERTIES });
|
|
90
|
+
return result.list.map((wire) => eventFromWire(wire));
|
|
91
|
+
});
|
|
92
|
+
}
|
|
55
93
|
async function getEvent(connection, id) {
|
|
56
94
|
return guard('events.get', async () => {
|
|
57
95
|
const accountId = await connection.accountId();
|
|
@@ -93,7 +131,14 @@ async function createEvent(connection, calendarId, event, options) {
|
|
|
93
131
|
return getEvent(connection, created.id);
|
|
94
132
|
});
|
|
95
133
|
}
|
|
96
|
-
|
|
134
|
+
/**
|
|
135
|
+
* `updateEvent`'s original body, unscoped: the patch lands on whatever `id`
|
|
136
|
+
* was given. This is also what `scope: 'occurrence'` means (a synthetic
|
|
137
|
+
* occurrence id already writes an occurrence-scoped override,
|
|
138
|
+
* `wire.ts`'s `WireEvent.recurrenceOverrides` doc comment) and what an
|
|
139
|
+
* omitted `scope` means, unchanged from before wtfalch/calendar#14.
|
|
140
|
+
*/
|
|
141
|
+
async function updateEventDirect(connection, id, patch, options) {
|
|
97
142
|
return guard('events.update', async () => {
|
|
98
143
|
const accountId = await connection.accountId();
|
|
99
144
|
const result = await connection.request('CalendarEvent/set', {
|
|
@@ -111,7 +156,8 @@ async function updateEvent(connection, id, patch, options) {
|
|
|
111
156
|
return getEvent(connection, id);
|
|
112
157
|
});
|
|
113
158
|
}
|
|
114
|
-
|
|
159
|
+
/** `deleteEvent`'s original body, unscoped -- see `updateEventDirect`. */
|
|
160
|
+
async function deleteEventDirect(connection, id, options) {
|
|
115
161
|
return guard('events.delete', async () => {
|
|
116
162
|
const accountId = await connection.accountId();
|
|
117
163
|
const result = await connection.request('CalendarEvent/set', {
|
|
@@ -128,6 +174,319 @@ async function deleteEvent(connection, id, options) {
|
|
|
128
174
|
}
|
|
129
175
|
});
|
|
130
176
|
}
|
|
177
|
+
// ---------------------------------------------------------------------------
|
|
178
|
+
// RecurrenceScope: 'series' and 'following'
|
|
179
|
+
// ---------------------------------------------------------------------------
|
|
180
|
+
//
|
|
181
|
+
// Live-confirmed against Stalwart v0.16.20 for wtfalch/calendar#14. Every
|
|
182
|
+
// event's own `CalendarEvent/get` -- master, non-recurring, or occurrence --
|
|
183
|
+
// carries `baseEventId`; on anything that is not an occurrence it equals the
|
|
184
|
+
// event's own `id` (`types.ts`'s `CalendarEvent.baseEventId` doc comment, the
|
|
185
|
+
// comment this ticket corrected). So `event.baseEventId ?? id` always names
|
|
186
|
+
// the top-level event to address for 'series', whether `id` was already that
|
|
187
|
+
// event or a synthetic occurrence id. `recurrenceId`'s presence, not
|
|
188
|
+
// `baseEventId`'s, is what tells an occurrence from a master.
|
|
189
|
+
//
|
|
190
|
+
// 'following' has no server verb at all. The standard way -- and the only
|
|
191
|
+
// one two live captures (`.plans/issue-14.md`) found -- is a rule truncation
|
|
192
|
+
// on the old master plus a new event carrying the change, both ordinary
|
|
193
|
+
// `CalendarEvent/set` calls.
|
|
194
|
+
/** The event `EVENT_GET_PROPERTIES` fetches, plus the one property only
|
|
195
|
+
* 'following' needs: the master's own per-occurrence overrides, so one
|
|
196
|
+
* stranded past a truncated rule can be excluded in the same write that
|
|
197
|
+
* truncates it (`truncateSeriesBefore`). Not part of the public contract --
|
|
198
|
+
* `recurrenceOverrides` never reaches `eventFromWire`. */
|
|
199
|
+
const MASTER_GET_PROPERTIES = [...EVENT_GET_PROPERTIES, 'recurrenceOverrides'];
|
|
200
|
+
async function getRawEvent(connection, accountId, id, operation) {
|
|
201
|
+
const result = await connection.request('CalendarEvent/get', { accountId, ids: [id], properties: MASTER_GET_PROPERTIES });
|
|
202
|
+
const wire = result.list[0];
|
|
203
|
+
if (!wire) {
|
|
204
|
+
throw new CalendarError(`no event ${id} on this account`, { type: 'notFound', operation });
|
|
205
|
+
}
|
|
206
|
+
return wire;
|
|
207
|
+
}
|
|
208
|
+
/** More than any IANA zone's real offset from UTC (the widest today is
|
|
209
|
+
* UTC-12 to UTC+14), so padding a query window by this much on both sides,
|
|
210
|
+
* after naively treating a local wall-clock string as UTC, can never leave
|
|
211
|
+
* the true occurrence at either edge outside the window -- whatever the
|
|
212
|
+
* event's actual `timeZone` shifts it by, DST included. */
|
|
213
|
+
const MAX_ZONE_OFFSET_MS = 26 * 60 * 60 * 1_000;
|
|
214
|
+
/**
|
|
215
|
+
* How many of `masterId`'s own occurrences fall strictly before `before`
|
|
216
|
+
* (`seriesStart`'s and `before`'s own shape: a local wall-clock
|
|
217
|
+
* `recurrenceId`, no offset) -- asked of `CalendarEvent/query`'s own
|
|
218
|
+
* expansion rather than counted by re-running the rule here, which would be
|
|
219
|
+
* exactly the RRULE expansion this package's client carries none of
|
|
220
|
+
* (`PRODUCT.md` "Stack"; `no-rrule-dependency.test.ts`). Only
|
|
221
|
+
* `updateFollowing`'s `count` case needs this -- an `until` bound or an
|
|
222
|
+
* unbounded rule carries over to the forward series untouched.
|
|
223
|
+
*
|
|
224
|
+
* **The query's own `after`/`before` only pick a wide net, not the answer.**
|
|
225
|
+
* They are built by treating `seriesStart`/`before` as if they were already
|
|
226
|
+
* UTC and padding by `MAX_ZONE_OFFSET_MS`, which is correct only as a
|
|
227
|
+
* "definitely wide enough" bound -- this package has no timezone library to
|
|
228
|
+
* turn a local wall-clock string plus an IANA name into the real UTC instant
|
|
229
|
+
* the server itself uses to filter (wtfalch/calendar#14 follow-up: a fixed
|
|
230
|
+
* one-second pad here, naively treated as UTC, silently dropped the true
|
|
231
|
+
* first occurrence in any zone but UTC, and dropped or added a whole
|
|
232
|
+
* occurrence across a DST change -- live-confirmed in `Europe/Oslo`,
|
|
233
|
+
* `.plans/issue-14.md`). What actually decides "elapsed" is a plain string
|
|
234
|
+
* comparison of each candidate's own `recurrenceId` against `before` -- both
|
|
235
|
+
* are wall-clock strings in the *same* zone, since they are occurrences of
|
|
236
|
+
* the same series, so lexicographic order on the fixed-width
|
|
237
|
+
* `YYYY-MM-DDTHH:mm:ss` shape already is chronological order, with no Date
|
|
238
|
+
* arithmetic and so no zone to get wrong.
|
|
239
|
+
*/
|
|
240
|
+
async function countElapsedOccurrences(connection, accountId, masterId, seriesStart, before) {
|
|
241
|
+
const after = new Date(new Date(`${seriesStart}Z`).getTime() - MAX_ZONE_OFFSET_MS).toISOString();
|
|
242
|
+
const wideBefore = new Date(new Date(`${before}Z`).getTime() + MAX_ZONE_OFFSET_MS).toISOString();
|
|
243
|
+
const query = await connection.request('CalendarEvent/query', { accountId, filter: { after, before: wideBefore }, expandRecurrences: true });
|
|
244
|
+
if (query.ids.length === 0)
|
|
245
|
+
return 0;
|
|
246
|
+
const result = await connection.request('CalendarEvent/get', {
|
|
247
|
+
accountId,
|
|
248
|
+
ids: [...query.ids],
|
|
249
|
+
properties: ['id', 'baseEventId', 'recurrenceId'],
|
|
250
|
+
});
|
|
251
|
+
return result.list.filter((event) => event.baseEventId === masterId && (event.recurrenceId ?? '') < before).length;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* End `masterId`'s rule the instant before `splitIso` (`occurrence.start`'s
|
|
255
|
+
* own local shape, no offset) -- one second earlier, rather than the exact
|
|
256
|
+
* previous occurrence's own date, because the server accepts any boundary,
|
|
257
|
+
* not only one that lands on a real occurrence, and that spares this
|
|
258
|
+
* package working an interval backwards itself (live-confirmed).
|
|
259
|
+
*
|
|
260
|
+
* Any override `masterWire` already carries at or after `splitIso` is
|
|
261
|
+
* excluded in the same write. Left alone, Stalwart keeps answering it past
|
|
262
|
+
* the rule's own new `until` (live-confirmed, `.plans/issue-14.md`) -- an
|
|
263
|
+
* "ended" series that still has an occurrence on it, which would break the
|
|
264
|
+
* one thing 'following' promises. This is the Decision the ticket asks for
|
|
265
|
+
* on an override past the split: dropped, not carried to the new series,
|
|
266
|
+
* because carrying it over would mean this package reading back every
|
|
267
|
+
* future occurrence to notice which ones differ from the plain rule, then
|
|
268
|
+
* re-keying each one against the new master -- a second `recurrenceRule`
|
|
269
|
+
* problem stacked on the one this function already solves.
|
|
270
|
+
*/
|
|
271
|
+
async function truncateSeriesBefore(connection, accountId, masterId, masterWire, splitIso, sendSchedulingMessages, operation) {
|
|
272
|
+
const rule = masterWire.recurrenceRule;
|
|
273
|
+
if (!rule) {
|
|
274
|
+
throw new CalendarError(`event ${masterId} carries no recurrence rule`, {
|
|
275
|
+
type: 'invalidArguments',
|
|
276
|
+
operation,
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
const boundary = new Date(new Date(`${splitIso}Z`).getTime() - 1_000).toISOString().slice(0, 19);
|
|
280
|
+
// Every descriptive part of the rule (byDay, byMonthDay, bySetPosition,
|
|
281
|
+
// firstDayOfWeek, ...) carries over unchanged; only the bound changes,
|
|
282
|
+
// from whatever `count`/`until` the rule had to the new `until` boundary
|
|
283
|
+
// (wtfalch/calendar#27 -- carrying only frequency/interval here is what
|
|
284
|
+
// dropped every other part on a split).
|
|
285
|
+
const parsed = recurrenceRuleFromWire(rule);
|
|
286
|
+
if (!parsed) {
|
|
287
|
+
throw new CalendarError(`event ${masterId} carries no recurrence rule`, {
|
|
288
|
+
type: 'invalidArguments',
|
|
289
|
+
operation,
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
const { count: _count, until: _until, ...truncatedParts } = parsed;
|
|
293
|
+
const truncated = { ...truncatedParts, until: boundary };
|
|
294
|
+
const wire = patchToWire({ recurrenceRule: truncated });
|
|
295
|
+
for (const [key, override] of Object.entries(masterWire.recurrenceOverrides ?? {})) {
|
|
296
|
+
if (key >= splitIso && override?.excluded !== true) {
|
|
297
|
+
wire[`recurrenceOverrides/${key}`] = { excluded: true };
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
const result = await connection.request('CalendarEvent/set', { accountId, update: { [masterId]: wire }, sendSchedulingMessages });
|
|
301
|
+
const refused = firstRefusal(result.notUpdated);
|
|
302
|
+
if (refused) {
|
|
303
|
+
throw new CalendarError(refused.description ?? 'the server refused to end the series there', {
|
|
304
|
+
type: refused.type ?? 'unknown',
|
|
305
|
+
operation,
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
/** `occurrence`'s own `recurrenceId`, or a thrown `CalendarError` --
|
|
310
|
+
* 'following' needs a date to split the series at, which only an
|
|
311
|
+
* occurrence has; 'series' has no such requirement (`updateSeries`,
|
|
312
|
+
* `deleteSeries` take a master id just as happily as an occurrence's). */
|
|
313
|
+
function requireOccurrence(event, operation) {
|
|
314
|
+
if (event.recurrenceId === undefined) {
|
|
315
|
+
throw new CalendarError("scope 'following' needs an occurrence id, not a master's", {
|
|
316
|
+
type: 'invalidArguments',
|
|
317
|
+
operation,
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
return event.recurrenceId;
|
|
321
|
+
}
|
|
322
|
+
/** 'series': resolve `id` (an occurrence or already the master) to the
|
|
323
|
+
* master, then patch that -- `updateEventDirect` already does exactly this
|
|
324
|
+
* once given the right id, so there is no separate wire call to write
|
|
325
|
+
* here. */
|
|
326
|
+
async function updateSeries(connection, id, patch, options) {
|
|
327
|
+
return guard('events.update', async () => {
|
|
328
|
+
const event = await getEvent(connection, id);
|
|
329
|
+
const masterId = event.baseEventId ?? id;
|
|
330
|
+
return updateEventDirect(connection, masterId, patch, options);
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
async function deleteSeries(connection, id, options) {
|
|
334
|
+
return guard('events.delete', async () => {
|
|
335
|
+
const event = await getEvent(connection, id);
|
|
336
|
+
const masterId = event.baseEventId ?? id;
|
|
337
|
+
return deleteEventDirect(connection, masterId, options);
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* The truncation `updateFollowing` and `deleteFollowing` share: find the
|
|
342
|
+
* occurrence's master, end the master's rule right before it, and hand back
|
|
343
|
+
* everything `updateFollowing` additionally needs to build the forward
|
|
344
|
+
* event -- the master (for its `calendarId`/`recurrenceRule`) and the
|
|
345
|
+
* occurrence itself (for its own, possibly overridden, fields).
|
|
346
|
+
*/
|
|
347
|
+
async function splitBeforeOccurrence(connection, id, sendSchedulingMessages, operation) {
|
|
348
|
+
const accountId = await connection.accountId();
|
|
349
|
+
const occurrence = await getEvent(connection, id);
|
|
350
|
+
const splitIso = requireOccurrence(occurrence, operation);
|
|
351
|
+
const masterId = occurrence.baseEventId ?? id;
|
|
352
|
+
const masterWire = await getRawEvent(connection, accountId, masterId, operation);
|
|
353
|
+
await truncateSeriesBefore(connection, accountId, masterId, masterWire, splitIso, sendSchedulingMessages, operation);
|
|
354
|
+
return { occurrence, master: eventFromWire(masterWire) };
|
|
355
|
+
}
|
|
356
|
+
async function updateFollowing(connection, id, patch, options) {
|
|
357
|
+
return guard('events.update', async () => {
|
|
358
|
+
const sendScheduling = options?.sendSchedulingMessages ?? false;
|
|
359
|
+
const { occurrence, master } = await splitBeforeOccurrence(connection, id, sendScheduling, 'events.update');
|
|
360
|
+
const rule = master.recurrenceRule;
|
|
361
|
+
if (!rule) {
|
|
362
|
+
throw new CalendarError(`event ${master.id} carries no recurrence rule`, {
|
|
363
|
+
type: 'invalidArguments',
|
|
364
|
+
operation: 'events.update',
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
// The new series' own end: an absolute `until` carries over untouched,
|
|
368
|
+
// an unbounded rule stays unbounded, and a relative `count` needs the
|
|
369
|
+
// occurrences already elapsed subtracted off (`countElapsedOccurrences`).
|
|
370
|
+
// Every other part of the rule (byDay, byMonthDay, bySetPosition,
|
|
371
|
+
// firstDayOfWeek, ...) carries over unchanged -- only the bound is
|
|
372
|
+
// recomputed (wtfalch/calendar#27 -- rebuilding from frequency/interval
|
|
373
|
+
// alone here is what dropped every other part on a split).
|
|
374
|
+
const { count: _forwardCount, until: _forwardUntil, ...forwardParts } = rule;
|
|
375
|
+
let forwardRule = { ...forwardParts };
|
|
376
|
+
if (rule.until !== undefined) {
|
|
377
|
+
forwardRule = { ...forwardRule, until: rule.until };
|
|
378
|
+
}
|
|
379
|
+
else if (rule.count !== undefined) {
|
|
380
|
+
const accountId = await connection.accountId();
|
|
381
|
+
// `occurrence.recurrenceId`, not `occurrence.start`: the split point
|
|
382
|
+
// is the occurrence's own scheduled slot in the rule's sequence, which
|
|
383
|
+
// an earlier per-occurrence override could have moved `start` away
|
|
384
|
+
// from without changing which slot it still is.
|
|
385
|
+
const elapsed = await countElapsedOccurrences(connection, accountId, master.id, master.start, occurrence.recurrenceId ?? occurrence.start);
|
|
386
|
+
const remaining = rule.count - elapsed;
|
|
387
|
+
if (remaining <= 0) {
|
|
388
|
+
throw new CalendarError('the chosen occurrence is at or past the end of the series', {
|
|
389
|
+
type: 'invalidArguments',
|
|
390
|
+
operation: 'events.update',
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
forwardRule = { ...forwardRule, count: remaining };
|
|
394
|
+
}
|
|
395
|
+
const { id: _id, recurrenceId: _recurrenceId, baseEventId: _baseEventId, sequence: _sequence, ...occurrenceFields } = occurrence;
|
|
396
|
+
const forwardDraft = {
|
|
397
|
+
...occurrenceFields,
|
|
398
|
+
uid: crypto.randomUUID(),
|
|
399
|
+
recurrenceRule: forwardRule,
|
|
400
|
+
...patch,
|
|
401
|
+
};
|
|
402
|
+
const forwardCalendarId = patch.calendarId ?? occurrence.calendarId;
|
|
403
|
+
return createEvent(connection, forwardCalendarId, forwardDraft, options);
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
async function deleteFollowing(connection, id, options) {
|
|
407
|
+
return guard('events.delete', async () => {
|
|
408
|
+
// Delete-following is just the truncation (the ticket's own wording) --
|
|
409
|
+
// no new event, so `splitBeforeOccurrence`'s master/occurrence are both
|
|
410
|
+
// unused here beyond the truncation it already performed.
|
|
411
|
+
await splitBeforeOccurrence(connection, id, options?.sendSchedulingMessages ?? false, 'events.delete');
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
async function updateEvent(connection, id, patch, options) {
|
|
415
|
+
const scope = options?.scope;
|
|
416
|
+
if (scope === 'series')
|
|
417
|
+
return updateSeries(connection, id, patch, options);
|
|
418
|
+
if (scope === 'following')
|
|
419
|
+
return updateFollowing(connection, id, patch, options);
|
|
420
|
+
return updateEventDirect(connection, id, patch, options);
|
|
421
|
+
}
|
|
422
|
+
async function deleteEvent(connection, id, options) {
|
|
423
|
+
const scope = options?.scope;
|
|
424
|
+
if (scope === 'series')
|
|
425
|
+
return deleteSeries(connection, id, options);
|
|
426
|
+
if (scope === 'following')
|
|
427
|
+
return deleteFollowing(connection, id, options);
|
|
428
|
+
return deleteEventDirect(connection, id, options);
|
|
429
|
+
}
|
|
430
|
+
/** Whether two calendar addresses name the same person, `mailto:` scheme
|
|
431
|
+
* and case aside -- mirrors `react/participants.ts`'s `sameAddress`, kept
|
|
432
|
+
* as its own copy because the framework-free client must not import from
|
|
433
|
+
* `./react`. */
|
|
434
|
+
function addressMatches(a, b) {
|
|
435
|
+
return a.replace(/^mailto:/i, '').toLowerCase() === b.replace(/^mailto:/i, '').toLowerCase();
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Reply to an invite, as an iTIP REPLY against the caller's own copy -- never
|
|
439
|
+
* `updateEvent`, the organiser's verb (#11). The wire's participant map key
|
|
440
|
+
* is assigned by whoever created the event and is never part of the contract
|
|
441
|
+
* (`wire.ts`'s `participantsToWire` note), so the caller's own entry is found
|
|
442
|
+
* by address, the same way `Calendar.tsx`'s RSVP row already has to. Only
|
|
443
|
+
* that entry's `participationStatus` -- and `participationComment`, when a
|
|
444
|
+
* comment was given, the wire name a live server actually answers to, not
|
|
445
|
+
* JSCalendar's plain `comment` (T11 live capture) -- is patched by JSON
|
|
446
|
+
* Pointer, never the whole `participants` map: live-confirmed against
|
|
447
|
+
* Stalwart v0.16.20 that this changes nothing else, `sequence` included, on
|
|
448
|
+
* the replier's own copy, while the organiser's copy of `participationStatus`
|
|
449
|
+
* updates on its own with no separate write on the organiser's side (T11 live
|
|
450
|
+
* capture, `wire-live-transcript.test.ts`).
|
|
451
|
+
*/
|
|
452
|
+
async function respond(connection, eventId, status, options) {
|
|
453
|
+
return guard('events.respond', async () => {
|
|
454
|
+
const accountId = await connection.accountId();
|
|
455
|
+
const identity = await connection.identity();
|
|
456
|
+
const result = await connection.request('CalendarEvent/get', { accountId, ids: [eventId], properties: ['id', 'participants'] });
|
|
457
|
+
const wire = result.list[0];
|
|
458
|
+
if (!wire) {
|
|
459
|
+
throw new CalendarError(`no event ${eventId} on this account`, {
|
|
460
|
+
type: 'notFound',
|
|
461
|
+
operation: 'events.respond',
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
const entry = Object.entries(wire.participants ?? {}).find(([, participant]) => addressMatches(participant.calendarAddress, identity.calendarAddress));
|
|
465
|
+
if (!entry) {
|
|
466
|
+
throw new CalendarError(`${identity.calendarAddress} is not a participant on event ${eventId}`, {
|
|
467
|
+
type: 'notFound',
|
|
468
|
+
operation: 'events.respond',
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
const [key] = entry;
|
|
472
|
+
const pointer = `participants/${key.replaceAll('~', '~0').replaceAll('/', '~1')}`;
|
|
473
|
+
const patch = { [`${pointer}/participationStatus`]: status };
|
|
474
|
+
if (options?.comment !== undefined)
|
|
475
|
+
patch[`${pointer}/participationComment`] = options.comment;
|
|
476
|
+
const set = await connection.request('CalendarEvent/set', {
|
|
477
|
+
accountId,
|
|
478
|
+
update: { [eventId]: patch },
|
|
479
|
+
sendSchedulingMessages: options?.sendSchedulingMessages ?? false,
|
|
480
|
+
});
|
|
481
|
+
const refused = firstRefusal(set.notUpdated);
|
|
482
|
+
if (refused) {
|
|
483
|
+
throw new CalendarError(refused.description ?? 'the server refused the reply', {
|
|
484
|
+
type: refused.type ?? 'unknown',
|
|
485
|
+
operation: 'events.respond',
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
});
|
|
489
|
+
}
|
|
131
490
|
/**
|
|
132
491
|
* Add or replace one grant in a calendar's `shareWith` ACL, via a JSON
|
|
133
492
|
* Pointer patch on that one grantee rather than replacing the whole map --
|
|
@@ -204,22 +563,66 @@ async function unshareCalendar(connection, calendarId, grantAccountId) {
|
|
|
204
563
|
});
|
|
205
564
|
}
|
|
206
565
|
/**
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
*
|
|
566
|
+
* Every named principal's busy periods over `range`, one JMAP request no
|
|
567
|
+
* matter how many principals — `Principal/getAvailability` batches like any
|
|
568
|
+
* other JMAP method call (RFC 8620 §3.3, measured live for issue #13).
|
|
569
|
+
*
|
|
570
|
+
* **The request shape an earlier slice guessed was wrong.** It sent
|
|
571
|
+
* `{accountId: principalId, after, before}`, on the theory that the method
|
|
572
|
+
* reads a *different* account the way a shared-mailbox read does. Live
|
|
573
|
+
* Stalwart answered `invalidArguments: "Missing principal id"` to that shape
|
|
574
|
+
* on every call. The real shape, measured against the same server: `accountId`
|
|
575
|
+
* is the *caller's own* account, the principal is a separate `id` argument,
|
|
576
|
+
* and the range travels as `utcStart`/`utcEnd`, never `after`/`before` — see
|
|
577
|
+
* `test/availability-many.test.ts` for the pinned transcript.
|
|
578
|
+
*
|
|
579
|
+
* A principal's own refusal (a malformed id, a transient server error) reads
|
|
580
|
+
* the same as no share at all: `'unknown'`, not a thrown error that would
|
|
581
|
+
* take every other principal in the batch down with it — one more way of not
|
|
582
|
+
* being able to see somebody, the same discipline
|
|
583
|
+
* `AvailabilityRibbon`/`Calendar.tsx` already apply to a failed read.
|
|
214
584
|
*/
|
|
215
|
-
async function getAvailability(connection,
|
|
585
|
+
async function getAvailability(connection, principalIds, range) {
|
|
216
586
|
return guard('availability.get', async () => {
|
|
217
|
-
const
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
587
|
+
const answers = new Map();
|
|
588
|
+
const uniqueIds = [...new Set(principalIds)];
|
|
589
|
+
if (uniqueIds.length === 0)
|
|
590
|
+
return answers;
|
|
591
|
+
const callerAccountId = await connection.accountId();
|
|
592
|
+
const results = await connection.requestMany(uniqueIds.map((id) => ({
|
|
593
|
+
id,
|
|
594
|
+
method: 'Principal/getAvailability',
|
|
595
|
+
args: { accountId: callerAccountId, id, utcStart: range.after, utcEnd: range.before },
|
|
596
|
+
})));
|
|
597
|
+
for (const principalId of uniqueIds) {
|
|
598
|
+
const outcome = results.get(principalId);
|
|
599
|
+
const list = outcome && 'data' in outcome
|
|
600
|
+
? (outcome.data.list ?? [])
|
|
601
|
+
: [];
|
|
602
|
+
const periods = availabilityFromWire(list);
|
|
603
|
+
// An empty list is the server's one answer to three different truths
|
|
604
|
+
// (never shared, shared then revoked, shared and genuinely free) --
|
|
605
|
+
// `AvailabilityResult`'s doc comment. It is never read as "known and
|
|
606
|
+
// free".
|
|
607
|
+
answers.set(principalId, periods.length > 0 ? { kind: 'known', periods } : { kind: 'unknown' });
|
|
608
|
+
}
|
|
609
|
+
return answers;
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* Slots at least `duration` long where every principal in `principalIds`
|
|
614
|
+
* that `getAvailability` could read is free. The search itself
|
|
615
|
+
* (`find-time.ts`'s `findFreeSlots`) is pure and only ever sees `'known'`
|
|
616
|
+
* answers — an unknown principal is reported in `FindTimeResult.unknown`,
|
|
617
|
+
* never treated as free (issue #13).
|
|
618
|
+
*/
|
|
619
|
+
async function findTime(connection, principalIds, range, duration, options) {
|
|
620
|
+
return guard('availability.findTime', async () => {
|
|
621
|
+
const uniqueIds = [...new Set(principalIds)];
|
|
622
|
+
const answers = await getAvailability(connection, uniqueIds, range);
|
|
623
|
+
const unknown = uniqueIds.filter((id) => answers.get(id)?.kind !== 'known');
|
|
624
|
+
const slots = findFreeSlots(answers, range, duration, options?.workingHours ?? false, uniqueIds);
|
|
625
|
+
return { slots, unknown };
|
|
223
626
|
});
|
|
224
627
|
}
|
|
225
628
|
export function createCalendarClient(options) {
|
|
@@ -228,15 +631,18 @@ export function createCalendarClient(options) {
|
|
|
228
631
|
identity: () => guard('client.identity', () => connection.identity()),
|
|
229
632
|
listCalendars: () => listCalendars(connection),
|
|
230
633
|
listEvents: (range) => listEvents(connection, range),
|
|
634
|
+
searchEvents: (query, options) => searchEvents(connection, query, options),
|
|
231
635
|
getEvent: (id) => getEvent(connection, id),
|
|
232
636
|
createEvent: (calendarId, event, options) => createEvent(connection, calendarId, event, options),
|
|
233
637
|
updateEvent: (id, patch, options) => updateEvent(connection, id, patch, options),
|
|
234
638
|
deleteEvent: (id, options) => deleteEvent(connection, id, options),
|
|
639
|
+
respond: (eventId, status, options) => respond(connection, eventId, status, options),
|
|
235
640
|
shareCalendar: (calendarId, grant) => shareCalendar(connection, calendarId, grant),
|
|
236
641
|
resolvePrincipal: (email) => resolvePrincipal(connection, email),
|
|
237
642
|
lookupPrincipals: (accountIds) => lookupPrincipals(connection, accountIds),
|
|
238
643
|
unshareCalendar: (calendarId, accountId) => unshareCalendar(connection, calendarId, accountId),
|
|
239
|
-
getAvailability: (
|
|
644
|
+
getAvailability: (principalIds, range) => getAvailability(connection, principalIds, range),
|
|
645
|
+
findTime: (principalIds, range, duration, findOptions) => findTime(connection, principalIds, range, duration, findOptions),
|
|
240
646
|
subscribe: (handler) => push(connection, handler),
|
|
241
647
|
};
|
|
242
648
|
}
|
|
@@ -373,6 +373,19 @@
|
|
|
373
373
|
font-size: var(--text-xs);
|
|
374
374
|
}
|
|
375
375
|
|
|
376
|
+
/* The weekly recurrence picker's seven day tiles (wtfalch/calendar#30), a row
|
|
377
|
+
rather than `Checkbox`'s own default full-width stack -- seven full-width
|
|
378
|
+
rows for "which day" would dwarf every other field in the form. */
|
|
379
|
+
.wtcal-weekdays {
|
|
380
|
+
display: flex;
|
|
381
|
+
flex-wrap: wrap;
|
|
382
|
+
gap: var(--space-2);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
.wtcal-weekdays > * {
|
|
386
|
+
flex: 1 1 3rem;
|
|
387
|
+
}
|
|
388
|
+
|
|
376
389
|
/* The recurring-scope question. Both answers are full-width buttons of equal
|
|
377
390
|
width: this screen exists because there is no safe default, and burying one
|
|
378
391
|
answer would invent one. */
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
* `fetch` is the only way a fake transport can answer real JMAP without a
|
|
24
24
|
* second, `jmap-jam`-shaped fake underneath it.
|
|
25
25
|
*/
|
|
26
|
+
import { CalendarError } from './types.ts';
|
|
26
27
|
export declare const CALENDAR_CAPABILITY = "urn:ietf:params:jmap:calendars";
|
|
27
28
|
export declare const CORE_CAPABILITY = "urn:ietf:params:jmap:core";
|
|
28
29
|
export type CalendarConnectionOptions = {
|
|
@@ -69,6 +70,32 @@ export declare class CalendarConnection {
|
|
|
69
70
|
* token every time (see the class doc comment).
|
|
70
71
|
*/
|
|
71
72
|
request<A extends object, R>(method: string, args: A): Promise<R>;
|
|
73
|
+
/**
|
|
74
|
+
* Several JMAP method calls in one HTTP request — RFC 8620 §3.3 batches
|
|
75
|
+
* method calls into one `methodCalls` array by design, and
|
|
76
|
+
* `Principal/getAvailability` batches exactly like every other JMAP
|
|
77
|
+
* method. Split into as many requests as `#maxCallsInRequest` demands
|
|
78
|
+
* (one call list that fits is one request; findTime's many-principal read
|
|
79
|
+
* is the caller that first needed more than 16 — issue #13) and merged
|
|
80
|
+
* back into one result map, so a caller never has to know the split
|
|
81
|
+
* happened at all.
|
|
82
|
+
*
|
|
83
|
+
* Each call's outcome is keyed by its own id and is either `{data}` or
|
|
84
|
+
* `{error}` — one principal being refused must not take the rest of the
|
|
85
|
+
* batch down with it. A transport-level failure (the request never
|
|
86
|
+
* reached the server, or the server refused a whole chunk's envelope)
|
|
87
|
+
* still throws, the same as `request` does: that is a real outage, not
|
|
88
|
+
* "this one principal cannot be seen."
|
|
89
|
+
*/
|
|
90
|
+
requestMany(calls: readonly {
|
|
91
|
+
id: string;
|
|
92
|
+
method: string;
|
|
93
|
+
args: Record<string, unknown>;
|
|
94
|
+
}[]): Promise<Map<string, {
|
|
95
|
+
data: unknown;
|
|
96
|
+
} | {
|
|
97
|
+
error: CalendarError;
|
|
98
|
+
}>>;
|
|
72
99
|
/** The event source URL for `subscribe`, with its template expanded. */
|
|
73
100
|
eventSourceUrl(options: {
|
|
74
101
|
types: string;
|