- {{post.User.Username}} -
- - {{/if}} -{{post.Views}}
-{{post.Upvotes}}
-{{post.Downvotes}}
-+ {{post.User.Username}} +
+ {{/if}} +{{post.Views}}
+{{post.Upvotes}}
+{{post.Downvotes}}
+{{{this.Description}}}
{{/if}} - {{/each}} + {{/each}}diff --git a/api/tag.go b/api/tag.go index f4c7c39..2533af7 100644 --- a/api/tag.go +++ b/api/tag.go @@ -3,9 +3,9 @@ package api import ( "io" "net/http" + "net/url" "strings" - "sync" - + "github.com/patrickmn/go-cache" "github.com/tidwall/gjson" ) @@ -21,6 +21,9 @@ type Tag struct { } func (client *Client) FetchTag(tag string, sort string, page string) (Tag, error) { + // Dots are automatically removed on Imgur, so more cache hits + tag = strings.ReplaceAll(tag, ".", "") + cacheData, found := client.Cache.Get(tag + sort + page + "-tag") if found { return cacheData.(Tag), nil @@ -64,47 +67,44 @@ func (client *Client) FetchTag(tag string, sort string, page string) (Tag, error data := gjson.Parse(string(body)) - wg := sync.WaitGroup{} posts := make([]Submission, 0) data.Get("posts").ForEach( func(key, value gjson.Result) bool { - wg.Add(1) + url, _ := url.Parse(strings.ReplaceAll(value.Get("url").String(), "https://imgur.com", "")) + q := url.Query() + q.Add("tag", tag+"."+sort+"."+page+"."+key.String()) + url.RawQuery = q.Encode() - go func() { - defer wg.Done() - posts = append(posts, Submission{ - Id: value.Get("id").String(), - Title: value.Get("title").String(), - Link: strings.ReplaceAll(value.Get("url").String(), "https://imgur.com", ""), - Cover: Media{ - Id: value.Get("cover_id").String(), - Type: value.Get("cover.type").String(), - Url: strings.ReplaceAll(value.Get("cover.url").String(), "https://i.imgur.com", ""), - }, - Points: value.Get("point_count").Int(), - Upvotes: value.Get("upvote_count").Int(), - Downvotes: value.Get("downvote_count").Int(), - Comments: value.Get("comment_count").Int(), - Views: value.Get("view_count").Int(), - IsAlbum: value.Get("is_album").Bool(), - }) - }() + posts = append(posts, Submission{ + Id: value.Get("id").String(), + Title: value.Get("title").String(), + Link: url.String(), + Cover: Media{ + Id: value.Get("cover_id").String(), + Type: value.Get("cover.type").String(), + Url: strings.ReplaceAll(value.Get("cover.url").String(), "https://i.imgur.com", ""), + }, + Points: value.Get("point_count").Int(), + Upvotes: value.Get("upvote_count").Int(), + Downvotes: value.Get("downvote_count").Int(), + Comments: value.Get("comment_count").Int(), + Views: value.Get("view_count").Int(), + IsAlbum: value.Get("is_album").Bool(), + }) return true }, ) - wg.Wait() - tagData := Tag{ - Tag: tag, - Display: data.Get("display").String(), - Sort: sort, - PostCount: data.Get("post_count").Int(), - Posts: posts, - Background: "/" + data.Get("background_id").String() + ".webp", + Tag: tag, + Display: data.Get("display").String(), + Sort: sort, + PostCount: data.Get("post_count").Int(), + Posts: posts, + Background: "/" + data.Get("background_id").String() + ".webp", } - client.Cache.Set(tag + sort + page + "-tag", tagData, cache.DefaultExpiration) + client.Cache.Set(tag+sort+page+"-tag", tagData, 4*cache.DefaultExpiration) return tagData, nil } diff --git a/pages/post.go b/pages/post.go index cfa8b47..978bd97 100644 --- a/pages/post.go +++ b/pages/post.go @@ -3,6 +3,7 @@ package pages import ( "crypto/rand" "fmt" + "strconv" "strings" "codeberg.org/rimgo/rimgo/api" @@ -10,6 +11,27 @@ import ( "github.com/gofiber/fiber/v2" ) +// Cursed function +func nextInTag(client *api.Client, tagname, sort, page, I string) string { + i, err := strconv.Atoi(I) + if err != nil || i < 0 { + return "" + } + tag, err := client.FetchTag(tagname, sort, page) + if err != nil { + return "" + } + if i >= len(tag.Posts)-1 { + pageNumber, _ := strconv.Atoi(page) + tagn, err := client.FetchTag(tagname, sort, strconv.Itoa(pageNumber+1)) + if err != nil { + return "" + } + return tagn.Posts[0].Link + } + return tag.Posts[i+1].Link +} + func HandlePost(c *fiber.Ctx) error { utils.SetHeaders(c) c.Set("X-Frame-Options", "DENY") @@ -56,8 +78,16 @@ func HandlePost(c *fiber.Ctx) error { } c.Set("Content-Security-Policy", csp) + var next string + tagParam := strings.Split(c.Query("tag"), ".") + if len(tagParam) == 4 { + tag, sort, page, index := tagParam[0], tagParam[1], tagParam[2], tagParam[3] + next = nextInTag(ApiClient, tag, sort, page, index) + } + return c.Render("post", fiber.Map{ "post": post, + "next": next, "comments": comments, "nonce": nonce, }) diff --git a/views/post.hbs b/views/post.hbs index 303799c..e14ac46 100644 --- a/views/post.hbs +++ b/views/post.hbs @@ -4,7 +4,7 @@
{{post.CreatedAt}}
- +- {{post.User.Username}} -
- - {{/if}} -{{post.Views}}
-{{post.Upvotes}}
-{{post.Downvotes}}
-+ {{post.User.Username}} +
+ {{/if}} +{{post.Views}}
+{{post.Upvotes}}
+{{post.Downvotes}}
+{{{this.Description}}}
{{/if}} - {{/each}} + {{/each}}