Add error page for ratelimit

This commit is contained in:
video-prize-ranch 2022-09-10 10:11:17 -04:00
parent f0c1b7f4fe
commit 1263e66f45
No known key found for this signature in database
GPG Key ID: D8EAA4C5B12A7281
6 changed files with 54 additions and 27 deletions

View File

@ -22,9 +22,11 @@ func HandleEmbed(c *fiber.Ctx) error {
default: default:
post, err = api.FetchMedia(c.Params("postID")) post, err = api.FetchMedia(c.Params("postID"))
} }
if err != nil && err.Error() == "ratelimited by imgur" {
return c.Status(429).Render("errors/429", nil)
}
if post.Id == "" || (err != nil && strings.Contains(err.Error(), "404")) { if post.Id == "" || (err != nil && strings.Contains(err.Error(), "404")) {
c.Status(404) return c.Status(404).Render("errors/404", nil)
return c.Render("errors/404", nil)
} }
if err != nil { if err != nil {
return err return err

View File

@ -23,9 +23,11 @@ func HandlePost(c *fiber.Ctx) error {
default: default:
post, err = api.FetchMedia(c.Params("postID")) post, err = api.FetchMedia(c.Params("postID"))
} }
if err != nil && err.Error() == "ratelimited by imgur" {
return c.Status(429).Render("errors/429", nil)
}
if post.Id == "" || (err != nil && strings.Contains(err.Error(), "404")) { if post.Id == "" || (err != nil && strings.Contains(err.Error(), "404")) {
c.Status(404) return c.Status(404).Render("errors/404", nil)
return c.Render("errors/404", nil)
} }
if err != nil { if err != nil {
return err return err

View File

@ -30,12 +30,14 @@ func HandleTag(c *fiber.Ctx) error {
} }
tag, err := api.FetchTag(c.Params("tag"), c.Query("sort"), page) tag, err := api.FetchTag(c.Params("tag"), c.Query("sort"), page)
if err != nil && err.Error() == "ratelimited by imgur" {
return c.Status(429).Render("errors/429", nil)
}
if err != nil { if err != nil {
return err return err
} }
if tag.Display == "" { if tag.Display == "" {
c.Status(404) return c.Status(404).Render("errors/404", nil)
return c.Render("errors/404", nil)
} }
return c.Render("tag", fiber.Map{ return c.Render("tag", fiber.Map{

View File

@ -2,7 +2,6 @@ package pages
import ( import (
"strconv" "strconv"
"sync"
"codeberg.org/video-prize-ranch/rimgo/api" "codeberg.org/video-prize-ranch/rimgo/api"
"codeberg.org/video-prize-ranch/rimgo/utils" "codeberg.org/video-prize-ranch/rimgo/utils"
@ -25,30 +24,24 @@ func HandleUser(c *fiber.Ctx) error {
pageNumber = 0 pageNumber = 0
} }
wg := sync.WaitGroup{} user, err := api.FetchUser(c.Params("userID"))
wg.Add(2) if err != nil && err.Error() == "ratelimited by imgur" {
user, err := api.User{}, error(nil) return c.Status(429).Render("errors/429", nil)
go func() { }
defer wg.Done()
user, err = api.FetchUser(c.Params("userID"))
}()
if err != nil { if err != nil {
return err return err
} }
submissions, err := []api.Submission{}, error(nil)
go func() {
defer wg.Done()
submissions, err = api.FetchSubmissions(c.Params("userID"), "newest", page)
}()
if err != nil {
return err
}
wg.Wait()
if user.Username == "" { if user.Username == "" {
c.Status(404) return c.Status(404).Render("errors/404", nil)
return c.Render("errors/404", nil) }
submissions, err := api.FetchSubmissions(c.Params("userID"), "newest", page)
if err != nil && err.Error() == "ratelimited by imgur" {
c.Status(429)
return c.Render("errors/429", nil)
}
if err != nil {
return err
} }
return c.Render("user", fiber.Map{ return c.Render("user", fiber.Map{

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"strconv"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
) )
@ -30,6 +31,13 @@ func GetJSON(url string) (gjson.Result, error) {
if err != nil { if err != nil {
return gjson.Result{}, err return gjson.Result{}, err
} }
rateLimitRemaining := res.Header.Get("X-RateLimit-UserRemaining")
if rateLimitRemaining != "" {
ratelimit, _ := strconv.Atoi(rateLimitRemaining)
if ratelimit <= 0 {
return gjson.Result{}, fmt.Errorf("ratelimited by imgur")
}
}
body, err := io.ReadAll(res.Body) body, err := io.ReadAll(res.Body)
if err != nil { if err != nil {

20
views/errors/429.hbs Normal file
View File

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
{{> 'partials/head' }}
<title>404 - rimgo</title>
</head>
<body>
{{> 'partials/header' }}
<main>
<h2 class="errorTitle">Rate limited by Imgur</h2>
<p class="errorTitle">This instance has been temporarily blocked by Imgur. <a href="https://codeberg.org/video-prize-ranch/rimgo#instances">Try using another instance</a>.</p>
</main>
</body>
</html>