Implement per board post number incrementing
This commit is contained in:
@@ -4,6 +4,11 @@ import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type Queryer interface {
|
||||
Exec(query string, args ...any) (sql.Result, error)
|
||||
QueryRow(query string, args ...any) *sql.Row
|
||||
}
|
||||
|
||||
func GetBoards(db *sql.DB) ([]Board, error) {
|
||||
rows, err := db.Query(`
|
||||
SELECT id, name, slug, tag
|
||||
@@ -83,7 +88,7 @@ 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
|
||||
SELECT id, thread_id, author, body, created_at, media_path, ip_hash, number
|
||||
FROM posts
|
||||
WHERE thread_id = ?`, threadId)
|
||||
|
||||
@@ -95,7 +100,7 @@ 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)
|
||||
err := rows.Scan(&p.Id, &p.ThreadId, &p.Author, &p.Body, &p.CreatedAt, &p.MediaPath, &p.IpHash, &p.Number)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -107,13 +112,13 @@ func GetPosts(db *sql.DB, threadId int) ([]Post, error) {
|
||||
|
||||
func GetOriginalPost(db *sql.DB, threadId int) (Post, error) {
|
||||
row := db.QueryRow(`
|
||||
SELECT id, thread_id, author, body, created_at, media_path, ip_hash
|
||||
SELECT id, thread_id, author, body, created_at, media_path, ip_hash, number
|
||||
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)
|
||||
err := row.Scan(&r.Id, &r.ThreadId, &r.Author, &r.Body, &r.CreatedAt, &r.MediaPath, &r.IpHash, &r.Number)
|
||||
if err != nil {
|
||||
return Post{}, err
|
||||
}
|
||||
@@ -140,11 +145,7 @@ func PutThread(db *sql.DB, boardSlug string, subject string, body string, mediaP
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = tx.Exec(`
|
||||
INSERT INTO posts (thread_id, body, media_path, ip_hash)
|
||||
VALUES (?, ?, ?, ?)`, threadId, body, mediaPath, ip_hash)
|
||||
|
||||
if err != nil {
|
||||
if err := PutPost(tx, boardSlug, int(threadId), body, mediaPath, ip_hash); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -155,10 +156,26 @@ func PutThread(db *sql.DB, boardSlug string, subject string, body string, mediaP
|
||||
return nil
|
||||
}
|
||||
|
||||
func PutPost(db *sql.DB, threadId int, body string, mediaPath string, ip_hash string) error {
|
||||
func PutPost(db Queryer, boardSlug string, threadId int, body string, mediaPath string, ip_hash string) error {
|
||||
row := db.QueryRow(`
|
||||
SELECT MAX(p.number)
|
||||
FROM posts p
|
||||
INNER JOIN threads t ON p.thread_id = t.id
|
||||
WHERE t.board_slug = ?`, boardSlug)
|
||||
|
||||
var latestPostNumber sql.NullInt64
|
||||
if err := row.Scan(&latestPostNumber); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
newPostNumber := 1
|
||||
if latestPostNumber.Valid {
|
||||
newPostNumber = int(latestPostNumber.Int64) + 1
|
||||
}
|
||||
|
||||
_, err := db.Exec(`
|
||||
INSERT INTO posts (thread_id, body, media_path, ip_hash)
|
||||
VALUES (?, ?, ?, ?)`, threadId, body, mediaPath, ip_hash)
|
||||
INSERT INTO posts (thread_id, body, media_path, ip_hash, number)
|
||||
VALUES (?, ?, ?, ?, ?)`, threadId, body, mediaPath, ip_hash, newPostNumber)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -25,4 +25,5 @@ type Post struct {
|
||||
CreatedAt time.Time
|
||||
MediaPath string
|
||||
IpHash string
|
||||
Number int
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ CREATE TABLE IF NOT EXISTS threads (
|
||||
CREATE TABLE IF NOT EXISTS posts (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
thread_id INTEGER NOT NULL,
|
||||
number INTEGER NOT NULL,
|
||||
author TEXT DEFAULT 'Anonymous',
|
||||
body TEXT NOT NULL,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
@@ -17,8 +17,11 @@ var (
|
||||
|
||||
var CooldownMutex sync.Mutex
|
||||
|
||||
const POST_COOLDOWN = 15 * time.Second
|
||||
const THREAD_COOLDOWN = 2 * time.Minute
|
||||
// const POST_COOLDOWN = 15 * time.Second
|
||||
const POST_COOLDOWN = 0 * time.Second
|
||||
|
||||
// const THREAD_COOLDOWN = 2 * time.Minute
|
||||
const THREAD_COOLDOWN = 0 * time.Minute
|
||||
|
||||
func IsOnCooldown(ip string, m map[string]time.Time, duration time.Duration) bool {
|
||||
CooldownMutex.Lock()
|
||||
|
||||
+2
-5
@@ -78,7 +78,6 @@ func main() {
|
||||
// THREAD PAGE
|
||||
r.Get("/{slug}/threads/{threadId}", func(w http.ResponseWriter, r *http.Request) {
|
||||
slug := chi.URLParam(r, "slug")
|
||||
// TODO: use relative thread_nums (see issue #1)
|
||||
threadIdStr := chi.URLParam(r, "threadId")
|
||||
threadId, err := strconv.Atoi(threadIdStr)
|
||||
if err != nil {
|
||||
@@ -165,8 +164,7 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
// slug := chi.URLParam(r, "slug")
|
||||
// TODO: use relative thread_nums (see issue #1)
|
||||
slug := chi.URLParam(r, "slug")
|
||||
threadIdStr := chi.URLParam(r, "threadId")
|
||||
threadId, err := strconv.Atoi(threadIdStr)
|
||||
if err != nil {
|
||||
@@ -201,7 +199,7 @@ func main() {
|
||||
mediaPath = filename
|
||||
}
|
||||
|
||||
if err := database.PutPost(db, threadId, body, mediaPath, util.HashIp(ip)); err != nil {
|
||||
if err := database.PutPost(db, slug, threadId, body, mediaPath, util.HashIp(ip)); err != nil {
|
||||
http.Error(w, "Failed to create post", http.StatusInternalServerError)
|
||||
log.Printf("PutPost: %v", err)
|
||||
return
|
||||
@@ -256,7 +254,6 @@ func main() {
|
||||
// THREAD POSTS
|
||||
r.Get("/hx/{slug}/threads/{threadId}/posts", func(w http.ResponseWriter, r *http.Request) {
|
||||
// slug := chi.URLParam(r, "slug")
|
||||
// TODO: use relative thread_nums (see issue #1)
|
||||
threadIdStr := chi.URLParam(r, "threadId")
|
||||
threadId, err := strconv.Atoi(threadIdStr)
|
||||
if err != nil {
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
|
||||
templ PostOriginal(post database.Post, thread *database.Thread) {
|
||||
{{ imgInfo := util.GetPostImageInfo(post.MediaPath) }}
|
||||
<article id={ fmt.Sprintf("post-%d", post.Id) } class="post-op">
|
||||
<article id={ fmt.Sprintf("post-%d", post.Number) } class="post-op">
|
||||
<div>
|
||||
File:
|
||||
<a href={ templ.URL("/static/img/posts/full/" + post.MediaPath) } class="post-filename">
|
||||
@@ -28,9 +28,9 @@ templ PostOriginal(post database.Post, thread *database.Thread) {
|
||||
<span class="post-author">{ post.Author }</span>
|
||||
<span class="post-date">{ post.CreatedAt.Format("01/02/2006") }</span>
|
||||
<span class="post-time">{ post.CreatedAt.Format("15:04") }</span>
|
||||
<span _={ fmt.Sprintf("on click set #newPostBody.value to #newPostBody.value + '>>%d\n'", post.Id) }
|
||||
<span _={ fmt.Sprintf("on click set #newPostBody.value to #newPostBody.value + '>>%d\n'", post.Number) }
|
||||
class=" post-num">
|
||||
No.{ strconv.Itoa(post.Id) }
|
||||
No.{ strconv.Itoa(post.Number) }
|
||||
</span>
|
||||
<span class="post-replies"></span>
|
||||
</header>
|
||||
@@ -42,14 +42,14 @@ templ PostOriginal(post database.Post, thread *database.Thread) {
|
||||
|
||||
templ PostReply(post database.Post) {
|
||||
{{ imgInfo := util.GetPostImageInfo(post.MediaPath) }}
|
||||
<article id={ fmt.Sprintf("post-%d", post.Id) } class="post">
|
||||
<article id={ fmt.Sprintf("post-%d", post.Number) } class="post">
|
||||
<header class="post-header">
|
||||
<span class="post-author">{ post.Author }</span>
|
||||
<span class="post-date">{ post.CreatedAt.Format("01/02/2006") }</span>
|
||||
<span class="post-time">{ post.CreatedAt.Format("15:04") }</span>
|
||||
<span _={ fmt.Sprintf("on click set #newPostBody.value to #newPostBody.value + '>>%d\n'", post.Id) }
|
||||
<span _={ fmt.Sprintf("on click set #newPostBody.value to #newPostBody.value + '>>%d\n'", post.Number) }
|
||||
class=" post-num">
|
||||
No.{ strconv.Itoa(post.Id) }
|
||||
No.{ strconv.Itoa(post.Number) }
|
||||
</span>
|
||||
<span class="post-replies"></span>
|
||||
</header>
|
||||
|
||||
Reference in New Issue
Block a user