Files
pages/internal/server/search_options.go
T
Anya Tokugawa d5d21b7841
Build and publish container images / images (push) Successful in 9m34s
Release container images / release (push) Successful in 36s
Refactor architecture into eight components and consolidate reliability work
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.
2026-11-01 15:23:31 +09:00

121 lines
3.9 KiB
Go

package server
import (
"fmt"
"net/http"
"net/url"
"strings"
"time"
"pageswiki/internal/store"
)
type searchFilterView struct {
FileType, Extension, DateField, DateFrom, DateTo, Timezone string
Applied, Open bool
}
func searchFiltersFromValues(values url.Values) searchFilterView {
view := searchFilterView{
FileType: strings.ToLower(strings.TrimSpace(values.Get("file_type"))),
Extension: strings.TrimPrefix(strings.ToLower(strings.TrimSpace(values.Get("ext"))), "."),
DateField: strings.ToLower(strings.TrimSpace(values.Get("date_field"))),
DateFrom: strings.TrimSpace(values.Get("from")),
DateTo: strings.TrimSpace(values.Get("to")),
}
if view.DateField == "" {
view.DateField = "updated"
}
view.Applied = view.FileType != "" || view.Extension != "" || view.DateFrom != "" || view.DateTo != ""
view.Open = view.Applied
return view
}
// Dates are calendar dates in the Domain timezone. The ending date is inclusive
// in the UI and becomes the next local midnight, not an assumed 24-hour day.
func parseSearchOptions(values url.Values, location *time.Location) (store.SearchOptions, searchFilterView, error) {
if location == nil {
location = time.UTC
}
view := searchFiltersFromValues(values)
view.Timezone = location.String()
options := store.SearchOptions{FileType: view.FileType, Extension: view.Extension, DateField: view.DateField}
invalid := func() (store.SearchOptions, searchFilterView, error) {
view.Open = true
return options, view, fmt.Errorf("invalid search filter: %w", store.ErrInvalidSearchOptions)
}
for _, key := range []string{"file_type", "ext", "date_field", "from", "to"} {
if len(values[key]) > 1 {
return invalid()
}
}
if view.DateFrom != "" {
from, err := time.ParseInLocation("2006-01-02", view.DateFrom, location)
if err != nil {
return invalid()
}
ms := from.UnixMilli()
options.FromMS = &ms
}
if view.DateTo != "" {
to, err := time.ParseInLocation("2006-01-02", view.DateTo, location)
if err != nil {
return invalid()
}
ms := to.AddDate(0, 0, 1).UnixMilli()
options.BeforeMS = &ms
}
if err := options.Validate(); err != nil {
view.Open = true
return options, view, err
}
return options, view, nil
}
func searchBaseValues(query, scope, localityKind, localityID string) url.Values {
values := url.Values{"q": {query}, "scope": {scope}}
if localityKind != "" {
values.Set("locality", localityKind)
values.Set("locality_id", localityID)
}
return values
}
func searchRetryValues(r *http.Request, query, scope, localityKind, localityID string) url.Values {
values := searchBaseValues(query, scope, localityKind, localityID)
for _, key := range []string{"file_type", "ext", "date_field", "from", "to"} {
if value := r.URL.Query().Get(key); value != "" {
values.Set(key, value)
}
}
return values
}
type searchCityViewResult struct {
store.CitySearchResult
KindKey, Href, PathHref, ContextLabel, Updated string
NameParts, PathParts, ExcerptParts []searchTextPart
}
func citySearchResultView(result store.CitySearchResult, query string, location *time.Location) searchCityViewResult {
title := result.DisplayTitle()
context := []string{}
if result.QuartierTitle != "" {
context = append(context, result.QuartierTitle)
}
if result.StreetTitle != "" {
context = append(context, result.StreetTitle)
}
return searchCityViewResult{
CitySearchResult: result,
KindKey: "city." + result.Kind,
Href: cityContentHref(result),
PathHref: cityContentHref(result),
ContextLabel: strings.Join(context, " / "),
Updated: time.UnixMilli(result.UpdatedAtMS).In(location).Format("2006-01-02 15:04"),
NameParts: highlightSearchText(title, query),
PathParts: highlightSearchText(result.ContentPath(), query),
ExcerptParts: highlightSearchText(searchExcerpt(result.Description, "text/plain", query), query),
}
}