@davidwoods/skylight-mcp 1.23.0 → 1.25.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/README.md +17 -5
- package/dist/lib-writes.d.ts +126 -1
- package/dist/lib-writes.d.ts.map +1 -1
- package/dist/lib-writes.js +341 -3
- package/dist/lib-writes.js.map +1 -1
- package/dist/lib.d.ts +22 -5
- package/dist/lib.d.ts.map +1 -1
- package/dist/lib.js +30 -4
- package/dist/lib.js.map +1 -1
- package/dist/tools/calendar.d.ts.map +1 -1
- package/dist/tools/calendar.js +65 -17
- package/dist/tools/calendar.js.map +1 -1
- package/dist/utils/series.d.ts +45 -0
- package/dist/utils/series.d.ts.map +1 -0
- package/dist/utils/series.js +181 -0
- package/dist/utils/series.js.map +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -334,7 +334,7 @@ encrypted in the OAuth grant, not in a database you manage.
|
|
|
334
334
|
The hosted server registers all **read and create/update tools**, plus these
|
|
335
335
|
deletes: `delete_chore`, `delete_list`, `delete_list_item`, `delete_reward`,
|
|
336
336
|
`delete_recipe`, `delete_meal_sitting`, `delete_task_box_item`, `delete_message` and
|
|
337
|
-
`delete_calendar_event` (events created in Skylight or on a Google calendar,
|
|
337
|
+
`delete_calendar_event` (events created in Skylight or on a Google calendar, repeating ones with an explicit scope, and occurrences edited on their own; never subscribed feeds). It does **not**
|
|
338
338
|
register `delete_source_calendar`, `delete_label` or photo uploads. The local (stdio) server
|
|
339
339
|
registers everything. This is a deliberate choice, not a limitation of the Worker:
|
|
340
340
|
|
|
@@ -522,9 +522,9 @@ Messages are the notes shown on the frame (Skylight's web app calls them "nudges
|
|
|
522
522
|
| `delete_source_calendar` | destructive | Stop syncing a subscribed calendar feed (a team, school or league schedule). Preview first, then `confirm: true`. Its events leave the family calendar and wall display until it is added again. Feeds only (`kind: webcal`); account calendars are refused. Local server only. |
|
|
523
523
|
| `update_source_calendar_profiles` | write | Link a subscribed calendar feed to family profiles, or unlink it (the "Link profiles" checkboxes). Preview first, then `confirm: true`; it sends the complete set so the calendar's label is kept, then reads it back. Feeds only (`kind: webcal`); a calendar can never be left with no profile or label. |
|
|
524
524
|
| `rename_source_calendar` | write | Rename a subscribed calendar feed as it appears in Skylight (feeds are named from the feed itself, often at length). Preview first, then `confirm: true`; reads it back and checks the linked profiles did not change. Feeds only (`kind: webcal`). |
|
|
525
|
-
| `create_calendar_event` | write | Add an event. `profiles` assigns it to named family members. Local times are sent with the frame's UTC offset. `repeat`
|
|
525
|
+
| `create_calendar_event` | write | Add an event. `profiles` assigns it to named family members. Local times are sent with the frame's UTC offset. `repeat` makes a series: daily or weekly (ends by `until` or `count`, at most 104 occurrences), monthly (`monthlyOn` `date`, e.g. the 15th, or `weekday`, e.g. the 3rd Tuesday) or yearly (on the start's date); monthly and yearly may have no end. Skylight skips a month without the day (the 29th to 31st) or a 5th weekday, and years without Feb 29; the preview names the skipped months or years as a WARNING. Monthly and yearly verified live through the tool on 2026-11-16 (monthly on the 31st and yearly all-day on Skylight's calendar; monthly 3rd Tuesday on a Google calendar, checked in Google); a series is only previewed until `confirm: true`, is created on Skylight's own calendar by default (not written to Google), and is refused for multi-profile. An all-day series (`allDay: true`, one day per occurrence, `endsAt` the day after `startsAt`) is allowed (verified live 2026-11-09 on Skylight's own calendar). `calendar` (the name of a linked Google calendar, only when asked for) writes a one-off event or a series to that Google calendar instead, shows for everyone who shares it, and is also previewed until `confirm: true`; it refuses a read-only calendar and one whose account cannot be determined (the account is read from the calendar's own record, falling back to an existing event on it within 90 days). A one-off event written to Google is verified live (2026-11-09); a series written to Google is not yet. `dinner: true` makes a free-tier "Dinner plan" instead (Skylight's stand-in for Meals on an account without Plus: an all-day event of kind `meal`, tagged "Dinner Plan" by Skylight; date only, no profiles, calendar or repeat); shape captured from the web app; the same request body verified live on a free account 2026-11-10 (create, read back, delete), the tool call itself not yet. |
|
|
526
526
|
| `update_calendar_event` | write | Change an event, including who it is assigned to (`profiles`). For a repeating event give the occurrence's `date` and a `scope`: `one` (that occurrence only, which becomes its own event) or `all` (the whole series; overwrites earlier single-occurrence edits). On a series you can change the title, the assignment and the time of day (`startsAt` and `endsAt` together, on the occurrence's date); the time change is verified live for both scopes (`one` detaches that occurrence as its own event; ids change, so re-read before the next call). Notes, location and all-day stay refused. There is no `future` scope: Skylight's API refuses it. Events on a Google calendar change at Google too. |
|
|
527
|
-
| `delete_calendar_event` | destructive | Delete one event, or part of a repeating one. Needs the event's `date`; preview first, then `confirm: true`. A repeating event needs a `scope`: `one`, `future` (this and later occurrences) or `all` (the whole series); the preview says which. Events on a Google calendar are deleted at Google too, for everyone who shares it. Refuses subscribed-feed events, non-editable events,
|
|
527
|
+
| `delete_calendar_event` | destructive | Delete one event, or part of a repeating one. Needs the event's `date`; preview first, then `confirm: true`. A repeating event needs a `scope`: `one`, `future` (this and later occurrences) or `all` (the whole series); the preview says which. Events on a Google calendar are deleted at Google too, for everyone who shares it. An occurrence that was edited on its own is its own event: deleted without a scope, the rest of the series stays (request captured and checked in Skylight and Google 2026-11-16). Refuses subscribed-feed events, non-editable events, a repeating event with no scope, and a scope on a one-off or split-off event. Reads the day back to confirm it is gone. Also on the hosted server. |
|
|
528
528
|
|
|
529
529
|
### Chore Tools
|
|
530
530
|
|
|
@@ -637,19 +637,31 @@ Behaviour an importer should plan for:
|
|
|
637
637
|
|
|
638
638
|
What is not there, on purpose: event updates and deletes (planned next), other writes, messages, source-calendar and feed management, and any function that takes a URL or a feed address (sign-in lives in the separate setup entry below, which accepts exactly one address: the Skylight redirect you paste). There is no host or fetch option; the API host is fixed in code and an unknown option is refused. Only the package root and `/setup` are importable (deep imports such as `dist/api/client.js` fail). The library holds a live sign-in, so it stops accidents and token leaks, not a malicious importer. Verified read-only against a real frame on 2026-11-09 (chores and list items were empty that day).
|
|
639
639
|
|
|
640
|
-
###
|
|
640
|
+
### Writing events (experimental)
|
|
641
641
|
|
|
642
642
|
```ts
|
|
643
643
|
const entry = { title: "Dentist", start: "2026-12-03T15:00", end: "2026-12-03T16:00", profileIds: ["123"], calendar: "456" };
|
|
644
644
|
const preview = await skylight.calendar.previewCreate(entry); // changes nothing; show it, get a yes
|
|
645
645
|
const event = await skylight.calendar.create(entry); // same arguments; returns the stored event
|
|
646
|
+
|
|
647
|
+
// later, from a read: id, the date it was read on, and its version
|
|
648
|
+
const ref = { id: event.id, date: "2026-12-03", version: event.version };
|
|
649
|
+
await skylight.calendar.previewUpdate({ ...ref, changes: { title: "Dentist (moved)" } });
|
|
650
|
+
await skylight.calendar.update({ ...ref, changes: { title: "Dentist (moved)" } }); // returns the event under its id after the change
|
|
651
|
+
await skylight.calendar.delete({ ...ref, scope: "one" }); // scope only for a repeating event
|
|
646
652
|
```
|
|
647
653
|
|
|
648
654
|
- Two functions, identical arguments. Both run the same check against fresh reads (profiles, labels, calendars), so the preview cannot drift from the write. `create` checks again, writes once, then reads the event back and resolves only when the read-back shows every field as asked.
|
|
649
655
|
- `calendar` is required: `"skylight"` for Skylight's own calendar (shows on Skylight only), or the `id` of a calendar from `calendar.sources()` whose `writable` is true: a Google calendar this account can edit. Skylight writes the event through to Google, where everyone who shares that calendar sees it, with that calendar's default reminders. The calendar's address and account are looked up inside the call and never returned.
|
|
650
656
|
- Times are wall-clock in the frame's time zone: `start` and `end` as `YYYY-MM-DDTHH:MM`, no offset or `Z`. A time the clocks skip is refused; an ambiguous time (the repeated hour) is read as the earlier one. All day: `allDay: true`, `start` as `YYYY-MM-DD`, `end` left out (one day). The preview shows the times as they will be sent (ISO with the frame's offset).
|
|
651
657
|
- Optional: `location` (500 characters), `notes` (5,000), `profileIds` (from `profiles.list()`), `labelIds` (from `labels.list()`; a label a synced calendar writes to is refused, Skylight assigns it itself). Title: one line, 200 characters. These limits are the library's; Skylight's own are not known.
|
|
652
|
-
-
|
|
658
|
+
- Repeating: `repeat: { every: "day" | "week" | "month" | "year", interval?, monthlyOn?: "date" | "weekday", until?: "YYYY-MM-DD", count? }`. Daily and weekly need an end (`until` or `count`); monthly and yearly may be open-ended (a birthday, a bill). Weekly repeats on the start's weekday; monthly on the start's day (`"date"`, e.g. the 15th) or its weekday (`"weekday"`, e.g. the 3rd Tuesday, as the Skylight app offers); yearly on the start's date. The preview's `repeat` gives the rule in words, the first dates and what is skipped, and `warnings` says so in a sentence: Skylight skips months without the day (a series on the 31st has no February, April, June, September or November occurrence) and years without Feb 29; nothing is moved. Show the warning with the preview. Multi-day all-day events are not supported yet.
|
|
659
|
+
- Update and delete take `{ id, date, version }` from a read. `version` is a fingerprint of the event's fields (including notes and the rule); if the event changed since that read, the call is refused with `stale` and nothing is written. Skylight has no conditional write, so a change landing between the library's re-read and its write is not caught.
|
|
660
|
+
- A repeating event's occurrence needs `scope`: update `"one"` (it becomes its own event with a new id) or `"all"`; Skylight refuses "this and following" for an edit. Delete `"one"`, `"future"` (this and every later one) or `"all"` (the whole series, including occurrences edited on their own). A one-off event, or an occurrence already edited on its own (`detached: true`), takes no scope. A missing or wrong scope is refused, never guessed.
|
|
661
|
+
- On a series, update changes only the title, people, labels and the time of day (start and end on the same date); location, notes and all-day on a series are refused as not verified. A one-off event can change any field. People and labels not mentioned stay as they are, including the label Skylight puts on a Google calendar's events.
|
|
662
|
+
- `update` resolves with the event as read back under its id after the change: an occurrence's id embeds its start time, so a time edit changes it, and a scope-`"one"` edit makes a new event. `delete` resolves `{ deleted: true, id, scope }` once the event is gone from that date.
|
|
663
|
+
- Verified live on 2026-11-16 on Skylight's calendar and a linked Google calendar: monthly (by date and by weekday), weekly and yearly series; scope-one and whole-series edits; deletes with every scope; deleting an occurrence edited on its own; a stale version refused. All test events deleted afterwards.
|
|
664
|
+
- More codes: `invalid_repeat`, `not_found` (no event with that id on that date), `stale`, `not_editable` (a subscribed calendar or an event Skylight locks), `scope_required`, `scope_not_allowed`.
|
|
653
665
|
- Errors are plain `Error`s with `name` `"SkylightWriteError"` and a `code`; check `name` and `code`, not `instanceof` (the root exports no classes). Input: `bad_input`, `invalid_title`, `invalid_time`, `end_before_start`, `unknown_profile`, `unknown_label`, `label_not_assignable` (each with `ids`), `unknown_calendar`, `calendar_not_writable`, `unsupported`. The write: `skylight_refused` (Skylight said no, with `status`; nothing was written), `outcome_unknown` (a network or server error after sending: it may or may not exist, read before retrying), `not_confirmed` (accepted, with `eventId`, but the read-back did not show it as asked within about 2 seconds; Skylight's list can lag, so read before retrying). Messages are the library's own; Skylight's response text is never included. A sign-in failure stays an `AuthenticationError`.
|
|
654
666
|
- After `create` resolves, a read of that day through the library shows the event (the read-back used the same list). On a Google calendar Skylight adds that calendar's own label to the event, and (seen live) writes it to Google during the create call itself; the library does not check Google.
|
|
655
667
|
- Verified on a real frame on 2026-11-16: a create on Skylight's calendar with a profile, and a create on a linked Google calendar that then showed in Google Calendar with the same title and times. Both test events were deleted afterwards.
|
package/dist/lib-writes.d.ts
CHANGED
|
@@ -15,7 +15,8 @@
|
|
|
15
15
|
* chosen by its `calendar.sources()` id and resolved inside the call.
|
|
16
16
|
*/
|
|
17
17
|
import type { CalendarEventResource, CreateCalendarEventRequest, SourceCalendarResource } from "./api/types.js";
|
|
18
|
-
|
|
18
|
+
import { type SeriesInput } from "./utils/series.js";
|
|
19
|
+
export type WriteErrorCode = "bad_input" | "invalid_title" | "invalid_time" | "end_before_start" | "unknown_profile" | "unknown_label" | "label_not_assignable" | "unknown_calendar" | "calendar_not_writable" | "unsupported" | "invalid_repeat" | "not_found" | "stale" | "not_editable" | "scope_required" | "scope_not_allowed" | "skylight_refused" | "outcome_unknown" | "not_confirmed";
|
|
19
20
|
export interface SkylightWriteError extends Error {
|
|
20
21
|
name: "SkylightWriteError";
|
|
21
22
|
code: WriteErrorCode;
|
|
@@ -52,6 +53,13 @@ export interface CreateEventInput {
|
|
|
52
53
|
/** Label ids from `labels.list()`. A label a synced calendar writes to is refused. */
|
|
53
54
|
labelIds?: string[];
|
|
54
55
|
calendar: string;
|
|
56
|
+
/**
|
|
57
|
+
* Make it a repeating series. `every`: "day", "week", "month" or "year"; `interval` (default 1);
|
|
58
|
+
* monthly `monthlyOn`: "date" (the start's day, default) or "weekday" (e.g. the 3rd Tuesday, from
|
|
59
|
+
* the start); end with `until` (YYYY-MM-DD) or `count`. Daily and weekly need an end; monthly and
|
|
60
|
+
* yearly may be open-ended. Weekly repeats on the start's weekday only.
|
|
61
|
+
*/
|
|
62
|
+
repeat?: SeriesInput;
|
|
55
63
|
}
|
|
56
64
|
/** Exactly what a create would write, in library terms. Times are as they will be sent (ISO with the frame's offset, or dates for all day). */
|
|
57
65
|
export interface CreatePreview {
|
|
@@ -72,6 +80,15 @@ export interface CreatePreview {
|
|
|
72
80
|
kind: "skylight" | "google";
|
|
73
81
|
};
|
|
74
82
|
timezone: string;
|
|
83
|
+
/** For a series: the rule in words, its first dates, and months or years it skips. Null for a one-off. */
|
|
84
|
+
repeat: {
|
|
85
|
+
rule: string;
|
|
86
|
+
first: string[];
|
|
87
|
+
skipped: string[];
|
|
88
|
+
last: string | null;
|
|
89
|
+
} | null;
|
|
90
|
+
/** Things the person may not expect, e.g. months a monthly rule skips. Show them with the preview. */
|
|
91
|
+
warnings: string[];
|
|
75
92
|
}
|
|
76
93
|
/** True when the library can create on this source: a Google calendar this account can write to. */
|
|
77
94
|
export declare function sourceWritable(cal: SourceCalendarResource): boolean;
|
|
@@ -85,6 +102,8 @@ interface CreatePlan {
|
|
|
85
102
|
endMs: number;
|
|
86
103
|
categoryIds: string[];
|
|
87
104
|
source: "skylight" | "google";
|
|
105
|
+
/** FREQ of a series, for the read-back; null for a one-off. */
|
|
106
|
+
freq: string | null;
|
|
88
107
|
}
|
|
89
108
|
/** The one check behind previewCreate and create. Reads categories and calendars fresh every time. */
|
|
90
109
|
export declare function planCreate(input: unknown, timeZone: string): Promise<CreatePlan>;
|
|
@@ -94,5 +113,111 @@ export declare function planCreate(input: unknown, timeZone: string): Promise<Cr
|
|
|
94
113
|
* Skylight's list can lag a write).
|
|
95
114
|
*/
|
|
96
115
|
export declare function commitCreate(input: unknown, timeZone: string, categoryIdsOf: (e: CalendarEventResource) => string[]): Promise<CalendarEventResource>;
|
|
116
|
+
/** The version of an event as read: changes when its title, times, all-day, location, notes, people, labels or rule change. */
|
|
117
|
+
export declare function eventVersion(event: CalendarEventResource, categoryIdsOf: (e: CalendarEventResource) => string[]): string;
|
|
118
|
+
/** What a read returned for one event: the id, the date it was read on, and its version. */
|
|
119
|
+
export interface EventRef {
|
|
120
|
+
id: string;
|
|
121
|
+
date: string;
|
|
122
|
+
version: string;
|
|
123
|
+
}
|
|
124
|
+
export interface UpdateEventInput extends EventRef {
|
|
125
|
+
/** Repeating events only: "one" (this occurrence; it becomes its own event) or "all" (the whole series). */
|
|
126
|
+
scope?: "one" | "all";
|
|
127
|
+
/**
|
|
128
|
+
* What to change; anything left out stays. One-off events (and occurrences already edited on their
|
|
129
|
+
* own): any field. A series: title, profileIds, labelIds, and start with end on the same date (time
|
|
130
|
+
* of day only). Times as for create.
|
|
131
|
+
*/
|
|
132
|
+
changes: {
|
|
133
|
+
title?: string;
|
|
134
|
+
start?: string;
|
|
135
|
+
end?: string;
|
|
136
|
+
allDay?: boolean;
|
|
137
|
+
location?: string;
|
|
138
|
+
notes?: string;
|
|
139
|
+
profileIds?: string[];
|
|
140
|
+
labelIds?: string[];
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
export interface DeleteEventInput extends EventRef {
|
|
144
|
+
/** Repeating events only: "one", "future" (this and every later one) or "all" (the whole series). */
|
|
145
|
+
scope?: "one" | "future" | "all";
|
|
146
|
+
}
|
|
147
|
+
/** The event as read before the change, in library terms. Produced by the caller's projection. */
|
|
148
|
+
export type EventProjection<T> = (event: CalendarEventResource) => Promise<T>;
|
|
149
|
+
export interface UpdatePreview<T> {
|
|
150
|
+
action: "update";
|
|
151
|
+
/** The event as it is now. */
|
|
152
|
+
event: T;
|
|
153
|
+
scope: "one" | "all" | null;
|
|
154
|
+
/** The fields that will be written, in library terms (times as they will be sent). */
|
|
155
|
+
changes: {
|
|
156
|
+
title?: string;
|
|
157
|
+
start?: string;
|
|
158
|
+
end?: string;
|
|
159
|
+
allDay?: boolean;
|
|
160
|
+
location?: string | null;
|
|
161
|
+
notes?: string | null;
|
|
162
|
+
profileIds?: string[];
|
|
163
|
+
labelIds?: string[];
|
|
164
|
+
};
|
|
165
|
+
/** What the change reaches, in words. */
|
|
166
|
+
effect: string;
|
|
167
|
+
/** True when the event is on a Google calendar: the change reaches Google for everyone who shares it. */
|
|
168
|
+
google: boolean;
|
|
169
|
+
warnings: string[];
|
|
170
|
+
}
|
|
171
|
+
export interface DeletePreview<T> {
|
|
172
|
+
action: "delete";
|
|
173
|
+
event: T;
|
|
174
|
+
scope: "one" | "future" | "all" | null;
|
|
175
|
+
effect: string;
|
|
176
|
+
google: boolean;
|
|
177
|
+
}
|
|
178
|
+
interface Found {
|
|
179
|
+
event: CalendarEventResource;
|
|
180
|
+
kind: "one-off" | "series" | "detached";
|
|
181
|
+
google: boolean;
|
|
182
|
+
}
|
|
183
|
+
interface UpdatePlan {
|
|
184
|
+
found: Found;
|
|
185
|
+
scope: "one" | "all" | null;
|
|
186
|
+
body: Record<string, unknown>;
|
|
187
|
+
changes: UpdatePreview<unknown>["changes"];
|
|
188
|
+
effect: string;
|
|
189
|
+
/** What the read-back must see. */
|
|
190
|
+
expect: {
|
|
191
|
+
title?: string;
|
|
192
|
+
startMs?: number;
|
|
193
|
+
endMs?: number;
|
|
194
|
+
allDay?: boolean;
|
|
195
|
+
location?: string | null;
|
|
196
|
+
notes?: string | null;
|
|
197
|
+
categoryIds: string[];
|
|
198
|
+
readDates: string[];
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
/** The one check behind previewUpdate and update. */
|
|
202
|
+
export declare function planUpdate(input: unknown, timeZone: string, categoryIdsOf: (e: CalendarEventResource) => string[]): Promise<UpdatePlan>;
|
|
203
|
+
/**
|
|
204
|
+
* Update, then read back. Resolves with the event as stored, under its id after the change: an edit
|
|
205
|
+
* of one occurrence makes a new event, and a time edit of a series changes every occurrence's id.
|
|
206
|
+
*/
|
|
207
|
+
export declare function commitUpdate(input: unknown, timeZone: string, categoryIdsOf: (e: CalendarEventResource) => string[]): Promise<CalendarEventResource>;
|
|
208
|
+
interface DeletePlan {
|
|
209
|
+
found: Found;
|
|
210
|
+
scope: "one" | "future" | "all" | null;
|
|
211
|
+
effect: string;
|
|
212
|
+
date: string;
|
|
213
|
+
}
|
|
214
|
+
/** The one check behind previewDelete and delete. */
|
|
215
|
+
export declare function planDelete(input: unknown, timeZone: string, categoryIdsOf: (e: CalendarEventResource) => string[]): Promise<DeletePlan>;
|
|
216
|
+
/** Delete, then read the date back until the event is gone. */
|
|
217
|
+
export declare function commitDelete(input: unknown, timeZone: string, categoryIdsOf: (e: CalendarEventResource) => string[]): Promise<{
|
|
218
|
+
deleted: true;
|
|
219
|
+
id: string;
|
|
220
|
+
scope: DeletePlan["scope"];
|
|
221
|
+
}>;
|
|
97
222
|
export {};
|
|
98
223
|
//# sourceMappingURL=lib-writes.d.ts.map
|
package/dist/lib-writes.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lib-writes.d.ts","sourceRoot":"","sources":["../src/lib-writes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAOH,OAAO,KAAK,EAAE,qBAAqB,EAAoB,0BAA0B,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"lib-writes.d.ts","sourceRoot":"","sources":["../src/lib-writes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAOH,OAAO,KAAK,EAAE,qBAAqB,EAAoB,0BAA0B,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAC;AAElI,OAAO,EAA2B,KAAK,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAE9E,MAAM,MAAM,cAAc,GACtB,WAAW,GACX,eAAe,GACf,cAAc,GACd,kBAAkB,GAClB,iBAAiB,GACjB,eAAe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,EAAE,MAAM,EAAE,aAAa,EAAE,CAAC,CAAC,EAAE,qBAAqB,KAAK,MAAM,EAAE,GAAG,OAAO,CAAC;IAAE,OAAO,EAAE,IAAI,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,UAAU,CAAC,OAAO,CAAC,CAAA;CAAE,CAAC,CAe9L"}
|
package/dist/lib-writes.js
CHANGED
|
@@ -15,11 +15,12 @@
|
|
|
15
15
|
* chosen by its `calendar.sources()` id and resolved inside the call.
|
|
16
16
|
*/
|
|
17
17
|
import { getClient } from "./api/client.js";
|
|
18
|
-
import { getCalendarEvents, createCalendarEvent } from "./api/endpoints/calendar.js";
|
|
18
|
+
import { getCalendarEvents, createCalendarEvent, deleteCalendarEvent, updateCalendarEvent } from "./api/endpoints/calendar.js";
|
|
19
19
|
import { getCategories } from "./api/endpoints/categories.js";
|
|
20
20
|
import { isoWithOffset } from "./api/endpoints/messages.js";
|
|
21
21
|
import { readBackUntil } from "./api/endpoints/source-calendars.js";
|
|
22
22
|
import { addDays, dateInZone, toLocalParts, zonedToEpoch } from "./utils/schedule.js";
|
|
23
|
+
import { planSeries, SeriesError } from "./utils/series.js";
|
|
23
24
|
export function writeError(code, message, extra = {}) {
|
|
24
25
|
const e = new Error(message);
|
|
25
26
|
e.name = "SkylightWriteError";
|
|
@@ -32,7 +33,7 @@ export function writeError(code, message, extra = {}) {
|
|
|
32
33
|
e.ids = extra.ids;
|
|
33
34
|
return e;
|
|
34
35
|
}
|
|
35
|
-
const CREATE_KEYS = new Set(["title", "start", "end", "allDay", "location", "notes", "profileIds", "labelIds", "calendar"]);
|
|
36
|
+
const CREATE_KEYS = new Set(["title", "start", "end", "allDay", "location", "notes", "profileIds", "labelIds", "calendar", "repeat"]);
|
|
36
37
|
const TITLE_MAX = 200;
|
|
37
38
|
const LOCATION_MAX = 500;
|
|
38
39
|
const NOTES_MAX = 5000;
|
|
@@ -156,6 +157,18 @@ export async function planCreate(input, timeZone) {
|
|
|
156
157
|
const notes = optionalText(raw.notes, "notes", NOTES_MAX);
|
|
157
158
|
const profileIds = idList(raw.profileIds, "profileIds");
|
|
158
159
|
const labelIds = idList(raw.labelIds, "labelIds");
|
|
160
|
+
let series = null;
|
|
161
|
+
if (raw.repeat !== undefined) {
|
|
162
|
+
const startTime = allDay ? "00:00" : `${String(toLocalParts(startMs, timeZone).hour).padStart(2, "0")}:${String(toLocalParts(startMs, timeZone).minute).padStart(2, "0")}`;
|
|
163
|
+
try {
|
|
164
|
+
series = planSeries(raw.repeat, startDate, startTime, timeZone);
|
|
165
|
+
}
|
|
166
|
+
catch (error) {
|
|
167
|
+
if (error instanceof SeriesError)
|
|
168
|
+
throw writeError("invalid_repeat", error.message);
|
|
169
|
+
throw error;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
159
172
|
if (typeof raw.calendar !== "string" || (raw.calendar !== "skylight" && !ID.test(raw.calendar))) {
|
|
160
173
|
throw writeError("bad_input", 'calendar is required: "skylight", or the id of a Google calendar from calendar.sources().');
|
|
161
174
|
}
|
|
@@ -206,6 +219,7 @@ export async function planCreate(input, timeZone) {
|
|
|
206
219
|
description: notes ?? "",
|
|
207
220
|
location: location ?? "",
|
|
208
221
|
category_ids: categoryIds,
|
|
222
|
+
...(series ? { rrule: [series.rrule] } : {}),
|
|
209
223
|
...(target ? { calendar_id: target.calendarId, calendar_account_id: target.accountId } : {}),
|
|
210
224
|
};
|
|
211
225
|
const preview = {
|
|
@@ -222,8 +236,11 @@ export async function planCreate(input, timeZone) {
|
|
|
222
236
|
labels: labelIds.map((id) => name(byId.get(id))),
|
|
223
237
|
calendar: target ? { id: target.id, label: target.label, kind: "google" } : { id: "skylight", label: "Skylight", kind: "skylight" },
|
|
224
238
|
timezone: timeZone,
|
|
239
|
+
repeat: series ? { rule: series.rule, first: series.first, skipped: series.skipped, last: series.last } : null,
|
|
240
|
+
warnings: series ? [...series.warnings] : [],
|
|
225
241
|
};
|
|
226
|
-
|
|
242
|
+
const freq = series ? /FREQ=([A-Z]+)/.exec(series.rrule)[1] : null;
|
|
243
|
+
return { preview, body, startDate, endDate, startMs, endMs, categoryIds, source: target ? "google" : "skylight", freq };
|
|
227
244
|
}
|
|
228
245
|
/** Map anything the write itself threw to the library's own error, never Skylight's text. Sign-in errors pass through. */
|
|
229
246
|
function mapWriteFailure(error) {
|
|
@@ -255,6 +272,13 @@ function matchesPlan(event, plan, categoryIdsOf) {
|
|
|
255
272
|
return false;
|
|
256
273
|
if (a.source !== plan.source)
|
|
257
274
|
return false;
|
|
275
|
+
if (plan.freq !== null) {
|
|
276
|
+
const rule = Array.isArray(a.rrule) ? a.rrule.join(";") : "";
|
|
277
|
+
if (a.recurring !== true || !rule.includes(`FREQ=${plan.freq}`))
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
else if (a.recurring === true)
|
|
281
|
+
return false;
|
|
258
282
|
const stored = new Set(categoryIdsOf(event));
|
|
259
283
|
return plan.categoryIds.every((id) => stored.has(id));
|
|
260
284
|
}
|
|
@@ -284,4 +308,318 @@ export async function commitCreate(input, timeZone, categoryIdsOf) {
|
|
|
284
308
|
}
|
|
285
309
|
return stored;
|
|
286
310
|
}
|
|
311
|
+
// ---------------------------------------------------------------------------------------------
|
|
312
|
+
// Stage 2: update and delete. An event is named by what a read returned: its id, the date it was
|
|
313
|
+
// read on, and its version (a fingerprint of the fields shown). The commit reads the event again
|
|
314
|
+
// and refuses if the version moved, so an edit made from an old screen is refused, not applied.
|
|
315
|
+
// Skylight has no conditional write, so a change landing between that re-read and the write is
|
|
316
|
+
// not caught.
|
|
317
|
+
// ---------------------------------------------------------------------------------------------
|
|
318
|
+
/** FNV-1a, twice with different seeds: a stable 16-hex fingerprint with no crypto (runs in Workers). */
|
|
319
|
+
function fnv(text, seed) {
|
|
320
|
+
let h = seed >>> 0;
|
|
321
|
+
for (let i = 0; i < text.length; i++) {
|
|
322
|
+
h ^= text.charCodeAt(i);
|
|
323
|
+
h = Math.imul(h, 0x01000193) >>> 0;
|
|
324
|
+
}
|
|
325
|
+
return h.toString(16).padStart(8, "0");
|
|
326
|
+
}
|
|
327
|
+
/** The version of an event as read: changes when its title, times, all-day, location, notes, people, labels or rule change. */
|
|
328
|
+
export function eventVersion(event, categoryIdsOf) {
|
|
329
|
+
const a = event.attributes;
|
|
330
|
+
const text = JSON.stringify([
|
|
331
|
+
String(event.id),
|
|
332
|
+
typeof a.summary === "string" ? a.summary : "",
|
|
333
|
+
Date.parse(String(a.starts_at)),
|
|
334
|
+
Date.parse(String(a.ends_at)),
|
|
335
|
+
a.all_day === true,
|
|
336
|
+
typeof a.location === "string" ? a.location.trim() : "",
|
|
337
|
+
typeof a.description === "string" ? a.description.trim() : "",
|
|
338
|
+
[...categoryIdsOf(event)].sort(),
|
|
339
|
+
Array.isArray(a.rrule) ? a.rrule.join("|") : "",
|
|
340
|
+
a.master_event_id == null ? "" : String(a.master_event_id),
|
|
341
|
+
]);
|
|
342
|
+
return fnv(text, 0x811c9dc5) + fnv(text, 0x9747b28c);
|
|
343
|
+
}
|
|
344
|
+
const REF_KEYS = ["id", "date", "version"];
|
|
345
|
+
const UPDATE_KEYS = new Set([...REF_KEYS, "scope", "changes"]);
|
|
346
|
+
const DELETE_KEYS = new Set([...REF_KEYS, "scope"]);
|
|
347
|
+
const CHANGE_KEYS = new Set(["title", "start", "end", "allDay", "location", "notes", "profileIds", "labelIds"]);
|
|
348
|
+
const EVENT_ID = /^\d+(-\d+)?$/;
|
|
349
|
+
/** Read the event fresh and check it is the one the caller saw and that Skylight can change it. */
|
|
350
|
+
async function findForWrite(raw, keys, timeZone, categoryIdsOf, verb) {
|
|
351
|
+
for (const key of Object.keys(raw))
|
|
352
|
+
if (!keys.has(key))
|
|
353
|
+
throw writeError("bad_input", `Unknown field "${key}".`);
|
|
354
|
+
if (typeof raw.id !== "string" || !EVENT_ID.test(raw.id))
|
|
355
|
+
throw writeError("bad_input", "id must be an event id from calendar.events().");
|
|
356
|
+
if (typeof raw.date !== "string" || !realDate(raw.date))
|
|
357
|
+
throw writeError("bad_input", "date must be the event's date, YYYY-MM-DD, as read.");
|
|
358
|
+
if (typeof raw.version !== "string" || raw.version === "")
|
|
359
|
+
throw writeError("bad_input", "version is required: pass the event's version from calendar.events().");
|
|
360
|
+
const list = await getCalendarEvents({ dateMin: raw.date, dateMax: raw.date, timezone: timeZone });
|
|
361
|
+
const event = list.find((e) => String(e.id) === raw.id);
|
|
362
|
+
if (!event)
|
|
363
|
+
throw writeError("not_found", `No event with that id on ${raw.date}: it may have moved or been deleted. Read the calendar again.`);
|
|
364
|
+
if (eventVersion(event, categoryIdsOf) !== raw.version) {
|
|
365
|
+
throw writeError("stale", `The event changed since it was read. Nothing was ${verb}: read it again and show the new version before asking again.`);
|
|
366
|
+
}
|
|
367
|
+
const a = event.attributes;
|
|
368
|
+
if (a.source !== "skylight" && a.source !== "google")
|
|
369
|
+
throw writeError("not_editable", "That event comes from a subscribed calendar, which is read-only.");
|
|
370
|
+
if (a.editable === false)
|
|
371
|
+
throw writeError("not_editable", "Skylight marks that event as not editable.");
|
|
372
|
+
const detached = a.master_event_id != null && a.master_event_id !== "";
|
|
373
|
+
const series = !detached && (a.recurring === true || a.recurring_config === true);
|
|
374
|
+
return { event, kind: detached ? "detached" : series ? "series" : "one-off", google: a.source === "google" };
|
|
375
|
+
}
|
|
376
|
+
function seriesKeyOf(event) {
|
|
377
|
+
const a = event.attributes;
|
|
378
|
+
return { uid: typeof a.uid === "string" ? a.uid : "", number: String(event.id).split("-")[0] };
|
|
379
|
+
}
|
|
380
|
+
/** The one check behind previewUpdate and update. */
|
|
381
|
+
export async function planUpdate(input, timeZone, categoryIdsOf) {
|
|
382
|
+
if (!input || typeof input !== "object" || Array.isArray(input))
|
|
383
|
+
throw writeError("bad_input", "update needs an object: id, date, version, changes.");
|
|
384
|
+
const raw = input;
|
|
385
|
+
const changesRaw = raw.changes;
|
|
386
|
+
if (!changesRaw || typeof changesRaw !== "object" || Array.isArray(changesRaw))
|
|
387
|
+
throw writeError("bad_input", "changes must be an object of the fields to change.");
|
|
388
|
+
const c = changesRaw;
|
|
389
|
+
for (const key of Object.keys(c))
|
|
390
|
+
if (!CHANGE_KEYS.has(key))
|
|
391
|
+
throw writeError("bad_input", `Unknown change "${key}".`);
|
|
392
|
+
if (Object.keys(c).length === 0)
|
|
393
|
+
throw writeError("bad_input", "Nothing to change.");
|
|
394
|
+
if (raw.scope !== undefined && raw.scope !== "one" && raw.scope !== "all") {
|
|
395
|
+
throw writeError("scope_not_allowed", raw.scope === "future" ? 'Skylight refuses "this and following" for an edit: use "one" or "all".' : 'scope must be "one" or "all".');
|
|
396
|
+
}
|
|
397
|
+
const found = await findForWrite(raw, UPDATE_KEYS, timeZone, categoryIdsOf, "changed");
|
|
398
|
+
const a = found.event.attributes;
|
|
399
|
+
const scope = raw.scope ?? null;
|
|
400
|
+
if (found.kind === "series" && scope === null)
|
|
401
|
+
throw writeError("scope_required", 'That is an occurrence of a repeating event: say scope "one" (only this one) or "all" (the whole series).');
|
|
402
|
+
if (found.kind !== "series" && scope !== null)
|
|
403
|
+
throw writeError("scope_not_allowed", "scope applies to an occurrence of a repeating event only.");
|
|
404
|
+
if (found.kind === "series") {
|
|
405
|
+
const bad = ["allDay", "location", "notes"].filter((k) => c[k] !== undefined);
|
|
406
|
+
if (bad.length)
|
|
407
|
+
throw writeError("unsupported", `On a repeating event only the title, people, labels and time of day can be changed here (${bad.join(", ")} on a series is not verified).`);
|
|
408
|
+
if ((c.start === undefined) !== (c.end === undefined))
|
|
409
|
+
throw writeError("invalid_time", "To change a repeating event's time give both start and end.");
|
|
410
|
+
}
|
|
411
|
+
const changes = {};
|
|
412
|
+
const body = {};
|
|
413
|
+
const expect = { categoryIds: [], readDates: [raw.date] };
|
|
414
|
+
if (c.title !== undefined) {
|
|
415
|
+
if (typeof c.title !== "string" || c.title.trim() === "")
|
|
416
|
+
throw writeError("invalid_title", "title must be non-empty text.");
|
|
417
|
+
const title = c.title.trim();
|
|
418
|
+
if (title.length > TITLE_MAX)
|
|
419
|
+
throw writeError("invalid_title", `title is longer than ${TITLE_MAX} characters.`);
|
|
420
|
+
if (/[\r\n]/.test(title) || CONTROL.test(title))
|
|
421
|
+
throw writeError("invalid_title", "title must be one line with no control characters.");
|
|
422
|
+
changes.title = title;
|
|
423
|
+
body.summary = title;
|
|
424
|
+
expect.title = title;
|
|
425
|
+
}
|
|
426
|
+
const wasAllDay = a.all_day === true;
|
|
427
|
+
if (c.allDay !== undefined && typeof c.allDay !== "boolean")
|
|
428
|
+
throw writeError("bad_input", "allDay must be true or false.");
|
|
429
|
+
const allDay = c.allDay === undefined ? wasAllDay : c.allDay;
|
|
430
|
+
if (c.allDay !== undefined && c.allDay !== wasAllDay && (c.start === undefined)) {
|
|
431
|
+
throw writeError("invalid_time", "Changing all-day needs a new start (and end for a timed event).");
|
|
432
|
+
}
|
|
433
|
+
if (c.start !== undefined || c.end !== undefined) {
|
|
434
|
+
if (c.start !== undefined && typeof c.start !== "string")
|
|
435
|
+
throw writeError("invalid_time", "start must be text.");
|
|
436
|
+
if (c.end !== undefined && typeof c.end !== "string")
|
|
437
|
+
throw writeError("invalid_time", "end must be text.");
|
|
438
|
+
let startMs;
|
|
439
|
+
let endMs;
|
|
440
|
+
if (allDay) {
|
|
441
|
+
const day = c.start ?? dateInZone(Date.parse(String(a.starts_at)), timeZone);
|
|
442
|
+
if (!realDate(day))
|
|
443
|
+
throw writeError("invalid_time", "For an all-day event start must be a date, YYYY-MM-DD.");
|
|
444
|
+
if (c.end !== undefined && c.end !== day)
|
|
445
|
+
throw writeError("unsupported", "An all-day event covers one day: leave end out or give the same date.");
|
|
446
|
+
startMs = zonedToEpoch(day, "00:00", timeZone);
|
|
447
|
+
endMs = zonedToEpoch(addDays(day, 1), "00:00", timeZone);
|
|
448
|
+
body.starts_at = day;
|
|
449
|
+
body.ends_at = addDays(day, 1);
|
|
450
|
+
changes.start = day;
|
|
451
|
+
changes.end = addDays(day, 1);
|
|
452
|
+
expect.readDates.push(day);
|
|
453
|
+
}
|
|
454
|
+
else {
|
|
455
|
+
startMs = c.start !== undefined ? localToEpoch(c.start, timeZone, "start") : Date.parse(String(a.starts_at));
|
|
456
|
+
endMs = c.end !== undefined ? localToEpoch(c.end, timeZone, "end") : Date.parse(String(a.ends_at));
|
|
457
|
+
if (endMs <= startMs)
|
|
458
|
+
throw writeError("end_before_start", "end must be after start.");
|
|
459
|
+
if (found.kind === "series" && (dateInZone(startMs, timeZone) !== raw.date || dateInZone(endMs - 1, timeZone) !== raw.date)) {
|
|
460
|
+
throw writeError("unsupported", `A time change on a repeating event keeps its date (${raw.date}): only the time of day changes.`);
|
|
461
|
+
}
|
|
462
|
+
body.starts_at = isoWithOffset(startMs, timeZone);
|
|
463
|
+
body.ends_at = isoWithOffset(endMs, timeZone);
|
|
464
|
+
changes.start = body.starts_at;
|
|
465
|
+
changes.end = body.ends_at;
|
|
466
|
+
expect.readDates.push(dateInZone(startMs, timeZone));
|
|
467
|
+
}
|
|
468
|
+
if (found.kind === "series")
|
|
469
|
+
body.timezone = timeZone;
|
|
470
|
+
expect.startMs = startMs;
|
|
471
|
+
expect.endMs = endMs;
|
|
472
|
+
}
|
|
473
|
+
if (c.allDay !== undefined) {
|
|
474
|
+
body.all_day = allDay;
|
|
475
|
+
changes.allDay = allDay;
|
|
476
|
+
expect.allDay = allDay;
|
|
477
|
+
}
|
|
478
|
+
if (c.location !== undefined) {
|
|
479
|
+
const v = optionalText(c.location, "location", LOCATION_MAX);
|
|
480
|
+
body.location = v ?? "";
|
|
481
|
+
changes.location = v;
|
|
482
|
+
expect.location = v;
|
|
483
|
+
}
|
|
484
|
+
if (c.notes !== undefined) {
|
|
485
|
+
const v = optionalText(c.notes, "notes", NOTES_MAX);
|
|
486
|
+
body.description = v ?? "";
|
|
487
|
+
changes.notes = v;
|
|
488
|
+
expect.notes = v;
|
|
489
|
+
}
|
|
490
|
+
// People and labels: the app re-sends the whole set with every edit. Keep what is not being changed,
|
|
491
|
+
// including the label Skylight puts on a Google calendar's events.
|
|
492
|
+
const categories = await getCategories(false);
|
|
493
|
+
const byId = new Map(categories.map((x) => [String(x.id), x]));
|
|
494
|
+
const current = categoryIdsOf(found.event);
|
|
495
|
+
const currentProfiles = current.filter((id) => byId.get(id)?.attributes.linked_to_profile === true);
|
|
496
|
+
const currentLabels = current.filter((id) => byId.get(id)?.attributes.linked_to_profile !== true);
|
|
497
|
+
let profiles = currentProfiles;
|
|
498
|
+
let labels = currentLabels;
|
|
499
|
+
if (c.profileIds !== undefined) {
|
|
500
|
+
profiles = idList(c.profileIds, "profileIds");
|
|
501
|
+
const bad = profiles.filter((id) => byId.get(id)?.attributes.linked_to_profile !== true);
|
|
502
|
+
if (bad.length)
|
|
503
|
+
throw writeError("unknown_profile", "Not a profile on this frame: see profiles.list().", { ids: bad });
|
|
504
|
+
changes.profileIds = profiles;
|
|
505
|
+
}
|
|
506
|
+
if (c.labelIds !== undefined) {
|
|
507
|
+
labels = idList(c.labelIds, "labelIds");
|
|
508
|
+
const unknown = labels.filter((id) => {
|
|
509
|
+
const x = byId.get(id);
|
|
510
|
+
return !x || x.attributes.linked_to_profile === true;
|
|
511
|
+
});
|
|
512
|
+
if (unknown.length)
|
|
513
|
+
throw writeError("unknown_label", "Not a label on this frame: see labels.list().", { ids: unknown });
|
|
514
|
+
const fed = labels.filter((id) => !currentLabels.includes(id) && (byId.get(id)?.relationships?.source_calendars?.data ?? []).length > 0);
|
|
515
|
+
if (fed.length)
|
|
516
|
+
throw writeError("label_not_assignable", "A synced calendar writes to this label; Skylight assigns it itself.", { ids: fed });
|
|
517
|
+
changes.labelIds = labels;
|
|
518
|
+
}
|
|
519
|
+
body.category_ids = [...profiles, ...labels];
|
|
520
|
+
expect.categoryIds = [...profiles, ...labels];
|
|
521
|
+
const title = typeof a.summary === "string" ? a.summary : "this event";
|
|
522
|
+
const effect = found.kind === "series"
|
|
523
|
+
? scope === "one"
|
|
524
|
+
? `only the occurrence on ${raw.date}; it becomes its own event with a new id`
|
|
525
|
+
: `the whole series "${title}", every occurrence, including ones edited on their own`
|
|
526
|
+
: found.kind === "detached"
|
|
527
|
+
? `this occurrence of "${title}", which was already edited on its own`
|
|
528
|
+
: `"${title}"`;
|
|
529
|
+
return { found, scope, body, changes, effect, expect };
|
|
530
|
+
}
|
|
531
|
+
function matchesExpect(event, expect, categoryIdsOf) {
|
|
532
|
+
const a = event.attributes;
|
|
533
|
+
if (expect.title !== undefined && a.summary !== expect.title)
|
|
534
|
+
return false;
|
|
535
|
+
if (expect.startMs !== undefined && Date.parse(String(a.starts_at)) !== expect.startMs)
|
|
536
|
+
return false;
|
|
537
|
+
if (expect.endMs !== undefined && Date.parse(String(a.ends_at)) !== expect.endMs)
|
|
538
|
+
return false;
|
|
539
|
+
if (expect.allDay !== undefined && (a.all_day === true) !== expect.allDay)
|
|
540
|
+
return false;
|
|
541
|
+
if (expect.location !== undefined && !sameText(a.location, expect.location))
|
|
542
|
+
return false;
|
|
543
|
+
if (expect.notes !== undefined && !sameText(a.description, expect.notes))
|
|
544
|
+
return false;
|
|
545
|
+
const stored = new Set(categoryIdsOf(event));
|
|
546
|
+
return expect.categoryIds.every((id) => stored.has(id));
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* Update, then read back. Resolves with the event as stored, under its id after the change: an edit
|
|
550
|
+
* of one occurrence makes a new event, and a time edit of a series changes every occurrence's id.
|
|
551
|
+
*/
|
|
552
|
+
export async function commitUpdate(input, timeZone, categoryIdsOf) {
|
|
553
|
+
const plan = await planUpdate(input, timeZone, categoryIdsOf);
|
|
554
|
+
const id = String(plan.found.event.id);
|
|
555
|
+
try {
|
|
556
|
+
await updateCalendarEvent(id, plan.body, plan.scope ?? undefined);
|
|
557
|
+
}
|
|
558
|
+
catch (error) {
|
|
559
|
+
throw mapWriteFailure(error);
|
|
560
|
+
}
|
|
561
|
+
const key = seriesKeyOf(plan.found.event);
|
|
562
|
+
const dates = [...new Set(plan.expect.readDates)].sort();
|
|
563
|
+
const pick = (list) => {
|
|
564
|
+
const fits = (e) => matchesExpect(e, plan.expect, categoryIdsOf);
|
|
565
|
+
if (plan.found.kind !== "series")
|
|
566
|
+
return list.find((e) => String(e.id) === id && fits(e));
|
|
567
|
+
if (plan.scope === "one") {
|
|
568
|
+
// The split-off occurrence: a plain id, pointing back at the series.
|
|
569
|
+
return list.find((e) => {
|
|
570
|
+
const m = e.attributes.master_event_id;
|
|
571
|
+
return m != null && (String(m) === key.uid || String(m) === key.number) && fits(e);
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
// Whole series: the occurrence on this date, same series.
|
|
575
|
+
return list.find((e) => String(e.id).split("-")[0] === key.number && fits(e));
|
|
576
|
+
};
|
|
577
|
+
const back = await readBackUntil(() => getCalendarEvents({ dateMin: dates[0], dateMax: dates[dates.length - 1], timezone: timeZone }), (list) => pick(list) !== undefined);
|
|
578
|
+
const stored = pick(back.value);
|
|
579
|
+
if (!back.ok || !stored) {
|
|
580
|
+
throw writeError("not_confirmed", "Skylight accepted the change but reading it back did not show it as asked. Read the calendar before trying again.", { eventId: id });
|
|
581
|
+
}
|
|
582
|
+
return stored;
|
|
583
|
+
}
|
|
584
|
+
/** The one check behind previewDelete and delete. */
|
|
585
|
+
export async function planDelete(input, timeZone, categoryIdsOf) {
|
|
586
|
+
if (!input || typeof input !== "object" || Array.isArray(input))
|
|
587
|
+
throw writeError("bad_input", "delete needs an object: id, date, version.");
|
|
588
|
+
const raw = input;
|
|
589
|
+
if (raw.scope !== undefined && raw.scope !== "one" && raw.scope !== "future" && raw.scope !== "all") {
|
|
590
|
+
throw writeError("bad_input", 'scope must be "one", "future" or "all".');
|
|
591
|
+
}
|
|
592
|
+
const found = await findForWrite(raw, DELETE_KEYS, timeZone, categoryIdsOf, "deleted");
|
|
593
|
+
const scope = raw.scope ?? null;
|
|
594
|
+
if (found.kind === "series" && scope === null) {
|
|
595
|
+
throw writeError("scope_required", 'That is an occurrence of a repeating event: say scope "one", "future" (this and every later one) or "all" (the whole series).');
|
|
596
|
+
}
|
|
597
|
+
if (found.kind !== "series" && scope !== null)
|
|
598
|
+
throw writeError("scope_not_allowed", "scope applies to an occurrence of a repeating event only.");
|
|
599
|
+
const a = found.event.attributes;
|
|
600
|
+
const title = typeof a.summary === "string" ? a.summary : "this event";
|
|
601
|
+
const date = raw.date;
|
|
602
|
+
const effect = scope === "one" ? `only the occurrence of "${title}" on ${date}`
|
|
603
|
+
: scope === "future" ? `"${title}" on ${date} and every later occurrence`
|
|
604
|
+
: scope === "all" ? `the whole series "${title}", every occurrence past and future, including ones edited on their own`
|
|
605
|
+
: found.kind === "detached" ? `this occurrence of "${title}", which was edited on its own (the rest of the series stays)`
|
|
606
|
+
: `"${title}"`;
|
|
607
|
+
return { found, scope, effect, date };
|
|
608
|
+
}
|
|
609
|
+
/** Delete, then read the date back until the event is gone. */
|
|
610
|
+
export async function commitDelete(input, timeZone, categoryIdsOf) {
|
|
611
|
+
const plan = await planDelete(input, timeZone, categoryIdsOf);
|
|
612
|
+
const id = String(plan.found.event.id);
|
|
613
|
+
try {
|
|
614
|
+
// An occurrence edited on its own is deleted by its own id with no scope, as the app does.
|
|
615
|
+
await deleteCalendarEvent(id, plan.scope ?? undefined);
|
|
616
|
+
}
|
|
617
|
+
catch (error) {
|
|
618
|
+
throw mapWriteFailure(error);
|
|
619
|
+
}
|
|
620
|
+
const back = await readBackUntil(() => getCalendarEvents({ dateMin: plan.date, dateMax: plan.date, timezone: timeZone }), (list) => !list.some((e) => String(e.id) === id));
|
|
621
|
+
if (!back.ok)
|
|
622
|
+
throw writeError("not_confirmed", "Skylight accepted the delete but the event still shows on reading it back. Read the calendar before trying again.", { eventId: id });
|
|
623
|
+
return { deleted: true, id, scope: plan.scope };
|
|
624
|
+
}
|
|
287
625
|
//# sourceMappingURL=lib-writes.js.map
|