Katlendar Developer manual GitHub

Sync

How calendars and task lists stay in step with servers, from the Provider trait to the background thread.

Sync has two halves:

  • src/sync/ knows how to talk to each kind of server. It knows nothing about the window.
  • src/syncing/ is the window's side: it decides when to sync, starts the work on a background thread and takes in the answers.

Provider#

Every kind of server is a Provider (in src/sync/mod.rs). All its calls block, so they only ever run on a background thread.

CallWhat it does
list_calendarsthe calendars and task lists on the account (RemoteCalendar)
pullnew, changed and deleted events since state, as a Delta
pull_tasksthe same for tasks
home, forget_homewhere the calendars were found, kept so the next sync skips the search
save_native, save_task_nativewrite an event or task in the server's own format
getread one item
putwrite an item as iCalendar, guarded by a Guard
deletedelete an item, if its etag still matches

Most calls have a default that does nothing, so a provider implements only what its server offers.

A Delta carries upserts, tasks, deletes and changed addresses, plus the new state and ctag. When full is set, upserts is everything the calendar holds, and anything missing from it is gone.

Writing back#

push sends a list of PushJobs and returns a PushResult for each one. A write is guarded:

  • a new item is written only if nothing is at its address yet;
  • a changed item is written only if the server still has the version that was read;
  • if the server gives no versions, the write goes through.

When the server's copy changed or went in the meantime, the server wins: its copy is kept, not the edit made here. An event deleted here that changed on the server stays.

Providers#

FileServerReadsWrites
caldav.rsany CalDAV server: iCloud, Nextcloud, Radicale, Google's CalDAVevents and tasksput, delete
google.rsGoogle Calendar, over CalDAV with an OAuth tokeneventsas CalDAV
graph.rsMicrosoft 365 and Outlook, over Microsoft Grapheventssave_native, delete
todoist.rsTodoist, over its v1 sync API with the person's own tokentaskssave_task_native, delete
proton.rsProton Calendar, over Proton's own APIeventssave_native, put, delete
nextcloud.rsNextcloud's Login Flow v2, which hands back an app password
oauth.rsthe browser sign-in Google and Microsoft share

A subscribed feed (ics.rs) is a provider too. It can only be read, and always whole.

Proton writes only to the person's own calendars. Shared calendars, calendars where they are a guest, and repeating events stay read-only. See Integrations for how sign-in works.

Errors#

SyncError says why a sync failed:

VariantMeaning
Auththe server refused the sign-in
Conflictthe item changed on the server since it was read
TokenExpiredthe server forgot the sync state; everything is read again
RateLimitedthe server is busy; it is tried again later
Gonenothing is at that address
Networkthe server couldn't be reached
Otheranything else

SyncError::text gives the words the person sees. syncing::account_error adds what to do for each kind of account, such as "sign out, then sign in once more".

When sync runs#

WhenConstant
every 15 minutesSYNC_EVERY_S
2 seconds after a change made herePUSH_AFTER_S
on coming back to the window, at most once a minuteSYNC_ON_FOCUS_S

A calendar or account that keeps failing waits longer each time: back_off doubles the wait, up to four hours. A Nextcloud browser sign-in is waited for 5 minutes (NEXTCLOUD_WAIT_S).

The window's side#

FileWhat it does
syncing/mod.rsin_background, opening an account as a Provider, the access-token cache
syncing/connect.rssubscribing, signing in and out, moving private links into the keyring
syncing/run.rssync_feed, sync_account, pull_account, sync_all, sync_due, back_off
syncing/edits.rsnote_edits: after each save, mark what changed and put deletes in the outbox
syncing/take.rspoll_sync: take in the SyncDone messages and merge them into the data
syncing/tests.rstests of the above
syncing/live.rschecks against real servers, off by default (see Testing)

Access tokens live in memory only (TokenCache). Refresh tokens and passwords live in the keyring.

Adding a kind of server#

  1. Write a Provider in src/sync/. Implement only the calls the server offers.
  2. Add a flag to Account with #[serde(default)].
  3. Open it in syncing::open_account, and sign in from syncing/connect.rs.
  4. Add its words to account_error.
  5. Put sample answers in src/sync/fixtures/ and test against them.
  6. Write its guide page on the site, and update the privacy statement: a new server is a new place data goes.

Improve this page on GitHub