Files
wanderer/db/util/meilisearch.go
Flomp c04a719ca6 Feat add actors to meilisearch (#1048)
* initial commit

* update api docs

* adds search token versioning

* make id filterable

* isLocal -> is_local

* add iri to actor index

* update gitignore

* Merge remote-tracking branch 'origin/main' into feat-add-actors-to-meilisearch

* fix sharing for newly discovered actors

* fix ActorSearchResult type

---------

Co-authored-by: Christian Beutel <>
2026-06-09 21:02:05 +02:00

521 lines
13 KiB
Go

package util
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"path"
"time"
"github.com/meilisearch/meilisearch-go"
"github.com/pocketbase/pocketbase/core"
)
func documentFromTrailRecord(r *core.Record, author *core.Record, includeShares bool) (map[string]interface{}, error) {
photos := r.GetStringSlice("photos")
thumbnail := ""
if len(photos) > 0 {
thumbnailIndex := r.GetInt("thumbnail")
if thumbnailIndex >= len(photos) {
thumbnailIndex = 0
}
thumbnail = photos[thumbnailIndex]
}
tagRecords := r.ExpandedAll("tags")
tags := make([]string, len(tagRecords))
for i, v := range tagRecords {
tags[i] = v.GetString("name")
}
category := ""
trailCategory := r.ExpandedOne("category")
if trailCategory != nil {
category = trailCategory.GetString("name")
}
bounds := getStoredBounds(r)
domain := ""
if !author.GetBool("is_local") {
domain = author.GetString("domain")
}
diagonal := r.GetFloat("bounding_box_diagonal")
if diagonal == 0 && (bounds[0] != bounds[1] || bounds[2] != bounds[3]) {
diagonal = HaversineDistance(bounds[0], bounds[2], bounds[1], bounds[3])
}
document := map[string]any{
"id": r.Id,
"author": author.Id,
"author_name": author.GetString("preferred_username"),
"author_avatar": author.GetString("icon"),
"name": r.GetString("name"),
"description": r.GetString("description"),
"location": r.GetString("location"),
"distance": r.GetFloat("distance"),
"elevation_gain": r.GetFloat("elevation_gain"),
"elevation_loss": r.GetFloat("elevation_loss"),
"duration": r.GetFloat("duration"),
"difficulty": difficultyToNumber(r.GetString("difficulty")),
"category": category,
"completed": r.GetBool("completed"),
"date": r.GetDateTime("date").Time().Unix(),
"created": r.GetDateTime("created").Time().Unix(),
"public": r.GetBool("public"),
"thumbnail": thumbnail,
"gpx": r.GetString("gpx"),
"tags": tags,
"polyline": r.GetString("polyline"),
"domain": domain,
"iri": r.GetString("iri"),
"min_lat": bounds[0],
"max_lat": bounds[1],
"min_lon": bounds[2],
"max_lon": bounds[3],
"bounding_box_diagonal": diagonal,
"_geo": map[string]float64{
"lat": r.GetFloat("lat"),
"lng": r.GetFloat("lon"),
},
}
if includeShares {
trailShares := r.ExpandedAll("trail_share_via_trail")
if trailShares != nil {
sharedIDs := make([]string, len(trailShares))
for i, v := range trailShares {
sharedIDs[i] = v.GetString("actor")
}
document["shares"] = sharedIDs
} else {
document["shares"] = []string{}
}
trailLikes := r.ExpandedAll("trail_like_via_trail")
if trailLikes != nil {
likeIDs := make([]string, len(trailLikes))
for i, v := range trailLikes {
likeIDs[i] = v.GetString("actor")
}
document["likes"] = likeIDs
document["like_count"] = len(trailLikes)
} else {
document["likes"] = []string{}
document["like_count"] = 0
}
}
return document, nil
}
func difficultyToNumber(difficulty string) int32 {
switch difficulty {
case "easy":
return 0
case "moderate":
return 1
case "difficult":
return 2
}
return 0
}
func getStoredBounds(r *core.Record) [4]float64 {
lat := r.GetFloat("lat")
lon := r.GetFloat("lon")
defaultBounds := [4]float64{lat, lat, lon, lon}
minLat := r.GetFloat("min_lat")
maxLat := r.GetFloat("max_lat")
minLon := r.GetFloat("min_lon")
maxLon := r.GetFloat("max_lon")
if minLat == 0 && maxLat == 0 && minLon == 0 && maxLon == 0 && (lat != 0 || lon != 0) {
return defaultBounds
}
return [4]float64{minLat, maxLat, minLon, maxLon}
}
func documentFromListRecord(r *core.Record, author *core.Record, includeShares bool) (map[string]any, error) {
totalElevationGain := 0.0
totalElevationLoss := 0.0
totalDistance := 0.0
totalDuration := 0.0
trails := len(r.GetStringSlice("trails"))
if r.GetString("iri") != "" && !author.GetBool("is_local") {
doc, err := documentFromRemoteRecord(r, "lists")
if err == nil {
totalElevationGain = doc["elevation_gain"].(float64)
totalElevationLoss = doc["elevation_loss"].(float64)
totalDistance = doc["distance"].(float64)
totalDuration = doc["duration"].(float64)
trails = int(doc["trails"].(float64))
}
} else {
allTrails := r.ExpandedAll("trails")
for _, t := range allTrails {
totalElevationGain += t.GetFloat("elevation_gain")
totalElevationLoss += t.GetFloat("elevation_loss")
totalDistance += t.GetFloat("distance")
totalDuration += t.GetFloat("duration")
}
}
domain := ""
if !author.GetBool("is_local") {
domain = author.GetString("domain")
}
document := map[string]any{
"id": r.Id,
"author": author.Id,
"author_name": author.GetString("preferred_username"),
"author_avatar": author.GetString("icon"),
"avatar": r.GetString("avatar"),
"name": r.GetString("name"),
"description": r.GetString("description"),
"elevation_gain": totalElevationGain,
"elevation_loss": totalElevationLoss,
"distance": totalDistance,
"duration": totalDuration,
"domain": domain,
"public": r.GetBool("public"),
"created": r.GetDateTime("created").Time().Unix(),
"trails": trails,
"iri": r.GetString("iri"),
}
if includeShares {
listShares := r.ExpandedAll("list_share_via_list")
if listShares != nil {
sharedIDs := make([]string, len(listShares))
for i, v := range listShares {
sharedIDs[i] = v.GetString("actor")
}
document["shares"] = sharedIDs
} else {
document["shares"] = []string{}
}
}
return document, nil
}
func documentFromActorRecord(r *core.Record) (map[string]any, error) {
document := map[string]any{
"id": r.Id,
"username": r.GetString("username"),
"preferred_username": r.GetString("preferred_username"),
"domain": r.GetString("domain"),
"iri": r.GetString("iri"),
"icon": r.GetString("icon"),
"is_local": r.GetBool("is_local"),
}
return document, nil
}
func documentFromRemoteRecord(r *core.Record, index string) (map[string]any, error) {
client := &http.Client{}
if r.GetString("iri") == "" {
return nil, fmt.Errorf("record has no iri")
}
iri := r.GetString("iri")
url, err := url.Parse(iri)
if err != nil {
return nil, err
}
remoteRecordId := path.Base(url.Path)
searchURL := fmt.Sprintf("%s://%s/api/v1/search/%s", url.Scheme, url.Host, index)
body := []byte(fmt.Sprintf(`{"q": "%s"}`, remoteRecordId))
req, err := http.NewRequest("POST", searchURL, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to fetch remote record: received status %d", resp.StatusCode)
}
respBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var searchResponse meilisearch.SearchResponse
json.Unmarshal(respBytes, &searchResponse)
if len(searchResponse.Hits) == 0 {
return nil, fmt.Errorf("no documents in result set")
}
var document map[string]any
documentByteData, err := json.Marshal(searchResponse.Hits[0])
if err != nil {
return nil, err
}
if err := json.Unmarshal(documentByteData, &document); err != nil {
return nil, err
}
return document, nil
}
func IndexTrails(app core.App, trails []*core.Record, client meilisearch.ServiceManager) error {
documents := make([]map[string]any, len(trails))
for i, r := range trails {
errs := app.ExpandRecord(r, []string{"tags"}, nil)
if len(errs) > 0 {
return fmt.Errorf("failed to expand tags: %v", errs)
}
errs = app.ExpandRecord(r, []string{"category"}, nil)
if len(errs) > 0 {
return fmt.Errorf("failed to expand category: %v", errs)
}
errs = app.ExpandRecord(r, []string{"trail_share_via_trail"}, nil)
if len(errs) > 0 {
return fmt.Errorf("failed to expand trail_share_via_trail: %v", errs)
}
errs = app.ExpandRecord(r, []string{"trail_like_via_trail"}, nil)
if len(errs) > 0 {
return fmt.Errorf("failed to expand trail_like_via_trail: %v", errs)
}
errs = app.ExpandRecord(r, []string{"author"}, nil)
if len(errs) > 0 {
return fmt.Errorf("failed to expand author: %v", errs)
}
author := r.ExpandedOne("author")
doc, err := documentFromTrailRecord(r, author, true)
if err != nil {
return err
}
documents[i] = doc
}
if _, err := client.Index("trails").AddDocuments(documents, nil); err != nil {
return err
}
return nil
}
func UpdateTrail(app core.App, r *core.Record, author *core.Record, client meilisearch.ServiceManager) error {
errs := app.ExpandRecord(r, []string{"tags"}, nil)
if len(errs) > 0 {
return fmt.Errorf("meilisearch update trail: failed to expand tags: %v", errs)
}
errs = app.ExpandRecord(r, []string{"category"}, nil)
if len(errs) > 0 {
return fmt.Errorf("meilisearch update trail: failed to expand category: %v", errs)
}
doc, err := documentFromTrailRecord(r, author, false)
if err != nil {
return err
}
documents := []map[string]interface{}{doc}
if _, err = client.Index("trails").UpdateDocuments(documents, nil); err != nil {
return err
}
return nil
}
func UpdateTrailShares(trailId string, shares []string, client meilisearch.ServiceManager) error {
documents := []map[string]interface{}{
{
"id": trailId,
"shares": shares,
},
}
if _, err := client.Index("trails").UpdateDocuments(documents, nil); err != nil {
return err
}
return nil
}
func UpdateTrailLikes(trailId string, likes []string, client meilisearch.ServiceManager) error {
documents := []map[string]interface{}{
{
"id": trailId,
"like_count": len(likes),
"likes": likes,
},
}
if _, err := client.Index("trails").UpdateDocuments(documents, nil); err != nil {
return err
}
return nil
}
func IndexLists(app core.App, lists []*core.Record, client meilisearch.ServiceManager) error {
documents := make([]map[string]any, len(lists))
for i, r := range lists {
errs := app.ExpandRecord(r, []string{"trails"}, nil)
if len(errs) > 0 {
return fmt.Errorf("failed to expand trails: %v", errs)
}
errs = app.ExpandRecord(r, []string{"list_share_via_list"}, nil)
if len(errs) > 0 {
return fmt.Errorf("failed to expand list_share_via_list: %v", errs)
}
errs = app.ExpandRecord(r, []string{"author"}, nil)
if len(errs) > 0 {
return fmt.Errorf("failed to expand author: %v", errs)
}
author := r.ExpandedOne("author")
doc, err := documentFromListRecord(r, author, true)
if err != nil {
return err
}
documents[i] = doc
}
if _, err := client.Index("lists").AddDocuments(documents, nil); err != nil {
return err
}
return nil
}
func UpdateList(app core.App, r *core.Record, author *core.Record, client meilisearch.ServiceManager) error {
errs := app.ExpandRecord(r, []string{"trails"}, nil)
if len(errs) > 0 {
return fmt.Errorf("failed to expand trails: %v", errs)
}
documents, err := documentFromListRecord(r, author, false)
if err != nil {
return err
}
if _, err = client.Index("lists").UpdateDocuments(documents, nil); err != nil {
return err
}
return nil
}
func IndexActors(actors []*core.Record, client meilisearch.ServiceManager) error {
documents := make([]map[string]any, len(actors))
for i, r := range actors {
doc, err := documentFromActorRecord(r)
if err != nil {
return err
}
documents[i] = doc
}
if _, err := client.Index("actors").AddDocuments(documents, nil); err != nil {
return err
}
return nil
}
func UpdateActor(r *core.Record, client meilisearch.ServiceManager) error {
documents, err := documentFromActorRecord(r)
if err != nil {
return err
}
if _, err = client.Index("actors").UpdateDocuments(documents, nil); err != nil {
return err
}
return nil
}
func UpdateListShares(listId string, shares []string, client meilisearch.ServiceManager) error {
documents := []map[string]interface{}{
{
"id": listId,
"shares": shares,
},
}
if _, err := client.Index("lists").UpdateDocuments(documents, nil); err != nil {
return err
}
return nil
}
func GenerateMeilisearchToken(rules map[string]interface{}, client meilisearch.ServiceManager) (string, error) {
var apiKeyUid string
var apiKey string
keys, err := client.GetKeys(&meilisearch.KeysQuery{Limit: 20})
if err != nil {
return "", fmt.Errorf("meilisearch connection error: %w", err)
}
for _, k := range keys.Results {
for _, action := range k.Actions {
if action == "search" || k.Name == "Default Search API Key" {
apiKeyUid = k.UID
apiKey = k.Key
break
}
}
if apiKey != "" {
break
}
}
if apiKey == "" || apiKeyUid == "" {
return "", errors.New("unable to locate a valid search API key")
}
expiresAt := time.Now().Add(24 * time.Hour)
options := &meilisearch.TenantTokenOptions{
APIKey: apiKey,
ExpiresAt: expiresAt,
}
return client.GenerateTenantToken(apiKeyUid, rules, options)
}