Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts. Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation. Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
181 lines
5.8 KiB
Go
181 lines
5.8 KiB
Go
package store
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const MaxBrowserNavigationLinks = 64
|
|
|
|
var ErrBrowserNavigationLimit = errors.New("browser navigator link limit reached")
|
|
|
|
type BrowserNavigationLink struct {
|
|
UUID string `json:"uuid"`
|
|
Label string `json:"label"`
|
|
Path string `json:"path"`
|
|
IsDir bool `json:"is_directory"`
|
|
Position int `json:"position"`
|
|
UpdatedAt int64 `json:"updated_at_ms"`
|
|
}
|
|
|
|
func (s *Store) EnsureBrowserNavigationSchema() error {
|
|
return s.ensureSchemaComponent("browser-navigation")
|
|
}
|
|
|
|
func (s *Store) ListBrowserNavigationLinks(domainUUID, principalUUID string) ([]BrowserNavigationLink, error) {
|
|
rows, err := s.DB.Query(`SELECT uuid,label,path,is_directory,position,updated_at_ms
|
|
FROM browser_navigation_links
|
|
WHERE domain_uuid=? AND principal_uuid=?
|
|
ORDER BY position,created_at_ms,uuid`, domainUUID, principalUUID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
links := make([]BrowserNavigationLink, 0)
|
|
for rows.Next() {
|
|
var link BrowserNavigationLink
|
|
if err := rows.Scan(&link.UUID, &link.Label, &link.Path, &link.IsDir, &link.Position, &link.UpdatedAt); err != nil {
|
|
return nil, err
|
|
}
|
|
links = append(links, link)
|
|
}
|
|
return links, rows.Err()
|
|
}
|
|
|
|
func (s *Store) AddBrowserNavigationLink(domainUUID, principalUUID, label, path string, isDir bool) (*BrowserNavigationLink, error) {
|
|
label = strings.TrimSpace(label)
|
|
path = normalizePath(path)
|
|
if domainUUID == "" || principalUUID == "" || label == "" || len(label) > 120 || path == "/" || len(path) > 2048 {
|
|
return nil, errors.New("invalid browser navigator link")
|
|
}
|
|
tx, err := s.DB.Begin()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer tx.Rollback()
|
|
var link BrowserNavigationLink
|
|
err = tx.QueryRow(`SELECT uuid,label,path,is_directory,position,updated_at_ms
|
|
FROM browser_navigation_links
|
|
WHERE domain_uuid=? AND principal_uuid=? AND path=?`, domainUUID, principalUUID, path).
|
|
Scan(&link.UUID, &link.Label, &link.Path, &link.IsDir, &link.Position, &link.UpdatedAt)
|
|
if err == nil {
|
|
return &link, nil
|
|
}
|
|
if !errors.Is(err, sql.ErrNoRows) {
|
|
return nil, err
|
|
}
|
|
var count, position int
|
|
if err := tx.QueryRow(`SELECT COUNT(*),COALESCE(MAX(position),-1)+1
|
|
FROM browser_navigation_links WHERE domain_uuid=? AND principal_uuid=?`, domainUUID, principalUUID).
|
|
Scan(&count, &position); err != nil {
|
|
return nil, err
|
|
}
|
|
if count >= MaxBrowserNavigationLinks {
|
|
return nil, ErrBrowserNavigationLimit
|
|
}
|
|
now := time.Now().UnixMilli()
|
|
link = BrowserNavigationLink{UUID: uuid.NewString(), Label: label, Path: path, IsDir: isDir, Position: position, UpdatedAt: now}
|
|
if _, err := tx.Exec(`INSERT INTO browser_navigation_links
|
|
(uuid,domain_uuid,principal_uuid,label,path,is_directory,position,created_at_ms,updated_at_ms)
|
|
VALUES(?,?,?,?,?,?,?,?,?)`, link.UUID, domainUUID, principalUUID, link.Label, link.Path, link.IsDir, link.Position, now, now); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := tx.Commit(); err != nil {
|
|
return nil, err
|
|
}
|
|
return &link, nil
|
|
}
|
|
|
|
func (s *Store) RenameBrowserNavigationLink(domainUUID, principalUUID, id, label string) error {
|
|
label = strings.TrimSpace(label)
|
|
if label == "" || len(label) > 120 {
|
|
return errors.New("invalid browser navigator label")
|
|
}
|
|
result, err := s.DB.Exec(`UPDATE browser_navigation_links SET label=?,updated_at_ms=?
|
|
WHERE uuid=? AND domain_uuid=? AND principal_uuid=?`, label, time.Now().UnixMilli(), id, domainUUID, principalUUID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
count, _ := result.RowsAffected()
|
|
if count != 1 {
|
|
return sql.ErrNoRows
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Store) DeleteBrowserNavigationLink(domainUUID, principalUUID, id string) error {
|
|
result, err := s.DB.Exec(`DELETE FROM browser_navigation_links
|
|
WHERE uuid=? AND domain_uuid=? AND principal_uuid=?`, id, domainUUID, principalUUID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
count, _ := result.RowsAffected()
|
|
if count != 1 {
|
|
return sql.ErrNoRows
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// renameBrowserNavigationPathsTx keeps a user's saved navigator targets aligned
|
|
// with semantic Path mutations. If the destination was already saved, the
|
|
// existing destination link wins and the now-duplicate source link is removed.
|
|
func renameBrowserNavigationPathsTx(tx *sql.Tx, domainUUID, oldPath, newPath string, recursive bool) error {
|
|
var tableExists bool
|
|
if err := tx.QueryRow(`SELECT EXISTS(
|
|
SELECT 1 FROM sqlite_master WHERE type='table' AND name='browser_navigation_links'
|
|
)`).Scan(&tableExists); err != nil {
|
|
return err
|
|
}
|
|
if !tableExists {
|
|
return nil
|
|
}
|
|
rows, err := tx.Query(`SELECT uuid,principal_uuid,path
|
|
FROM browser_navigation_links
|
|
WHERE domain_uuid=? AND (path=? OR (? AND path>=? AND path<?))
|
|
ORDER BY length(path),path`, domainUUID, oldPath, recursive, oldPath+"/", oldPath+"0")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
type target struct{ id, principal, old, next string }
|
|
var targets []target
|
|
for rows.Next() {
|
|
var item target
|
|
if err := rows.Scan(&item.id, &item.principal, &item.old); err != nil {
|
|
rows.Close()
|
|
return err
|
|
}
|
|
item.next = newPath + strings.TrimPrefix(item.old, oldPath)
|
|
targets = append(targets, item)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
rows.Close()
|
|
return err
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return err
|
|
}
|
|
now := time.Now().UnixMilli()
|
|
for _, item := range targets {
|
|
var existing string
|
|
err := tx.QueryRow(`SELECT uuid FROM browser_navigation_links
|
|
WHERE domain_uuid=? AND principal_uuid=? AND path=?`, domainUUID, item.principal, item.next).Scan(&existing)
|
|
if err == nil && existing != item.id {
|
|
if _, err := tx.Exec(`DELETE FROM browser_navigation_links WHERE uuid=?`, item.id); err != nil {
|
|
return err
|
|
}
|
|
continue
|
|
}
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
return err
|
|
}
|
|
if _, err := tx.Exec(`UPDATE browser_navigation_links SET path=?,updated_at_ms=? WHERE uuid=?`, item.next, now, item.id); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|