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
|
// 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()
|
CooldownMutex.Lock()
|
||||||
defer CooldownMutex.Unlock()
|
defer CooldownMutex.Unlock()
|
||||||
|
|
||||||
last, exists := m[ip]
|
last, exists := m[ip]
|
||||||
if !exists || time.Since(last) >= duration {
|
if !exists || time.Since(last) >= duration {
|
||||||
m[ip] = time.Now()
|
m[ip] = time.Now()
|
||||||
return false
|
return time.Duration(0)
|
||||||
}
|
}
|
||||||
return true
|
return duration - time.Since(last)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetIP(r *http.Request) string {
|
func GetIP(r *http.Request) string {
|
||||||
|
|||||||
+12
-4
@@ -126,8 +126,12 @@ func main() {
|
|||||||
// CREATE THREAD
|
// CREATE THREAD
|
||||||
r.Post("/{slug}/threads", func(w http.ResponseWriter, r *http.Request) {
|
r.Post("/{slug}/threads", func(w http.ResponseWriter, r *http.Request) {
|
||||||
ip := util.GetIP(r)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,8 +171,12 @@ func main() {
|
|||||||
// CREATE POST
|
// CREATE POST
|
||||||
r.Post("/{slug}/threads/{threadId}", func(w http.ResponseWriter, r *http.Request) {
|
r.Post("/{slug}/threads/{threadId}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
ip := util.GetIP(r)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -128,6 +128,10 @@ body {
|
|||||||
color: #ff0000
|
color: #ff0000
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#newPostForm table {
|
||||||
|
margin: auto;
|
||||||
|
}
|
||||||
|
|
||||||
#newPostForm button,
|
#newPostForm button,
|
||||||
#newThreadForm button {
|
#newThreadForm button {
|
||||||
display: block;
|
display: block;
|
||||||
|
|||||||
@@ -9,9 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
templ NewThreadForm(board database.Board) {
|
templ NewThreadForm(board database.Board) {
|
||||||
<div style="display: none;" id="newPostWarning" class="warning">
|
<div style="display: none;" id="newPostWarning" class="warning"></div>
|
||||||
<p>Please wait until the 2 minute cooldown is finished.</p>
|
|
||||||
</div>
|
|
||||||
<form
|
<form
|
||||||
hx-encoding="multipart/form-data"
|
hx-encoding="multipart/form-data"
|
||||||
style="display: none;"
|
style="display: none;"
|
||||||
@@ -29,6 +27,7 @@ templ NewThreadForm(board database.Board) {
|
|||||||
then trigger refreshThreads on body
|
then trigger refreshThreads on body
|
||||||
else
|
else
|
||||||
show #newPostWarning
|
show #newPostWarning
|
||||||
|
then put event.detail.xhr.responseText into #newPostWarning
|
||||||
end"
|
end"
|
||||||
>
|
>
|
||||||
<table>
|
<table>
|
||||||
|
|||||||
+64
-43
@@ -1,16 +1,16 @@
|
|||||||
package views
|
package views
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/dominicf2001/comfychan/internal/database"
|
"github.com/dominicf2001/comfychan/internal/database"
|
||||||
"github.com/dominicf2001/comfychan/internal/util"
|
"github.com/dominicf2001/comfychan/internal/util"
|
||||||
"github.com/dominicf2001/comfychan/web/views/shared"
|
"github.com/dominicf2001/comfychan/web/views/shared"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
templ PostOriginal(post database.Post, thread *database.Thread) {
|
templ PostOriginal(post database.Post, thread *database.Thread) {
|
||||||
{{ imgInfo := util.GetPostImageInfo(post.MediaPath) }}
|
{{ imgInfo := util.GetPostImageInfo(post.MediaPath) }}
|
||||||
<article id={ fmt.Sprintf("post-%d", post.Number) } class="post-op">
|
<article id={ fmt.Sprintf("post-%d", post.Number) } class="post-op">
|
||||||
<div>
|
<div>
|
||||||
File:
|
File:
|
||||||
<a href={ templ.URL("/static/img/posts/full/" + post.MediaPath) } class="post-filename">
|
<a href={ templ.URL("/static/img/posts/full/" + post.MediaPath) } class="post-filename">
|
||||||
@@ -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>)
|
<span>{ strconv.Itoa(imgInfo.Width) }</span>x<span>{ strconv.Itoa(imgInfo.Height) }</span>)
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<img onclick="togglePostImage(this)" loading="lazy" src={ fmt.Sprintf("/static/img/posts/thumb/%s", post.MediaPath)
|
<img
|
||||||
} class="post-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">
|
<header style="margin-top: 10px;" class="post-header">
|
||||||
<h1 class="thread-subject">{ thread.Subject } -</h1>
|
<h1 class="thread-subject">{ thread.Subject } -</h1>
|
||||||
<span class="post-author">{ post.Author }</span>
|
<span class="post-author">{ post.Author }</span>
|
||||||
<span class="post-date">{ post.CreatedAt.Format("01/02/2006") }</span>
|
<span class="post-date">{ post.CreatedAt.Format("01/02/2006") }</span>
|
||||||
<span class="post-time">{ post.CreatedAt.Format("15:04") }</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) }
|
<span
|
||||||
class=" post-num">
|
_={ fmt.Sprintf("on click set #newPostBody.value to #newPostBody.value + '>>%d\n'", post.Number) }
|
||||||
|
class=" post-num"
|
||||||
|
>
|
||||||
No.{ strconv.Itoa(post.Number) }
|
No.{ strconv.Itoa(post.Number) }
|
||||||
</span>
|
</span>
|
||||||
<span class="post-replies"></span>
|
<span class="post-replies"></span>
|
||||||
@@ -37,18 +43,20 @@ templ PostOriginal(post database.Post, thread *database.Thread) {
|
|||||||
<p class="post-body">
|
<p class="post-body">
|
||||||
@templ.Raw(util.EnrichPost(post.Body))
|
@templ.Raw(util.EnrichPost(post.Body))
|
||||||
</p>
|
</p>
|
||||||
</article>
|
</article>
|
||||||
}
|
}
|
||||||
|
|
||||||
templ PostReply(post database.Post) {
|
templ PostReply(post database.Post) {
|
||||||
{{ imgInfo := util.GetPostImageInfo(post.MediaPath) }}
|
{{ imgInfo := util.GetPostImageInfo(post.MediaPath) }}
|
||||||
<article id={ fmt.Sprintf("post-%d", post.Number) } class="post">
|
<article id={ fmt.Sprintf("post-%d", post.Number) } class="post">
|
||||||
<header class="post-header">
|
<header class="post-header">
|
||||||
<span class="post-author">{ post.Author }</span>
|
<span class="post-author">{ post.Author }</span>
|
||||||
<span class="post-date">{ post.CreatedAt.Format("01/02/2006") }</span>
|
<span class="post-date">{ post.CreatedAt.Format("01/02/2006") }</span>
|
||||||
<span class="post-time">{ post.CreatedAt.Format("15:04") }</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) }
|
<span
|
||||||
class=" post-num">
|
_={ fmt.Sprintf("on click set #newPostBody.value to #newPostBody.value + '>>%d\n'", post.Number) }
|
||||||
|
class=" post-num"
|
||||||
|
>
|
||||||
No.{ strconv.Itoa(post.Number) }
|
No.{ strconv.Itoa(post.Number) }
|
||||||
</span>
|
</span>
|
||||||
<span class="post-replies"></span>
|
<span class="post-replies"></span>
|
||||||
@@ -65,36 +73,43 @@ templ PostReply(post database.Post) {
|
|||||||
<span>{ strconv.Itoa(imgInfo.Width) }</span>x<span>{ strconv.Itoa(imgInfo.Height) }</span>)
|
<span>{ strconv.Itoa(imgInfo.Width) }</span>x<span>{ strconv.Itoa(imgInfo.Height) }</span>)
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<img onclick="togglePostImage(this)" loading="lazy" src={ fmt.Sprintf("/static/img/posts/thumb/%s",
|
<img
|
||||||
post.MediaPath) } class="post-img" />
|
onclick="togglePostImage(this)"
|
||||||
|
loading="lazy"
|
||||||
|
src={ fmt.Sprintf("/static/img/posts/thumb/%s",
|
||||||
|
post.MediaPath) }
|
||||||
|
class="post-img"
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
<p class="post-body">
|
<p class="post-body">
|
||||||
@templ.Raw(util.EnrichPost(post.Body))
|
@templ.Raw(util.EnrichPost(post.Body))
|
||||||
</p>
|
</p>
|
||||||
</article>
|
</article>
|
||||||
}
|
}
|
||||||
|
|
||||||
templ Posts(posts []database.Post, thread database.Thread) {
|
templ Posts(posts []database.Post, thread database.Thread) {
|
||||||
@PostOriginal(posts[0], &thread)
|
@PostOriginal(posts[0], &thread)
|
||||||
<div>
|
<div>
|
||||||
for _, post := range (posts[1:]) {
|
for _, post := range (posts[1:]) {
|
||||||
@PostReply(post)
|
@PostReply(post)
|
||||||
<br />
|
<br/>
|
||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
|
||||||
templ Thread(board database.Board, thread database.Thread, posts []database.Post) {
|
templ Thread(board database.Board, thread database.Thread, posts []database.Post) {
|
||||||
@shared.Layout() {
|
@shared.Layout() {
|
||||||
<script src="/static/thread.js" defer></script>
|
<script src="/static/thread.js" defer></script>
|
||||||
@BoardHeader(board)
|
@BoardHeader(board)
|
||||||
<div class="new-post-container">
|
<div class="new-post-container">
|
||||||
<div style="display: none;" id="newPostWarning" class="warning">
|
<div style="display: none;" id="newPostWarning" class="warning"></div>
|
||||||
<p>Please wait until the 15 second cooldown is finished.</p>
|
<form
|
||||||
</div>
|
hx-encoding="multipart/form-data"
|
||||||
<form hx-encoding="multipart/form-data" id="newPostForm" hx-swap="none" hx-post={ fmt.Sprintf("/%s/threads/%d",
|
id="newPostForm"
|
||||||
board.Slug, thread.Id) } _="on htmx:afterRequest
|
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
|
if event.detail.xhr.status is not 429
|
||||||
set #newPostBody.value to ''
|
set #newPostBody.value to ''
|
||||||
then set #newPostFile.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
|
then trigger refreshPosts on body
|
||||||
else
|
else
|
||||||
show #newPostWarning
|
show #newPostWarning
|
||||||
end">
|
then put event.detail.xhr.responseText into #newPostWarning
|
||||||
|
end"
|
||||||
|
>
|
||||||
<table>
|
<table>
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr class="new-post-form-field">
|
<tr class="new-post-form-field">
|
||||||
@@ -111,22 +128,26 @@ templ Thread(board database.Board, thread database.Thread, posts []database.Post
|
|||||||
</tr>
|
</tr>
|
||||||
<tr class="new-post-form-field">
|
<tr class="new-post-form-field">
|
||||||
<th>File</th>
|
<th>File</th>
|
||||||
<td><input id="newPostFile" name="file" type="file" /></td>
|
<td><input id="newPostFile" name="file" type="file"/></td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
<button type="submit">Submit</button>
|
<button type="submit">Submit</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
<hr />
|
<hr/>
|
||||||
<div style="margin-left: 25px;">
|
<div style="margin-left: 25px;">
|
||||||
<a href={ templ.URL("/" + thread.BoardSlug) } style="margin-right: 5px;" class="link-button">[Catalog]</a>
|
<a href={ templ.URL("/" + thread.BoardSlug) } style="margin-right: 5px;" class="link-button">[Catalog]</a>
|
||||||
<a _="on click trigger refreshPosts on body" class="link-button">[Refresh]</a>
|
<a _="on click trigger refreshPosts on body" class="link-button">[Refresh]</a>
|
||||||
</div>
|
</div>
|
||||||
<hr />
|
<hr/>
|
||||||
<div class="thread" hx-get={ fmt.Sprintf("/hx/%s/threads/%d/posts", thread.BoardSlug, thread.Id) }
|
<div
|
||||||
hx-trigger="refreshPosts from:body" _="on htmx:afterSwap call insertHeaderReplies()">
|
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)
|
@Posts(posts, thread)
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user