Files
pages/internal/store/city_reference.go
T
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
2026-09-27 14:44:43 +09:00

114 lines
4.1 KiB
Go

package store
import (
"context"
"database/sql"
"errors"
"strings"
)
var ErrCityNameAmbiguous = errors.New("City name is ambiguous; qualify the Street by its Quartier or use uuid:")
type CityNodeAncestor struct {
UUID, Name, Title, PathPrefix string
}
// CityNode keeps the trusted Store API. HTTP callers propagate cancellation
// through CityNodeContext. A bare UUID-looking string remains a name; UUIDs
// require the explicit uuid: selector, so identity does not depend on spelling.
func (s *Store) CityNode(domainUUID, kind, id string) (CityNodeDetail, error) {
return s.CityNodeContext(context.Background(), domainUUID, kind, id)
}
// A Street reference can be a unique legacy name, uuid:<id>, or
// <Quartier-name>/<Street-name>. Qualified lookup and parent checks happen in
// the same SQL snapshot, without enumerating the City or opening nested cursors.
func (s *Store) CityNodeContext(parent context.Context, domainUUID, kind, id string) (detail CityNodeDetail, lookupErr error) {
id = strings.ToLower(strings.TrimSpace(id))
if kind != "quartier" && kind != "street" && kind != "spot" {
return detail, sql.ErrNoRows
}
if id == "" || len(id) > 256 || strings.ContainsAny(id, "\\\x00") {
return detail, sql.ErrNoRows
}
parts := strings.Split(id, "/")
if len(parts) > 1 && (kind != "street" || len(parts) != 2) {
return detail, sql.ErrNoRows
}
for _, part := range parts {
if part == "" || part == "." || part == ".." {
return detail, sql.ErrNoRows
}
}
ctx, cancel := context.WithTimeout(parent, cityReadTimeout)
defer cancel()
defer func() {
if lookupErr != nil && ctx.Err() != nil {
lookupErr = ctx.Err()
}
}()
from := "zones n"
columns := `n.uuid,n.domain_uuid,n.kind,COALESCE(n.parent_uuid,''),n.name,n.title,n.description,n.state,n.path_prefix,n.sort_key,n.version`
ancestors := `,'','','','','','','',''`
where := `n.domain_uuid=? AND n.state!='archived' AND n.kind=?`
args := []any{domainUUID, kind}
if kind == "street" {
from += ` JOIN zones q ON q.uuid=n.parent_uuid AND q.domain_uuid=n.domain_uuid AND q.kind='quartier' AND q.state!='archived'`
ancestors = `,q.uuid,q.name,q.title,q.path_prefix,'','','',''`
} else if kind == "spot" {
from = `spots n JOIN street_spots ss ON ss.spot_uuid=n.uuid
JOIN zones st ON st.uuid=ss.street_uuid AND st.domain_uuid=n.domain_uuid AND st.kind='street' AND st.state!='archived'
JOIN zones q ON q.uuid=st.parent_uuid AND q.domain_uuid=st.domain_uuid AND q.kind='quartier' AND q.state!='archived'`
columns = `n.uuid,n.domain_uuid,'spot',st.uuid,n.name,n.title,n.summary,n.state,n.path_prefix,ss.sort_key,n.version`
ancestors = `,q.uuid,q.name,q.title,q.path_prefix,st.uuid,st.name,st.title,st.path_prefix`
where = `n.domain_uuid=? AND n.state!='archived'`
args = []any{domainUUID}
}
if len(parts) == 2 {
// A qualified URL is name based. Explicit UUID selectors use the short
// /s/uuid:... route instead of mixing two different identity models.
where += " AND q.name=? AND n.name=?"
args = append(args, parts[0], parts[1])
} else if strings.HasPrefix(id, "uuid:") {
where += " AND n.uuid=?"
args = append(args, strings.TrimPrefix(id, "uuid:"))
} else {
where += " AND n.name=?"
args = append(args, id)
}
rows, err := s.DB.QueryContext(ctx, `SELECT `+columns+ancestors+` FROM `+from+` WHERE `+where+` LIMIT 2`, args...)
if err != nil {
return detail, err
}
defer rows.Close()
count := 0
for rows.Next() {
var node CityNodeDetail
var q, st CityNodeAncestor
if err := rows.Scan(&node.UUID, &node.DomainUUID, &node.Kind, &node.ParentUUID,
&node.Name, &node.Title, &node.Description, &node.State, &node.PathPrefix,
&node.SortKey, &node.Version, &q.UUID, &q.Name, &q.Title, &q.PathPrefix,
&st.UUID, &st.Name, &st.Title, &st.PathPrefix); err != nil {
return CityNodeDetail{}, err
}
count++
if count > 1 {
return CityNodeDetail{}, ErrCityNameAmbiguous
}
if q.UUID != "" {
node.Quartier = &q
}
if st.UUID != "" {
node.Street = &st
}
detail = node
}
if err := rows.Err(); err != nil {
return CityNodeDetail{}, err
}
if count == 0 {
return CityNodeDetail{}, sql.ErrNoRows
}
return detail, nil
}