Files
tajniak81andClaude Opus 5.5 3d1cf362d7 Add calendars, a web clipper, more importers and workspace snapshots
Phase F of the AFFiNE plan (ADR-16):

- Calendars: people subscribe to iCal feeds (webcal, Google's secret
  address, Outlook) or CalDAV accounts in account settings; their
  events show at the top of journal pages in the reader's time zone.
  Repeats, moved and cancelled occurrences, IANA and Windows zones
  are handled. Events are read on demand and kept in memory for 15
  minutes; passwords are sealed with DOCWELL_SECRET_KEY; sources
  follow the webhook address rules. New calendar_accounts collection.
- Importers: zips of Markdown (Obsidian vaults, Bear TextBundles and
  .bear2bk, Readwise's Markdown, Docwell's own space export) keep
  folders as pages, resolve [[wikilinks]] and embeds by name, and turn
  front matter and #tags into properties and tags. OneNote .mht
  exports, Readwise CSV, and any other CSV as a database page.
- Exports: PNG of a page drawn in the browser, print styles that print
  only the page in the light theme, and workspace snapshots (records,
  Yjs states, JSON and files in one zip) that restore as a new
  workspace. Old ids are swapped byte for byte inside the Yjs updates,
  so links point at the restored pages and comments stay anchored.
- Web-Clipper/: a Manifest V3 extension for Chrome, Edge and Firefox
  that saves the article or the selection as Markdown through the
  import API with a personal API token.
- OpenAPI, generated clients, translations and user docs.

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

167 lines
5.5 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package directory
import (
"context"
"encoding/json"
"errors"
"docwell/server/internal/access"
"docwell/server/internal/apperr"
"docwell/server/internal/collab"
)
// ---- Page content ------------------------------------------------------------
//
// A page body is a Yjs document owned by the collab hub (Docs). These methods
// add the permission checks; edits made here reach open editors live.
// maxContentBytes caps one saved document (the PocketBase field allows 20 MB).
const maxContentBytes = 8 << 20
// Docs is the part of collab.Hub that holds page documents.
type Docs interface {
Content(ctx context.Context, page string) (json.RawMessage, error)
Replace(ctx context.Context, page, user string, doc json.RawMessage, label string) error
// State is the page's document as one Yjs update.
State(ctx context.Context, page string) ([]byte, error)
// Apply merges a Yjs update into the page as an edit by user.
Apply(ctx context.Context, page, user string, update []byte) error
// Kick reconnects the page's editors, so they pick up changed rights.
Kick(page string)
// Drop forgets a deleted page's live document.
Drop(page string)
Versions(ctx context.Context, page string) ([]collab.Version, error)
VersionContent(ctx context.Context, page, id string) (json.RawMessage, error)
}
// PageContent is a page's document as Tiptap JSON; Content is null for a page
// that was never edited.
type PageContent struct {
Content json.RawMessage `json:"content"`
Updated string `json:"updated"`
}
// GetPageContent returns a page's current document (viewer).
func (s *Service) GetPageContent(ctx context.Context, c access.Caller, id string) (PageContent, error) {
p, _, err := s.pageFor(ctx, c, id)
if err != nil {
return PageContent{}, err
}
doc, err := s.docs.Content(ctx, p.ID)
if err != nil {
return PageContent{}, err
}
return PageContent{Content: doc, Updated: p.Updated}, nil
}
type SavePageContentInput struct {
Content json.RawMessage `json:"content"`
}
// SavePageContent replaces a page's document (editor). Connected editors
// receive the change like any other edit.
func (s *Service) SavePageContent(ctx context.Context, c access.Caller, id string, in SavePageContentInput) (PageContent, error) {
p, err := s.editablePage(ctx, c, id)
if err != nil {
return PageContent{}, err
}
if len(in.Content) > maxContentBytes {
return PageContent{}, apperr.InvalidFields(map[string]string{"content": "The page is too large to save."})
}
if !isDoc(in.Content) {
return PageContent{}, apperr.InvalidFields(map[string]string{"content": "Content must be a Tiptap document."})
}
if err := s.docs.Replace(ctx, p.ID, c.ID, in.Content, "before-replace"); err != nil {
return PageContent{}, err
}
return s.GetPageContent(ctx, c, p.ID)
}
// CollabAccess reports whether c may open the page's live document and
// whether their edits are accepted (editor, page not in the trash or archived).
func (s *Service) CollabAccess(ctx context.Context, c access.Caller, id string) (canWrite bool, err error) {
page, err := s.GetPage(ctx, c, id)
if err != nil {
return false, err
}
return !page.InTrash && !page.Archived && access.EditPages(page.Role) == nil, nil
}
// ListVersions lists a page's history, newest first (viewer).
func (s *Service) ListVersions(ctx context.Context, c access.Caller, id string) ([]collab.Version, error) {
p, _, err := s.pageFor(ctx, c, id)
if err != nil {
return nil, err
}
return s.docs.Versions(ctx, p.ID)
}
// GetVersion returns one version's document (viewer).
func (s *Service) GetVersion(ctx context.Context, c access.Caller, id, versionID string) (PageContent, error) {
p, _, err := s.pageFor(ctx, c, id)
if err != nil {
return PageContent{}, err
}
doc, err := s.versionContent(ctx, p.ID, versionID)
return PageContent{Content: doc}, err
}
// RestoreVersion makes an old version the page's document again (editor). It
// is recorded as a new edit, so the current text stays in the history.
func (s *Service) RestoreVersion(ctx context.Context, c access.Caller, id, versionID string) (PageContent, error) {
p, err := s.editablePage(ctx, c, id)
if err != nil {
return PageContent{}, err
}
doc, err := s.versionContent(ctx, p.ID, versionID)
if err != nil {
return PageContent{}, err
}
if !isDoc(doc) {
doc = json.RawMessage(`{"type":"doc","content":[]}`)
}
if err := s.docs.Replace(ctx, p.ID, c.ID, doc, "before-restore"); err != nil {
return PageContent{}, err
}
return s.GetPageContent(ctx, c, p.ID)
}
func (s *Service) versionContent(ctx context.Context, page, versionID string) (json.RawMessage, error) {
if !validID(versionID) {
return nil, apperr.NotFound("version not found")
}
doc, err := s.docs.VersionContent(ctx, page, versionID)
if errors.Is(err, collab.ErrNotFound) {
return nil, apperr.NotFound("version not found")
}
return doc, err
}
// editablePage loads a page the caller may edit and that is not in the
// trash or archived.
func (s *Service) editablePage(ctx context.Context, c access.Caller, id string) (pageRecord, error) {
p, a, err := s.pageFor(ctx, c, id)
if err != nil {
return p, err
}
if err := access.EditPages(a.role); err != nil {
return p, err
}
if p.DeletedAt != "" {
return p, apperr.Invalid("restore the page from the trash first")
}
if s.isArchived(ctx, p) {
return p, apperr.Invalid("unarchive the page first")
}
return p, nil
}
func isDoc(raw json.RawMessage) bool {
var doc struct {
Type string `json:"type"`
}
return json.Unmarshal(raw, &doc) == nil && doc.Type == "doc"
}