203 lines
7.4 KiB
Go
203 lines
7.4 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
|
|
"pageswiki/internal/db"
|
|
|
|
"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 {
|
|
if s.sqlBackend == storePostgreSQL {
|
|
return db.ApplyPostgreSQLMigrations(context.Background(), s.DB, "store-browser-navigation", []db.PostgreSQLMigration{{Version: 1, Name: "browser navigator links", Statements: []string{
|
|
`CREATE TABLE browser_navigation_links(uuid TEXT COLLATE "C" PRIMARY KEY,domain_uuid TEXT COLLATE "C" NOT NULL,principal_uuid TEXT COLLATE "C" NOT NULL,label TEXT NOT NULL,path TEXT COLLATE "C" NOT NULL,is_directory INTEGER NOT NULL DEFAULT 0,position BIGINT NOT NULL DEFAULT 0,created_at_ms BIGINT NOT NULL,updated_at_ms BIGINT NOT NULL,UNIQUE(domain_uuid,principal_uuid,path))`,
|
|
}, Assertions: []string{`SELECT uuid,domain_uuid,principal_uuid,label,path,is_directory,position,created_at_ms,updated_at_ms FROM browser_navigation_links WHERE 1=0`}}})
|
|
}
|
|
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=`+s.sqlParam(1)+` AND principal_uuid=`+s.sqlParam(2)+`
|
|
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, cancel, err := s.beginPathMutation(domainUUID)
|
|
defer cancel()
|
|
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=`+s.sqlParam(1)+` AND principal_uuid=`+s.sqlParam(2)+` AND path=`+s.sqlParam(3), 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=`+s.sqlParam(1)+` AND principal_uuid=`+s.sqlParam(2), 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(`+s.sqlValues(9)+`)`, link.UUID, domainUUID, principalUUID, link.Label, link.Path, boolToInt(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")
|
|
}
|
|
if s.ReadOnly {
|
|
return ErrReadOnly
|
|
}
|
|
result, err := s.DB.Exec(`UPDATE browser_navigation_links SET label=`+s.sqlParam(1)+`,updated_at_ms=`+s.sqlParam(2)+`
|
|
WHERE uuid=`+s.sqlParam(3)+` AND domain_uuid=`+s.sqlParam(4)+` AND principal_uuid=`+s.sqlParam(5), 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 {
|
|
if s.ReadOnly {
|
|
return ErrReadOnly
|
|
}
|
|
result, err := s.DB.Exec(`DELETE FROM browser_navigation_links
|
|
WHERE uuid=`+s.sqlParam(1)+` AND domain_uuid=`+s.sqlParam(2)+` AND principal_uuid=`+s.sqlParam(3), 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 (s *Store) renameBrowserNavigationPathsTx(tx *sql.Tx, domainUUID, oldPath, newPath string, recursive bool) error {
|
|
var tableExists bool
|
|
existsSQL := `SELECT EXISTS(
|
|
SELECT 1 FROM sqlite_master WHERE type='table' AND name='browser_navigation_links'
|
|
)`
|
|
if s.sqlBackend == storePostgreSQL {
|
|
existsSQL = `SELECT to_regclass('browser_navigation_links') IS NOT NULL`
|
|
}
|
|
if err := tx.QueryRow(existsSQL).Scan(&tableExists); err != nil {
|
|
return err
|
|
}
|
|
if !tableExists {
|
|
if s.sqlBackend == storePostgreSQL {
|
|
return ErrPostgreSQLComponentNotReady
|
|
}
|
|
return nil
|
|
}
|
|
rows, err := tx.Query(`SELECT uuid,principal_uuid,path
|
|
FROM browser_navigation_links
|
|
WHERE domain_uuid=`+s.sqlParam(1)+` AND (path=`+s.sqlParam(2)+` OR (`+s.sqlParam(3)+` AND path>=`+s.sqlParam(4)+` AND path<`+s.sqlParam(5)+`))
|
|
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=`+s.sqlParam(1)+` AND principal_uuid=`+s.sqlParam(2)+` AND path=`+s.sqlParam(3), domainUUID, item.principal, item.next).Scan(&existing)
|
|
if err == nil && existing != item.id {
|
|
if _, err := tx.Exec(`DELETE FROM browser_navigation_links WHERE uuid=`+s.sqlParam(1), 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=`+s.sqlParam(1)+`,updated_at_ms=`+s.sqlParam(2)+` WHERE uuid=`+s.sqlParam(3), item.next, now, item.id); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|