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.
124 lines
4.0 KiB
Go
124 lines
4.0 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.Title
|
|
if title == "" {
|
|
title = result.Name
|
|
}
|
|
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: cityDetailHref(result.Kind, result.Name, result.QuartierName, result.UUID),
|
|
PathHref: (&url.URL{Path: result.PathPrefix}).EscapedPath(),
|
|
ContextLabel: strings.Join(context, " / "),
|
|
Updated: time.UnixMilli(result.UpdatedAtMS).In(location).Format("2006-01-02 15:04"),
|
|
NameParts: highlightSearchText(title, query),
|
|
PathParts: highlightSearchText(result.PathPrefix, query),
|
|
ExcerptParts: highlightSearchText(searchExcerpt(result.Description, "text/plain", query), query),
|
|
}
|
|
}
|