From 3059909deb5e21746d3d95e4989a78db51ccec33 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sat, 21 May 2022 11:15:37 -0600 Subject: Publish new posts to mailing list --- srv/src/http/posts.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'srv/src/http') diff --git a/srv/src/http/posts.go b/srv/src/http/posts.go index c779eed..daa756c 100644 --- a/srv/src/http/posts.go +++ b/srv/src/http/posts.go @@ -131,11 +131,17 @@ func (a *api) renderPostHandler() http.Handler { func (a *api) renderPostsIndexHandler() http.Handler { + renderEditPostHandler := a.renderEditPostHandler() tpl := a.mustParseBasedTpl("posts.html") const pageCount = 20 return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + if _, ok := r.URL.Query()["edit"]; ok { + renderEditPostHandler.ServeHTTP(rw, r) + return + } + page, err := apiutil.StrToInt(r.FormValue("p"), 0) if err != nil { apiutil.BadRequest( @@ -239,13 +245,28 @@ func (a *api) postPostHandler() http.Handler { return } - if err := a.params.PostStore.Set(p, time.Now()); err != nil { + first, err := a.params.PostStore.Set(p, time.Now()) + + if err != nil { apiutil.InternalServerError( rw, r, fmt.Errorf("storing post with id %q: %w", p.ID, err), ) return } + if first { + + a.params.Logger.Info(r.Context(), "publishing blog post to mailing list") + urlStr := a.params.PublicURL.String() + filepath.Join("/posts", p.ID) + + if err := a.params.MailingList.Publish(p.Title, urlStr); err != nil { + apiutil.InternalServerError( + rw, r, fmt.Errorf("publishing post with id %q: %w", p.ID, err), + ) + return + } + } + redirectPath := fmt.Sprintf("posts/%s?edit", p.ID) a.executeRedirectTpl(rw, r, redirectPath) -- cgit v1.2.3