Repository basics: - CONTRIBUTING.md and CODE_OF_CONDUCT.md (Contributor Covenant 2.1) - SPDX AGPL-3.0-or-later headers on all hand-written sources, checked in CI - golangci-lint (shared .golangci.yml), ESLint and Prettier, all run in CI API codegen from api/openapi.yaml (npm run generate, drift fails CI): - Go types via oapi-codegen in Server/internal/api - a contract test that the router and the spec list the same routes - Dart models via Phone-App/tool/generate_api.dart; the app uses them Spike: shared Tiptap editor in a Flutter WebView (ADR-4) - @docwell/editor-bridge builds the editor into one HTML page with a versioned JSON protocol; Phone-App hosts it with webview_flutter and a native toolbar. Integration test passes on an Android API 35 emulator. - flutter_inappwebview dropped: its Android plugin fails on AGP 9. Spike: Wails v3 desktop (ADR-11) - system tray, docwell:// deep links (page and space only) with single-instance hand-off, and the built-in updater with a required ed25519 signing key. Verified on Windows. - regenerated build assets register the scheme and drop template placeholders. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
159 lines
5.1 KiB
Go
159 lines
5.1 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
|
|
// 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).
|
|
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 && 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.
|
|
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")
|
|
}
|
|
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"
|
|
}
|