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.
213 lines
8.2 KiB
Go
213 lines
8.2 KiB
Go
package mestionora
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
CitySearchCandidateLimit = 1000 // Per locality kind, before authorization.
|
|
CitySearchResultLimit = 30
|
|
citySearchTimeout = 2 * time.Second
|
|
)
|
|
|
|
// CitySearchResult describes a locality, not its contained Pages. Description
|
|
// is a bounded plain-text excerpt around the match and is never trusted HTML.
|
|
type CitySearchResult struct {
|
|
UUID, Kind, Name, Title, Description, PathPrefix string
|
|
QuartierTitle, StreetTitle, QuartierName string
|
|
CreatedAtMS, UpdatedAtMS int64
|
|
NameMatch, PathMatch, DescriptionMatch bool
|
|
NavigationPath string
|
|
HasPage bool
|
|
}
|
|
|
|
type citySearchCandidate struct {
|
|
CitySearchResult
|
|
quartierPath, streetPath string
|
|
}
|
|
|
|
// SearchCityInDomain searches locality metadata only. It does not construct a
|
|
// CityMap, expand descendants, read Page bodies, or count hidden localities.
|
|
// Scope selects a locality's own boundary, not an ancestor of the scope. Every
|
|
// boundary in its parent chain must be readable before the result is exposed.
|
|
func (s *SQLite) SearchCityInDomain(parent context.Context, domainUUID, scope, query string, limit int, allowed func(string) bool, options SearchOptions) (found []CitySearchResult, searchErr error) {
|
|
if err := options.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
query = strings.TrimSpace(query)
|
|
if len(query) > 512 || len(strings.Fields(query)) > 16 {
|
|
return nil, errors.New("search query is too long")
|
|
}
|
|
// File filters are intentionally not applied to locality metadata: a Spot
|
|
// containing a PDF is not itself a PDF and must not claim to match one.
|
|
if allowed == nil || options.FileType != "" || options.Extension != "" || (query == "" && !options.HasFilters()) {
|
|
return nil, nil
|
|
}
|
|
if limit <= 0 || limit > CitySearchResultLimit {
|
|
limit = CitySearchResultLimit
|
|
}
|
|
ctx, cancel := context.WithTimeout(parent, citySearchTimeout)
|
|
defer cancel()
|
|
defer func() {
|
|
if searchErr != nil && ctx.Err() != nil {
|
|
searchErr = ctx.Err()
|
|
}
|
|
}()
|
|
tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer tx.Rollback()
|
|
scope = normalizePath(scope)
|
|
prefix, upper := strings.TrimRight(scope, "/")+"/", strings.TrimRight(scope, "/")+"0"
|
|
args := []any{
|
|
sql.Named("domain", domainUUID), sql.Named("scope", scope),
|
|
sql.Named("prefix", prefix), sql.Named("upper", upper),
|
|
sql.Named("query", query), sql.Named("limit", CitySearchCandidateLimit),
|
|
}
|
|
dateColumn := "n.updated_at_ms"
|
|
if options.DateField == "created" {
|
|
dateColumn = "n.created_at_ms"
|
|
}
|
|
dateWhere := ""
|
|
if options.FromMS != nil {
|
|
dateWhere += " AND " + dateColumn + ">=:from_ms"
|
|
args = append(args, sql.Named("from_ms", *options.FromMS))
|
|
}
|
|
if options.BeforeMS != nil {
|
|
dateWhere += " AND " + dateColumn + "<:before_ms"
|
|
args = append(args, sql.Named("before_ms", *options.BeforeMS))
|
|
}
|
|
candidates := make([]citySearchCandidate, 0)
|
|
for _, kind := range []string{"quartier", "street", "spot"} {
|
|
rows, err := tx.QueryContext(ctx, citySearchSQL(kind, dateWhere), args...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for rows.Next() {
|
|
var candidate citySearchCandidate
|
|
candidate.Kind = kind
|
|
if err := rows.Scan(&candidate.UUID, &candidate.Name, &candidate.Title, &candidate.Description,
|
|
&candidate.PathPrefix, &candidate.CreatedAtMS, &candidate.UpdatedAtMS,
|
|
&candidate.NameMatch, &candidate.PathMatch, &candidate.DescriptionMatch,
|
|
&candidate.QuartierTitle, &candidate.StreetTitle, &candidate.quartierPath, &candidate.streetPath, &candidate.QuartierName,
|
|
&candidate.NavigationPath, &candidate.HasPage); err != nil {
|
|
rows.Close()
|
|
return nil, err
|
|
}
|
|
candidates = append(candidates, candidate)
|
|
}
|
|
err = rows.Err()
|
|
closeErr := rows.Close()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if closeErr != nil {
|
|
return nil, closeErr
|
|
}
|
|
}
|
|
// Release the sole read connection before calling authorization, which may
|
|
// itself need a database connection. HTTP normally passes its cached policy.
|
|
if err := tx.Rollback(); err != nil && !errors.Is(err, sql.ErrTxDone) {
|
|
return nil, err
|
|
}
|
|
sort.SliceStable(candidates, func(i, j int) bool {
|
|
a, b := candidates[i], candidates[j]
|
|
if a.Kind != b.Kind {
|
|
return citySearchKindOrder(a.Kind) < citySearchKindOrder(b.Kind)
|
|
}
|
|
if a.NameMatch != b.NameMatch {
|
|
return a.NameMatch
|
|
}
|
|
if a.PathMatch != b.PathMatch {
|
|
return a.PathMatch
|
|
}
|
|
if a.PathPrefix != b.PathPrefix {
|
|
return a.PathPrefix < b.PathPrefix
|
|
}
|
|
return a.UUID < b.UUID
|
|
})
|
|
readable := make(map[string]bool)
|
|
canRead := func(path string) bool {
|
|
if value, exists := readable[path]; exists {
|
|
return value
|
|
}
|
|
value := allowed(path)
|
|
readable[path] = value
|
|
return value
|
|
}
|
|
for _, candidate := range candidates {
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
if !canRead(candidate.PathPrefix) || (candidate.quartierPath != "" && !canRead(candidate.quartierPath)) || (candidate.streetPath != "" && !canRead(candidate.streetPath)) {
|
|
continue
|
|
}
|
|
found = append(found, candidate.CitySearchResult)
|
|
if len(found) == limit {
|
|
break
|
|
}
|
|
}
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return found, nil
|
|
}
|
|
|
|
func citySearchKindOrder(kind string) int {
|
|
switch kind {
|
|
case "quartier":
|
|
return 0
|
|
case "street":
|
|
return 1
|
|
default:
|
|
return 2
|
|
}
|
|
}
|
|
|
|
// All interpolated SQL fragments are package-owned constants. User input is
|
|
// bound, so %, _, quotes and backslashes are literal substring characters.
|
|
func citySearchSQL(kind, dateWhere string) string {
|
|
from, description := "zones n", "n.description"
|
|
navigationPath := "n.path_prefix"
|
|
parentColumns := "'','','','',''"
|
|
where := "n.kind='quartier'"
|
|
switch kind {
|
|
case "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'`
|
|
parentColumns = "substr(COALESCE(NULLIF(q.title,''),q.name),1,256),'',q.path_prefix,'',q.name"
|
|
where = "n.kind='street' AND " + citySearchAbsolutePathSQL("q.path_prefix")
|
|
case "spot":
|
|
navigationPath = `CASE WHEN substr(n.path_prefix,-11)='/_undefined' THEN st.path_prefix ELSE n.path_prefix END`
|
|
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'`
|
|
description = "n.summary"
|
|
parentColumns = "substr(COALESCE(NULLIF(q.title,''),q.name),1,256),substr(COALESCE(NULLIF(st.title,''),st.name),1,256),q.path_prefix,st.path_prefix,q.name"
|
|
where = citySearchAbsolutePathSQL("q.path_prefix") + " AND " + citySearchAbsolutePathSQL("st.path_prefix")
|
|
}
|
|
nameMatch := `(:query!='' AND (instr(lower(n.name),lower(:query))>0 OR instr(lower(n.title),lower(:query))>0))`
|
|
pathMatch := `(:query!='' AND instr(lower(n.path_prefix),lower(:query))>0)`
|
|
descriptionMatch := `(:query!='' AND instr(lower(` + description + `),lower(:query))>0)`
|
|
return `SELECT n.uuid,substr(n.name,1,256),substr(n.title,1,256),
|
|
substr(` + description + `,max(1,instr(lower(` + description + `),lower(:query))-160),512),
|
|
n.path_prefix,n.created_at_ms,n.updated_at_ms,` + nameMatch + `,` + pathMatch + `,` + descriptionMatch + `,` + parentColumns + `,` + navigationPath + `,
|
|
EXISTS(SELECT 1 FROM paths cp WHERE cp.domain_uuid=n.domain_uuid AND cp.path=(` + navigationPath + `))
|
|
FROM ` + from + ` WHERE n.domain_uuid=:domain AND n.state!='archived' AND ` + where + `
|
|
AND ` + citySearchAbsolutePathSQL("n.path_prefix") + `
|
|
AND (n.path_prefix COLLATE BINARY=:scope OR (n.path_prefix COLLATE BINARY>=:prefix AND n.path_prefix COLLATE BINARY<:upper))
|
|
AND (:query='' OR ` + nameMatch + ` OR ` + pathMatch + ` OR ` + descriptionMatch + `)` + dateWhere + `
|
|
ORDER BY ` + nameMatch + ` DESC,` + pathMatch + ` DESC,n.path_prefix COLLATE BINARY,n.uuid LIMIT :limit`
|
|
}
|
|
|
|
func citySearchAbsolutePathSQL(column string) string {
|
|
// Keep authorization paths intact rather than truncating them. Malformed
|
|
// legacy boundaries and impractically large metadata are not navigable hits.
|
|
return `(substr(` + column + `,1,1)='/' AND length(` + column + `)<=4096 AND instr(` + column + `,char(0))=0)`
|
|
}
|