- Start-up keeps the saved session while the server is unreachable and retries the refresh; only a 4xx from the server signs the user out. - Rooms of deleted pages are dropped (on purge, or when a write fails and the page is gone) instead of retrying their save every second. - Trashing or restoring a page reconnects its editors (close code 4409), and open connections recheck access every 30 s; the editor reloads the page and reopens the document with its new rights. - Restoring a version or replacing content first keeps unversioned changes as a "before-restore"/"before-replace" version; whether the saved document has a version is now worked out when a room loads. - Destroying the collab provider no longer recurses through awareness listeners, which overflowed the stack on a 4409 close. - Caret name labels are hidden from assistive tech. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
157 lines
5.1 KiB
Go
157 lines
5.1 KiB
Go
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"
|
|
}
|