Add locked thread guards

This commit is contained in:
Dominic Ferrando
2025-04-24 20:44:48 -04:00
parent f9aad52dc5
commit f8a8bf5b1b
3 changed files with 24 additions and 10 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ import (
"strings" "strings"
) )
const DevMode = true const DevMode = false
func FormatBytes(bytes int64) string { func FormatBytes(bytes int64) string {
const ( const (
+20 -8
View File
@@ -271,6 +271,13 @@ func main() {
slug := chi.URLParam(r, "slug") slug := chi.URLParam(r, "slug")
ipHash := util.HashIp(util.GetIP(r)) ipHash := util.HashIp(util.GetIP(r))
threadIdStr := chi.URLParam(r, "threadId")
threadId, err := strconv.Atoi(threadIdStr)
if err != nil {
http.Error(w, "Invalid thread id", http.StatusBadRequest)
return
}
// guard banned ips // guard banned ips
ban, err := database.GetBan(db, ipHash) ban, err := database.GetBan(db, ipHash)
if err != nil { if err != nil {
@@ -295,6 +302,19 @@ func main() {
return return
} }
// guard if thread locked
isLocked := true
row := db.QueryRow(`SELECT locked FROM threads where id = ?`, threadId)
if err := row.Scan(&isLocked); err != nil {
http.Error(w, "Failed to check if thread locked", http.StatusInternalServerError)
return
}
if isLocked {
http.Error(w, "This thread is locked", http.StatusForbidden)
return
}
// parse form // parse form
r.Body = http.MaxBytesReader(w, r.Body, util.MAX_REQUEST_BYTES) r.Body = http.MaxBytesReader(w, r.Body, util.MAX_REQUEST_BYTES)
if err := r.ParseMultipartForm(util.FILE_MEM_LIMIT); err != nil { if err := r.ParseMultipartForm(util.FILE_MEM_LIMIT); err != nil {
@@ -366,14 +386,6 @@ func main() {
thumbPath = savedThumbPath thumbPath = savedThumbPath
} }
// put post into DB
threadIdStr := chi.URLParam(r, "threadId")
threadId, err := strconv.Atoi(threadIdStr)
if err != nil {
http.Error(w, "Invalid thread id", http.StatusBadRequest)
return
}
if err := database.PutPost(db, slug, threadId, body, mediaPath, thumbPath, ipHash); err != nil { if err := database.PutPost(db, slug, threadId, body, mediaPath, thumbPath, ipHash); err != nil {
http.Error(w, "Failed to create post", http.StatusInternalServerError) http.Error(w, "Failed to create post", http.StatusInternalServerError)
log.Printf("PutPost: %v", err) log.Printf("PutPost: %v", err)
+3 -1
View File
@@ -216,7 +216,9 @@ templ Thread(board database.Board, thread database.Thread, posts []database.Post
<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">
@shared.NewPostForm(board, fmt.Sprintf("/%s/threads/%d", board.Slug, thread.Id), false) if !thread.Locked {
@shared.NewPostForm(board, fmt.Sprintf("/%s/threads/%d", board.Slug, thread.Id), false)
}
</div> </div>
@ThreadActionBar(thread, "top") @ThreadActionBar(thread, "top")
<div <div