add previous button

This commit is contained in:
orangix
2024-02-06 01:41:28 +01:00
parent 337796b9be
commit 0171d76fae
4 changed files with 47 additions and 5 deletions

View File

@@ -11,6 +11,34 @@ import (
"github.com/gofiber/fiber/v2"
)
func prevInTag(client *api.Client, tagname, sort, page, I string) string {
i, err := strconv.Atoi(I)
if err != nil || i < 0 {
return ""
}
if i == 0 {
// Don't go before the first in tag
if page == "1" {
return ""
}
pagen, err := strconv.Atoi(page)
if err != nil || pagen < 0 {
return ""
}
pagen--
page = strconv.Itoa(pagen)
}
tag, err := client.FetchTag(tagname, sort, page)
if err != nil {
return ""
}
if i == 0 {
return tag.Posts[len(tag.Posts)-1].Link
}
return tag.Posts[i-1].Link
}
// Cursed function
func nextInTag(client *api.Client, tagname, sort, page, I string) string {
i, err := strconv.Atoi(I)
@@ -78,15 +106,17 @@ func HandlePost(c *fiber.Ctx) error {
}
c.Set("Content-Security-Policy", csp)
var next string
var prev, next string
tagParam := strings.Split(c.Query("tag"), ".")
if len(tagParam) == 4 {
tag, sort, page, index := tagParam[0], tagParam[1], tagParam[2], tagParam[3]
prev = prevInTag(ApiClient, tag, sort, page, index)
next = nextInTag(ApiClient, tag, sort, page, index)
}
return c.Render("post", fiber.Map{
"post": post,
"prev": prev,
"next": next,
"comments": comments,
"nonce": nonce,