Add locked thread guards
This commit is contained in:
@@ -10,7 +10,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const DevMode = true
|
||||
const DevMode = false
|
||||
|
||||
func FormatBytes(bytes int64) string {
|
||||
const (
|
||||
|
||||
+20
-8
@@ -271,6 +271,13 @@ func main() {
|
||||
slug := chi.URLParam(r, "slug")
|
||||
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
|
||||
ban, err := database.GetBan(db, ipHash)
|
||||
if err != nil {
|
||||
@@ -295,6 +302,19 @@ func main() {
|
||||
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
|
||||
r.Body = http.MaxBytesReader(w, r.Body, util.MAX_REQUEST_BYTES)
|
||||
if err := r.ParseMultipartForm(util.FILE_MEM_LIMIT); err != nil {
|
||||
@@ -366,14 +386,6 @@ func main() {
|
||||
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 {
|
||||
http.Error(w, "Failed to create post", http.StatusInternalServerError)
|
||||
log.Printf("PutPost: %v", err)
|
||||
|
||||
@@ -216,7 +216,9 @@ 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">
|
||||
@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>
|
||||
@ThreadActionBar(thread, "top")
|
||||
<div
|
||||
|
||||
Reference in New Issue
Block a user