From bb8bcdb6e2bc78cacef9947863306d291d4b30af Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Fri, 23 Sep 2022 14:15:29 +0200 Subject: [PATCH] Add support for seperate micropub channel request --- micropub.go | 34 +++++++++++++++++++++------------- micropub_test.go | 5 +++++ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/micropub.go b/micropub.go index 550d3d5..958fd9a 100644 --- a/micropub.go +++ b/micropub.go @@ -24,19 +24,7 @@ func (a *goBlog) serveMicropubQuery(w http.ResponseWriter, r *http.Request) { var result any switch query := r.URL.Query(); query.Get("q") { case "config": - channels := []map[string]any{} - for b, bc := range a.cfg.Blogs { - channels = append(channels, map[string]any{ - "name": fmt.Sprintf("%s: %s", b, bc.Title), - "uid": b, - }) - for s, sc := range bc.Sections { - channels = append(channels, map[string]any{ - "name": fmt.Sprintf("%s/%s: %s", b, s, sc.Name), - "uid": fmt.Sprintf("%s/%s", b, s), - }) - } - } + channels := a.getMicropubChannelsMap() result = map[string]any{ "channels": channels, "media-endpoint": a.getFullAddress(micropubPath + micropubMediaSubPath), @@ -81,6 +69,9 @@ func (a *goBlog) serveMicropubQuery(w http.ResponseWriter, r *http.Request) { allCategories = append(allCategories, values...) } result = map[string]any{"categories": allCategories} + case "channel": + channels := a.getMicropubChannelsMap() + result = map[string]any{"channels": channels} default: a.serve404(w, r) return @@ -95,6 +86,23 @@ func (a *goBlog) serveMicropubQuery(w http.ResponseWriter, r *http.Request) { _ = a.min.Get().Minify(contenttype.JSON, w, buf) } +func (a *goBlog) getMicropubChannelsMap() []map[string]any { + channels := []map[string]any{} + for b, bc := range a.cfg.Blogs { + channels = append(channels, map[string]any{ + "name": fmt.Sprintf("%s: %s", b, bc.Title), + "uid": b, + }) + for s, sc := range bc.Sections { + channels = append(channels, map[string]any{ + "name": fmt.Sprintf("%s/%s: %s", b, s, sc.Name), + "uid": fmt.Sprintf("%s/%s", b, s), + }) + } + } + return channels +} + func (a *goBlog) serveMicropubPost(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() switch mt, _, _ := mime.ParseMediaType(r.Header.Get(contentType)); mt { diff --git a/micropub_test.go b/micropub_test.go index 348fac9..b2eb2af 100644 --- a/micropub_test.go +++ b/micropub_test.go @@ -57,6 +57,11 @@ func Test_micropubQuery(t *testing.T) { want: "{\"categories\":[\"test\",\"test2\"]}", wantStatus: http.StatusOK, }, + { + query: "channel", + want: "{\"channels\":[{\"name\":\"default: My Blog\",\"uid\":\"default\"},{\"name\":\"default/posts: posts\",\"uid\":\"default/posts\"}]}", + wantStatus: http.StatusOK, + }, { query: "somethingelse", wantStatus: http.StatusNotFound,