251 lines
10 KiB
Go
251 lines
10 KiB
Go
package mestionora
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"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) {
|
|
return searchCity(s.db, false, parent, domainUUID, scope, query, limit, allowed, options)
|
|
}
|
|
|
|
func (s *PostgreSQL) SearchCityInDomain(parent context.Context, domainUUID, scope, query string, limit int, allowed func(string) bool, options SearchOptions) ([]CitySearchResult, error) {
|
|
return searchCity(s.db, true, parent, domainUUID, scope, query, limit, allowed, options)
|
|
}
|
|
|
|
func searchCity(database *sql.DB, native bool, 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 := database.BeginTx(ctx, &sql.TxOptions{ReadOnly: true, Isolation: sql.LevelRepeatableRead})
|
|
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),
|
|
}
|
|
if native {
|
|
args = []any{domainUUID, scope, prefix, upper, query, CitySearchCandidateLimit}
|
|
}
|
|
dateColumn := "n.updated_at_ms"
|
|
if options.DateField == "created" {
|
|
dateColumn = "n.created_at_ms"
|
|
}
|
|
dateWhere := ""
|
|
if options.FromMS != nil {
|
|
if native {
|
|
args = append(args, *options.FromMS)
|
|
dateWhere += " AND " + dateColumn + ">=" + fmt.Sprintf("$%d", len(args))
|
|
} else {
|
|
dateWhere += " AND " + dateColumn + ">=:from_ms"
|
|
args = append(args, sql.Named("from_ms", *options.FromMS))
|
|
}
|
|
}
|
|
if options.BeforeMS != nil {
|
|
if native {
|
|
args = append(args, *options.BeforeMS)
|
|
dateWhere += " AND " + dateColumn + "<" + fmt.Sprintf("$%d", len(args))
|
|
} else {
|
|
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, citySearchDialectSQL(kind, dateWhere, native), 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 {
|
|
return citySearchDialectSQL(kind, dateWhere, false)
|
|
}
|
|
|
|
func citySearchDialectSQL(kind, dateWhere string, native bool) string {
|
|
domain, scope, prefix, upper, query, limit := ":domain", ":scope", ":prefix", ":upper", ":query", ":limit"
|
|
collation, position, greatest := " COLLATE BINARY", "instr", "max"
|
|
absolute := citySearchAbsolutePathSQL
|
|
if native {
|
|
domain, scope, prefix, upper, query, limit = "$1", "$2", "$3", "$4", "$5", "$6"
|
|
collation, position, greatest = ` COLLATE "C"`, "strpos", "greatest"
|
|
absolute = func(column string) string { return `(substr(` + column + `,1,1)='/' AND length(` + column + `)<=4096)` }
|
|
}
|
|
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 " + absolute("q.path_prefix")
|
|
case "spot":
|
|
navigationPath = `CASE WHEN substr(n.path_prefix,-11)='/_undefined' THEN st.path_prefix ELSE n.path_prefix END`
|
|
if native {
|
|
navigationPath = `CASE WHEN right(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 = absolute("q.path_prefix") + " AND " + absolute("st.path_prefix")
|
|
}
|
|
contains := func(column string) string { return position + "(lower(" + column + "),lower(" + query + "))" }
|
|
nameMatch := `(` + query + `!='' AND (` + contains("n.name") + `>0 OR ` + contains("n.title") + `>0))`
|
|
pathMatch := `(` + query + `!='' AND ` + contains("n.path_prefix") + `>0)`
|
|
descriptionMatch := `(` + query + `!='' AND ` + contains(description) + `>0)`
|
|
return `SELECT n.uuid,substr(n.name,1,256),substr(n.title,1,256),
|
|
substr(` + description + `,` + greatest + `(1,` + contains(description) + `-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 ` + absolute("n.path_prefix") + `
|
|
AND (n.path_prefix` + collation + `=` + scope + ` OR (n.path_prefix` + collation + `>=` + prefix + ` AND n.path_prefix` + collation + `<` + upper + `))
|
|
AND (` + query + `='' OR ` + nameMatch + ` OR ` + pathMatch + ` OR ` + descriptionMatch + `)` + dateWhere + `
|
|
ORDER BY ` + nameMatch + ` DESC,` + pathMatch + ` DESC,n.path_prefix` + collation + `,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)`
|
|
}
|