diff --git a/internal/database/handlers.go b/internal/database/handlers.go index 62af2ea..8226fcb 100644 --- a/internal/database/handlers.go +++ b/internal/database/handlers.go @@ -6,6 +6,7 @@ import ( "io/fs" "os" "path" + "time" "github.com/dominicf2001/comfychan/internal/util" ) @@ -95,7 +96,8 @@ func GetThread(db *sql.DB, threadId int) (Thread, error) { func GetPosts(db *sql.DB, threadId int) ([]Post, error) { rows, err := db.Query(` - SELECT id, thread_id, author, body, created_at, media_path, ip_hash, number, thumb_path + SELECT id, thread_id, author, body, created_at, media_path, + ip_hash, number, thumb_path, banned FROM posts WHERE thread_id = ?`, threadId) @@ -107,7 +109,9 @@ func GetPosts(db *sql.DB, threadId int) ([]Post, error) { var result []Post for rows.Next() { var p Post - err := rows.Scan(&p.Id, &p.ThreadId, &p.Author, &p.Body, &p.CreatedAt, &p.MediaPath, &p.IpHash, &p.Number, &p.ThumbPath) + err := rows.Scan( + &p.Id, &p.ThreadId, &p.Author, &p.Body, &p.CreatedAt, &p.MediaPath, + &p.IpHash, &p.Number, &p.ThumbPath, &p.Banned) if err != nil { return nil, err } @@ -117,15 +121,35 @@ func GetPosts(db *sql.DB, threadId int) ([]Post, error) { return result, rows.Err() } +func GetPost(db *sql.DB, postId int) (Post, error) { + row := db.QueryRow(` + SELECT id, thread_id, author, body, created_at, media_path, + ip_hash, number, thumb_path, banned + FROM posts + WHERE id = ?`, postId) + + var r Post + err := row.Scan( + &r.Id, &r.ThreadId, &r.Author, &r.Body, &r.CreatedAt, &r.MediaPath, + &r.IpHash, &r.Number, &r.ThumbPath, &r.Banned) + if err != nil { + return Post{}, err + } + return r, row.Err() +} + func GetOriginalPost(db *sql.DB, threadId int) (Post, error) { row := db.QueryRow(` - SELECT id, thread_id, author, body, created_at, media_path, ip_hash, number, thumb_path + SELECT id, thread_id, author, body, created_at, media_path, + ip_hash, number, thumb_path, banned FROM posts WHERE thread_id = ? ORDER BY created_at ASC LIMIT 1`, threadId) var r Post - err := row.Scan(&r.Id, &r.ThreadId, &r.Author, &r.Body, &r.CreatedAt, &r.MediaPath, &r.IpHash, &r.Number, &r.ThumbPath) + err := row.Scan( + &r.Id, &r.ThreadId, &r.Author, &r.Body, &r.CreatedAt, &r.MediaPath, + &r.IpHash, &r.Number, &r.ThumbPath, &r.Banned) if err != nil { return Post{}, err } @@ -324,3 +348,10 @@ func GetAdmin(db *sql.DB, username string) (Admin, error) { return result, nil } + +func BanIp(db *sql.DB, ip string, reason string, expiration time.Time) error { + _, err := db.Exec(` + INSERT INTO bans (ip_hash, reason, expiration) + VALUES (?, ?, ?)`, ip, reason, expiration) + return err +} diff --git a/internal/database/models.go b/internal/database/models.go index 6a98077..c99d897 100644 --- a/internal/database/models.go +++ b/internal/database/models.go @@ -27,6 +27,7 @@ type Post struct { ThumbPath string IpHash string Number int + Banned bool } type Admin struct { diff --git a/internal/database/seed.sql b/internal/database/seed.sql index 69cd003..25b1174 100644 --- a/internal/database/seed.sql +++ b/internal/database/seed.sql @@ -24,6 +24,7 @@ CREATE TABLE IF NOT EXISTS posts ( id INTEGER PRIMARY KEY AUTOINCREMENT, thread_id INTEGER NOT NULL, number INTEGER NOT NULL , + banned BOOLEAN NOT NULL DEFAULT 0, author TEXT DEFAULT 'Anonymous', body TEXT NOT NULL, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, @@ -39,6 +40,13 @@ CREATE TABLE IF NOT EXISTS admins ( password TEXT NOT NULL ); +CREATE TABLE IF NOT EXISTS bans ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + ip_hash TEXT NOT NULL, + reason TEXT NOT NULL, + expiration DATETIME NOT NULL +); + -- ====================== -- Seed data -- ====================== diff --git a/web/main.go b/web/main.go index ac9dbba..2ed5fc9 100644 --- a/web/main.go +++ b/web/main.go @@ -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 + } + }) }) // ----------------- diff --git a/web/static/index.css b/web/static/index.css index a62751a..38bf901 100644 --- a/web/static/index.css +++ b/web/static/index.css @@ -246,6 +246,12 @@ body { color: #ff0000; } +.post-banned-message { + margin-top: 2px; + font-weight: bold; + color: red; +} + .greentext { color: #789922 } diff --git a/web/views/thread.templ b/web/views/thread.templ index 4ec20ed..9893de3 100644 --- a/web/views/thread.templ +++ b/web/views/thread.templ @@ -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 + + } templ PostOriginal(post database.Post, thread database.Thread) { @@ -130,6 +163,9 @@ templ PostReply(post database.Post, threadContext ThreadContext) { }
@templ.Raw(util.EnrichPost(post.Body)) + if post.Banned { + (USER WAS BANNED FOR THIS POST) + }
@PostAdminDialog(post)