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

158 lines
4.2 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package directory
import (
"context"
"slices"
"strings"
"time"
"docwell/server/internal/access"
"docwell/server/internal/apperr"
"docwell/server/internal/search"
)
// Searcher is the part of search.Index this package uses.
type Searcher interface {
Query(ctx context.Context, workspace string, spaces []string, text string, f search.Filter, limit int) ([]search.Hit, int, error)
}
// UseSearch enables Search.
func (s *Service) UseSearch(x Searcher) { s.search = x }
type SearchHit struct {
search.Hit
SpaceName string `json:"spaceName"`
}
type SearchResult struct {
Items []SearchHit `json:"items"`
Total int `json:"total"`
}
// SearchQuery is a search request. Space, Tag, Author, UpdatedAfter and
// UpdatedBefore are optional; the dates are RFC 3339 times or plain dates
// (UpdatedBefore then covers the whole day).
type SearchQuery struct {
Text string
Space string
Tag string
Author string
UpdatedAfter string
UpdatedBefore string
Limit int
}
func parseSearchTime(v string, endOfDay bool) (time.Time, error) {
if v == "" {
return time.Time{}, nil
}
if t, err := time.Parse(time.RFC3339, v); err == nil {
return t, nil
}
t, err := time.Parse(time.DateOnly, v)
if err != nil {
return time.Time{}, apperr.Invalid("Dates must look like 2026-11-02 or 2026-11-02T15:04:05Z.")
}
if endOfDay {
t = t.Add(24*time.Hour - time.Millisecond)
}
return t, nil
}
// Search finds pages in a workspace, only the ones c can open.
func (s *Service) Search(ctx context.Context, c access.Caller, wsID string, sq SearchQuery) (SearchResult, error) {
f := search.Filter{Author: sq.Author}
var err error
if f.After, err = parseSearchTime(sq.UpdatedAfter, false); err != nil {
return SearchResult{}, err
}
if f.Before, err = parseSearchTime(sq.UpdatedBefore, true); err != nil {
return SearchResult{}, err
}
spaces, err := s.ListSpaces(ctx, c, wsID)
if err != nil {
return SearchResult{}, err
}
if sq.Space != "" {
// A space the caller cannot see finds nothing rather than an error,
// so its existence does not leak.
spaces = slices.DeleteFunc(spaces, func(sp Space) bool { return sp.ID != sq.Space })
}
out := SearchResult{Items: []SearchHit{}}
if s.search == nil || len(spaces) == 0 {
return out, nil
}
ids := make([]string, len(spaces))
names := make(map[string]string, len(spaces))
rules := make(map[string]pageRules, len(spaces))
filtered := false // some pages of these spaces are hidden from c
for i, sp := range spaces {
ids[i], names[sp.ID] = sp.ID, sp.Name
_, r, err := s.spaceRules(ctx, c, sp.ID)
if err != nil {
return SearchResult{}, err
}
rules[sp.ID] = r
filtered = filtered || (!r.plain() && r.spaceRole != access.SpaceAdmin)
}
limit := sq.Limit
if limit <= 0 {
limit = 20
}
// A tag narrows the hits to the pages carrying it.
var tagged map[string]bool
if sq.Tag != "" {
if !validID(sq.Tag) {
return out, nil
}
pages, err := s.taggedPages(ctx, wsID, sq.Tag)
if err != nil {
return SearchResult{}, err
}
tagged = make(map[string]bool, len(pages))
for _, p := range pages {
tagged[p.ID] = true
}
if len(tagged) == 0 {
return out, nil
}
filtered = true
// The index does not know tags: with no text and no other filter,
// have it list every page (newest first) for the tag to narrow.
if strings.TrimSpace(sq.Text) == "" && f.Author == "" && f.After.IsZero() && f.Before.IsZero() {
f.After = time.Unix(1, 0)
}
}
// Hidden pages are dropped after the query, so ask for more to keep
// the page of results full.
want := limit
if filtered {
want = limit*3 + 20
}
if tagged != nil {
want = limit*10 + 200
}
hits, total, err := s.search.Query(ctx, wsID, ids, sq.Text, f, want)
if err != nil {
return SearchResult{}, err
}
dropped := 0
for _, h := range hits {
if rules[h.Space].role(h.ID) == "" || (tagged != nil && !tagged[h.ID]) {
dropped++
continue
}
if len(out.Items) < limit {
out.Items = append(out.Items, SearchHit{Hit: h, SpaceName: names[h.Space]})
}
}
out.Total = max(total-dropped, len(out.Items))
if tagged != nil {
// Only the hits read here are known to carry the tag.
out.Total = len(hits) - dropped
}
return out, nil
}