Files
Docwell/Server/internal/directory/journals.go
T
tajniak81andClaude Opus 5.5 ec11abd8b8 Add page properties, tags, All docs, collections and journals
Phase B (items 1-6) of docs/AFFINE.md.

Properties: workspaces define custom page properties (text, number,
checkbox, date, select, multi-select, person, link) in the new
workspace_properties collection; pages keep their values in
pages.properties, checked against each property's type. The Info panel
under the title shows tags, custom and built-in properties, following
each property's visibility; the Info side panel shows them all, with the
page's collections and the journal. Settings gains Page properties and
Tags sections.

Tags: coloured, per workspace (tags collection, pages.tags); create from
the picker, rename, recolour, merge and delete. Search filters by tag.

All docs: GET /workspaces/{id}/docs lists the pages the caller can open
with their properties, narrowed by space, tag, collection or filter
rules evaluated on the server. The view shows a list, grid or cards,
groups and orders by any property, picks the properties shown, and acts
on several pages at once (tag, move, trash, collect).

Collections (page_collections): saved rules plus pages added or taken
out by hand, private to their creator until shared with the workspace,
listed in the sidebar.

Journals: one page per person and day in a private journal space
(spaces.journal_owner) that workspace admins do not see into, made on
first use. A week strip marks days with pages, new days can start from
a template, and dates in read mode open that day's journal.

The migration and schema snapshot, OpenAPI spec and generated Go, TS and
Dart types are updated (the Dart generator learns untyped values), the
mobile editor bundle is rebuilt, the new strings are translated into all
five languages, and the user guide covers the new features.

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

