diff --git a/api/tag.go b/api/tag.go index 2533af7..d3d7b65 100644 --- a/api/tag.go +++ b/api/tag.go @@ -72,7 +72,8 @@ func (client *Client) FetchTag(tag string, sort string, page string) (Tag, error func(key, value gjson.Result) bool { url, _ := url.Parse(strings.ReplaceAll(value.Get("url").String(), "https://imgur.com", "")) q := url.Query() - q.Add("tag", tag+"."+sort+"."+page+"."+key.String()) + ts := tag + "." + sort + "." + page + "." + key.String() + q.Add("tag", ts) url.RawQuery = q.Encode() posts = append(posts, Submission{ @@ -90,6 +91,8 @@ func (client *Client) FetchTag(tag string, sort string, page string) (Tag, error Comments: value.Get("comment_count").Int(), Views: value.Get("view_count").Int(), IsAlbum: value.Get("is_album").Bool(), + + tagstring: ts, }) return true diff --git a/api/user.go b/api/user.go index 576373f..5adf892 100644 --- a/api/user.go +++ b/api/user.go @@ -33,6 +33,8 @@ type Submission struct { Comments int64 Views int64 IsAlbum bool + + tagstring string } func (client *Client) FetchUser(username string) (User, error) { diff --git a/pages/post.go b/pages/post.go index 978bd97..457e1c7 100644 --- a/pages/post.go +++ b/pages/post.go @@ -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, diff --git a/views/post.hbs b/views/post.hbs index e14ac46..ec9aa20 100644 --- a/views/post.hbs +++ b/views/post.hbs @@ -53,9 +53,16 @@ {{#noteq next ""}} - - - +
{{/noteq}}