Get thread link buttons working
This commit is contained in:
@@ -15,14 +15,14 @@ var (
|
||||
ThreadCooldowns = make(map[string]time.Time)
|
||||
)
|
||||
|
||||
var cooldownMutex sync.Mutex
|
||||
var CooldownMutex sync.Mutex
|
||||
|
||||
const POST_COOLDOWN = 15 * time.Second
|
||||
const THREAD_COOLDOWN = 2 * time.Minute
|
||||
|
||||
func IsOnCooldown(ip string, m map[string]time.Time, duration time.Duration) bool {
|
||||
cooldownMutex.Lock()
|
||||
defer cooldownMutex.Unlock()
|
||||
CooldownMutex.Lock()
|
||||
defer CooldownMutex.Unlock()
|
||||
|
||||
last, exists := m[ip]
|
||||
if !exists || time.Since(last) >= duration {
|
||||
|
||||
+10
-1
@@ -264,6 +264,13 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
thread, err := database.GetThread(db, threadId)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to get thread", http.StatusBadRequest)
|
||||
log.Printf("GetThread: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
posts, err := database.GetPosts(db, threadId)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to get posts", http.StatusBadRequest)
|
||||
@@ -278,7 +285,7 @@ func main() {
|
||||
}
|
||||
|
||||
// dont pass the op post. only replies
|
||||
views.PostReplies(posts[1:]).Render(r.Context(), w)
|
||||
views.Posts(posts, thread).Render(r.Context(), w)
|
||||
})
|
||||
|
||||
// -----------------
|
||||
@@ -289,6 +296,8 @@ func main() {
|
||||
|
||||
go func() {
|
||||
for range time.Tick(10 * time.Minute) {
|
||||
util.CooldownMutex.Lock()
|
||||
defer util.CooldownMutex.Unlock()
|
||||
for ip, cooldown := range util.PostCooldowns {
|
||||
if time.Since(cooldown) >= util.POST_COOLDOWN {
|
||||
delete(util.PostCooldowns, ip)
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
body {
|
||||
background: #EEF2FF;
|
||||
padding-left: 4px;
|
||||
padding-right: 4px;
|
||||
margin: 0 4px;
|
||||
}
|
||||
|
||||
/* INDEX */
|
||||
@@ -274,3 +277,28 @@ body {
|
||||
margin: 5px 0;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.link-button {
|
||||
background: none;
|
||||
border: none;
|
||||
padding: 0;
|
||||
font-size: .8rem;
|
||||
color: #34345C;
|
||||
cursor: pointer;
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.link-button:hover {
|
||||
color: #ff0000;
|
||||
}
|
||||
|
||||
hr {
|
||||
border: none;
|
||||
border-top-width: medium;
|
||||
border-top-style: none;
|
||||
border-top-color: currentcolor;
|
||||
border-top:
|
||||
1px solid #B7C5D9;
|
||||
height: 0;
|
||||
clear: left;
|
||||
}
|
||||
|
||||
+99
-115
@@ -1,105 +1,90 @@
|
||||
package views
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/dominicf2001/comfychan/internal/database"
|
||||
"github.com/dominicf2001/comfychan/internal/util"
|
||||
"github.com/dominicf2001/comfychan/web/views/shared"
|
||||
"strconv"
|
||||
"fmt"
|
||||
"github.com/dominicf2001/comfychan/internal/database"
|
||||
"github.com/dominicf2001/comfychan/internal/util"
|
||||
"github.com/dominicf2001/comfychan/web/views/shared"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
templ PostOriginal(post database.Post, thread *database.Thread) {
|
||||
<article id={ fmt.Sprintf("post-%d", post.Id) } class="post-op">
|
||||
<div>
|
||||
<article id={ fmt.Sprintf("post-%d", post.Id) } class="post-op">
|
||||
<div>
|
||||
File:
|
||||
<a href={ templ.URL("/static/img/posts/full/" + post.MediaPath) } class="post-filename">
|
||||
{ post.MediaPath }
|
||||
</a>
|
||||
</div>
|
||||
<img onclick="togglePostImage(this)" loading="lazy" src={ fmt.Sprintf("/static/img/posts/thumb/%s", post.MediaPath)
|
||||
} class="post-img" />
|
||||
<header 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.Id) }
|
||||
class=" post-num">
|
||||
No.{ strconv.Itoa(post.Id) }
|
||||
</span>
|
||||
<span class="post-replies"></span>
|
||||
</header>
|
||||
<p class="post-body">
|
||||
@templ.Raw(util.EnrichPost(post.Body))
|
||||
</p>
|
||||
</article>
|
||||
}
|
||||
|
||||
templ PostReply(post database.Post) {
|
||||
<article id={ fmt.Sprintf("post-%d", post.Id) } class="post">
|
||||
<header class="post-header">
|
||||
<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.Id) }
|
||||
class=" post-num">
|
||||
No.{ strconv.Itoa(post.Id) }
|
||||
</span>
|
||||
<span class="post-replies"></span>
|
||||
</header>
|
||||
if post.MediaPath != "" {
|
||||
<div>
|
||||
<div style="margin-bottom: 2px;">
|
||||
File:
|
||||
<a href={ templ.URL("/static/img/posts/full/" + post.MediaPath) } class="post-filename">
|
||||
{ post.MediaPath }
|
||||
</a>
|
||||
</div>
|
||||
<img
|
||||
onclick="togglePostImage(this)"
|
||||
loading="lazy"
|
||||
src={ fmt.Sprintf("/static/img/posts/thumb/%s", post.MediaPath) }
|
||||
class="post-img"
|
||||
/>
|
||||
<header 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.Id) }
|
||||
class=" post-num"
|
||||
>
|
||||
No.{ strconv.Itoa(post.Id) }
|
||||
</span>
|
||||
<span class="post-replies"></span>
|
||||
</header>
|
||||
<p class="post-body">
|
||||
@templ.Raw(util.EnrichPost(post.Body))
|
||||
</p>
|
||||
</article>
|
||||
}
|
||||
|
||||
templ PostReply(post database.Post) {
|
||||
<article id={ fmt.Sprintf("post-%d", post.Id) } class="post">
|
||||
<header class="post-header">
|
||||
<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.Id) }
|
||||
class=" post-num"
|
||||
>
|
||||
No.{ strconv.Itoa(post.Id) }
|
||||
</span>
|
||||
<span class="post-replies"></span>
|
||||
</header>
|
||||
if post.MediaPath != "" {
|
||||
<div>
|
||||
<div style="margin-bottom: 2px;">
|
||||
File:
|
||||
<a href={ templ.URL("/static/img/posts/full/" + post.MediaPath) } class="post-filename">
|
||||
{ post.MediaPath }
|
||||
</a>
|
||||
</div>
|
||||
<img
|
||||
onclick="togglePostImage(this)"
|
||||
loading="lazy"
|
||||
src={ fmt.Sprintf("/static/img/posts/thumb/%s",
|
||||
post.MediaPath) }
|
||||
class="post-img"
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
<p class="post-body">
|
||||
@templ.Raw(util.EnrichPost(post.Body))
|
||||
</p>
|
||||
</article>
|
||||
}
|
||||
|
||||
templ PostReplies(posts []database.Post) {
|
||||
for _, post := range (posts) {
|
||||
@PostReply(post)
|
||||
<br/>
|
||||
<img onclick="togglePostImage(this)" loading="lazy" src={ fmt.Sprintf("/static/img/posts/thumb/%s",
|
||||
post.MediaPath) } class="post-img" />
|
||||
</div>
|
||||
}
|
||||
<p class="post-body">
|
||||
@templ.Raw(util.EnrichPost(post.Body))
|
||||
</p>
|
||||
</article>
|
||||
}
|
||||
|
||||
templ Posts(posts []database.Post, thread database.Thread) {
|
||||
@PostOriginal(posts[0], &thread)
|
||||
<div>
|
||||
for _, post := range (posts[1:]) {
|
||||
@PostReply(post)
|
||||
<br />
|
||||
}
|
||||
</div>
|
||||
}
|
||||
|
||||
templ Thread(board database.Board, thread database.Thread, posts []database.Post) {
|
||||
@shared.Layout() {
|
||||
<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
|
||||
@shared.Layout() {
|
||||
<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
|
||||
if event.detail.xhr.status is not 429
|
||||
set #newPostBody.value to ''
|
||||
then set #newPostFile.value to ''
|
||||
@@ -107,32 +92,31 @@ templ Thread(board database.Board, thread database.Thread, posts []database.Post
|
||||
then trigger refreshPosts on body
|
||||
else
|
||||
show #newPostWarning
|
||||
end"
|
||||
>
|
||||
<table>
|
||||
<tbody>
|
||||
<tr class="new-post-form-field">
|
||||
<th>comment</th>
|
||||
<td><textarea id="newPostBody" name="body" required></textarea></td>
|
||||
</tr>
|
||||
<tr class="new-post-form-field">
|
||||
<th>File</th>
|
||||
<td><input id="newPostFile" name="file" type="file"/></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<button type="submit">Submit</button>
|
||||
</form>
|
||||
</div>
|
||||
<div class="thread">
|
||||
@PostOriginal(posts[0], &thread)
|
||||
<div
|
||||
hx-get={ fmt.Sprintf("/hx/%s/threads/%d/posts", board.Slug, thread.Id) }
|
||||
hx-trigger="refreshPosts from:body"
|
||||
_="on htmx:afterSwap call insertHeaderReplies()"
|
||||
>
|
||||
@PostReplies(posts[1:])
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
end">
|
||||
<table>
|
||||
<tbody>
|
||||
<tr class="new-post-form-field">
|
||||
<th>comment</th>
|
||||
<td><textarea id="newPostBody" name="body" required></textarea></td>
|
||||
</tr>
|
||||
<tr class="new-post-form-field">
|
||||
<th>File</th>
|
||||
<td><input id="newPostFile" name="file" type="file" /></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<button type="submit">Submit</button>
|
||||
</form>
|
||||
</div>
|
||||
<hr />
|
||||
<div style="margin-left: 25px;" class="link-button-container">
|
||||
<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>
|
||||
</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()">
|
||||
@Posts(posts, thread)
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user