245 lines
8.1 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package directory
import (
"context"
"errors"
"fmt"
"strings"
"time"
"docwell/server/internal/access"
"docwell/server/internal/apperr"
"docwell/server/internal/store/pocketbase"
)
// ---- Journals --------------------------------------------------------------
//
// Everyone who works in a workspace may keep a journal there: one page per
// day, in their own journal space (spaces.journal_owner). The space is made
// the first time it is needed, private, with its owner as admin; workspace
// admins do not see into it (access.JournalSpaceRole). A day's page is made
// when it is first opened, filled from the journal template if one is set,
// and kept newest first at the top of the space.
// JournalDay is a day that has a journal page.
type JournalDay struct {
Date string `json:"date"`
Page string `json:"page"`
Title string `json:"title"`
}
// JournalInfo is the caller's journal in a workspace. Space and Template are
// empty until they exist; Days lists the requested range.
type JournalInfo struct {
Space string `json:"space"`
Template string `json:"template"`
Days []JournalDay `json:"days"`
}
// journalSpace finds c's journal space in ws; with create it makes one.
func (s *Service) journalSpace(ctx context.Context, c access.Caller, ws workspaceRecord, create bool) (spaceRecord, error) {
res, err := pocketbase.List[spaceRecord](ctx, s.pb, "spaces", pocketbase.ListOptions{
PerPage: 1, Filter: fmt.Sprintf("workspace = '%s' && journal_owner = '%s'", ws.ID, c.ID), Sort: "created",
})
if err != nil {
return spaceRecord{}, err
}
if len(res.Items) > 0 || !create {
if len(res.Items) == 0 {
return spaceRecord{}, nil
}
return res.Items[0], nil
}
base := slugify("journal-"+c.ID, "journal")
for i := 1; i <= 50; i++ {
slug := base
if i > 1 {
slug = fmt.Sprintf("%s-%d", base, i)
}
rec, err := pocketbase.Create[spaceRecord](ctx, s.pb, "spaces", map[string]any{
"workspace": ws.ID, "name": "Journal", "slug": slug, "icon": "📔",
"description": "", "visibility": access.SpacePrivate, "default_role": access.SpaceEditor,
"journal_owner": c.ID,
})
var pbErr *pocketbase.Error
if errors.As(err, &pbErr) && pbErr.Data["slug"].Code == "validation_not_unique" {
continue
}
if err != nil {
return spaceRecord{}, mapPB(err, "space not found")
}
if _, err := pocketbase.Create[spaceMemberRecord](ctx, s.pb, "space_members", map[string]any{
"space": rec.ID, "principal_type": "user", "principal_id": c.ID, "role": access.SpaceAdmin,
}); err != nil {
return spaceRecord{}, mapPB(err, "space not found")
}
return rec, nil
}
return spaceRecord{}, apperr.Conflict("could not find a free address for the journal space")
}
func (s *Service) journalWorkspace(ctx context.Context, c access.Caller, wsID string) (workspaceRecord, error) {
ws, role, err := s.workspaceFor(ctx, c, wsID)
if err != nil {
return ws, err
}
return ws, access.KeepJournal(role)
}
func parseDay(v, field string) (string, error) {
if _, err := time.Parse(time.DateOnly, v); err != nil {
return "", apperr.InvalidFields(map[string]string{field: "Dates look like 2026-11-14."})
}
return v, nil
}
// GetJournal lists the caller's journal days from from to to (inclusive,
// YYYY-MM-DD; at most a year), newest first.
func (s *Service) GetJournal(ctx context.Context, c access.Caller, wsID, from, to string) (JournalInfo, error) {
ws, err := s.journalWorkspace(ctx, c, wsID)
if err != nil {
return JournalInfo{}, err
}
out := JournalInfo{Days: []JournalDay{}}
if from, err = parseDay(from, "from"); err != nil {
return out, err
}
if to, err = parseDay(to, "to"); err != nil {
return out, err
}
f, _ := time.Parse(time.DateOnly, from)
t, _ := time.Parse(time.DateOnly, to)
if t.Before(f) || t.Sub(f) > 366*24*time.Hour {
return out, apperr.InvalidFields(map[string]string{"to": "Pick a range of at most a year."})
}
sp, err := s.journalSpace(ctx, c, ws, false)
if err != nil || sp.ID == "" {
return out, err
}
out.Space, out.Template = sp.ID, sp.JournalTemplate
rows, err := listAll[pageRecord](ctx, s, "pages", pocketbase.ListOptions{
Filter: fmt.Sprintf("space = '%s' && deleted_at = '' && journal_date >= '%s' && journal_date <= '%s'", sp.ID, from, to),
Fields: "id,title,journal_date", Sort: "-journal_date,created",
})
if err != nil {
return out, err
}
seen := map[string]bool{}
for _, p := range rows {
if !seen[p.JournalDate] {
seen[p.JournalDate] = true
out.Days = append(out.Days, JournalDay{Date: p.JournalDate, Page: p.ID, Title: p.Title})
}
}
return out, nil
}
// SetJournalTemplate picks the page new journal pages are filled from ("" = none).
func (s *Service) SetJournalTemplate(ctx context.Context, c access.Caller, wsID, template string) (JournalInfo, error) {
ws, err := s.journalWorkspace(ctx, c, wsID)
if err != nil {
return JournalInfo{}, err
}
if template != "" {
t, _, err := s.pageFor(ctx, c, template)
if err != nil || t.Workspace != ws.ID || t.DeletedAt != "" {
return JournalInfo{}, apperr.InvalidFields(map[string]string{"template": "Template not found."})
}
}
sp, err := s.journalSpace(ctx, c, ws, true)
if err != nil {
return JournalInfo{}, err
}
if _, err := pocketbase.Update[spaceRecord](ctx, s.pb, "spaces", sp.ID, map[string]any{"journal_template": template}); err != nil {
return JournalInfo{}, mapPB(err, "space not found")
}
return JournalInfo{Space: sp.ID, Template: template, Days: []JournalDay{}}, nil
}
// OpenJournal returns the caller's journal page for a day, making it (and
// the journal space) when there is none yet. title names a new page; it
// defaults to the date.
func (s *Service) OpenJournal(ctx context.Context, c access.Caller, wsID, date, title string) (Page, error) {
ws, err := s.journalWorkspace(ctx, c, wsID)
if err != nil {
return Page{}, err
}
if date, err = parseDay(date, "date"); err != nil {
return Page{}, err
}
sp, err := s.journalSpace(ctx, c, ws, true)
if err != nil {
return Page{}, err
}
existing, err := pocketbase.List[pageRecord](ctx, s.pb, "pages", pocketbase.ListOptions{
PerPage: 1, Filter: fmt.Sprintf("space = '%s' && journal_date = '%s' && deleted_at = ''", sp.ID, date),
Fields: "id", Sort: "created",
})
if err != nil {
return Page{}, err
}
if len(existing.Items) > 0 {
return s.GetPage(ctx, c, existing.Items[0].ID)
}
if title, err = cleanTitle(title); err != nil {
return Page{}, err
}
if title == "" {
title = date
}
t, err := s.loadTree(ctx, sp.ID)
if err != nil {
return Page{}, err
}
siblings := t.children[""]
pos, renumber := placement(siblings, journalIndex(siblings, date))
if err := s.renumber(ctx, renumber); err != nil {
return Page{}, err
}
rec, err := pocketbase.Create[pageRecord](ctx, s.pb, "pages", map[string]any{
"workspace": ws.ID, "space": sp.ID, "title": title, "position": pos, "journal_date": date,
"creator": c.ID, "last_editor": c.ID, "mentions": []string{}, "links": []string{},
})
if err != nil {
return Page{}, mapPB(err, "page not found")
}
if sp.JournalTemplate != "" {
s.fillFromTemplate(ctx, c, rec, sp.JournalTemplate)
}
s.autoWatch(ctx, rec.ID, c.ID)
s.emitPage(ctx, "page.created", rec, c.ID, nil)
return s.GetPage(ctx, c, rec.ID)
}
// journalIndex is where a day goes among the space's top-level pages: after
// every later day, so the newest day comes first.
func journalIndex(siblings []pageRecord, date string) int {
i := 0
for _, p := range siblings {
if p.JournalDate > date {
i++
}
}
return i
}
// fillFromTemplate copies a template's content into a new page (best effort:
// a template that is gone or out of reach leaves the page empty).
func (s *Service) fillFromTemplate(ctx context.Context, c access.Caller, p pageRecord, templateID string) {
t, _, err := s.pageFor(ctx, c, templateID)
if err != nil || t.Workspace != p.Workspace || t.DeletedAt != "" {
return
}
doc, err := s.docs.Content(ctx, t.ID)
if err != nil || !isDoc(doc) {
return
}
_ = s.docs.Replace(ctx, p.ID, c.ID, doc, "template")
if t.Icon != "" && strings.TrimSpace(p.Icon) == "" {
_, _ = pocketbase.Update[pageRecord](ctx, s.pb, "pages", p.ID, map[string]any{"icon": t.Icon})
}
}