From 293655452cfc6a106c55384e839f9c07d340b954 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sat, 21 Jan 2023 16:46:11 +0100 Subject: Continue to polish up posts pages --- src/gmi/tpl.go | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) (limited to 'src/gmi/tpl.go') 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, -- cgit v1.2.3