diff --git a/db/migrations/1731157565_updated_trails_filter.go b/db/migrations/1731157565_updated_trails_filter.go new file mode 100644 index 00000000..e5fc58be --- /dev/null +++ b/db/migrations/1731157565_updated_trails_filter.go @@ -0,0 +1,342 @@ +package migrations + +import ( + "encoding/json" + + "github.com/pocketbase/dbx" + "github.com/pocketbase/pocketbase/daos" + m "github.com/pocketbase/pocketbase/migrations" + "github.com/pocketbase/pocketbase/models/schema" +) + +func init() { + m.Register(func(db dbx.Builder) error { + dao := daos.New(db); + + collection, err := dao.FindCollectionByNameOrId("4wbv9tz5zjdrjh1") + if err != nil { + return err + } + + options := map[string]any{} + if err := json.Unmarshal([]byte(`{ + "query": "SELECT users.id, COALESCE(MAX(trails.distance), 0) AS max_distance,\n COALESCE(MAX(trails.elevation_gain), 0) AS max_elevation_gain, \n COALESCE(MAX(trails.elevation_loss), 0) AS max_elevation_loss, \n COALESCE(MAX(trails.duration), 0) AS max_duration, \n COALESCE(MIN(trails.distance), 0) AS min_distance, \n COALESCE(MIN(trails.elevation_gain), 0) AS min_elevation_gain, \n COALESCE(MIN(trails.elevation_loss), 0) AS min_elevation_loss, \n COALESCE(MIN(trails.duration), 0) AS min_duration \nFROM users \n LEFT JOIN trails ON \n users.id = trails.author OR \n trails.public = 1 OR \n EXISTS (\n SELECT 1 \n FROM trail_share \n WHERE trail_share.trail = trails.id \n AND trail_share.user = users.id\n ) GROUP BY users.id;" + }`), &options); err != nil { + return err + } + collection.SetOptions(options) + + // remove + collection.Schema.RemoveField("ixfa8u8m") + + // remove + collection.Schema.RemoveField("bhn0jj40") + + // remove + collection.Schema.RemoveField("3kujiwzh") + + // remove + collection.Schema.RemoveField("yf3zwzn4") + + // remove + collection.Schema.RemoveField("skvtkith") + + // remove + collection.Schema.RemoveField("gydvo1ug") + + // add + new_max_distance := &schema.SchemaField{} + if err := json.Unmarshal([]byte(`{ + "system": false, + "id": "oyvx8oaq", + "name": "max_distance", + "type": "json", + "required": false, + "presentable": false, + "unique": false, + "options": { + "maxSize": 1 + } + }`), new_max_distance); err != nil { + return err + } + collection.Schema.AddField(new_max_distance) + + // add + new_max_elevation_gain := &schema.SchemaField{} + if err := json.Unmarshal([]byte(`{ + "system": false, + "id": "4xu7voeh", + "name": "max_elevation_gain", + "type": "json", + "required": false, + "presentable": false, + "unique": false, + "options": { + "maxSize": 1 + } + }`), new_max_elevation_gain); err != nil { + return err + } + collection.Schema.AddField(new_max_elevation_gain) + + // add + new_max_elevation_loss := &schema.SchemaField{} + if err := json.Unmarshal([]byte(`{ + "system": false, + "id": "sqzilovv", + "name": "max_elevation_loss", + "type": "json", + "required": false, + "presentable": false, + "unique": false, + "options": { + "maxSize": 1 + } + }`), new_max_elevation_loss); err != nil { + return err + } + collection.Schema.AddField(new_max_elevation_loss) + + // add + new_max_duration := &schema.SchemaField{} + if err := json.Unmarshal([]byte(`{ + "system": false, + "id": "rupidtew", + "name": "max_duration", + "type": "json", + "required": false, + "presentable": false, + "unique": false, + "options": { + "maxSize": 1 + } + }`), new_max_duration); err != nil { + return err + } + collection.Schema.AddField(new_max_duration) + + // add + new_min_distance := &schema.SchemaField{} + if err := json.Unmarshal([]byte(`{ + "system": false, + "id": "2y7dbhf9", + "name": "min_distance", + "type": "json", + "required": false, + "presentable": false, + "unique": false, + "options": { + "maxSize": 1 + } + }`), new_min_distance); err != nil { + return err + } + collection.Schema.AddField(new_min_distance) + + // add + new_min_elevation_gain := &schema.SchemaField{} + if err := json.Unmarshal([]byte(`{ + "system": false, + "id": "ooiwexpc", + "name": "min_elevation_gain", + "type": "json", + "required": false, + "presentable": false, + "unique": false, + "options": { + "maxSize": 1 + } + }`), new_min_elevation_gain); err != nil { + return err + } + collection.Schema.AddField(new_min_elevation_gain) + + // add + new_min_elevation_loss := &schema.SchemaField{} + if err := json.Unmarshal([]byte(`{ + "system": false, + "id": "ondmfwce", + "name": "min_elevation_loss", + "type": "json", + "required": false, + "presentable": false, + "unique": false, + "options": { + "maxSize": 1 + } + }`), new_min_elevation_loss); err != nil { + return err + } + collection.Schema.AddField(new_min_elevation_loss) + + // add + new_min_duration := &schema.SchemaField{} + if err := json.Unmarshal([]byte(`{ + "system": false, + "id": "kcrhzmaw", + "name": "min_duration", + "type": "json", + "required": false, + "presentable": false, + "unique": false, + "options": { + "maxSize": 1 + } + }`), new_min_duration); err != nil { + return err + } + collection.Schema.AddField(new_min_duration) + + return dao.SaveCollection(collection) + }, func(db dbx.Builder) error { + dao := daos.New(db); + + collection, err := dao.FindCollectionByNameOrId("4wbv9tz5zjdrjh1") + if err != nil { + return err + } + + options := map[string]any{} + if err := json.Unmarshal([]byte(`{ + "query": "SELECT users.id, COALESCE(MAX(trails.distance), 0) AS max_distance, COALESCE(MAX(trails.elevation_gain), 0) AS max_elevation_gain, COALESCE(MAX(trails.duration), 0) AS max_duration, COALESCE(MIN(trails.distance), 0) AS min_distance, COALESCE(MIN(trails.elevation_gain), 0) AS min_elevation_gain, COALESCE(MIN(trails.duration), 0) AS min_duration FROM users LEFT JOIN trails ON users.id = trails.author OR trails.public = 1 OR EXISTS (\n SELECT 1 \n FROM trail_share \n WHERE trail_share.trail = trails.id \n AND trail_share.user = users.id\n ) GROUP BY users.id;" + }`), &options); err != nil { + return err + } + collection.SetOptions(options) + + // add + del_max_distance := &schema.SchemaField{} + if err := json.Unmarshal([]byte(`{ + "system": false, + "id": "ixfa8u8m", + "name": "max_distance", + "type": "json", + "required": false, + "presentable": false, + "unique": false, + "options": { + "maxSize": 1 + } + }`), del_max_distance); err != nil { + return err + } + collection.Schema.AddField(del_max_distance) + + // add + del_max_elevation_gain := &schema.SchemaField{} + if err := json.Unmarshal([]byte(`{ + "system": false, + "id": "bhn0jj40", + "name": "max_elevation_gain", + "type": "json", + "required": false, + "presentable": false, + "unique": false, + "options": { + "maxSize": 1 + } + }`), del_max_elevation_gain); err != nil { + return err + } + collection.Schema.AddField(del_max_elevation_gain) + + // add + del_max_duration := &schema.SchemaField{} + if err := json.Unmarshal([]byte(`{ + "system": false, + "id": "3kujiwzh", + "name": "max_duration", + "type": "json", + "required": false, + "presentable": false, + "unique": false, + "options": { + "maxSize": 1 + } + }`), del_max_duration); err != nil { + return err + } + collection.Schema.AddField(del_max_duration) + + // add + del_min_distance := &schema.SchemaField{} + if err := json.Unmarshal([]byte(`{ + "system": false, + "id": "yf3zwzn4", + "name": "min_distance", + "type": "json", + "required": false, + "presentable": false, + "unique": false, + "options": { + "maxSize": 1 + } + }`), del_min_distance); err != nil { + return err + } + collection.Schema.AddField(del_min_distance) + + // add + del_min_elevation_gain := &schema.SchemaField{} + if err := json.Unmarshal([]byte(`{ + "system": false, + "id": "skvtkith", + "name": "min_elevation_gain", + "type": "json", + "required": false, + "presentable": false, + "unique": false, + "options": { + "maxSize": 1 + } + }`), del_min_elevation_gain); err != nil { + return err + } + collection.Schema.AddField(del_min_elevation_gain) + + // add + del_min_duration := &schema.SchemaField{} + if err := json.Unmarshal([]byte(`{ + "system": false, + "id": "gydvo1ug", + "name": "min_duration", + "type": "json", + "required": false, + "presentable": false, + "unique": false, + "options": { + "maxSize": 1 + } + }`), del_min_duration); err != nil { + return err + } + collection.Schema.AddField(del_min_duration) + + // remove + collection.Schema.RemoveField("oyvx8oaq") + + // remove + collection.Schema.RemoveField("4xu7voeh") + + // remove + collection.Schema.RemoveField("sqzilovv") + + // remove + collection.Schema.RemoveField("rupidtew") + + // remove + collection.Schema.RemoveField("2y7dbhf9") + + // remove + collection.Schema.RemoveField("ooiwexpc") + + // remove + collection.Schema.RemoveField("ondmfwce") + + // remove + collection.Schema.RemoveField("kcrhzmaw") + + return dao.SaveCollection(collection) + }) +} diff --git a/db/migrations/1731158298_update_sortable_filterable_attributes.go b/db/migrations/1731158298_update_sortable_filterable_attributes.go new file mode 100644 index 00000000..52ab1775 --- /dev/null +++ b/db/migrations/1731158298_update_sortable_filterable_attributes.go @@ -0,0 +1,54 @@ +package migrations + +import ( + "os" + + "github.com/meilisearch/meilisearch-go" + "github.com/pocketbase/dbx" + + m "github.com/pocketbase/pocketbase/migrations" +) + +func init() { + client := meilisearch.NewClient(meilisearch.ClientConfig{ + Host: os.Getenv("MEILI_URL"), + APIKey: os.Getenv("MEILI_MASTER_KEY"), + }) + m.Register(func(db dbx.Builder) error { + _, err := client.Index("trails").UpdateSortableAttributes(&[]string{ + "created", "date", "difficulty", "distance", "elevation_gain", "elevation_loss", "name", + }) + + if err != nil { + return err + } + + _, err = client.Index("trails").UpdateFilterableAttributes(&[]string{ + "_geo", "author", "category", "completed", "date", "difficulty", "distance", "elevation_gain", "elevation_loss", "public", "shares", + }) + + if err != nil { + return err + } + + return nil + }, func(db dbx.Builder) error { + _, err := client.Index("trails").UpdateSortableAttributes(&[]string{ + "created", "date", "difficulty", "distance", "elevation_gain", "name", + }) + + if err != nil { + return err + } + + _, err = client.Index("trails").UpdateFilterableAttributes(&[]string{ + "_geo", "author", "category", "completed", "date", "difficulty", "distance", "elevation_gain", "public", "shares", + }) + + if err != nil { + return err + } + + return nil + }) +} diff --git a/db/util/meilisearch.go b/db/util/meilisearch.go index f5e6aaea..2e436ca4 100644 --- a/db/util/meilisearch.go +++ b/db/util/meilisearch.go @@ -17,6 +17,7 @@ func documentFromTrailRecord(r *models.Record, includeShares bool) map[string]in "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": r.Get("difficulty"), "category": r.Get("category"), diff --git a/docs/src/content/docs/getting-started/changelog.md b/docs/src/content/docs/getting-started/changelog.md index 6894f8b4..9337c9ab 100644 --- a/docs/src/content/docs/getting-started/changelog.md +++ b/docs/src/content/docs/getting-started/changelog.md @@ -3,6 +3,17 @@ title: Changelog description: What changed in the last patch? --- +## v0.10.1 +### Features +- Adds elevation loss to trails. Please note that trails created before this version will have a default elevation loss of 0. Edit & save to update. + +### Bug fixes +- Fixes bug that caused auto-added summit logs to not have distance, durtaion etc. +- Fixes error in the auto-upload feature +- Fixes access permissions for profile page +- A summit log is now also created automatically when you upload a trail directly +- Fixes link to the API documentation in footer + ## v0.10.0 ### Features - A new summit log entry is now added automatically when uploading a new GPX file for a new or existing trail diff --git a/web/cron.sh b/web/cron.sh index 7c67bc01..d312f949 100644 --- a/web/cron.sh +++ b/web/cron.sh @@ -29,7 +29,7 @@ login() { upload_and_delete() { local file="$1" - ls $file + ls "$file" # API call to upload file response=$(curl -b cookie.txt --location --request PUT "$API_URL/trail/upload" --header 'Content-Type: multipart/form-data' -F "file=@$file" -F "name=$base_name") diff --git a/web/src/lib/components/trail/trail_filter_panel.svelte b/web/src/lib/components/trail/trail_filter_panel.svelte index ed65393a..4084b603 100644 --- a/web/src/lib/components/trail/trail_filter_panel.svelte +++ b/web/src/lib/components/trail/trail_filter_panel.svelte @@ -239,17 +239,36 @@ >
+

{$_("elevation-loss")}

+ update()} + > +
+ {formatElevation(filter.elevationLossMin)} + {formatElevation( + filter.elevationLossMax, + )}{filter.elevationLossMax == filter.elevationLossLimit + ? "+" + : ""} +
+
diff --git a/web/src/lib/components/trail/trail_list.svelte b/web/src/lib/components/trail/trail_list.svelte index 6d470731..6de7d2f8 100644 --- a/web/src/lib/components/trail/trail_list.svelte +++ b/web/src/lib/components/trail/trail_list.svelte @@ -31,6 +31,7 @@ { text: $_("difficulty"), value: "difficulty" }, { text: $_("distance"), value: "distance" }, { text: $_("elevation-gain"), value: "elevation_gain" }, + { text: $_("elevation-loss"), value: "elevation_loss" }, ]; onMount(() => { diff --git a/web/src/lib/models/trail.ts b/web/src/lib/models/trail.ts index 3c8ccf65..f735bd1f 100644 --- a/web/src/lib/models/trail.ts +++ b/web/src/lib/models/trail.ts @@ -108,6 +108,9 @@ interface TrailFilter { elevationGainMin: number; elevationGainMax: number; elevationGainLimit: number; + elevationLossMin: number; + elevationLossMax: number; + elevationLossLimit: number; startDate?: string; endDate?: string; completed?: boolean; @@ -120,6 +123,8 @@ interface TrailFilterValues { max_distance: number, min_elevation_gain: number, max_elevation_gain: number, + min_elevation_loss: number, + max_elevation_loss: number, min_durtation: number, max_duration: number, } diff --git a/web/src/lib/stores/trail_store.ts b/web/src/lib/stores/trail_store.ts index 1164bb3f..c2f235bc 100644 --- a/web/src/lib/stores/trail_store.ts +++ b/web/src/lib/stores/trail_store.ts @@ -153,7 +153,7 @@ export async function trails_show(id: string, loadGPX?: boolean, f: (url: Reques } response.expand.gpx_data = gpxData; - + for (const log of response.expand.summit_logs ?? []) { const gpxData: string = await fetchGPX(log, f); @@ -387,7 +387,7 @@ export async function fetchGPX(trail: { gpx?: string } & Record, f: function buildFilterText(filter: TrailFilter, includeGeo: boolean): string { let filterText: string = ""; - filterText += `distance >= ${Math.floor(filter.distanceMin)} AND elevation_gain >= ${Math.floor(filter.elevationGainMin)}` + filterText += `distance >= ${Math.floor(filter.distanceMin)} AND elevation_gain >= ${Math.floor(filter.elevationGainMin)} AND elevation_loss >= ${Math.floor(filter.elevationLossMin)}` if (filter.distanceMax < filter.distanceLimit) { filterText += ` AND distance <= ${Math.ceil(filter.distanceMax)}` @@ -397,6 +397,10 @@ function buildFilterText(filter: TrailFilter, includeGeo: boolean): string { filterText += ` AND elevation_gain <= ${Math.ceil(filter.elevationGainMax)}` } + if (filter.elevationLossMax < filter.elevationLossLimit) { + filterText += ` AND elevation_loss <= ${Math.ceil(filter.elevationLossMax)}` + } + filterText += ` AND difficulty IN [${filter.difficulty.join(",")}]` if (filter.startDate) { diff --git a/web/src/routes/map/+page.ts b/web/src/routes/map/+page.ts index 5c8d81dc..324d8ef1 100644 --- a/web/src/routes/map/+page.ts +++ b/web/src/routes/map/+page.ts @@ -20,6 +20,9 @@ export const load: ServerLoad = async ({ params, locals, fetch }) => { elevationGainMin: 0, elevationGainMax: filterValues.max_elevation_gain, elevationGainLimit: filterValues.max_elevation_gain, + elevationLossMin: 0, + elevationLossMax: filterValues.max_elevation_loss, + elevationLossLimit: filterValues.max_elevation_gain, sort: "created", sortOrder: "+", }; diff --git a/web/src/routes/trails/+page.server.ts b/web/src/routes/trails/+page.server.ts index 1163c383..23b3d81b 100644 --- a/web/src/routes/trails/+page.server.ts +++ b/web/src/routes/trails/+page.server.ts @@ -19,6 +19,9 @@ export const load: ServerLoad = async ({ params, locals, url, fetch }) => { elevationGainMin: 0, elevationGainMax: filterValues.max_elevation_gain, elevationGainLimit: filterValues.max_elevation_gain, + elevationLossMin: 0, + elevationLossMax: filterValues.max_elevation_loss, + elevationLossLimit: filterValues.max_elevation_loss, sort: "created", sortOrder: "+", };