1
mirror of https://github.com/jlelse/GoBlog synced 2024-07-15 11:22:59 +00:00

Add config option for maps to display locations etc. from all blogs

This commit is contained in:
Jan-Lukas Else 2024-07-08 10:45:54 +02:00
parent 50276d388e
commit 573ece108b
10 changed files with 47 additions and 25 deletions

View File

@ -181,8 +181,9 @@ type configComments struct {
}
type configGeoMap struct {
Enabled bool `mapstructure:"enabled"`
Path string `mapstructure:"path"`
Enabled bool `mapstructure:"enabled"`
Path string `mapstructure:"path"`
AllBlogs bool `mapstructure:"allBlogs"`
}
type configContact struct {

View File

@ -261,6 +261,7 @@ blogs:
map:
enabled: true # Enable the map feature (shows a map with all post locations)
path: /map # (Optional) Set a custom path (relative to blog path), default is /map
allBlogs: true # Show all locations, paths and tracks from all blogs, default is false
# Contact form
contact:
enabled: true # Enable a contact form

View File

@ -3,10 +3,19 @@ package main
import (
"cmp"
"net/http"
"github.com/samber/lo"
)
const defaultGeoMapPath = "/map"
func (a *goBlog) blogsOnMap(blog string, bc *configBlog) []string {
if bc.Map != nil && bc.Map.AllBlogs {
return lo.Keys(a.cfg.Blogs)
}
return []string{blog}
}
func (a *goBlog) serveGeoMap(w http.ResponseWriter, r *http.Request) {
blog, bc := a.getBlog(r)
@ -14,9 +23,11 @@ func (a *goBlog) serveGeoMap(w http.ResponseWriter, r *http.Request) {
canonical := a.getFullAddress(mapPath)
allPostsWithLocationRequestConfig := &postsRequestConfig{
blog: blog,
anyParams: []string{a.cfg.Micropub.LocationParam, gpxParameter},
fetchParams: []string{a.cfg.Micropub.LocationParam, gpxParameter},
blogs: a.blogsOnMap(blog, bc),
anyParams: []string{a.cfg.Micropub.LocationParam, gpxParameter},
fetchWithoutParams: true,
excludeParameter: showRouteParam,
excludeParameterValue: "false", // Don't show hidden route tracks
}
allPostsWithLocationRequestConfig.status, allPostsWithLocationRequestConfig.visibility = a.getDefaultPostStates(r)
@ -51,10 +62,10 @@ func (a *goBlog) serveGeoMap(w http.ResponseWriter, r *http.Request) {
const geoMapTracksSubpath = "/tracks.json"
func (a *goBlog) serveGeoMapTracks(w http.ResponseWriter, r *http.Request) {
blog, _ := a.getBlog(r)
blog, bc := a.getBlog(r)
allPostsWithTracksRequestConfig := &postsRequestConfig{
blog: blog,
blogs: a.blogsOnMap(blog, bc),
anyParams: []string{gpxParameter},
fetchParams: []string{gpxParameter},
excludeParameter: showRouteParam,
@ -91,10 +102,10 @@ func (a *goBlog) serveGeoMapTracks(w http.ResponseWriter, r *http.Request) {
const geoMapLocationsSubpath = "/locations.json"
func (a *goBlog) serveGeoMapLocations(w http.ResponseWriter, r *http.Request) {
blog, _ := a.getBlog(r)
blog, bc := a.getBlog(r)
allPostsWithLocationRequestConfig := &postsRequestConfig{
blog: blog,
blogs: a.blogsOnMap(blog, bc),
anyParams: []string{a.cfg.Micropub.LocationParam},
fetchParams: []string{a.cfg.Micropub.LocationParam},
}

4
go.mod
View File

@ -49,7 +49,7 @@ require (
github.com/spf13/cast v1.6.0
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
github.com/tdewolff/minify/v2 v2.20.35
github.com/tdewolff/minify/v2 v2.20.36
github.com/tiptophelmet/cspolicy v0.1.1
github.com/tkrajina/gpxgo v1.4.0
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80
@ -118,7 +118,7 @@ require (
github.com/valyala/fastjson v1.6.4 // indirect
go.mau.fi/util v0.5.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/exp v0.0.0-20240707233637-46b078467d37 // indirect
golang.org/x/image v0.18.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sys v0.22.0 // indirect

8
go.sum
View File

@ -251,8 +251,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/tdewolff/minify/v2 v2.20.35 h1:/Vq/oivpkFyi2PViD25XHZZbJz+eO4OmPSgePex1kBU=
github.com/tdewolff/minify/v2 v2.20.35/go.mod h1:L1VYef/jwKw6Wwyk5A+T0mBjjn3mMPgmjjA688RNsxU=
github.com/tdewolff/minify/v2 v2.20.36 h1:uhbCO5NNS0UgJfEyE/ZR+xU5DL9Dz0ngrJ8W9A6coCQ=
github.com/tdewolff/minify/v2 v2.20.36/go.mod h1:L1VYef/jwKw6Wwyk5A+T0mBjjn3mMPgmjjA688RNsxU=
github.com/tdewolff/parse/v2 v2.7.15 h1:hysDXtdGZIRF5UZXwpfn3ZWRbm+ru4l53/ajBRGpCTw=
github.com/tdewolff/parse/v2 v2.7.15/go.mod h1:3FbJWZp3XT9OWVN3Hmfp0p/a08v4h8J9W1aghka0soA=
github.com/tdewolff/test v1.0.11-0.20231101010635-f1265d231d52/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE=
@ -304,8 +304,8 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI=
golang.org/x/exp v0.0.0-20240707233637-46b078467d37 h1:uLDX+AfeFCct3a2C7uIWBKMJIR3CJMhcgfrUAqjRK6w=
golang.org/x/exp v0.0.0-20240707233637-46b078467d37/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E=

View File

@ -333,7 +333,7 @@ func (a *goBlog) serveIndex(w http.ResponseWriter, r *http.Request) {
}
// Create paginator
p := paginator.New(&postPaginationAdapter{config: &postsRequestConfig{
blog: blog,
blogs: []string{blog},
sections: sections,
taxonomy: ic.tax,
taxonomyValue: ic.taxValue,

View File

@ -365,7 +365,7 @@ func (db *database) replacePostParam(path, param string, values []string) error
type postsRequestConfig struct {
search string
blog string
blogs []string
path string
limit int
offset int
@ -436,9 +436,18 @@ func buildPostsQuery(c *postsRequestConfig, selection string) (query string, arg
}
queryBuilder.WriteString(")")
}
if c.blog != "" {
queryBuilder.WriteString(" and blog = @blog")
args = append(args, sql.Named("blog", c.blog))
if len(c.blogs) > 0 {
queryBuilder.WriteString(" and blog in (")
for i, blog := range c.blogs {
if i > 0 {
queryBuilder.WriteString(", ")
}
named := "blog" + strconv.Itoa(i)
queryBuilder.WriteString("@")
queryBuilder.WriteString(named)
args = append(args, sql.Named(named, blog))
}
queryBuilder.WriteString(")")
}
allParams := append(c.allParams, c.parameter)
allParamValues := append(c.allParamValues, c.parameterValue)
@ -678,7 +687,7 @@ func (a *goBlog) getRandomPostPath(blog string) (path string, err error) {
query, params, err := buildPostsQuery(&postsRequestConfig{
randomOrder: true,
limit: 1,
blog: blog,
blogs: []string{blog},
sections: sections,
visibility: []postVisibility{visibilityPublic},
status: []postStatus{statusPublished},

View File

@ -65,7 +65,7 @@ func Test_postsDb(t *testing.T) {
// Check drafts
drafts, _ := app.getPosts(&postsRequestConfig{
blog: "en",
blogs: []string{"en"},
status: []postStatus{statusDraft},
})
is.Len(drafts, 1)

View File

@ -57,7 +57,7 @@ func (a *goBlog) settingsDeleteSection(w http.ResponseWriter, r *http.Request) {
section := r.FormValue("sectionname")
// Check if any post uses this section
count, err := a.db.countPosts(&postsRequestConfig{
blog: blog,
blogs: []string{blog},
sections: []string{section},
})
if err != nil {

View File

@ -165,7 +165,7 @@ func (a *goBlog) serveSitemapBlogPosts(w http.ResponseWriter, r *http.Request) {
posts, _ := a.getPosts(&postsRequestConfig{
status: []postStatus{statusPublished},
visibility: []postVisibility{visibilityPublic},
blog: blog,
blogs: []string{blog},
fetchWithoutParams: true,
})
// Add posts to sitemap
@ -222,7 +222,7 @@ select distinct '/x/x/' || day from alldates;
func (a *goBlog) sitemapDatePaths(blog string, sections []string) (paths []string, err error) {
query, args, err := buildPostsQuery(&postsRequestConfig{
blog: blog,
blogs: []string{blog},
sections: sections,
status: []postStatus{statusPublished},
visibility: []postVisibility{visibilityPublic},