mirror of
https://github.com/Flomp/wanderer.git
synced 2025-12-17 02:34:25 -06:00
reverses waypoint relation
This commit is contained in:
@@ -205,27 +205,27 @@ func syncTrailWithTours(app core.App, k *KomootApi, i KomootIntegration, user st
|
||||
app.Logger().Warn(fmt.Sprintf("Unable to generate GPX for tour '%s': %v", tour.Name, err))
|
||||
continue
|
||||
}
|
||||
wpIds, err := createWaypointsFromTour(app, detailedTour, user)
|
||||
if err != nil {
|
||||
app.Logger().Warn(fmt.Sprintf("Unable to create waypoints for tour '%s': %v", tour.Name, err))
|
||||
continue
|
||||
}
|
||||
err = createTrailFromTour(app, k, detailedTour, gpx, actor, wpIds)
|
||||
trailid, err := createTrailFromTour(app, k, detailedTour, gpx, actor)
|
||||
if err != nil {
|
||||
app.Logger().Warn(fmt.Sprintf("Unable to create trail for tour '%s': %v", tour.Name, err))
|
||||
continue
|
||||
}
|
||||
err = createWaypointsFromTour(app, detailedTour, user, trailid)
|
||||
if err != nil {
|
||||
app.Logger().Warn(fmt.Sprintf("Unable to create waypoints for tour '%s': %v", tour.Name, err))
|
||||
continue
|
||||
}
|
||||
|
||||
}
|
||||
return hasNewTours, nil
|
||||
}
|
||||
|
||||
func createTrailFromTour(app core.App, k *KomootApi, detailedTour *DetailedKomootTour, gpx *filesystem.File, actor string, wpIds []string) error {
|
||||
func createTrailFromTour(app core.App, k *KomootApi, detailedTour *DetailedKomootTour, gpx *filesystem.File, actor string) (string, error) {
|
||||
trailid := security.RandomStringWithAlphabet(core.DefaultIdLength, core.DefaultIdAlphabet)
|
||||
|
||||
collection, err := app.FindCollectionByNameOrId("trails")
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
|
||||
record := core.NewRecord(collection)
|
||||
@@ -251,12 +251,12 @@ func createTrailFromTour(app core.App, k *KomootApi, detailedTour *DetailedKomoo
|
||||
if len(detailedTour.Embedded.CoverImages.Embedded.Items) > 0 {
|
||||
photos, err = fetchRoutePhotos(k, detailedTour)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
} else {
|
||||
photo, err := fetchPhoto(detailedTour.MapImage.Src, "", "")
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
photos = append(photos, photo)
|
||||
}
|
||||
@@ -281,7 +281,6 @@ func createTrailFromTour(app core.App, k *KomootApi, detailedTour *DetailedKomoo
|
||||
"lon": detailedTour.StartPoint.Lng,
|
||||
"difficulty": diffculty,
|
||||
"category": categoryId,
|
||||
"waypoints": wpIds,
|
||||
"author": actor,
|
||||
})
|
||||
|
||||
@@ -293,13 +292,13 @@ func createTrailFromTour(app core.App, k *KomootApi, detailedTour *DetailedKomoo
|
||||
}
|
||||
|
||||
if err := app.Save(record); err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
|
||||
if detailedTour.Type == "tour_recorded" {
|
||||
collection, err := app.FindCollectionByNameOrId("summit_logs")
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
|
||||
summitLogRecord := core.NewRecord(collection)
|
||||
@@ -313,25 +312,23 @@ func createTrailFromTour(app core.App, k *KomootApi, detailedTour *DetailedKomoo
|
||||
"trail": trailid,
|
||||
})
|
||||
if err := app.Save(summitLogRecord); err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return trailid, nil
|
||||
}
|
||||
|
||||
func createWaypointsFromTour(app core.App, tour *DetailedKomootTour, user string) ([]string, error) {
|
||||
func createWaypointsFromTour(app core.App, tour *DetailedKomootTour, user string, trailid string) error {
|
||||
collection, err := app.FindCollectionByNameOrId("waypoints")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
wpIds := make([]string, len(tour.Embedded.Timeline.Embedded.Items))
|
||||
|
||||
for i, wp := range tour.Embedded.Timeline.Embedded.Items {
|
||||
for _, wp := range tour.Embedded.Timeline.Embedded.Items {
|
||||
photos, err := fetchWaypointPhotos(wp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
record := core.NewRecord(collection)
|
||||
|
||||
@@ -358,6 +355,7 @@ func createWaypointsFromTour(app core.App, tour *DetailedKomootTour, user string
|
||||
"icon": "circle",
|
||||
"author": user,
|
||||
"distance_from_start": 0,
|
||||
"trail": trailid,
|
||||
})
|
||||
|
||||
if photos != nil {
|
||||
@@ -365,13 +363,11 @@ func createWaypointsFromTour(app core.App, tour *DetailedKomootTour, user string
|
||||
}
|
||||
|
||||
if err := app.Save(record); err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
wpIds[i] = record.Id
|
||||
}
|
||||
|
||||
return wpIds, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func fetchRoutePhotos(k *KomootApi, tour *DetailedKomootTour) ([]*filesystem.File, error) {
|
||||
|
||||
@@ -251,17 +251,16 @@ func syncTrailsWithRoutes(app core.App, accessToken string, user string, actor s
|
||||
app.Logger().Warn(fmt.Sprintf("Unable to fetch GPX for route '%s': %v", route.Name, err))
|
||||
continue
|
||||
}
|
||||
wpIds, err := createWaypointsFromRoute(app, route, user)
|
||||
if err != nil {
|
||||
app.Logger().Warn(fmt.Sprintf("Unable to create waypoints for route '%s': %v", route.Name, err))
|
||||
continue
|
||||
}
|
||||
err = createTrailFromRoute(app, route, gpx, actor, wpIds)
|
||||
trailid, err := createTrailFromRoute(app, route, gpx, actor)
|
||||
if err != nil {
|
||||
app.Logger().Warn(fmt.Sprintf("Unable to create trail for route '%s': %v", route.Name, err))
|
||||
continue
|
||||
}
|
||||
|
||||
err = createWaypointsFromRoute(app, route, user, trailid)
|
||||
if err != nil {
|
||||
app.Logger().Warn(fmt.Sprintf("Unable to create waypoints for route '%s': %v", route.Name, err))
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
return hasNewRoutes, nil
|
||||
@@ -305,10 +304,12 @@ func fetchRouteGPX(route StravaRoute, accessToken string) (*filesystem.File, err
|
||||
return gpxFile, nil
|
||||
}
|
||||
|
||||
func createTrailFromRoute(app core.App, route StravaRoute, gpx *filesystem.File, actor string, wpIds []string) error {
|
||||
func createTrailFromRoute(app core.App, route StravaRoute, gpx *filesystem.File, actor string) (string, error) {
|
||||
trailid := security.RandomStringWithAlphabet(core.DefaultIdLength, core.DefaultIdAlphabet)
|
||||
|
||||
collection, err := app.FindCollectionByNameOrId("trails")
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
|
||||
record := core.NewRecord(collection)
|
||||
@@ -337,6 +338,7 @@ func createTrailFromRoute(app core.App, route StravaRoute, gpx *filesystem.File,
|
||||
}
|
||||
|
||||
record.Load(map[string]any{
|
||||
"id": trailid,
|
||||
"name": route.Name,
|
||||
"description": route.Description,
|
||||
"public": !route.Private,
|
||||
@@ -348,7 +350,6 @@ func createTrailFromRoute(app core.App, route StravaRoute, gpx *filesystem.File,
|
||||
"external_id": route.IDStr,
|
||||
"lat": lat,
|
||||
"lon": lon,
|
||||
"waypoints": wpIds,
|
||||
"difficulty": "easy",
|
||||
"category": category,
|
||||
"author": actor,
|
||||
@@ -359,20 +360,18 @@ func createTrailFromRoute(app core.App, route StravaRoute, gpx *filesystem.File,
|
||||
}
|
||||
|
||||
if err := app.Save(record); err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
|
||||
return nil
|
||||
return trailid, err
|
||||
}
|
||||
|
||||
func createWaypointsFromRoute(app core.App, route StravaRoute, user string) ([]string, error) {
|
||||
func createWaypointsFromRoute(app core.App, route StravaRoute, user string, trailid string) error {
|
||||
collection, err := app.FindCollectionByNameOrId("waypoints")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
wpIds := make([]string, len(route.Waypoints))
|
||||
|
||||
for i, wp := range route.Waypoints {
|
||||
record := core.NewRecord(collection)
|
||||
|
||||
@@ -383,13 +382,15 @@ func createWaypointsFromRoute(app core.App, route StravaRoute, user string) ([]s
|
||||
record.Set("icon", "circle")
|
||||
record.Set("author", user)
|
||||
record.Set("distance_from_start", wp.DistanceIntoRoute)
|
||||
record.Set("trail", trailid)
|
||||
|
||||
app.Save(record)
|
||||
if err := app.Save(record); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
wpIds[i] = record.Id
|
||||
}
|
||||
|
||||
return wpIds, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func syncTrailsWithActivities(app core.App, accessToken string, user string, actor string, activities []StravaActivity) (bool, error) {
|
||||
|
||||
62
db/migrations/1757689107_updated_waypoints.go
Normal file
62
db/migrations/1757689107_updated_waypoints.go
Normal file
@@ -0,0 +1,62 @@
|
||||
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("goeo2ubp103rzp9")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// update collection data
|
||||
if err := json.Unmarshal([]byte(`{
|
||||
"listRule": "author = @request.auth.id || trail.author.user ?= @request.auth.id || trail.public ?= true || trail.trail_share_via_trail.actor.user ?= @request.auth.id\n|| \n(trail.trail_link_share_via_trail.token != \"\" && trail.trail_link_share_via_trail.token = @request.query.share)",
|
||||
"viewRule": "author = @request.auth.id || trail.author.user ?= @request.auth.id || trail.public ?= true || trail.trail_share_via_trail.actor.user ?= @request.auth.id\n|| \n(trail.trail_link_share_via_trail.token != \"\" && trail.trail_link_share_via_trail.token = @request.query.share)"
|
||||
}`), &collection); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// add field
|
||||
if err := collection.Fields.AddMarshaledJSONAt(9, []byte(`{
|
||||
"cascadeDelete": true,
|
||||
"collectionId": "e864strfxo14pm4",
|
||||
"hidden": false,
|
||||
"id": "relation2993194383",
|
||||
"maxSelect": 1,
|
||||
"minSelect": 0,
|
||||
"name": "trail",
|
||||
"presentable": false,
|
||||
"required": true,
|
||||
"system": false,
|
||||
"type": "relation"
|
||||
}`)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return app.Save(collection)
|
||||
}, func(app core.App) error {
|
||||
collection, err := app.FindCollectionByNameOrId("goeo2ubp103rzp9")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// update collection data
|
||||
if err := json.Unmarshal([]byte(`{
|
||||
"listRule": "author = @request.auth.id || trails_via_waypoints.author.user ?= @request.auth.id || trails_via_waypoints.public ?= true || \n(@collection.trail_share.trail.id ?= trails_via_waypoints.id && @collection.trail_share.actor.user ?= @request.auth.id)\n|| \n(@collection.trail_link_share.token != \"\" && @collection.trail_link_share.trail.waypoints.id ?= id)",
|
||||
"viewRule": "author = @request.auth.id || trails_via_waypoints.author.user ?= @request.auth.id || trails_via_waypoints.public ?= true || \n(@collection.trail_share.trail.id ?= trails_via_waypoints.id && @collection.trail_share.actor.user ?= @request.auth.id)\n|| \n(@collection.trail_link_share.token != \"\" && @collection.trail_link_share.trail.waypoints.id ?= id)"
|
||||
}`), &collection); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// remove field
|
||||
collection.Fields.RemoveById("relation2993194383")
|
||||
|
||||
return app.Save(collection)
|
||||
})
|
||||
}
|
||||
34
db/migrations/1757689185_migrate_waypoint_trails.go
Normal file
34
db/migrations/1757689185_migrate_waypoint_trails.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"github.com/pocketbase/dbx"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
m "github.com/pocketbase/pocketbase/migrations"
|
||||
)
|
||||
|
||||
func init() {
|
||||
m.Register(func(app core.App) error {
|
||||
wps, err := app.FindAllRecords("waypoints")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, wp := range wps {
|
||||
trail, err := app.FindFirstRecordByFilter("trails", "waypoints ?~ {:id}", dbx.Params{"id": wp.Id})
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
wp.Set("trail", trail.Id)
|
||||
err = app.UnsafeWithoutHooks().Save(wp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}, func(app core.App) error {
|
||||
// add down queries...
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
44
db/migrations/1757690946_updated_trails.go
Normal file
44
db/migrations/1757690946_updated_trails.go
Normal file
@@ -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
|
||||
}
|
||||
|
||||
// remove field
|
||||
collection.Fields.RemoveById("ppq2sist")
|
||||
|
||||
return app.Save(collection)
|
||||
}, func(app core.App) error {
|
||||
collection, err := app.FindCollectionByNameOrId("e864strfxo14pm4")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// add field
|
||||
if err := collection.Fields.AddMarshaledJSONAt(16, []byte(`{
|
||||
"cascadeDelete": false,
|
||||
"collectionId": "goeo2ubp103rzp9",
|
||||
"hidden": false,
|
||||
"id": "ppq2sist",
|
||||
"maxSelect": 2147483647,
|
||||
"minSelect": 0,
|
||||
"name": "waypoints",
|
||||
"presentable": false,
|
||||
"required": false,
|
||||
"system": false,
|
||||
"type": "relation"
|
||||
}`)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return app.Save(collection)
|
||||
})
|
||||
}
|
||||
@@ -658,7 +658,7 @@
|
||||
<MapWithElevationMaplibre
|
||||
trails={[trail]}
|
||||
activeTrail={0}
|
||||
waypoints={trail.expand?.waypoints}
|
||||
waypoints={trail.expand?.waypoints_via_trail}
|
||||
showElevation={true}
|
||||
elevationProfileContainer={"epc-container"}
|
||||
showStyleSwitcher={false}
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
<div class="">
|
||||
<p class="font-semibold">{$_("start")}</p>
|
||||
</div>
|
||||
{#each trail.expand?.waypoints ?? [] as wp, i}
|
||||
{#each trail.expand?.waypoints_via_trail ?? [] as wp, i}
|
||||
<div
|
||||
class="bg-background cursor-pointer"
|
||||
role="presentation"
|
||||
|
||||
@@ -18,7 +18,6 @@ const TrailCreateSchema = z.object({
|
||||
duration: z.number({ coerce: true }).nonnegative().optional(),
|
||||
photos: z.array(z.string()).default([]),
|
||||
thumbnail: z.number().int().nonnegative().optional(),
|
||||
waypoints: z.array(z.string()).default([]),
|
||||
like_count: z.number().int().min(0).optional().default(0),
|
||||
category: z.string().length(15).optional().or(z.literal('')),
|
||||
tags: z.array(z.string()).default([]),
|
||||
@@ -44,7 +43,6 @@ const TrailUpdateSchema = z.object({
|
||||
"photos-": z.string().optional(),
|
||||
"photos+": z.string().optional(),
|
||||
thumbnail: z.number().int().nonnegative().optional(),
|
||||
waypoints: z.array(z.string()).optional(),
|
||||
like_count: z.number().int().min(0).optional().default(0),
|
||||
category: z.string().optional(),
|
||||
tags: z.array(z.string()).optional(),
|
||||
|
||||
@@ -6,20 +6,21 @@ const WaypointCreateSchema = z.object({
|
||||
id: z.string().length(15).optional(),
|
||||
name: z.string().optional(),
|
||||
description: z.string().optional(),
|
||||
lat: z.number({coerce: true}).min(-90).max(90),
|
||||
lon: z.number({coerce: true}).min(-180).max(180),
|
||||
distance_from_start: z.number({coerce: true}).min(0).optional(),
|
||||
lat: z.number({ coerce: true }).min(-90).max(90),
|
||||
lon: z.number({ coerce: true }).min(-180).max(180),
|
||||
distance_from_start: z.number({ coerce: true }).min(0).optional(),
|
||||
icon: z.enum(icons).optional(),
|
||||
author: z.string().length(15),
|
||||
photos: z.array(z.string()).default([]),
|
||||
trail: z.string().length(15).optional()
|
||||
}) satisfies ZodType<Partial<Waypoint>>
|
||||
|
||||
const WaypointUpdateSchema = z.object({
|
||||
name: z.string().optional(),
|
||||
description: z.string().optional(),
|
||||
lat: z.number({coerce: true}).min(-90).max(90).optional(),
|
||||
lon: z.number({coerce: true}).min(-180).max(180).optional(),
|
||||
distance_from_start: z.number({coerce: true}).min(0).optional(),
|
||||
lat: z.number({ coerce: true }).min(-90).max(90).optional(),
|
||||
lon: z.number({ coerce: true }).min(-180).max(180).optional(),
|
||||
distance_from_start: z.number({ coerce: true }).min(0).optional(),
|
||||
icon: z.enum(icons).default("circle").optional(),
|
||||
photos: z.array(z.string()).optional(),
|
||||
"photos-": z.string().optional(),
|
||||
|
||||
@@ -28,7 +28,6 @@ class Trail {
|
||||
updated?: string;
|
||||
category?: string;
|
||||
tags: string[];
|
||||
waypoints: string[];
|
||||
polyline?: string;
|
||||
domain?: string;
|
||||
iri?: string;
|
||||
@@ -36,7 +35,7 @@ class Trail {
|
||||
expand?: {
|
||||
tags?: Tag[]
|
||||
category?: Category;
|
||||
waypoints?: Waypoint[]
|
||||
waypoints_via_trail?: Waypoint[]
|
||||
summit_logs_via_trail?: SummitLog[]
|
||||
author?: Actor
|
||||
comments_via_trail?: Comment[]
|
||||
@@ -90,13 +89,12 @@ class Trail {
|
||||
this.lon = params?.lon;
|
||||
this.thumbnail = params?.thumbnail ?? 0;
|
||||
this.photos = params?.photos ?? [];
|
||||
this.waypoints = [];
|
||||
this.tags = []
|
||||
this.gpx = params?.gpx;
|
||||
this.like_count = 0
|
||||
this.expand = {
|
||||
category: params?.category,
|
||||
waypoints: params?.waypoints ?? [],
|
||||
waypoints_via_trail: params?.waypoints ?? [],
|
||||
summit_logs_via_trail: params?.summit_logs ?? [],
|
||||
comments_via_trail: params?.comments ?? [],
|
||||
trail_share_via_trail: params?.shares ?? []
|
||||
|
||||
@@ -13,10 +13,12 @@ class Waypoint {
|
||||
photos: string[];
|
||||
_photos?: File[];
|
||||
author: string;
|
||||
trail?: string;
|
||||
|
||||
constructor(lat: number, lon: number, params?: {
|
||||
id?: string, name?: string, description?: string, icon?: typeof icons[number], marker?: M.Marker, photos?: string[];
|
||||
id?: string, name?: string, description?: string, icon?: typeof icons[number], marker?: M.Marker, photos?: string[], trail?: string
|
||||
}) {
|
||||
this.trail = params?.trail;
|
||||
this.id = params?.id;
|
||||
this.name = params?.name ?? "";
|
||||
this.description = params?.description ?? "";
|
||||
|
||||
@@ -22,7 +22,7 @@ export const editTrail: Writable<Trail> = writable(new Trail(""));
|
||||
export async function trails_index(perPage: number = 21, random: boolean = false, f: (url: RequestInfo | URL, config?: RequestInit) => Promise<Response> = fetch) {
|
||||
const r = await f('/api/v1/trail?' + new URLSearchParams({
|
||||
"perPage": perPage.toString(),
|
||||
expand: "category,waypoints,summit_logs_via_trail,tags",
|
||||
expand: "category,waypoints_via_trail,summit_logs_via_trail,tags",
|
||||
sort: random ? "@random" : "",
|
||||
}), {
|
||||
method: 'GET',
|
||||
@@ -136,7 +136,7 @@ export async function trails_search_bounding_box(northEast: M.LngLat, southWest:
|
||||
export async function trails_show(id: string, handle?: string, share?: string, loadGPX?: boolean, f: (url: RequestInfo | URL, config?: RequestInit) => Promise<Response> = fetch) {
|
||||
|
||||
const r = await f(`/api/v1/trail/${id}?` + new URLSearchParams({
|
||||
expand: "category,waypoints,summit_logs_via_trail,summit_logs_via_trail.author,trail_share_via_trail.actor,trail_like_via_trail,tags,author",
|
||||
expand: "category,waypoints_via_trail,summit_logs_via_trail,summit_logs_via_trail.author,trail_share_via_trail.actor,trail_like_via_trail,tags,author",
|
||||
...(handle ? { handle } : {}),
|
||||
...(share ? { share } : {})
|
||||
}), {
|
||||
@@ -148,7 +148,7 @@ export async function trails_show(id: string, handle?: string, share?: string, l
|
||||
throw new APIError(r.status, response.message, response.detail)
|
||||
}
|
||||
|
||||
const response = await r.json()
|
||||
const response: Trail = await r.json()
|
||||
|
||||
if (loadGPX) {
|
||||
if (!response.expand) {
|
||||
@@ -171,8 +171,8 @@ export async function trails_show(id: string, handle?: string, share?: string, l
|
||||
}
|
||||
}
|
||||
|
||||
response.expand.waypoints = response.expand.waypoints || [];
|
||||
response.expand.summit_logs = response.expand.summit_logs?.sort((a: SummitLog, b: SummitLog) => Date.parse(a.date) - Date.parse(b.date)) || [];
|
||||
response.expand!.waypoints_via_trail = response.expand!.waypoints_via_trail || [];
|
||||
response.expand!.summit_logs_via_trail = response.expand!.summit_logs_via_trail?.sort((a: SummitLog, b: SummitLog) => Date.parse(a.date) - Date.parse(b.date)) || [];
|
||||
|
||||
trail.set(response);
|
||||
|
||||
@@ -185,13 +185,6 @@ export async function trails_create(trail: Trail, photos: File[], gpx: File | Bl
|
||||
throw Error("Unauthenticated")
|
||||
}
|
||||
|
||||
for (const waypoint of trail.expand?.waypoints ?? []) {
|
||||
const model = await waypoints_create({
|
||||
...waypoint,
|
||||
marker: undefined,
|
||||
}, f, user);
|
||||
trail.waypoints.push(model.id!);
|
||||
}
|
||||
for (const tag of trail.expand?.tags ?? []) {
|
||||
if (!tag.id) {
|
||||
const model = await tags_create(tag)
|
||||
@@ -214,7 +207,7 @@ export async function trails_create(trail: Trail, photos: File[], gpx: File | Bl
|
||||
}
|
||||
|
||||
let r = await f(`/api/v1/trail/form?` + new URLSearchParams({
|
||||
expand: "category,waypoints,summit_logs_via_trail,trail_share_via_trail,tags",
|
||||
expand: "category,waypoints_via_trail,summit_logs_via_trail,trail_share_via_trail,tags",
|
||||
}), {
|
||||
method: 'PUT',
|
||||
body: formData,
|
||||
@@ -232,6 +225,14 @@ export async function trails_create(trail: Trail, photos: File[], gpx: File | Bl
|
||||
await summit_logs_create(summitLog, f);
|
||||
}
|
||||
|
||||
for (const wp of trail.expand?.waypoints_via_trail ?? []) {
|
||||
wp.trail = model.id!;
|
||||
await waypoints_create({
|
||||
...wp,
|
||||
marker: undefined,
|
||||
}, f, user);
|
||||
}
|
||||
|
||||
return model;
|
||||
|
||||
}
|
||||
@@ -239,18 +240,17 @@ export async function trails_create(trail: Trail, photos: File[], gpx: File | Bl
|
||||
export async function trails_update(oldTrail: Trail, newTrail: Trail, photos?: File[], gpx?: File | Blob | null) {
|
||||
newTrail.author = oldTrail.author
|
||||
|
||||
const waypointUpdates = compareObjectArrays<Waypoint>(oldTrail.expand?.waypoints ?? [], newTrail.expand?.waypoints ?? []);
|
||||
const waypointUpdates = compareObjectArrays<Waypoint>(oldTrail.expand?.waypoints_via_trail ?? [], newTrail.expand?.waypoints_via_trail ?? []);
|
||||
|
||||
for (const addedWaypoint of waypointUpdates.added) {
|
||||
const model = await waypoints_create({
|
||||
...addedWaypoint,
|
||||
marker: undefined,
|
||||
},);
|
||||
newTrail.waypoints.push(model.id!);
|
||||
}
|
||||
|
||||
for (const updatedWaypoint of waypointUpdates.updated) {
|
||||
const oldWaypoint = oldTrail.expand?.waypoints?.find(w => w.id == updatedWaypoint.id);
|
||||
const oldWaypoint = oldTrail.expand?.waypoints_via_trail?.find(w => w.id == updatedWaypoint.id);
|
||||
const model = await waypoints_update(oldWaypoint!, {
|
||||
...updatedWaypoint,
|
||||
marker: undefined,
|
||||
@@ -313,7 +313,7 @@ export async function trails_update(oldTrail: Trail, newTrail: Trail, photos?: F
|
||||
|
||||
|
||||
let r = await fetch(`/api/v1/trail/form/${newTrail.id}?` + new URLSearchParams({
|
||||
expand: "category,waypoints,summit_logs_via_trail,trail_share_via_trail,tags",
|
||||
expand: "category,waypoints_via_trail,summit_logs_via_trail,trail_share_via_trail,tags",
|
||||
}), {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
@@ -340,17 +340,6 @@ export async function trails_update(oldTrail: Trail, newTrail: Trail, photos?: F
|
||||
|
||||
|
||||
export async function trails_delete(trail: Trail) {
|
||||
if (trail.expand?.waypoints) {
|
||||
for (const waypoint of trail.expand.waypoints) {
|
||||
await waypoints_delete(waypoint);
|
||||
}
|
||||
}
|
||||
if (trail.expand?.summit_logs_via_trail) {
|
||||
for (const summit_log of trail.expand.summit_logs_via_trail) {
|
||||
await summit_logs_delete(summit_log);
|
||||
}
|
||||
}
|
||||
|
||||
const r = await fetch('/api/v1/trail/' + trail.id, {
|
||||
method: 'DELETE',
|
||||
})
|
||||
|
||||
@@ -45,7 +45,7 @@ export async function gpx2trail(gpxString: string, fallbackName?: string, correc
|
||||
wp.id = cryptoRandomString({ length: 15 });
|
||||
wp.name = wpt.name ?? ""
|
||||
wp.description = wpt.desc;
|
||||
trail.expand!.waypoints?.push(wp);
|
||||
trail.expand!.waypoints_via_trail?.push(wp);
|
||||
}
|
||||
|
||||
const totals = gpx.features
|
||||
@@ -108,7 +108,7 @@ export async function trail2gpx(trail: Trail, user?: AuthRecord) {
|
||||
gpx.wpt = [];
|
||||
}
|
||||
|
||||
for (const wp of gpxTrail.expand!.waypoints ?? []) {
|
||||
for (const wp of gpxTrail.expand!.waypoints_via_trail ?? []) {
|
||||
const gpxWpt = gpx.wpt.find((w) => w.$.lat == wp.lat && w.$.lon == wp.lon)
|
||||
if (!gpxWpt) {
|
||||
gpx.wpt.push(new GPXWaypoint({
|
||||
|
||||
@@ -15,7 +15,7 @@ export async function GET(event: RequestEvent) {
|
||||
t.expand = {} as any
|
||||
}
|
||||
|
||||
t.expand?.waypoints?.sort((a, b) => (a.distance_from_start ?? 0) - (b.distance_from_start ?? 0))
|
||||
t.expand?.waypoints_via_trail?.sort((a, b) => (a.distance_from_start ?? 0) - (b.distance_from_start ?? 0))
|
||||
}
|
||||
return json(r)
|
||||
} catch (e: any) {
|
||||
|
||||
@@ -84,7 +84,7 @@ export async function GET(event: RequestEvent) {
|
||||
l.expand.author.isLocal = false
|
||||
}
|
||||
})
|
||||
t.expand?.waypoints?.forEach(w => {
|
||||
t.expand?.waypoints_via_trail?.forEach(w => {
|
||||
|
||||
w.photos = w.photos.map(p =>
|
||||
`${origin}/api/v1/files/waypoints/${w.id}/${p}`
|
||||
@@ -131,7 +131,7 @@ export async function GET(event: RequestEvent) {
|
||||
await enrichRecord(event.locals.pb, t);
|
||||
|
||||
// sort waypoints by distance
|
||||
t.expand?.waypoints?.sort((a, b) => (a.distance_from_start ?? 0) - (b.distance_from_start ?? 0))
|
||||
t.expand?.waypoints_via_trail?.sort((a, b) => (a.distance_from_start ?? 0) - (b.distance_from_start ?? 0))
|
||||
return json(t)
|
||||
} catch (e: any) {
|
||||
return handleError(e)
|
||||
|
||||
@@ -71,7 +71,7 @@
|
||||
let selectedTrailIndex = $derived(selectedTrail ? 0 : null);
|
||||
|
||||
let selectedTrailWaypoints = $derived(
|
||||
(selectedTrail as Trail | null)?.expand?.waypoints,
|
||||
(selectedTrail as Trail | null)?.expand?.waypoints_via_trail,
|
||||
);
|
||||
|
||||
onMount(() => {
|
||||
|
||||
@@ -45,7 +45,7 @@
|
||||
<div id="trail-details">
|
||||
<MapWithElevationMaplibre
|
||||
trails={[trail]}
|
||||
waypoints={trail.expand?.waypoints}
|
||||
waypoints={trail.expand?.waypoints_via_trail}
|
||||
activeTrail={0}
|
||||
bind:markers
|
||||
showTerrain={true}
|
||||
|
||||
@@ -390,7 +390,7 @@
|
||||
currentHeight += getTextHeight($trail.description, doc, width - 32) + 8;
|
||||
}
|
||||
|
||||
if (includeWaypoints && $trail.expand?.waypoints) {
|
||||
if (includeWaypoints && $trail.expand?.waypoints_via_trail) {
|
||||
const header = $_("waypoints", { values: { n: 2 } })
|
||||
let textHeight = getTextHeight(header, doc, width - 32)
|
||||
if (currentHeight + textHeight + 8 > height) {
|
||||
@@ -401,7 +401,7 @@
|
||||
currentHeight += textHeight + 8;
|
||||
doc.setFont("IBMPlexSans-Regular", "normal");
|
||||
|
||||
($trail.expand.waypoints || []).forEach(waypoint => {
|
||||
($trail.expand.waypoints_via_trail || []).forEach(waypoint => {
|
||||
let description = waypoint.description || "";
|
||||
let name = waypoint.name || "";
|
||||
|
||||
@@ -616,7 +616,7 @@
|
||||
<div class="basis-full">
|
||||
<MapWithElevationMaplibre
|
||||
trails={[$trail]}
|
||||
waypoints={$trail.expand?.waypoints}
|
||||
waypoints={$trail.expand?.waypoints_via_trail}
|
||||
activeTrail={0}
|
||||
onzoom={updateScale}
|
||||
bind:map
|
||||
|
||||
@@ -139,7 +139,7 @@
|
||||
summit_logs_via_trail: z
|
||||
.array(SummitLogCreateSchema)
|
||||
.optional(),
|
||||
waypoints: z
|
||||
waypoints_via_trail: z
|
||||
.array(
|
||||
WaypointCreateSchema.extend({
|
||||
marker: z.any().optional(),
|
||||
@@ -384,11 +384,10 @@
|
||||
}
|
||||
|
||||
function clearWaypoints() {
|
||||
for (const waypoint of $formData.expand!.waypoints ?? []) {
|
||||
for (const waypoint of $formData.expand!.waypoints_via_trail ?? []) {
|
||||
waypoint.marker?.remove();
|
||||
}
|
||||
$formData.expand!.waypoints = [];
|
||||
$formData.waypoints = [];
|
||||
$formData.expand!.waypoints_via_trail = [];
|
||||
}
|
||||
|
||||
function initRouteAnchors(gpx: GPX, addToMap: boolean = false) {
|
||||
@@ -445,29 +444,28 @@
|
||||
}
|
||||
|
||||
function deleteWaypoint(index: number) {
|
||||
const wp = $formData.expand!.waypoints?.splice(index, 1);
|
||||
$formData.waypoints.splice(index, 1);
|
||||
const wp = $formData.expand!.waypoints_via_trail?.splice(index, 1);
|
||||
|
||||
if (!$formData.expand!.waypoints?.length) {
|
||||
$formData.expand!.waypoints = [];
|
||||
if (!$formData.expand!.waypoints_via_trail?.length) {
|
||||
$formData.expand!.waypoints_via_trail = [];
|
||||
}
|
||||
$formData.expand!.waypoints = $formData.expand!.waypoints;
|
||||
$formData.expand!.waypoints_via_trail = $formData.expand!.waypoints_via_trail;
|
||||
|
||||
// updateTrailOnMap();
|
||||
}
|
||||
|
||||
function saveWaypoint(savedWaypoint: Waypoint) {
|
||||
let editedWaypointIndex =
|
||||
$formData.expand!.waypoints?.findIndex(
|
||||
$formData.expand!.waypoints_via_trail?.findIndex(
|
||||
(s) => s.id == savedWaypoint.id,
|
||||
) ?? -1;
|
||||
|
||||
if (editedWaypointIndex >= 0) {
|
||||
$formData.expand!.waypoints![editedWaypointIndex] = savedWaypoint;
|
||||
$formData.expand!.waypoints_via_trail![editedWaypointIndex] = savedWaypoint;
|
||||
} else {
|
||||
savedWaypoint.id = cryptoRandomString({ length: 15 });
|
||||
$formData.expand!.waypoints = [
|
||||
...($formData.expand!.waypoints ?? []),
|
||||
$formData.expand!.waypoints_via_trail = [
|
||||
...($formData.expand!.waypoints_via_trail ?? []),
|
||||
savedWaypoint,
|
||||
];
|
||||
|
||||
@@ -478,15 +476,15 @@
|
||||
function moveMarker(marker: M.Marker, wpId?: string) {
|
||||
const position = marker.getLngLat();
|
||||
const editableWaypointIndex =
|
||||
$formData.expand!.waypoints?.findIndex((w) => w.id == wpId) ?? -1;
|
||||
$formData.expand!.waypoints_via_trail?.findIndex((w) => w.id == wpId) ?? -1;
|
||||
const editableWaypoint =
|
||||
$formData.expand!.waypoints![editableWaypointIndex];
|
||||
$formData.expand!.waypoints_via_trail![editableWaypointIndex];
|
||||
if (!editableWaypoint) {
|
||||
return;
|
||||
}
|
||||
editableWaypoint.lat = position.lat;
|
||||
editableWaypoint.lon = position.lng;
|
||||
$formData.expand!.waypoints = [...($formData.expand!.waypoints ?? [])];
|
||||
$formData.expand!.waypoints_via_trail = [...($formData.expand!.waypoints_via_trail ?? [])];
|
||||
// updateTrailOnMap();
|
||||
}
|
||||
|
||||
@@ -1369,7 +1367,7 @@
|
||||
{$_("waypoints", { values: { n: 2 } })}
|
||||
</h3>
|
||||
<ul>
|
||||
{#each $formData.expand?.waypoints ?? [] as waypoint, i}
|
||||
{#each $formData.expand?.waypoints_via_trail ?? [] as waypoint, i}
|
||||
<li
|
||||
onmouseenter={() => openMarkerPopup(waypoint)}
|
||||
onmouseleave={() => openMarkerPopup(waypoint)}
|
||||
@@ -1501,7 +1499,7 @@
|
||||
<div id="trail-map">
|
||||
<MapWithElevationMaplibre
|
||||
trails={mapTrail}
|
||||
waypoints={$formData.expand?.waypoints}
|
||||
waypoints={$formData.expand?.waypoints_via_trail}
|
||||
drawing={drawingActive}
|
||||
showTerrain={true}
|
||||
onmarkerdragend={moveMarker}
|
||||
|
||||
Reference in New Issue
Block a user