Files
Docwell/Server/internal/directory/collections.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

281 lines
8.9 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package directory
import (
"context"
"fmt"
"slices"
"sort"
"strings"
"time"
"docwell/server/internal/access"
"docwell/server/internal/apperr"
"docwell/server/internal/store/pocketbase"
)
// ---- Collections -----------------------------------------------------------
//
// A collection is a saved view of pages: the pages its rules match, plus
// pages added by hand (include), minus pages taken out by hand (exclude). It
// is its creator's own, or shared with everyone in the workspace. Listing a
// collection's pages still shows only pages the caller can open.
const maxCollectionPages = 2000
// Collection is a saved view of pages in a workspace.
type Collection struct {
ID string `json:"id"`
Workspace string `json:"workspace"`
Name string `json:"name"`
Icon string `json:"icon"`
Filter DocFilter `json:"filter"`
Include []string `json:"include"`
Exclude []string `json:"exclude"`
Shared bool `json:"shared"`
Creator string `json:"creator"`
// CanEdit is whether the caller may change or delete it.
CanEdit bool `json:"canEdit"`
Updated string `json:"updated"`
}
type collectionRecord struct {
ID string `json:"id"`
Workspace string `json:"workspace"`
Creator string `json:"creator"`
Name string `json:"name"`
Icon string `json:"icon"`
Filter DocFilter `json:"filter"`
Include []string `json:"include"`
Exclude []string `json:"exclude"`
Shared bool `json:"shared"`
Updated string `json:"updated"`
}
func (r collectionRecord) view(c access.Caller, wsRole string) Collection {
f := r.Filter
if f.Rules == nil {
f.Rules = []DocRule{}
}
if f.Match == "" {
f.Match = "all"
}
return Collection{
ID: r.ID, Workspace: r.Workspace, Name: r.Name, Icon: r.Icon, Filter: f,
Include: nonNil(r.Include), Exclude: nonNil(r.Exclude), Shared: r.Shared, Creator: r.Creator,
CanEdit: access.EditCollection(c.ID, wsRole, r.Creator, r.Shared) == nil, Updated: r.Updated,
}
}
// contains reports whether a page belongs in the collection. A collection
// without rules holds only the pages added to it.
func (r collectionRecord) contains(d DocItem, defs map[string]propertyRecord, now time.Time) bool {
if slices.Contains(r.Exclude, d.ID) {
return false
}
if slices.Contains(r.Include, d.ID) {
return true
}
return len(r.Filter.Rules) > 0 && r.Filter.matches(d, defs, now)
}
// visibleTo reports whether c may see the collection at all.
func (r collectionRecord) visibleTo(c access.Caller) bool { return r.Shared || r.Creator == c.ID }
// ListCollections lists the caller's own collections and the shared ones, by name.
func (s *Service) ListCollections(ctx context.Context, c access.Caller, wsID string) ([]Collection, error) {
ws, role, err := s.workspaceFor(ctx, c, wsID)
if err != nil {
return nil, err
}
rows, err := listAll[collectionRecord](ctx, s, "page_collections", pocketbase.ListOptions{
Filter: fmt.Sprintf("workspace = '%s' && (shared = true || creator = '%s')", ws.ID, c.ID),
})
if err != nil {
return nil, err
}
out := make([]Collection, 0, len(rows))
for _, r := range rows {
out = append(out, r.view(c, role))
}
sort.SliceStable(out, func(i, j int) bool { return strings.ToLower(out[i].Name) < strings.ToLower(out[j].Name) })
return out, nil
}
// CollectionInput creates a collection, or changes the fields that are set.
type CollectionInput struct {
Name *string `json:"name"`
Icon *string `json:"icon"`
Filter *DocFilter `json:"filter"`
Include *[]string `json:"include"`
Exclude *[]string `json:"exclude"`
Shared *bool `json:"shared"`
}
func collectionPatch(in CollectionInput) (map[string]any, error) {
patch := map[string]any{}
if in.Name != nil {
name, err := cleanName(*in.Name, "Collection")
if err != nil {
return nil, err
}
patch["name"] = name
}
if in.Icon != nil {
icon := strings.TrimSpace(*in.Icon)
if len(icon) > 32 {
return nil, apperr.InvalidFields(map[string]string{"icon": "Icon is too long."})
}
patch["icon"] = icon
}
if in.Filter != nil {
f := *in.Filter
if err := f.check(); err != nil {
return nil, err
}
if f.Match == "" {
f.Match = "all"
}
if f.Rules == nil {
f.Rules = []DocRule{}
}
patch["filter"] = f
}
for key, list := range map[string]*[]string{"include": in.Include, "exclude": in.Exclude} {
if list == nil {
continue
}
if len(*list) > maxCollectionPages {
return nil, apperr.InvalidFields(map[string]string{key: "At most 2000 pages."})
}
ids := []string{}
for _, id := range *list {
if !validID(id) {
return nil, apperr.InvalidFields(map[string]string{key: "Invalid page id."})
}
if !slices.Contains(ids, id) {
ids = append(ids, id)
}
}
patch[key] = ids
}
if in.Shared != nil {
patch["shared"] = *in.Shared
}
return patch, nil
}
// CreateCollection saves a new collection (members).
func (s *Service) CreateCollection(ctx context.Context, c access.Caller, wsID string, in CollectionInput) (Collection, error) {
ws, role, err := s.workspaceFor(ctx, c, wsID)
if err != nil {
return Collection{}, err
}
if err := access.OrganizeWorkspace(role); err != nil {
return Collection{}, err
}
if in.Name == nil {
return Collection{}, apperr.InvalidFields(map[string]string{"name": "Collection name is required."})
}
body, err := collectionPatch(in)
if err != nil {
return Collection{}, err
}
body["workspace"], body["creator"] = ws.ID, c.ID
for key, empty := range map[string]any{"filter": DocFilter{Match: "all", Rules: []DocRule{}}, "include": []string{}, "exclude": []string{}} {
if _, ok := body[key]; !ok {
body[key] = empty
}
}
rec, err := pocketbase.Create[collectionRecord](ctx, s.pb, "page_collections", body)
if err != nil {
return Collection{}, mapPB(err, "collection not found")
}
return rec.view(c, role), nil
}
// collectionFor loads a collection c may see.
func (s *Service) collectionFor(ctx context.Context, c access.Caller, id string) (collectionRecord, error) {
rec, _, err := s.collectionAccess(ctx, c, id)
return rec, err
}
func (s *Service) collectionAccess(ctx context.Context, c access.Caller, id string) (collectionRecord, string, error) {
if !validID(id) {
return collectionRecord{}, "", apperr.NotFound("collection not found")
}
rec, err := pocketbase.Get[collectionRecord](ctx, s.pb, "page_collections", id)
if err != nil {
return rec, "", mapPB(err, "collection not found")
}
_, role, err := s.workspaceFor(ctx, c, rec.Workspace)
if err != nil || !rec.visibleTo(c) {
return collectionRecord{}, "", apperr.NotFound("collection not found")
}
return rec, role, nil
}
// UpdateCollection changes a collection (its creator; workspace admins for shared ones).
func (s *Service) UpdateCollection(ctx context.Context, c access.Caller, id string, in CollectionInput) (Collection, error) {
rec, role, err := s.collectionAccess(ctx, c, id)
if err != nil {
return Collection{}, err
}
if err := access.EditCollection(c.ID, role, rec.Creator, rec.Shared); err != nil {
return Collection{}, err
}
// Only the creator decides who sees it.
if in.Shared != nil && *in.Shared != rec.Shared && c.ID != rec.Creator {
return Collection{}, apperr.Forbidden("only the collection's creator can change who sees it")
}
patch, err := collectionPatch(in)
if err != nil {
return Collection{}, err
}
if len(patch) > 0 {
if rec, err = pocketbase.Update[collectionRecord](ctx, s.pb, "page_collections", rec.ID, patch); err != nil {
return Collection{}, mapPB(err, "collection not found")
}
}
return rec.view(c, role), nil
}
// DeleteCollection deletes a collection; its pages stay where they are.
func (s *Service) DeleteCollection(ctx context.Context, c access.Caller, id string) error {
rec, role, err := s.collectionAccess(ctx, c, id)
if err != nil {
return err
}
if err := access.EditCollection(c.ID, role, rec.Creator, rec.Shared); err != nil {
return err
}
return mapPB(s.pb.Delete(ctx, "page_collections", rec.ID), "collection not found")
}
// SetCollectionPage adds a page to a collection by hand, or takes it out.
func (s *Service) SetCollectionPage(ctx context.Context, c access.Caller, id, pageID string, in bool) (Collection, error) {
rec, role, err := s.collectionAccess(ctx, c, id)
if err != nil {
return Collection{}, err
}
if err := access.EditCollection(c.ID, role, rec.Creator, rec.Shared); err != nil {
return Collection{}, err
}
p, _, err := s.pageFor(ctx, c, pageID)
if err != nil {
return Collection{}, err
}
if p.Workspace != rec.Workspace {
return Collection{}, apperr.Invalid("the page is in another workspace")
}
include, exclude := slices.DeleteFunc(rec.Include, func(x string) bool { return x == p.ID }),
slices.DeleteFunc(rec.Exclude, func(x string) bool { return x == p.ID })
if in {
include = append(include, p.ID)
} else {
exclude = append(exclude, p.ID)
}
return s.UpdateCollection(ctx, c, rec.ID, CollectionInput{Include: &include, Exclude: &exclude})
}