Get thread link buttons working

This commit is contained in:
Dominic Ferrando
2025-04-20 22:19:21 -04:00
parent c8b549115a
commit 2818c98e4e
4 changed files with 140 additions and 119 deletions
+3 -3
View File
@@ -15,14 +15,14 @@ var (
ThreadCooldowns = make(map[string]time.Time) ThreadCooldowns = make(map[string]time.Time)
) )
var cooldownMutex sync.Mutex var CooldownMutex sync.Mutex
const POST_COOLDOWN = 15 * time.Second const POST_COOLDOWN = 15 * time.Second
const THREAD_COOLDOWN = 2 * time.Minute const THREAD_COOLDOWN = 2 * 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) bool {
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 {
+10 -1
View File
@@ -264,6 +264,13 @@ func main() {
return 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) posts, err := database.GetPosts(db, threadId)
if err != nil { if err != nil {
http.Error(w, "Failed to get posts", http.StatusBadRequest) http.Error(w, "Failed to get posts", http.StatusBadRequest)
@@ -278,7 +285,7 @@ func main() {
} }
// dont pass the op post. only replies // 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() { go func() {
for range time.Tick(10 * time.Minute) { for range time.Tick(10 * time.Minute) {
util.CooldownMutex.Lock()
defer util.CooldownMutex.Unlock()
for ip, cooldown := range util.PostCooldowns { for ip, cooldown := range util.PostCooldowns {
if time.Since(cooldown) >= util.POST_COOLDOWN { if time.Since(cooldown) >= util.POST_COOLDOWN {
delete(util.PostCooldowns, ip) delete(util.PostCooldowns, ip)
+28
View File
@@ -1,5 +1,8 @@
body { body {
background: #EEF2FF; background: #EEF2FF;
padding-left: 4px;
padding-right: 4px;
margin: 0 4px;
} }
/* INDEX */ /* INDEX */
@@ -274,3 +277,28 @@ body {
margin: 5px 0; margin: 5px 0;
font-size: 12px; 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;
}
+43 -59
View File
@@ -1,36 +1,30 @@
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) {
<article id={ fmt.Sprintf("post-%d", post.Id) } class="post-op"> <article id={ fmt.Sprintf("post-%d", post.Id) } 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">
{ post.MediaPath } { post.MediaPath }
</a> </a>
</div> </div>
<img <img onclick="togglePostImage(this)" loading="lazy" src={ fmt.Sprintf("/static/img/posts/thumb/%s", post.MediaPath)
onclick="togglePostImage(this)" } class="post-img" />
loading="lazy"
src={ fmt.Sprintf("/static/img/posts/thumb/%s", post.MediaPath) }
class="post-img"
/>
<header class="post-header"> <header 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 <span _={ fmt.Sprintf("on click set #newPostBody.value to #newPostBody.value + '>>%d\n'", post.Id) }
_={ fmt.Sprintf("on click set #newPostBody.value to #newPostBody.value + '>>%d\n'", post.Id) } class=" post-num">
class=" post-num"
>
No.{ strconv.Itoa(post.Id) } No.{ strconv.Itoa(post.Id) }
</span> </span>
<span class="post-replies"></span> <span class="post-replies"></span>
@@ -38,19 +32,17 @@ 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) {
<article id={ fmt.Sprintf("post-%d", post.Id) } class="post"> <article id={ fmt.Sprintf("post-%d", post.Id) } 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 <span _={ fmt.Sprintf("on click set #newPostBody.value to #newPostBody.value + '>>%d\n'", post.Id) }
_={ fmt.Sprintf("on click set #newPostBody.value to #newPostBody.value + '>>%d\n'", post.Id) } class=" post-num">
class=" post-num"
>
No.{ strconv.Itoa(post.Id) } No.{ strconv.Itoa(post.Id) }
</span> </span>
<span class="post-replies"></span> <span class="post-replies"></span>
@@ -63,43 +55,36 @@ templ PostReply(post database.Post) {
{ post.MediaPath } { post.MediaPath }
</a> </a>
</div> </div>
<img <img onclick="togglePostImage(this)" loading="lazy" src={ fmt.Sprintf("/static/img/posts/thumb/%s",
onclick="togglePostImage(this)" post.MediaPath) } class="post-img" />
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 PostReplies(posts []database.Post) { templ Posts(posts []database.Post, thread database.Thread) {
for _, post := range (posts) { @PostOriginal(posts[0], &thread)
<div>
for _, post := range (posts[1:]) {
@PostReply(post) @PostReply(post)
<br/> <br />
} }
</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">
<p>Please wait until the 15 second cooldown is finished.</p> <p>Please wait until the 15 second cooldown is finished.</p>
</div> </div>
<form <form hx-encoding="multipart/form-data" id="newPostForm" hx-swap="none" hx-post={ fmt.Sprintf("/%s/threads/%d",
hx-encoding="multipart/form-data" board.Slug, thread.Id) } _="on htmx:afterRequest
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 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 ''
@@ -107,8 +92,7 @@ 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" end">
>
<table> <table>
<tbody> <tbody>
<tr class="new-post-form-field"> <tr class="new-post-form-field">
@@ -117,22 +101,22 @@ 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>
<div class="thread"> <hr />
@PostOriginal(posts[0], &thread) <div style="margin-left: 25px;" class="link-button-container">
<div <a href={ templ.URL("/" + thread.BoardSlug) } style="margin-right: 5px;" class="link-button">[Catalog]</a>
hx-get={ fmt.Sprintf("/hx/%s/threads/%d/posts", board.Slug, thread.Id) } <a _="on click trigger refreshPosts on body" class="link-button">[Refresh]</a>
hx-trigger="refreshPosts from:body" </div>
_="on htmx:afterSwap call insertHeaderReplies()" <hr />
> <div class="thread" hx-get={ fmt.Sprintf("/hx/%s/threads/%d/posts", thread.BoardSlug, thread.Id) }
@PostReplies(posts[1:]) hx-trigger="refreshPosts from:body" _="on htmx:afterSwap call insertHeaderReplies()">
</div> @Posts(posts, thread)
</div> </div>
} }
} }