Skip to content

Commit

Permalink
Add config option for maps to display locations etc. from all blogs
Browse files Browse the repository at this point in the history
  • Loading branch information
jlelse committed Jul 8, 2024
1 parent 50276d3 commit 573ece1
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 25 deletions.
5 changes: 3 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions example-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 18 additions & 7 deletions geoMap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,31 @@ 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)

mapPath := bc.getRelativePath(cmp.Or(bc.Map.Path, defaultGeoMapPath))
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)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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},
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion posts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 14 additions & 5 deletions postsDb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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},
Expand Down
2 changes: 1 addition & 1 deletion postsDb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions sitemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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},
Expand Down

0 comments on commit 573ece1

Please sign in to comment.