diff options
author | Brian Picciano <mediocregopher@gmail.com> | 2023-01-21 16:46:11 +0100 |
---|---|---|
committer | Brian Picciano <mediocregopher@gmail.com> | 2023-01-21 16:46:11 +0100 |
commit | 293655452cfc6a106c55384e839f9c07d340b954 (patch) | |
tree | c01202bb1722f6f6a0f500f304fa2fbf3c35c34b /src/gmi/tpl.go | |
parent | 7878db5c95e5eb430b7b878fe6eb8084f15441a7 (diff) |
Continue to polish up posts pages
Diffstat (limited to 'src/gmi/tpl.go')
-rw-r--r-- | src/gmi/tpl.go | 44 |
1 files changed, 40 insertions, 4 deletions
diff --git a/src/gmi/tpl.go b/src/gmi/tpl.go index 7aa1a2f..7959b8e 100644 --- a/src/gmi/tpl.go +++ b/src/gmi/tpl.go @@ -25,6 +25,11 @@ type rendererGetPostsRes struct { HasMore bool } +type rendererGetPostSeriesNextPreviousRes struct { + Next *post.StoredPost + Previous *post.StoredPost +} + type renderer struct { url *url.URL postStore post.Store @@ -43,6 +48,41 @@ func (r renderer) GetPostByID(id string) (post.StoredPost, error) { return p, nil } +func (r renderer) GetPostSeriesNextPrevious(p post.StoredPost) (rendererGetPostSeriesNextPreviousRes, error) { + + seriesPosts, err := r.postStore.GetBySeries(p.Series) + if err != nil { + return rendererGetPostSeriesNextPreviousRes{}, fmt.Errorf( + "fetching posts for series %q: %w", p.Series, err, + ) + } + + var ( + res rendererGetPostSeriesNextPreviousRes + foundThis bool + ) + + for i := range seriesPosts { + + seriesPost := seriesPosts[i] + + if seriesPost.ID == p.ID { + foundThis = true + continue + } + + if !foundThis { + res.Next = &seriesPost + continue + } + + res.Previous = &seriesPost + break + } + + return res, nil +} + func (r renderer) GetQueryValue(key, def string) string { v := r.url.Query().Get(key) if v == "" { @@ -97,10 +137,6 @@ func (a *api) tplHandler() (gemini.Handler, error) { r *gemini.Request, ) { - if strings.HasSuffix(r.URL.Path, "/") { - r.URL.Path += "index.gmi" - } - tplPath := strings.TrimPrefix(r.URL.Path, "/") ctx = mctx.Annotate(ctx, |