- Search can be narrowed by space, author and last change; the index records a mapping version and rebuilds when it changes. - Editors can pin up to 12 pages to a space's home. - Find and replace in the editor (Ctrl+F), with match case. - Pages can be set to full width and a serif or mono font. - Tailwind now scans the editor package, so its classes are generated. - Fix golangci-lint findings, including push device names that were saved before being trimmed and capped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
125 lines
3.3 KiB
Go
125 lines
3.3 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package directory
|
|
|
|
import (
|
|
"context"
|
|
"slices"
|
|
"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, 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
|
|
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
|
|
}
|
|
// 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
|
|
}
|
|
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) == "" {
|
|
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))
|
|
return out, nil
|
|
}
|