diff --git a/internal/util/cooldowns.go b/internal/util/cooldowns.go index aab1096..e1576f4 100644 --- a/internal/util/cooldowns.go +++ b/internal/util/cooldowns.go @@ -25,16 +25,16 @@ const THREAD_COOLDOWN = 2 * time.Minute // const THREAD_COOLDOWN = 0 * time.Minute -func IsOnCooldown(ip string, m map[string]time.Time, duration time.Duration) bool { +func IsOnCooldown(ip string, m map[string]time.Time, duration time.Duration) time.Duration { CooldownMutex.Lock() defer CooldownMutex.Unlock() last, exists := m[ip] if !exists || time.Since(last) >= duration { m[ip] = time.Now() - return false + return time.Duration(0) } - return true + return duration - time.Since(last) } func GetIP(r *http.Request) string { diff --git a/web/main.go b/web/main.go index f2fdc4d..b7e24e9 100644 --- a/web/main.go +++ b/web/main.go @@ -126,8 +126,12 @@ func main() { // CREATE THREAD r.Post("/{slug}/threads", func(w http.ResponseWriter, r *http.Request) { ip := util.GetIP(r) - if util.IsOnCooldown(ip, util.ThreadCooldowns, util.THREAD_COOLDOWN) { - http.Error(w, "Too many posts at once.", http.StatusTooManyRequests) + + timeRemaining := util.IsOnCooldown(ip, util.ThreadCooldowns, util.THREAD_COOLDOWN) + if timeRemaining > 0 { + response := fmt.Sprintf("Please wait %.0f seconds.", timeRemaining.Seconds()) + + http.Error(w, response, http.StatusTooManyRequests) return } @@ -167,8 +171,12 @@ func main() { // CREATE POST r.Post("/{slug}/threads/{threadId}", func(w http.ResponseWriter, r *http.Request) { ip := util.GetIP(r) - if util.IsOnCooldown(ip, util.PostCooldowns, util.POST_COOLDOWN) { - http.Error(w, "Too many posts at once.", http.StatusTooManyRequests) + + timeRemaining := util.IsOnCooldown(ip, util.PostCooldowns, util.POST_COOLDOWN) + if timeRemaining > 0 { + response := fmt.Sprintf("Please wait %.0f seconds.", timeRemaining.Seconds()) + + http.Error(w, response, http.StatusTooManyRequests) return } diff --git a/web/static/index.css b/web/static/index.css index ab280d7..2cc6b40 100644 --- a/web/static/index.css +++ b/web/static/index.css @@ -128,6 +128,10 @@ body { color: #ff0000 } +#newPostForm table { + margin: auto; +} + #newPostForm button, #newThreadForm button { display: block; diff --git a/web/views/board.templ b/web/views/board.templ index 21cdbe8..2dd2c0b 100644 --- a/web/views/board.templ +++ b/web/views/board.templ @@ -9,9 +9,7 @@ import ( ) templ NewThreadForm(board database.Board) { -
+