Add ability to delete posts
This commit is contained in:
@@ -242,14 +242,18 @@ func DeleteThread(db Queryer, threadId int) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.Remove(path.Join(util.POST_MEDIA_FULL_PATH, pruneMediaFullPath)); err != nil {
|
||||
if !errors.Is(err, fs.ErrNotExist) {
|
||||
return err
|
||||
if pruneMediaFullPath != "" {
|
||||
if err := os.Remove(path.Join(util.POST_MEDIA_FULL_PATH, pruneMediaFullPath)); err != nil {
|
||||
if !errors.Is(err, fs.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := os.Remove(path.Join(util.POST_MEDIA_THUMB_PATH, pruneMediaThumbPath)); err != nil {
|
||||
if !errors.Is(err, fs.ErrNotExist) {
|
||||
return err
|
||||
if pruneMediaThumbPath != "" {
|
||||
if err := os.Remove(path.Join(util.POST_MEDIA_THUMB_PATH, pruneMediaThumbPath)); err != nil {
|
||||
if !errors.Is(err, fs.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -265,6 +269,48 @@ func DeleteThread(db Queryer, threadId int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeletePost(db *sql.DB, postId int) error {
|
||||
// cleanup images
|
||||
row := db.QueryRow(`
|
||||
SELECT media_path, thumb_path
|
||||
FROM posts
|
||||
WHERE id = ?`, postId)
|
||||
|
||||
var (
|
||||
pruneMediaThumbPath string
|
||||
pruneMediaFullPath string
|
||||
)
|
||||
if err := row.Scan(&pruneMediaFullPath, &pruneMediaThumbPath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if pruneMediaFullPath != "" {
|
||||
if err := os.Remove(path.Join(util.POST_MEDIA_FULL_PATH, pruneMediaFullPath)); err != nil {
|
||||
if !errors.Is(err, fs.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if pruneMediaThumbPath != "" {
|
||||
if err := os.Remove(path.Join(util.POST_MEDIA_THUMB_PATH, pruneMediaThumbPath)); err != nil {
|
||||
if !errors.Is(err, fs.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// delete post
|
||||
_, err := db.Exec(`
|
||||
DELETE FROM posts
|
||||
WHERE id = ?`, postId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetAdmin(db *sql.DB, username string) (Admin, error) {
|
||||
row := db.QueryRow(`
|
||||
SELECT username, password
|
||||
|
||||
@@ -30,7 +30,7 @@ const MAX_THREAD_COUNT = 50
|
||||
const MAX_BODY_LEN = 3000
|
||||
const MAX_SUBJECT_LEN = 50
|
||||
|
||||
var SUPPORTED_IMAGE_MIME_TYPES = []string{"image/jpeg", "image/png", "image/gif", "image/webp"}
|
||||
var SUPPORTED_IMAGE_MIME_TYPES = []string{"image/jpeg", "image/png", "image/gif"}
|
||||
var SUPPORTED_VIDEO_MIME_TYPES = []string{"video/webm", "video/mp4", "video/ogg"}
|
||||
|
||||
type PostMediaType int64
|
||||
|
||||
+25
-6
@@ -150,7 +150,9 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
views.Thread(board, thread, posts).Render(r.Context(), w)
|
||||
views.Thread(board, thread, posts, views.ThreadContext{
|
||||
IsAdmin: isAdmin(r),
|
||||
}).Render(r.Context(), w)
|
||||
})
|
||||
|
||||
// CREATE THREAD
|
||||
@@ -428,7 +430,9 @@ func main() {
|
||||
}
|
||||
|
||||
// dont pass the op post. only replies
|
||||
views.Posts(posts, thread).Render(r.Context(), w)
|
||||
views.Posts(posts, thread, views.ThreadContext{
|
||||
IsAdmin: isAdmin(r),
|
||||
}).Render(r.Context(), w)
|
||||
})
|
||||
|
||||
// -----------------
|
||||
@@ -509,8 +513,7 @@ func main() {
|
||||
})
|
||||
})
|
||||
|
||||
r.Delete("/{slug}/threads/{threadId}", func(w http.ResponseWriter, r *http.Request) {
|
||||
// slug := chi.URLParam(r, "slug")
|
||||
r.Delete("/threads/{threadId}", func(w http.ResponseWriter, r *http.Request) {
|
||||
threadIdStr := chi.URLParam(r, "threadId")
|
||||
threadId, err := strconv.Atoi(threadIdStr)
|
||||
if err != nil {
|
||||
@@ -520,11 +523,27 @@ func main() {
|
||||
|
||||
err = database.DeleteThread(db, threadId)
|
||||
if err != nil {
|
||||
log.Println("DeleteThread: %v", err)
|
||||
http.Error(w, "Error deleting thread: "+threadIdStr, http.StatusInternalServerError)
|
||||
log.Println("DeleteThread: ", err)
|
||||
http.Error(w, "Failed to delete thread: "+threadIdStr, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
r.Delete("/posts/{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 = database.DeletePost(db, postId)
|
||||
if err != nil {
|
||||
log.Println("DeletePost: ", err)
|
||||
http.Error(w, "Failed to delete post: "+postIdStr, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
// -----------------
|
||||
|
||||
@@ -105,7 +105,7 @@ templ ThreadsCatalog(previews []CatalogThreadPreview, catalogContext CatalogCont
|
||||
<h1>{ elThreadId }</h1>
|
||||
<button
|
||||
class="link-button"
|
||||
hx-delete={ fmt.Sprintf("/admin/%s/threads/%d", catalogContext.BoardSlug, preview.ThreadId) }
|
||||
hx-delete={ fmt.Sprintf("/admin/threads/%d", preview.ThreadId) }
|
||||
hx-swap="none"
|
||||
_="on htmx:afterRequest trigger refreshPosts on body"
|
||||
hx-confirm="Are you sure you wish to delete this thread?"
|
||||
|
||||
+44
-7
@@ -8,7 +8,28 @@ import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
templ PostOriginal(post database.Post, thread *database.Thread) {
|
||||
templ PostAdminDialog(post database.Post) {
|
||||
{{ elPostId := fmt.Sprintf("post-%d", post.Id) }}
|
||||
<dialog
|
||||
id={ elPostId + "-dialog" }
|
||||
class="admin-dialog"
|
||||
>
|
||||
<h1>{ elPostId }</h1>
|
||||
<button
|
||||
class="link-button"
|
||||
hx-delete={ fmt.Sprintf("/admin/posts/%d", post.Id) }
|
||||
hx-swap="none"
|
||||
_="on htmx:afterRequest trigger refreshPosts on body"
|
||||
hx-confirm="Are you sure you wish to delete this post?"
|
||||
>Delete</button>
|
||||
<button
|
||||
class="admin-dialog-close-btn link-button"
|
||||
_={ fmt.Sprintf("on click call #%s-dialog.close()", elPostId) }
|
||||
>Close</button>
|
||||
</dialog>
|
||||
}
|
||||
|
||||
templ PostOriginal(post database.Post, thread database.Thread) {
|
||||
<article id={ fmt.Sprintf("post-%d", post.Number) } class="post-op">
|
||||
<div>
|
||||
<span
|
||||
@@ -53,9 +74,20 @@ templ PostOriginal(post database.Post, thread *database.Thread) {
|
||||
</article>
|
||||
}
|
||||
|
||||
templ PostReply(post database.Post) {
|
||||
templ PostReply(post database.Post, threadContext ThreadContext) {
|
||||
{{ elPostId := fmt.Sprintf("post-%d", post.Id) }}
|
||||
<article id={ fmt.Sprintf("post-%d", post.Number) } class="post">
|
||||
<header class="post-header">
|
||||
if threadContext.IsAdmin {
|
||||
<span
|
||||
id={ fmt.Sprintf("%s-admintoggle", elPostId) }
|
||||
class="link-button"
|
||||
style="float: left;"
|
||||
_={ fmt.Sprintf(`on click call #%s-dialog.showModal()`, elPostId) }
|
||||
>
|
||||
M
|
||||
</span>
|
||||
}
|
||||
<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>
|
||||
@@ -99,20 +131,25 @@ templ PostReply(post database.Post) {
|
||||
<p class="post-body">
|
||||
@templ.Raw(util.EnrichPost(post.Body))
|
||||
</p>
|
||||
@PostAdminDialog(post)
|
||||
</article>
|
||||
}
|
||||
|
||||
templ Posts(posts []database.Post, thread database.Thread) {
|
||||
@PostOriginal(posts[0], &thread)
|
||||
templ Posts(posts []database.Post, thread database.Thread, threadContext ThreadContext) {
|
||||
@PostOriginal(posts[0], thread)
|
||||
<div>
|
||||
for _, post := range (posts[1:]) {
|
||||
@PostReply(post)
|
||||
@PostReply(post, threadContext)
|
||||
<br/>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
|
||||
templ Thread(board database.Board, thread database.Thread, posts []database.Post) {
|
||||
type ThreadContext struct {
|
||||
IsAdmin bool
|
||||
}
|
||||
|
||||
templ Thread(board database.Board, thread database.Thread, posts []database.Post, threadContext ThreadContext) {
|
||||
@shared.Layout() {
|
||||
<script src="/static/thread.js" defer></script>
|
||||
@BoardHeader(board)
|
||||
@@ -131,7 +168,7 @@ templ Thread(board database.Board, thread database.Thread, posts []database.Post
|
||||
hx-trigger="refreshPosts from:body"
|
||||
_="on htmx:afterSwap call insertHeaderReplies()"
|
||||
>
|
||||
@Posts(posts, thread)
|
||||
@Posts(posts, thread, threadContext)
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user