From b261ada15cf9260c66ccaca8331146bed9f8d12b Mon Sep 17 00:00:00 2001 From: Christian Beutel <> Date: Wed, 19 Mar 2025 22:30:09 +0100 Subject: [PATCH] adds tags --- db/main.go | 6 +- db/migrations/1742167033_init_meilisearch.go | 2 +- db/migrations/1742409454_created_tags.go | 89 ++++++++++ db/migrations/1742411241_updated_tags.go | 44 +++++ db/migrations/1742411270_updated_tags.go | 42 +++++ db/migrations/1742412356_updated_trails.go | 44 +++++ db/util/meilisearch.go | 23 ++- web/src/lib/components/base/chip.svelte | 30 ++++ web/src/lib/components/base/combobox.svelte | 168 +++++++++++++++--- .../lib/components/base/multi_select.svelte | 18 +- .../lib/components/trail/trail_card.svelte | 8 + .../trail/trail_filter_panel.svelte | 30 ++++ .../components/trail/trail_info_panel.svelte | 21 ++- .../components/trail/trail_list_item.svelte | 8 + web/src/lib/i18n/locales/de.json | 2 + web/src/lib/i18n/locales/en.json | 2 + web/src/lib/i18n/locales/es.json | 2 + web/src/lib/i18n/locales/fr.json | 2 + web/src/lib/i18n/locales/hu.json | 2 + web/src/lib/i18n/locales/it.json | 2 + web/src/lib/i18n/locales/nl.json | 2 + web/src/lib/i18n/locales/pl.json | 2 + web/src/lib/i18n/locales/pt.json | 2 + web/src/lib/i18n/locales/zh.json | 2 + web/src/lib/models/api/tag_schema.ts | 15 ++ web/src/lib/models/api/trail_schema.ts | 2 + web/src/lib/models/tag.ts | 21 +++ web/src/lib/models/trail.ts | 8 +- web/src/lib/stores/tag_store.ts | 42 +++++ web/src/lib/stores/trail_store.ts | 39 +++- web/src/lib/util/api_util.ts | 1 + web/src/routes/api/v1/tag/+server.ts | 23 +++ web/src/routes/api/v1/tag/[id]/+server.ts | 31 ++++ web/src/routes/map/+page.ts | 1 + web/src/routes/profile/[id]/trails/+page.ts | 1 + web/src/routes/trail/edit/[id]/+page.svelte | 63 ++++++- web/src/routes/trails/+page.ts | 1 + 37 files changed, 728 insertions(+), 73 deletions(-) create mode 100644 db/migrations/1742409454_created_tags.go create mode 100644 db/migrations/1742411241_updated_tags.go create mode 100644 db/migrations/1742411270_updated_tags.go create mode 100644 db/migrations/1742412356_updated_trails.go create mode 100644 web/src/lib/components/base/chip.svelte create mode 100644 web/src/lib/models/api/tag_schema.ts create mode 100644 web/src/lib/models/tag.ts create mode 100644 web/src/lib/stores/tag_store.ts create mode 100644 web/src/routes/api/v1/tag/+server.ts create mode 100644 web/src/routes/api/v1/tag/[id]/+server.ts diff --git a/db/main.go b/db/main.go index 46183266..87e47be6 100644 --- a/db/main.go +++ b/db/main.go @@ -130,7 +130,7 @@ func createTrailHandler(client meilisearch.ServiceManager) func(e *core.RecordEv if err != nil { return err } - if err := util.IndexTrail(record, author, client); err != nil { + if err := util.IndexTrail(e.App, record, author, client); err != nil { return err } @@ -160,7 +160,7 @@ func updateTrailHandler(client meilisearch.ServiceManager) func(e *core.RecordEv if err != nil { return err } - err = util.UpdateTrail(record, author, client) + err = util.UpdateTrail(e.App, record, author, client) if err != nil { return err } @@ -789,7 +789,7 @@ func bootstrapMeilisearchTrails(app core.App, client meilisearch.ServiceManager) if err != nil { return err } - if err := util.IndexTrail(trail, author, client); err != nil { + if err := util.IndexTrail(app, trail, author, client); err != nil { return err } diff --git a/db/migrations/1742167033_init_meilisearch.go b/db/migrations/1742167033_init_meilisearch.go index 6229687b..3e75d45c 100644 --- a/db/migrations/1742167033_init_meilisearch.go +++ b/db/migrations/1742167033_init_meilisearch.go @@ -29,7 +29,7 @@ func init() { } _, err = client.Index("trails").UpdateFilterableAttributes(&[]string{ - "_geo", "author", "category", "completed", "date", "difficulty", "distance", "elevation_gain", "elevation_loss", "public", "shares", + "_geo", "author", "category", "completed", "date", "difficulty", "distance", "elevation_gain", "elevation_loss", "public", "shares", "tags", }) if err != nil { diff --git a/db/migrations/1742409454_created_tags.go b/db/migrations/1742409454_created_tags.go new file mode 100644 index 00000000..a40e319e --- /dev/null +++ b/db/migrations/1742409454_created_tags.go @@ -0,0 +1,89 @@ +package migrations + +import ( + "encoding/json" + + "github.com/pocketbase/pocketbase/core" + m "github.com/pocketbase/pocketbase/migrations" +) + +func init() { + m.Register(func(app core.App) error { + jsonData := `{ + "createRule": null, + "deleteRule": null, + "fields": [ + { + "autogeneratePattern": "[a-z0-9]{15}", + "hidden": false, + "id": "text3208210256", + "max": 15, + "min": 15, + "name": "id", + "pattern": "^[a-z0-9]+$", + "presentable": false, + "primaryKey": true, + "required": true, + "system": true, + "type": "text" + }, + { + "autogeneratePattern": "", + "hidden": false, + "id": "text1579384326", + "max": 0, + "min": 0, + "name": "name", + "pattern": "", + "presentable": false, + "primaryKey": false, + "required": false, + "system": false, + "type": "text" + }, + { + "hidden": false, + "id": "autodate2990389176", + "name": "created", + "onCreate": true, + "onUpdate": false, + "presentable": false, + "system": false, + "type": "autodate" + }, + { + "hidden": false, + "id": "autodate3332085495", + "name": "updated", + "onCreate": true, + "onUpdate": true, + "presentable": false, + "system": false, + "type": "autodate" + } + ], + "id": "pbc_1219621782", + "indexes": [], + "listRule": null, + "name": "tags", + "system": false, + "type": "base", + "updateRule": null, + "viewRule": null + }` + + collection := &core.Collection{} + if err := json.Unmarshal([]byte(jsonData), &collection); err != nil { + return err + } + + return app.Save(collection) + }, func(app core.App) error { + collection, err := app.FindCollectionByNameOrId("pbc_1219621782") + if err != nil { + return err + } + + return app.Delete(collection) + }) +} diff --git a/db/migrations/1742411241_updated_tags.go b/db/migrations/1742411241_updated_tags.go new file mode 100644 index 00000000..b9ce1f42 --- /dev/null +++ b/db/migrations/1742411241_updated_tags.go @@ -0,0 +1,44 @@ +package migrations + +import ( + "encoding/json" + + "github.com/pocketbase/pocketbase/core" + m "github.com/pocketbase/pocketbase/migrations" +) + +func init() { + m.Register(func(app core.App) error { + collection, err := app.FindCollectionByNameOrId("pbc_1219621782") + if err != nil { + return err + } + + // update collection data + if err := json.Unmarshal([]byte(`{ + "createRule": "@request.auth.id != \"\"", + "listRule": "", + "viewRule": "" + }`), &collection); err != nil { + return err + } + + return app.Save(collection) + }, func(app core.App) error { + collection, err := app.FindCollectionByNameOrId("pbc_1219621782") + if err != nil { + return err + } + + // update collection data + if err := json.Unmarshal([]byte(`{ + "createRule": null, + "listRule": null, + "viewRule": null + }`), &collection); err != nil { + return err + } + + return app.Save(collection) + }) +} diff --git a/db/migrations/1742411270_updated_tags.go b/db/migrations/1742411270_updated_tags.go new file mode 100644 index 00000000..e11b9113 --- /dev/null +++ b/db/migrations/1742411270_updated_tags.go @@ -0,0 +1,42 @@ +package migrations + +import ( + "encoding/json" + + "github.com/pocketbase/pocketbase/core" + m "github.com/pocketbase/pocketbase/migrations" +) + +func init() { + m.Register(func(app core.App) error { + collection, err := app.FindCollectionByNameOrId("pbc_1219621782") + if err != nil { + return err + } + + // update collection data + if err := json.Unmarshal([]byte(`{ + "listRule": "@request.auth.id != \"\"", + "viewRule": "@request.auth.id != \"\"" + }`), &collection); err != nil { + return err + } + + return app.Save(collection) + }, func(app core.App) error { + collection, err := app.FindCollectionByNameOrId("pbc_1219621782") + if err != nil { + return err + } + + // update collection data + if err := json.Unmarshal([]byte(`{ + "listRule": "", + "viewRule": "" + }`), &collection); err != nil { + return err + } + + return app.Save(collection) + }) +} diff --git a/db/migrations/1742412356_updated_trails.go b/db/migrations/1742412356_updated_trails.go new file mode 100644 index 00000000..72d87e9f --- /dev/null +++ b/db/migrations/1742412356_updated_trails.go @@ -0,0 +1,44 @@ +package migrations + +import ( + "github.com/pocketbase/pocketbase/core" + m "github.com/pocketbase/pocketbase/migrations" +) + +func init() { + m.Register(func(app core.App) error { + collection, err := app.FindCollectionByNameOrId("e864strfxo14pm4") + if err != nil { + return err + } + + // add field + if err := collection.Fields.AddMarshaledJSONAt(14, []byte(`{ + "cascadeDelete": false, + "collectionId": "pbc_1219621782", + "hidden": false, + "id": "relation1874629670", + "maxSelect": 999, + "minSelect": 0, + "name": "tags", + "presentable": false, + "required": false, + "system": false, + "type": "relation" + }`)); err != nil { + return err + } + + return app.Save(collection) + }, func(app core.App) error { + collection, err := app.FindCollectionByNameOrId("e864strfxo14pm4") + if err != nil { + return err + } + + // remove field + collection.Fields.RemoveById("relation1874629670") + + return app.Save(collection) + }) +} diff --git a/db/util/meilisearch.go b/db/util/meilisearch.go index 731f6c3d..658840dc 100644 --- a/db/util/meilisearch.go +++ b/db/util/meilisearch.go @@ -2,6 +2,7 @@ package util import ( "errors" + "fmt" "log" "github.com/meilisearch/meilisearch-go" @@ -19,6 +20,13 @@ func documentFromTrailRecord(r *core.Record, author *core.Record, includeShares thumbnail = photos[thumbnailIndex] } + tagRecords := r.ExpandedAll("tags") + tags := make([]string, len(tagRecords)) + + for i, v := range tagRecords { + tags[i] = v.GetString("name") + } + document := map[string]interface{}{ "id": r.Id, "author": r.GetString("author"), @@ -39,6 +47,7 @@ func documentFromTrailRecord(r *core.Record, author *core.Record, includeShares "public": r.GetBool("public"), "thumbnail": thumbnail, "gpx": r.GetString("gpx"), + "tags": tags, "_geo": map[string]float64{ "lat": r.GetFloat("lat"), "lng": r.GetFloat("lon"), @@ -70,7 +79,12 @@ func documentFromListRecord(r *core.Record, includeShares bool) map[string]inter return document } -func IndexTrail(r *core.Record, author *core.Record, client meilisearch.ServiceManager) error { +func IndexTrail(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("failed to expand: %v", errs) + } + documents := []map[string]interface{}{documentFromTrailRecord(r, author, true)} if _, err := client.Index("trails").AddDocuments(documents); err != nil { @@ -80,7 +94,12 @@ func IndexTrail(r *core.Record, author *core.Record, client meilisearch.ServiceM return nil } -func UpdateTrail(r *core.Record, author *core.Record, client meilisearch.ServiceManager) error { +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("failed to expand: %v", errs) + } + documents := documentFromTrailRecord(r, author, false) if _, err := client.Index("trails").UpdateDocuments(documents); err != nil { diff --git a/web/src/lib/components/base/chip.svelte b/web/src/lib/components/base/chip.svelte new file mode 100644 index 00000000..d6e67c66 --- /dev/null +++ b/web/src/lib/components/base/chip.svelte @@ -0,0 +1,30 @@ + + +
{item.text}
{/each} diff --git a/web/src/lib/components/base/multi_select.svelte b/web/src/lib/components/base/multi_select.svelte index 96b46d6a..1783a2b4 100644 --- a/web/src/lib/components/base/multi_select.svelte +++ b/web/src/lib/components/base/multi_select.svelte @@ -1,6 +1,7 @@+
{$_("by")}
{/if}
-