Files
tajniak81andClaude Opus 5.5 6984c7d162 Connect Google Calendar, import OneNote notebooks, sync Readwise, and package the clipper
Phase F follow-ups (ADR-16):

- Calendars: events are now stored in calendar_events by a
  calendars.sync job (every 15 minutes per account, 90 days back and
  400 ahead), so journals open at once and keep showing events while a
  source is down. Google accounts connect with OAuth (calendar.readonly)
  through DOCWELL_GOOGLE_CLIENT_ID/SECRET; reconnecting renews access.
- OneNote: whole notebooks import through Microsoft Graph (Notes.Read,
  DOCWELL_MICROSOFT_CLIENT_ID/SECRET/TENANT) in the background, with
  section groups, sections, subpages, pictures and attached files. The
  Microsoft sign-in lives in memory for an hour and is never stored.
- Readwise: an hourly sync with the person's sealed access token; new
  books get a page under "Readwise", new highlights are appended once.
  New readwise_syncs collection.
- internal/oauth: signed states and one callback route per provider,
  routed by purpose. directory gains ImportConverted and CallerOf.
- Phone app: "Today's journal" on the home screen and calendar events
  on journal pages.
- Web clipper: `npm run package` builds Chrome/Edge, Firefox and source
  zips; store/ holds listing texts, permission notes and a privacy
  policy.
- OpenAPI, generated clients, translations, compose and .env examples,
  and user docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 14:59:34 +01:00

154 lines
5.6 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package httpapi
import (
"context"
"net/http"
"net/url"
"docwell/server/internal/access"
"docwell/server/internal/apperr"
"docwell/server/internal/onenote"
"docwell/server/internal/readwise"
)
// OAuthCallbacks finishes sign-ins at Google and Microsoft: it returns where
// the browser goes next.
type OAuthCallbacks interface {
Callback(ctx context.Context, q url.Values) string
}
// OneNoteService imports OneNote notebooks through Microsoft Graph.
type OneNoteService interface {
Enabled() bool
ConnectURL(c access.Caller, space, parent string) (string, error)
Account(c access.Caller, connID string) (string, error)
Notebooks(ctx context.Context, c access.Caller, connID string) ([]onenote.Notebook, error)
Import(ctx context.Context, c access.Caller, connID, notebook, space, parent string) (onenote.Job, error)
JobStatus(c access.Caller, id string) (onenote.Job, error)
}
// ReadwiseService keeps people's Readwise highlights in Docwell.
type ReadwiseService interface {
Get(ctx context.Context, c access.Caller) (readwise.Connection, error)
Save(ctx context.Context, c access.Caller, in readwise.Input) (readwise.Connection, error)
Delete(ctx context.Context, c access.Caller) error
SyncNow(ctx context.Context, c access.Caller) (readwise.Connection, error)
}
// Integrations says which outside services this server can reach for people.
type Integrations struct {
// Google is set when people can connect Google Calendar.
Google bool `json:"google"`
// Microsoft is set when people can import OneNote notebooks.
Microsoft bool `json:"microsoft"`
// Readwise is set when Readwise highlights can be synced.
Readwise bool `json:"readwise"`
}
var errNoIntegration = apperr.NotFound("this integration is not available on this server")
func (a *api) integrationRoutes(mux *routeMux) {
mux.HandleFunc("GET /api/v1/integrations", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
v := Integrations{Readwise: a.readwise != nil}
if a.calendars != nil {
v.Google = a.calendars.GoogleEnabled()
}
if a.onenote != nil {
v.Microsoft = a.onenote.Enabled()
}
writeJSON(w, http.StatusOK, v)
}))
// The providers send the browser back here; the signed state says who
// asked and for what, so this route needs no session.
mux.HandleFunc("GET /api/v1/oauth/{provider}/callback", func(w http.ResponseWriter, r *http.Request) {
if a.oauth == nil {
a.writeError(w, errNoIntegration)
return
}
switch r.PathValue("provider") {
case "google", "microsoft":
default:
a.writeError(w, apperr.NotFound("not found"))
return
}
// The target is built by the server from DOCWELL_WEB_URL, never taken from the request.
http.Redirect(w, r, a.oauth.Callback(r.Context(), r.URL.Query()), http.StatusFound) //nolint:gosec // see above
})
mux.HandleFunc("GET /api/v1/calendars/google/connect", a.withCalendars(func(w http.ResponseWriter, r *http.Request) {
u, err := a.calendars.GoogleConnectURL(r.Context(), caller(r))
a.respond(w, map[string]string{"url": u}, err)
}))
withOneNote := func(next http.HandlerFunc) http.HandlerFunc {
return a.requireUser(func(w http.ResponseWriter, r *http.Request) {
if a.onenote == nil || !a.onenote.Enabled() {
a.writeError(w, errNoIntegration)
return
}
next(w, r)
})
}
mux.HandleFunc("GET /api/v1/onenote/connect", withOneNote(func(w http.ResponseWriter, r *http.Request) {
u, err := a.onenote.ConnectURL(caller(r), r.URL.Query().Get("space"), r.URL.Query().Get("parent"))
a.respond(w, map[string]string{"url": u}, err)
}))
mux.HandleFunc("GET /api/v1/onenote/connections/{id}", withOneNote(func(w http.ResponseWriter, r *http.Request) {
email, err := a.onenote.Account(caller(r), r.PathValue("id"))
if err != nil {
a.writeError(w, err)
return
}
nbs, err := a.onenote.Notebooks(r.Context(), caller(r), r.PathValue("id"))
a.respond(w, map[string]any{"account": email, "notebooks": nbs}, err)
}))
mux.HandleFunc("POST /api/v1/onenote/connections/{id}/import", withOneNote(func(w http.ResponseWriter, r *http.Request) {
var in struct {
Notebook string `json:"notebook"`
Space string `json:"space"`
Parent string `json:"parent"`
}
if !a.decode(w, r, &in) {
return
}
job, err := a.onenote.Import(r.Context(), caller(r), r.PathValue("id"), in.Notebook, in.Space, in.Parent)
a.respondStatus(w, http.StatusAccepted, job, err)
}))
mux.HandleFunc("GET /api/v1/onenote/jobs/{id}", withOneNote(func(w http.ResponseWriter, r *http.Request) {
job, err := a.onenote.JobStatus(caller(r), r.PathValue("id"))
a.respond(w, job, err)
}))
withReadwise := func(next http.HandlerFunc) http.HandlerFunc {
return a.requireUser(func(w http.ResponseWriter, r *http.Request) {
if a.readwise == nil {
a.writeError(w, errNoIntegration)
return
}
next(w, r)
})
}
mux.HandleFunc("GET /api/v1/readwise", withReadwise(func(w http.ResponseWriter, r *http.Request) {
v, err := a.readwise.Get(r.Context(), caller(r))
a.respond(w, v, err)
}))
mux.HandleFunc("PUT /api/v1/readwise", withReadwise(func(w http.ResponseWriter, r *http.Request) {
var in readwise.Input
if !a.decode(w, r, &in) {
return
}
v, err := a.readwise.Save(r.Context(), caller(r), in)
a.respond(w, v, err)
}))
mux.HandleFunc("DELETE /api/v1/readwise", withReadwise(func(w http.ResponseWriter, r *http.Request) {
a.respondNoContent(w, a.readwise.Delete(r.Context(), caller(r)))
}))
mux.HandleFunc("POST /api/v1/readwise/sync", withReadwise(func(w http.ResponseWriter, r *http.Request) {
v, err := a.readwise.SyncNow(r.Context(), caller(r))
a.respond(w, v, err)
}))
}