Files
pages/internal/store/city_search.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

207 lines
7.8 KiB
Go

package store
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
}
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 *Store) 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); 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"
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":
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 + `
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)`
}