Add ability to ban ips

This commit is contained in:
Dominic Ferrando
2025-04-23 20:47:23 -04:00
parent c5453ed50a
commit 807ac94cc6
6 changed files with 126 additions and 4 deletions
+40
View File
@@ -544,6 +544,46 @@ func main() {
return
}
})
// bans the ip stored in the post id
r.Post("/ban/{postId}", func(w http.ResponseWriter, r *http.Request) {
postIdStr := chi.URLParam(r, "postId")
postId, err := strconv.Atoi(postIdStr)
if err != nil {
http.Error(w, "Invalid post id", http.StatusBadRequest)
return
}
_, err = db.Exec(`UPDATE posts SET banned = 1 WHERE id = ?`, postId)
if err != nil {
http.Error(w, "Failed update post to banned", http.StatusInternalServerError)
return
}
post, err := database.GetPost(db, postId)
if err != nil {
log.Println("GetPost: ", err)
http.Error(w, "Failed to get post: "+postIdStr, http.StatusInternalServerError)
return
}
ipToBan := post.IpHash
reason := r.FormValue("reason")
expirationInput := r.FormValue("expiration")
expiration, err := time.Parse("2006-01-02T15:04", expirationInput)
if err != nil {
http.Error(w, "Invalid expiration datetime value", http.StatusBadRequest)
return
}
err = database.BanIp(db, ipToBan, reason, expiration)
if err != nil {
log.Println("BanIp: ", err)
http.Error(w, "Failed to ban ip: ", http.StatusInternalServerError)
return
}
})
})
// -----------------
+6
View File
@@ -246,6 +246,12 @@ body {
color: #ff0000;
}
.post-banned-message {
margin-top: 2px;
font-weight: bold;
color: red;
}
.greentext {
color: #789922
}
+36
View File
@@ -22,11 +22,44 @@ templ PostAdminDialog(post database.Post) {
_="on htmx:afterRequest trigger refreshPosts on body"
hx-confirm="Are you sure you wish to delete this post?"
>Delete</button>
<button
class="link-button"
_={ fmt.Sprintf(`on click call #%s-dialog-2.showModal()`, elPostId) }
>IP ban</button>
<button
class="admin-dialog-close-btn link-button"
_={ fmt.Sprintf("on click call #%s-dialog.close()", elPostId) }
>Close</button>
</dialog>
<dialog
id={ elPostId + "-dialog-2" }
class="admin-dialog"
>
<h1>Ban info</h1>
<form
hx-post={ fmt.Sprintf("/admin/ban/%d", post.Id) }
hx-swap="none"
_="on htmx:afterRequest trigger refreshPosts on body"
hx-confirm="Are you sure you wish to ban this post's ip?"
>
<div style="margin-bottom: 5px;">
<span>Reason: </span>
<input name="reason"/>
</div>
<div>
<span>Days: </span>
<input type="datetime-local" name="expiration"/>
</div>
<button
type="submit"
class="link-button"
>Ban</button>
</form>
<button
class="admin-dialog-close-btn link-button"
_={ fmt.Sprintf("on click call #%s-dialog-2.close()", elPostId) }
>Cancel</button>
</dialog>
}
templ PostOriginal(post database.Post, thread database.Thread) {
@@ -130,6 +163,9 @@ templ PostReply(post database.Post, threadContext ThreadContext) {
}
<p class="post-body">
@templ.Raw(util.EnrichPost(post.Body))
if post.Banned {
<strong class="post-banned-message">(USER WAS BANNED FOR THIS POST)</strong>
}
</p>
@PostAdminDialog(post)
</article>