Files
Docwell/Server/internal/directory/content.go
T
tajniak81andClaude Opus 5.5 40d515d15b Add toggles, embeds, link cards, block links, archive, version diff, space export and desktop 1.0
Editor:
- Toggles (Tiptap Details). Open state is per reader, not saved.
- Embeds for YouTube, Vimeo, Loom, Figma, CodePen, Spotify, Google Docs
  and Maps, in sandboxed frames. Other addresses become link cards.
- Link cards with title, description and picture read by docwell-server
  (GET /link-preview). The fetcher refuses private, loopback, link-local
  and CGNAT addresses after DNS resolution, redirects included.
- Block links: a margin button gives a block a short id and copies
  /p/<id>#b-<block>. Opening one jumps to the block and flashes it.
- Markdown, HTML and PDF exports handle the new blocks; HTML import reads
  <details> as a toggle.

Pages and spaces:
- History gets a Changes view: block and word diff against the previous
  version or the page as it is now.
- Archive: archived pages leave the tree and become read-only but stay
  readable; a space's Archive lists them with Unarchive. New migration
  (pages.archived_at) and page.archived / page.unarchived webhook events.
- Full space export (GET /spaces/{id}/export): a zip of Markdown or HTML
  files with subpages in folders, an index, and attachments under _files/.
  Links between exported pages and to attachments become relative paths.

Desktop 1.0:
- Several servers, each with its own sign-in; old settings files carry over.
- Session tokens in the system keychain (go-keyring), moved out of
  localStorage on first start.
- Native menu bar (File, Edit, View, Window, Help) with a Server submenu.
- docwell:// links may point at a block.
- Release workflow builds Windows NSIS, macOS DMG and Linux deb, rpm and
  AppImage installers and attaches them to the release;
  build/set-version.mjs stamps the version into their metadata.

Not yet run: the release workflow, the desktop menus by eye, and macOS or
Linux builds.

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

163 lines
5.3 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 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"
}