Improve warning response rendering
This commit is contained in:
@@ -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 {
|
||||
|
||||
+12
-4
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -128,6 +128,10 @@ body {
|
||||
color: #ff0000
|
||||
}
|
||||
|
||||
#newPostForm table {
|
||||
margin: auto;
|
||||
}
|
||||
|
||||
#newPostForm button,
|
||||
#newThreadForm button {
|
||||
display: block;
|
||||
|
||||
@@ -9,9 +9,7 @@ import (
|
||||
)
|
||||
|
||||
templ NewThreadForm(board database.Board) {
|
||||
<div style="display: none;" id="newPostWarning" class="warning">
|
||||
<p>Please wait until the 2 minute cooldown is finished.</p>
|
||||
</div>
|
||||
<div style="display: none;" id="newPostWarning" class="warning"></div>
|
||||
<form
|
||||
hx-encoding="multipart/form-data"
|
||||
style="display: none;"
|
||||
@@ -29,6 +27,7 @@ templ NewThreadForm(board database.Board) {
|
||||
then trigger refreshThreads on body
|
||||
else
|
||||
show #newPostWarning
|
||||
then put event.detail.xhr.responseText into #newPostWarning
|
||||
end"
|
||||
>
|
||||
<table>
|
||||
|
||||
+37
-16
@@ -21,15 +21,21 @@ templ PostOriginal(post database.Post, thread *database.Thread) {
|
||||
<span>{ strconv.Itoa(imgInfo.Width) }</span>x<span>{ strconv.Itoa(imgInfo.Height) }</span>)
|
||||
</div>
|
||||
</div>
|
||||
<img onclick="togglePostImage(this)" loading="lazy" src={ fmt.Sprintf("/static/img/posts/thumb/%s", post.MediaPath)
|
||||
} class="post-img" />
|
||||
<img
|
||||
onclick="togglePostImage(this)"
|
||||
loading="lazy"
|
||||
src={ fmt.Sprintf("/static/img/posts/thumb/%s", post.MediaPath) }
|
||||
class="post-img"
|
||||
/>
|
||||
<header style="margin-top: 10px;" class="post-header">
|
||||
<h1 class="thread-subject">{ thread.Subject } -</h1>
|
||||
<span class="post-author">{ post.Author }</span>
|
||||
<span class="post-date">{ post.CreatedAt.Format("01/02/2006") }</span>
|
||||
<span class="post-time">{ post.CreatedAt.Format("15:04") }</span>
|
||||
<span _={ fmt.Sprintf("on click set #newPostBody.value to #newPostBody.value + '>>%d\n'", post.Number) }
|
||||
class=" post-num">
|
||||
<span
|
||||
_={ fmt.Sprintf("on click set #newPostBody.value to #newPostBody.value + '>>%d\n'", post.Number) }
|
||||
class=" post-num"
|
||||
>
|
||||
No.{ strconv.Itoa(post.Number) }
|
||||
</span>
|
||||
<span class="post-replies"></span>
|
||||
@@ -47,8 +53,10 @@ templ PostReply(post database.Post) {
|
||||
<span class="post-author">{ post.Author }</span>
|
||||
<span class="post-date">{ post.CreatedAt.Format("01/02/2006") }</span>
|
||||
<span class="post-time">{ post.CreatedAt.Format("15:04") }</span>
|
||||
<span _={ fmt.Sprintf("on click set #newPostBody.value to #newPostBody.value + '>>%d\n'", post.Number) }
|
||||
class=" post-num">
|
||||
<span
|
||||
_={ fmt.Sprintf("on click set #newPostBody.value to #newPostBody.value + '>>%d\n'", post.Number) }
|
||||
class=" post-num"
|
||||
>
|
||||
No.{ strconv.Itoa(post.Number) }
|
||||
</span>
|
||||
<span class="post-replies"></span>
|
||||
@@ -65,8 +73,13 @@ templ PostReply(post database.Post) {
|
||||
<span>{ strconv.Itoa(imgInfo.Width) }</span>x<span>{ strconv.Itoa(imgInfo.Height) }</span>)
|
||||
</div>
|
||||
</div>
|
||||
<img onclick="togglePostImage(this)" loading="lazy" src={ fmt.Sprintf("/static/img/posts/thumb/%s",
|
||||
post.MediaPath) } class="post-img" />
|
||||
<img
|
||||
onclick="togglePostImage(this)"
|
||||
loading="lazy"
|
||||
src={ fmt.Sprintf("/static/img/posts/thumb/%s",
|
||||
post.MediaPath) }
|
||||
class="post-img"
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
<p class="post-body">
|
||||
@@ -90,11 +103,13 @@ templ Thread(board database.Board, thread database.Thread, posts []database.Post
|
||||
<script src="/static/thread.js" defer></script>
|
||||
@BoardHeader(board)
|
||||
<div class="new-post-container">
|
||||
<div style="display: none;" id="newPostWarning" class="warning">
|
||||
<p>Please wait until the 15 second cooldown is finished.</p>
|
||||
</div>
|
||||
<form hx-encoding="multipart/form-data" id="newPostForm" hx-swap="none" hx-post={ fmt.Sprintf("/%s/threads/%d",
|
||||
board.Slug, thread.Id) } _="on htmx:afterRequest
|
||||
<div style="display: none;" id="newPostWarning" class="warning"></div>
|
||||
<form
|
||||
hx-encoding="multipart/form-data"
|
||||
id="newPostForm"
|
||||
hx-target="#newPostWarning"
|
||||
hx-post={ fmt.Sprintf("/%s/threads/%d", board.Slug, thread.Id) }
|
||||
_="on htmx:afterRequest
|
||||
if event.detail.xhr.status is not 429
|
||||
set #newPostBody.value to ''
|
||||
then set #newPostFile.value to ''
|
||||
@@ -102,7 +117,9 @@ templ Thread(board database.Board, thread database.Thread, posts []database.Post
|
||||
then trigger refreshPosts on body
|
||||
else
|
||||
show #newPostWarning
|
||||
end">
|
||||
then put event.detail.xhr.responseText into #newPostWarning
|
||||
end"
|
||||
>
|
||||
<table>
|
||||
<tbody>
|
||||
<tr class="new-post-form-field">
|
||||
@@ -124,8 +141,12 @@ templ Thread(board database.Board, thread database.Thread, posts []database.Post
|
||||
<a _="on click trigger refreshPosts on body" class="link-button">[Refresh]</a>
|
||||
</div>
|
||||
<hr/>
|
||||
<div class="thread" hx-get={ fmt.Sprintf("/hx/%s/threads/%d/posts", thread.BoardSlug, thread.Id) }
|
||||
hx-trigger="refreshPosts from:body" _="on htmx:afterSwap call insertHeaderReplies()">
|
||||
<div
|
||||
class="thread"
|
||||
hx-get={ fmt.Sprintf("/hx/%s/threads/%d/posts", thread.BoardSlug, thread.Id) }
|
||||
hx-trigger="refreshPosts from:body"
|
||||
_="on htmx:afterSwap call insertHeaderReplies()"
|
||||
>
|
||||
@Posts(posts, thread)
|
||||
</div>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user