Files
Docwell/Server/internal/directory/search.go
T
tajniak81andClaude Opus 5.5 7968e82cc2 Add page-level permissions and guests
Pages can now be shared with people and groups beyond their space, and
restricted so that only those grants count. A grant covers the page and
everything under it. Space admins always keep access, and an editor who
restricts a page keeps editor through a grant of their own.

Sharing a page with someone from the organization who is outside the
workspace adds them as a guest. Anyone else gets a guest invite that
carries the page grant, which is made when they accept. Guests see a
"limited" space holding only the pages shared with them.

The page tree, breadcrumbs, search, favorites, recents, trash and import
all follow the page role. Public links skip restricted subtrees. Grants
are cleaned up when a member leaves or a group is deleted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 21:39:50 +01:00

79 lines
2.0 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package directory
import (
"context"
"docwell/server/internal/access"
"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, 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"`
}
// Search finds pages in a workspace, only the ones c can open.
func (s *Service) Search(ctx context.Context, c access.Caller, wsID, text string, limit int) (SearchResult, error) {
spaces, err := s.ListSpaces(ctx, c, wsID)
if err != nil {
return SearchResult{}, err
}
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)
}
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, text, 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
}