Files
Docwell/Server/internal/directory/pins.go
T
tajniak81andClaude Opus 5.5 f20e5f96f9 Add search filters, space pins, find and replace and page layout
- 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>
2026-11-02 14:35:18 +01:00

109 lines
3.0 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package directory
import (
"context"
"sort"
"time"
"docwell/server/internal/access"
"docwell/server/internal/apperr"
"docwell/server/internal/store/pocketbase"
)
// maxPins keeps the space home short; pinning is for the few pages everyone
// should find first.
const maxPins = 12
type PinnedPage struct {
ID string `json:"id"`
Title string `json:"title"`
Icon string `json:"icon"`
Updated string `json:"updated"`
PinnedAt string `json:"pinnedAt"`
}
// PinPage pins a page to its space's home (editor).
func (s *Service) PinPage(ctx context.Context, c access.Caller, id string) (Page, error) {
p, a, err := s.pageFor(ctx, c, id)
if err != nil {
return Page{}, err
}
if err := access.EditPages(a.role); err != nil {
return Page{}, err
}
if p.PinnedAt != "" {
return s.GetPage(ctx, c, p.ID)
}
t, err := s.loadTree(ctx, a.space.ID)
if err != nil {
return Page{}, err
}
if !t.live(p.ID) || t.archived(p.ID) {
return Page{}, apperr.Invalid("trashed and archived pages cannot be pinned")
}
if len(pinned(t)) >= maxPins {
return Page{}, apperr.Invalid("a space can have at most 12 pinned pages; unpin one first")
}
if _, err := pocketbase.Update[pageRecord](ctx, s.pb, "pages", p.ID, map[string]any{"pinned_at": pbTime(time.Now())}); err != nil {
return Page{}, mapPB(err, "page not found")
}
return s.GetPage(ctx, c, p.ID)
}
// UnpinPage takes a page off its space's home (editor).
func (s *Service) UnpinPage(ctx context.Context, c access.Caller, id string) (Page, error) {
p, a, err := s.pageFor(ctx, c, id)
if err != nil {
return Page{}, err
}
if err := access.EditPages(a.role); err != nil {
return Page{}, err
}
if p.PinnedAt != "" {
if _, err := pocketbase.Update[pageRecord](ctx, s.pb, "pages", p.ID, map[string]any{"pinned_at": ""}); err != nil {
return Page{}, mapPB(err, "page not found")
}
}
return s.GetPage(ctx, c, p.ID)
}
// ListPinned lists the pinned pages of a space that c can open, in the order
// they were pinned. Pages in the trash or the archive (themselves or through
// an ancestor) keep their pin but are not listed, so restoring brings it back.
func (s *Service) ListPinned(ctx context.Context, c access.Caller, spaceID string) ([]PinnedPage, error) {
a, rules, err := s.spaceRules(ctx, c, spaceID)
if err != nil {
return nil, err
}
t, err := s.loadTree(ctx, a.space.ID)
if err != nil {
return nil, err
}
out := []PinnedPage{}
for _, p := range pinned(t) {
if rules.role(p.ID) != "" {
out = append(out, PinnedPage{ID: p.ID, Title: p.Title, Icon: p.Icon, Updated: p.Updated, PinnedAt: p.PinnedAt})
}
}
return out, nil
}
// pinned is the space's listed pins, oldest first.
func pinned(t tree) []pageRecord {
var out []pageRecord
for _, p := range t.byID {
if p.PinnedAt != "" && t.live(p.ID) && !t.archived(p.ID) {
out = append(out, p)
}
}
sort.Slice(out, func(i, j int) bool {
if out[i].PinnedAt != out[j].PinnedAt {
return out[i].PinnedAt < out[j].PinnedAt
}
return out[i].ID < out[j].ID
})
return out
}