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>
494 lines
13 KiB
Go
494 lines
13 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package directory
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"slices"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"docwell/server/internal/access"
|
|
"docwell/server/internal/apperr"
|
|
"docwell/server/internal/store/pocketbase"
|
|
)
|
|
|
|
// ---- All docs --------------------------------------------------------------
|
|
//
|
|
// The All docs view lists a workspace's live pages the caller can open, with
|
|
// their properties, so the apps can group, order and lay them out. It can be
|
|
// narrowed to one space, one tag, a collection, or filter rules; the rules
|
|
// are the same ones collections save.
|
|
|
|
// DocItem is one page in the All docs list.
|
|
type DocItem struct {
|
|
ID string `json:"id"`
|
|
Title string `json:"title"`
|
|
Icon string `json:"icon"`
|
|
Space string `json:"space"`
|
|
SpaceName string `json:"spaceName"`
|
|
Parent string `json:"parent"`
|
|
Created string `json:"created"`
|
|
Updated string `json:"updated"`
|
|
CreatedBy string `json:"createdBy"`
|
|
LastEditedBy string `json:"lastEditedBy"`
|
|
IsTemplate bool `json:"isTemplate"`
|
|
JournalDate string `json:"journalDate"`
|
|
FullWidth bool `json:"fullWidth"`
|
|
Tags []string `json:"tags"`
|
|
Properties map[string]json.RawMessage `json:"properties"`
|
|
// Role is the caller's role on the page, for bulk actions.
|
|
Role string `json:"role"`
|
|
}
|
|
|
|
// DocList is the All docs result; People names everyone it mentions (as
|
|
// creator, last editor or in a member property).
|
|
type DocList struct {
|
|
Items []DocItem `json:"items"`
|
|
People map[string]string `json:"people"`
|
|
}
|
|
|
|
// DocFilter is a set of rules; Match is "all" (the default) or "any".
|
|
type DocFilter struct {
|
|
Match string `json:"match"`
|
|
Rules []DocRule `json:"rules"`
|
|
}
|
|
|
|
// DocRule tests one field of a page. Field is a built-in (title, space,
|
|
// tags, created, updated, createdBy, lastEditedBy, template, journal) or
|
|
// "prop:<property id>". Op is one of is, isNot, contains, notContains,
|
|
// before, after, withinDays, gt, lt, empty, notEmpty; Value is a string, a
|
|
// list of strings, a number or a boolean as the op needs.
|
|
type DocRule struct {
|
|
Field string `json:"field"`
|
|
Op string `json:"op"`
|
|
Value json.RawMessage `json:"value"`
|
|
}
|
|
|
|
const maxRules = 50
|
|
|
|
var ruleOps = []string{"is", "isNot", "contains", "notContains", "before", "after", "withinDays", "gt", "lt", "empty", "notEmpty"}
|
|
|
|
// check rejects rules that could never be understood.
|
|
func (f DocFilter) check() error {
|
|
if f.Match != "" && f.Match != "all" && f.Match != "any" {
|
|
return apperr.InvalidFields(map[string]string{"filter": "match must be all or any."})
|
|
}
|
|
if len(f.Rules) > maxRules {
|
|
return apperr.InvalidFields(map[string]string{"filter": "At most 50 rules."})
|
|
}
|
|
for _, r := range f.Rules {
|
|
if !slices.Contains(ruleOps, r.Op) {
|
|
return apperr.InvalidFields(map[string]string{"filter": "Unknown rule op " + r.Op + "."})
|
|
}
|
|
switch r.Field {
|
|
case "title", "space", "tags", "created", "updated", "createdBy", "lastEditedBy", "template", "journal":
|
|
default:
|
|
if !strings.HasPrefix(r.Field, "prop:") {
|
|
return apperr.InvalidFields(map[string]string{"filter": "Unknown rule field " + r.Field + "."})
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// fieldValue is a page field reduced to what rules compare.
|
|
type fieldValue struct {
|
|
kind string // text, list, date, number, bool
|
|
text string // text, or a date as YYYY-MM-DD
|
|
list []string
|
|
num *float64
|
|
b bool
|
|
}
|
|
|
|
func (v fieldValue) empty() bool {
|
|
switch v.kind {
|
|
case "list":
|
|
return len(v.list) == 0
|
|
case "number":
|
|
return v.num == nil
|
|
case "bool":
|
|
return !v.b
|
|
}
|
|
return v.text == ""
|
|
}
|
|
|
|
// value reads one field of d; defs are the workspace's properties by id.
|
|
func (d DocItem) value(field string, defs map[string]propertyRecord) (fieldValue, bool) {
|
|
day := func(ts string) string {
|
|
if len(ts) >= 10 {
|
|
return ts[:10]
|
|
}
|
|
return ""
|
|
}
|
|
one := func(s string) []string {
|
|
if s == "" {
|
|
return nil
|
|
}
|
|
return []string{s}
|
|
}
|
|
switch field {
|
|
case "title":
|
|
return fieldValue{kind: "text", text: d.Title}, true
|
|
case "space":
|
|
return fieldValue{kind: "list", list: one(d.Space)}, true
|
|
case "tags":
|
|
return fieldValue{kind: "list", list: d.Tags}, true
|
|
case "created":
|
|
return fieldValue{kind: "date", text: day(d.Created)}, true
|
|
case "updated":
|
|
return fieldValue{kind: "date", text: day(d.Updated)}, true
|
|
case "createdBy":
|
|
return fieldValue{kind: "list", list: one(d.CreatedBy)}, true
|
|
case "lastEditedBy":
|
|
return fieldValue{kind: "list", list: one(d.LastEditedBy)}, true
|
|
case "template":
|
|
return fieldValue{kind: "bool", b: d.IsTemplate}, true
|
|
case "journal":
|
|
return fieldValue{kind: "date", text: d.JournalDate}, true
|
|
}
|
|
def, ok := defs[strings.TrimPrefix(field, "prop:")]
|
|
if !ok {
|
|
return fieldValue{}, false
|
|
}
|
|
raw := d.Properties[def.ID]
|
|
switch def.Type {
|
|
case PropNumber:
|
|
var n float64
|
|
if json.Unmarshal(raw, &n) == nil && len(raw) > 0 {
|
|
return fieldValue{kind: "number", num: &n}, true
|
|
}
|
|
return fieldValue{kind: "number"}, true
|
|
case PropCheckbox:
|
|
var b bool
|
|
_ = json.Unmarshal(raw, &b)
|
|
return fieldValue{kind: "bool", b: b}, true
|
|
case PropDate:
|
|
var s string
|
|
_ = json.Unmarshal(raw, &s)
|
|
return fieldValue{kind: "date", text: s}, true
|
|
case PropSelect:
|
|
var s string
|
|
_ = json.Unmarshal(raw, &s)
|
|
return fieldValue{kind: "list", list: one(s)}, true
|
|
case PropMultiSelect, PropMember:
|
|
var l []string
|
|
_ = json.Unmarshal(raw, &l)
|
|
return fieldValue{kind: "list", list: l}, true
|
|
}
|
|
var s string
|
|
_ = json.Unmarshal(raw, &s)
|
|
return fieldValue{kind: "text", text: s}, true
|
|
}
|
|
|
|
// ruleStrings reads a rule value as a list of strings (a single string counts).
|
|
func ruleStrings(raw json.RawMessage) []string {
|
|
var list []string
|
|
if json.Unmarshal(raw, &list) == nil {
|
|
return list
|
|
}
|
|
var s string
|
|
if json.Unmarshal(raw, &s) == nil && s != "" {
|
|
return []string{s}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ruleString(raw json.RawMessage) string {
|
|
if l := ruleStrings(raw); len(l) > 0 {
|
|
return l[0]
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// test reports whether d passes the rule. A rule on a property that no
|
|
// longer exists passes, so deleting a property does not empty collections,
|
|
// and so does a rule with nothing picked yet.
|
|
func (r DocRule) test(d DocItem, defs map[string]propertyRecord, now time.Time) bool {
|
|
v, ok := d.value(r.Field, defs)
|
|
if !ok {
|
|
return true
|
|
}
|
|
switch r.Op {
|
|
case "empty":
|
|
return v.empty()
|
|
case "notEmpty":
|
|
return !v.empty()
|
|
}
|
|
switch v.kind {
|
|
case "text":
|
|
want := strings.ToLower(strings.TrimSpace(ruleString(r.Value)))
|
|
have := strings.ToLower(v.text)
|
|
switch r.Op {
|
|
case "is":
|
|
return have == want
|
|
case "isNot":
|
|
return have != want
|
|
case "contains":
|
|
return strings.Contains(have, want)
|
|
case "notContains":
|
|
return !strings.Contains(have, want)
|
|
}
|
|
case "list":
|
|
want := ruleStrings(r.Value)
|
|
if len(want) == 0 {
|
|
// A rule still being written (nothing picked yet) narrows nothing.
|
|
return true
|
|
}
|
|
hit := slices.ContainsFunc(want, func(w string) bool { return slices.Contains(v.list, w) })
|
|
switch r.Op {
|
|
case "is", "contains":
|
|
return hit
|
|
case "isNot", "notContains":
|
|
return !hit
|
|
}
|
|
case "bool":
|
|
var want bool
|
|
if json.Unmarshal(r.Value, &want) != nil {
|
|
want = true
|
|
}
|
|
switch r.Op {
|
|
case "is":
|
|
return v.b == want
|
|
case "isNot":
|
|
return v.b != want
|
|
}
|
|
case "number":
|
|
var want float64
|
|
if v.num == nil || json.Unmarshal(r.Value, &want) != nil {
|
|
return false
|
|
}
|
|
switch r.Op {
|
|
case "is":
|
|
return *v.num == want
|
|
case "isNot":
|
|
return *v.num != want
|
|
case "gt":
|
|
return *v.num > want
|
|
case "lt":
|
|
return *v.num < want
|
|
}
|
|
case "date":
|
|
if v.text == "" {
|
|
return false
|
|
}
|
|
if r.Op == "withinDays" {
|
|
var days float64
|
|
if json.Unmarshal(r.Value, &days) != nil || days < 0 {
|
|
return false
|
|
}
|
|
from := now.AddDate(0, 0, -int(days)).Format(time.DateOnly)
|
|
return v.text >= from && v.text <= now.Format(time.DateOnly)
|
|
}
|
|
want := ruleString(r.Value)
|
|
if want == "" {
|
|
return true
|
|
}
|
|
switch r.Op {
|
|
case "is":
|
|
return v.text == want
|
|
case "isNot":
|
|
return v.text != want
|
|
case "before":
|
|
return v.text < want
|
|
case "after":
|
|
return v.text > want
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// matches reports whether d passes the filter; no rules match everything.
|
|
func (f DocFilter) matches(d DocItem, defs map[string]propertyRecord, now time.Time) bool {
|
|
if len(f.Rules) == 0 {
|
|
return true
|
|
}
|
|
anyOf := f.Match == "any"
|
|
for _, r := range f.Rules {
|
|
ok := r.test(d, defs, now)
|
|
if anyOf && ok {
|
|
return true
|
|
}
|
|
if !anyOf && !ok {
|
|
return false
|
|
}
|
|
}
|
|
return !anyOf
|
|
}
|
|
|
|
// DocQuery narrows the All docs list; every part is optional.
|
|
type DocQuery struct {
|
|
Space string
|
|
Tag string
|
|
Collection string
|
|
Filter *DocFilter
|
|
}
|
|
|
|
// workspaceDocs loads every live, unarchived page of the workspace that c
|
|
// can open, with the spaces' names. Pages in journal spaces of other people
|
|
// are left out like the spaces themselves.
|
|
func (s *Service) workspaceDocs(ctx context.Context, c access.Caller, wsID string) ([]DocItem, error) {
|
|
spaces, err := s.ListSpaces(ctx, c, wsID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
rules := map[string]pageRules{}
|
|
names := map[string]string{}
|
|
for _, sp := range spaces {
|
|
_, r, err := s.spaceRules(ctx, c, sp.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
rules[sp.ID], names[sp.ID] = r, sp.Name
|
|
}
|
|
all, err := listAll[pageRecord](ctx, s, "pages", pocketbase.ListOptions{
|
|
Filter: fmt.Sprintf("workspace = '%s'", wsID), Fields: pageDetailFields,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
trees := map[string]tree{}
|
|
for _, p := range all {
|
|
t, ok := trees[p.Space]
|
|
if !ok {
|
|
t = tree{byID: map[string]pageRecord{}}
|
|
trees[p.Space] = t
|
|
}
|
|
t.byID[p.ID] = p
|
|
}
|
|
out := []DocItem{}
|
|
for _, p := range all {
|
|
r, ok := rules[p.Space]
|
|
if !ok {
|
|
continue
|
|
}
|
|
t := trees[p.Space]
|
|
if !t.live(p.ID) || t.archived(p.ID) {
|
|
continue
|
|
}
|
|
role := r.role(p.ID)
|
|
if role == "" {
|
|
continue
|
|
}
|
|
props := p.Properties
|
|
if props == nil {
|
|
props = map[string]json.RawMessage{}
|
|
}
|
|
out = append(out, DocItem{
|
|
ID: p.ID, Title: p.Title, Icon: p.Icon, Space: p.Space, SpaceName: names[p.Space], Parent: p.Parent,
|
|
Created: p.Created, Updated: p.Updated, CreatedBy: p.Creator, LastEditedBy: p.LastEditor,
|
|
IsTemplate: p.IsTemplate, JournalDate: p.JournalDate, FullWidth: p.FullWidth,
|
|
Tags: nonNil(p.Tags), Properties: props, Role: role,
|
|
})
|
|
}
|
|
sort.Slice(out, func(i, j int) bool {
|
|
if out[i].Updated != out[j].Updated {
|
|
return out[i].Updated > out[j].Updated
|
|
}
|
|
return out[i].ID < out[j].ID
|
|
})
|
|
return out, nil
|
|
}
|
|
|
|
// ListDocs lists the pages of a workspace c can open, newest change first.
|
|
func (s *Service) ListDocs(ctx context.Context, c access.Caller, wsID string, q DocQuery) (DocList, error) {
|
|
ws, _, err := s.workspaceFor(ctx, c, wsID)
|
|
if err != nil {
|
|
return DocList{}, err
|
|
}
|
|
if q.Filter != nil {
|
|
if err := q.Filter.check(); err != nil {
|
|
return DocList{}, err
|
|
}
|
|
}
|
|
var col *collectionRecord
|
|
if q.Collection != "" {
|
|
rec, err := s.collectionFor(ctx, c, q.Collection)
|
|
if err != nil {
|
|
return DocList{}, err
|
|
}
|
|
if rec.Workspace != ws.ID {
|
|
return DocList{}, apperr.NotFound("collection not found")
|
|
}
|
|
col = &rec
|
|
}
|
|
docs, err := s.workspaceDocs(ctx, c, ws.ID)
|
|
if err != nil {
|
|
return DocList{}, err
|
|
}
|
|
defsList, err := s.workspaceProperties(ctx, ws.ID)
|
|
if err != nil {
|
|
return DocList{}, err
|
|
}
|
|
defs := make(map[string]propertyRecord, len(defsList))
|
|
for _, d := range defsList {
|
|
defs[d.ID] = d
|
|
}
|
|
now := time.Now().UTC()
|
|
items := []DocItem{}
|
|
for _, d := range docs {
|
|
if q.Space != "" && d.Space != q.Space {
|
|
continue
|
|
}
|
|
if q.Tag != "" && !slices.Contains(d.Tags, q.Tag) {
|
|
continue
|
|
}
|
|
if col != nil && !col.contains(d, defs, now) {
|
|
continue
|
|
}
|
|
if q.Filter != nil && !q.Filter.matches(d, defs, now) {
|
|
continue
|
|
}
|
|
items = append(items, d)
|
|
}
|
|
return DocList{Items: items, People: s.peopleOf(ctx, items, defs)}, nil
|
|
}
|
|
|
|
// peopleOf names the people the items mention (best effort).
|
|
func (s *Service) peopleOf(ctx context.Context, items []DocItem, defs map[string]propertyRecord) map[string]string {
|
|
ids := map[string]bool{}
|
|
for _, d := range items {
|
|
ids[d.CreatedBy], ids[d.LastEditedBy] = true, true
|
|
for id, raw := range d.Properties {
|
|
if def, ok := defs[id]; ok && def.Type == PropMember {
|
|
var l []string
|
|
_ = json.Unmarshal(raw, &l)
|
|
for _, u := range l {
|
|
ids[u] = true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
delete(ids, "")
|
|
out := map[string]string{}
|
|
list := make([]string, 0, len(ids))
|
|
for id := range ids {
|
|
if validID(id) {
|
|
list = append(list, id)
|
|
}
|
|
}
|
|
sort.Strings(list)
|
|
// One query per 50 people keeps the filter short.
|
|
for start := 0; start < len(list); start += 50 {
|
|
chunk := list[start:min(start+50, len(list))]
|
|
parts := make([]string, len(chunk))
|
|
for i, id := range chunk {
|
|
parts[i] = fmt.Sprintf("id = '%s'", id)
|
|
}
|
|
res, err := pocketbase.List[struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
}](ctx, s.pb, "users", pocketbase.ListOptions{PerPage: 50, Filter: strings.Join(parts, " || "), Fields: "id,name"})
|
|
if err != nil {
|
|
continue
|
|
}
|
|
for _, u := range res.Items {
|
|
out[u.ID] = u.Name
|
|
}
|
|
}
|
|
return out
|
|
}
|