summaryrefslogtreecommitdiff
path: root/srv/src/http
diff options
context:
space:
mode:
authorBrian Picciano <mediocregopher@gmail.com>2022-05-21 11:15:37 -0600
committerBrian Picciano <mediocregopher@gmail.com>2022-05-21 11:15:37 -0600
commit3059909deb5e21746d3d95e4989a78db51ccec33 (patch)
treee0baba8c3d29b38deb94d67ef2b9781efd22cb71 /srv/src/http
parent3e67823205a59420d60efe536d4319fd7f1e457a (diff)
Publish new posts to mailing list
Diffstat (limited to 'srv/src/http')
-rw-r--r--srv/src/http/posts.go23
1 files changed, 22 insertions, 1 deletions
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)