2021-07-06 19:06:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-07-03 08:42:26 +00:00
|
|
|
"io"
|
2021-07-06 19:06:39 +00:00
|
|
|
"net/http"
|
2022-07-03 08:42:26 +00:00
|
|
|
|
|
|
|
"go.goblog.app/app/pkgs/contenttype"
|
2021-07-06 19:06:39 +00:00
|
|
|
)
|
|
|
|
|
2021-07-12 14:19:28 +00:00
|
|
|
const defaultGeoMapPath = "/map"
|
|
|
|
|
2021-07-06 19:06:39 +00:00
|
|
|
func (a *goBlog) serveGeoMap(w http.ResponseWriter, r *http.Request) {
|
2021-12-20 13:00:11 +00:00
|
|
|
blog, bc := a.getBlog(r)
|
2021-07-06 19:06:39 +00:00
|
|
|
|
2021-11-22 16:19:07 +00:00
|
|
|
mapPath := bc.getRelativePath(defaultIfEmpty(bc.Map.Path, defaultGeoMapPath))
|
|
|
|
canonical := a.getFullAddress(mapPath)
|
|
|
|
|
2022-09-23 09:05:07 +00:00
|
|
|
allPostsWithLocationRequestConfig := &postsRequestConfig{
|
2021-07-06 19:06:39 +00:00
|
|
|
blog: blog,
|
2021-11-13 19:19:46 +00:00
|
|
|
parameters: []string{a.cfg.Micropub.LocationParam, gpxParameter},
|
|
|
|
withOnlyParameters: []string{a.cfg.Micropub.LocationParam, gpxParameter},
|
2022-09-23 09:05:07 +00:00
|
|
|
}
|
|
|
|
allPostsWithLocationRequestConfig.status, allPostsWithLocationRequestConfig.visibility = a.getDefaultPostStates(r)
|
|
|
|
|
|
|
|
allPostsWithLocation, err := a.db.countPosts(allPostsWithLocationRequestConfig)
|
2021-07-06 19:06:39 +00:00
|
|
|
if err != nil {
|
|
|
|
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-03 08:42:26 +00:00
|
|
|
if allPostsWithLocation == 0 {
|
2022-01-30 15:40:53 +00:00
|
|
|
a.render(w, r, a.renderGeoMap, &renderData{
|
2021-12-20 13:00:11 +00:00
|
|
|
Canonical: canonical,
|
2022-01-20 17:22:10 +00:00
|
|
|
Data: &geoMapRenderData{
|
|
|
|
noLocations: true,
|
2021-07-06 19:06:39 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-03 08:42:26 +00:00
|
|
|
a.render(w, r, a.renderGeoMap, &renderData{
|
|
|
|
Canonical: canonical,
|
|
|
|
Data: &geoMapRenderData{
|
|
|
|
locations: "url:" + canonical + geoMapLocationsSubpath,
|
|
|
|
tracks: "url:" + canonical + geoMapTracksSubpath,
|
|
|
|
attribution: a.getMapAttribution(),
|
|
|
|
minZoom: a.getMinZoom(),
|
|
|
|
maxZoom: a.getMaxZoom(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const geoMapTracksSubpath = "/tracks.json"
|
|
|
|
|
|
|
|
func (a *goBlog) serveGeoMapTracks(w http.ResponseWriter, r *http.Request) {
|
|
|
|
blog, _ := a.getBlog(r)
|
|
|
|
|
2022-09-23 09:05:07 +00:00
|
|
|
allPostsWithTracksRequestConfig := &postsRequestConfig{
|
2022-07-13 14:02:08 +00:00
|
|
|
blog: blog,
|
|
|
|
parameters: []string{gpxParameter},
|
|
|
|
withOnlyParameters: []string{gpxParameter},
|
|
|
|
excludeParameter: showRouteParam,
|
|
|
|
excludeParameterValue: "false", // Don't show hidden route tracks
|
2022-09-23 09:05:07 +00:00
|
|
|
}
|
|
|
|
allPostsWithTracksRequestConfig.status, allPostsWithTracksRequestConfig.visibility = a.getDefaultPostStates(r)
|
|
|
|
|
|
|
|
allPostsWithTracks, err := a.getPosts(allPostsWithTracksRequestConfig)
|
2022-07-03 08:42:26 +00:00
|
|
|
if err != nil {
|
|
|
|
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
2021-07-06 19:06:39 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 19:19:46 +00:00
|
|
|
type templateTrack struct {
|
2021-11-16 17:01:11 +00:00
|
|
|
Paths [][]*trackPoint
|
|
|
|
Points []*trackPoint
|
|
|
|
Post string
|
2021-11-13 19:19:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var tracks []*templateTrack
|
2022-07-03 08:42:26 +00:00
|
|
|
for _, p := range allPostsWithTracks {
|
2022-07-13 14:02:08 +00:00
|
|
|
if t, err := a.getTrack(p, true); err == nil && t != nil {
|
2021-11-13 19:19:46 +00:00
|
|
|
tracks = append(tracks, &templateTrack{
|
2021-11-16 17:01:11 +00:00
|
|
|
Paths: t.Paths,
|
|
|
|
Points: t.Points,
|
|
|
|
Post: p.Path,
|
2021-11-13 19:19:46 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-24 08:40:54 +00:00
|
|
|
pr, pw := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
_ = pw.CloseWithError(json.NewEncoder(pw).Encode(tracks))
|
|
|
|
}()
|
2022-07-03 08:42:26 +00:00
|
|
|
w.Header().Set(contentType, contenttype.JSONUTF8)
|
2023-01-24 08:40:54 +00:00
|
|
|
_ = pr.CloseWithError(a.min.Get().Minify(contenttype.JSON, w, pr))
|
2022-07-03 08:42:26 +00:00
|
|
|
}
|
2021-07-06 19:06:39 +00:00
|
|
|
|
2022-07-03 08:42:26 +00:00
|
|
|
const geoMapLocationsSubpath = "/locations.json"
|
|
|
|
|
|
|
|
func (a *goBlog) serveGeoMapLocations(w http.ResponseWriter, r *http.Request) {
|
|
|
|
blog, _ := a.getBlog(r)
|
|
|
|
|
2022-09-23 09:05:07 +00:00
|
|
|
allPostsWithLocationRequestConfig := &postsRequestConfig{
|
2022-07-03 08:42:26 +00:00
|
|
|
blog: blog,
|
|
|
|
parameters: []string{a.cfg.Micropub.LocationParam},
|
|
|
|
withOnlyParameters: []string{a.cfg.Micropub.LocationParam},
|
2022-09-23 09:05:07 +00:00
|
|
|
}
|
|
|
|
allPostsWithLocationRequestConfig.status, allPostsWithLocationRequestConfig.visibility = a.getDefaultPostStates(r)
|
|
|
|
|
|
|
|
allPostsWithLocations, err := a.getPosts(allPostsWithLocationRequestConfig)
|
2022-07-03 08:42:26 +00:00
|
|
|
if err != nil {
|
|
|
|
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type templateLocation struct {
|
|
|
|
Lat float64
|
|
|
|
Lon float64
|
|
|
|
Post string
|
|
|
|
}
|
|
|
|
|
|
|
|
var locations []*templateLocation
|
|
|
|
for _, p := range allPostsWithLocations {
|
|
|
|
for _, g := range a.geoURIs(p) {
|
|
|
|
locations = append(locations, &templateLocation{
|
|
|
|
Lat: g.Latitude,
|
|
|
|
Lon: g.Longitude,
|
|
|
|
Post: p.Path,
|
|
|
|
})
|
2021-11-13 19:40:25 +00:00
|
|
|
}
|
2021-07-06 19:06:39 +00:00
|
|
|
}
|
|
|
|
|
2023-01-24 08:40:54 +00:00
|
|
|
pr, pw := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
_ = pw.CloseWithError(json.NewEncoder(pw).Encode(locations))
|
|
|
|
}()
|
2022-07-03 08:42:26 +00:00
|
|
|
w.Header().Set(contentType, contenttype.JSONUTF8)
|
2023-01-24 08:40:54 +00:00
|
|
|
_ = pr.CloseWithError(a.min.Get().Minify(contenttype.JSON, w, pr))
|
2021-07-06 19:06:39 +00:00
|
|
|
}
|