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.
| Call | What it does |
|---|---|
list_calendars | the calendars and task lists on the account (RemoteCalendar) |
pull | new, changed and deleted events since state, as a Delta |
pull_tasks | the same for tasks |
home, forget_home | where the calendars were found, kept so the next sync skips the search |
save_native, save_task_native | write an event or task in the server's own format |
get | read one item |
put | write an item as iCalendar, guarded by a Guard |
delete | delete 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#
| File | Server | Reads | Writes |
|---|---|---|---|
caldav.rs | any CalDAV server: iCloud, Nextcloud, Radicale, Google's CalDAV | events and tasks | put, delete |
google.rs | Google Calendar, over CalDAV with an OAuth token | events | as CalDAV |
graph.rs | Microsoft 365 and Outlook, over Microsoft Graph | events | save_native, delete |
todoist.rs | Todoist, over its v1 sync API with the person's own token | tasks | save_task_native, delete |
proton.rs | Proton Calendar, over Proton's own API | events | save_native, put, delete |
nextcloud.rs | Nextcloud's Login Flow v2, which hands back an app password | ||
oauth.rs | the 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:
| Variant | Meaning |
|---|---|
Auth | the server refused the sign-in |
Conflict | the item changed on the server since it was read |
TokenExpired | the server forgot the sync state; everything is read again |
RateLimited | the server is busy; it is tried again later |
Gone | nothing is at that address |
Network | the server couldn't be reached |
Other | anything 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#
| When | Constant |
|---|---|
| every 15 minutes | SYNC_EVERY_S |
| 2 seconds after a change made here | PUSH_AFTER_S |
| on coming back to the window, at most once a minute | SYNC_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#
| File | What it does |
|---|---|
syncing/mod.rs | in_background, opening an account as a Provider, the access-token cache |
syncing/connect.rs | subscribing, signing in and out, moving private links into the keyring |
syncing/run.rs | sync_feed, sync_account, pull_account, sync_all, sync_due, back_off |
syncing/edits.rs | note_edits: after each save, mark what changed and put deletes in the outbox |
syncing/take.rs | poll_sync: take in the SyncDone messages and merge them into the data |
syncing/tests.rs | tests of the above |
syncing/live.rs | checks 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#
- Write a
Providerinsrc/sync/. Implement only the calls the server offers. - Add a flag to
Accountwith#[serde(default)]. - Open it in
syncing::open_account, and sign in fromsyncing/connect.rs. - Add its words to
account_error. - Put sample answers in
src/sync/fixtures/and test against them. - Write its guide page on the site, and update the privacy statement: a new server is a new place data goes.