...
1 package server
2
3 import (
4 "fmt"
5 "net/http"
6 "os"
7 "path/filepath"
8 "strconv"
9
10 "codeberg.org/tslocum/sriracha/internal/database"
11 . "codeberg.org/tslocum/sriracha/model"
12 . "codeberg.org/tslocum/sriracha/util"
13 )
14
15 func (s *Server) serveDelete(db *database.DB, w http.ResponseWriter, r *http.Request) {
16 data := s.buildData(db, w, r)
17
18 boardDir := FormString(r, "board")
19 b := db.BoardByDir(boardDir)
20 if b == nil {
21 data.BoardError(w, Get(b, data.Account, "No board specified."))
22 return
23 }
24
25 subscribe := FormString(r, "subscribe") != ""
26
27 var post *Post
28 postID, err := strconv.Atoi(r.FormValue("delete[]"))
29 if err == nil && postID > 0 {
30 post = db.PostByID(postID)
31 }
32 if subscribe {
33 if post == nil {
34 threadID := FormInt(r, "thread")
35 if threadID > 0 {
36 post = db.PostByID(threadID)
37 }
38 }
39 if post == nil {
40 data.BoardError(w, Get(b, data.Account, "Invalid post."))
41 return
42 }
43 url := fmt.Sprintf("/sriracha/subscribe/post/%d", post.ID)
44 http.Redirect(w, r, url, http.StatusFound)
45 return
46 } else if data.Account != nil {
47 if post == nil {
48 threadID := FormInt(r, "thread")
49 if threadID > 0 {
50 post = db.PostByID(threadID)
51 }
52 }
53 url := fmt.Sprintf("/sriracha/board/mod/%d", b.ID)
54 if post != nil {
55 url += fmt.Sprintf("/%d#%d", post.Thread(), post.ID)
56 }
57 http.Redirect(w, r, url, http.StatusFound)
58 return
59 } else if post != nil {
60 password := r.FormValue("password")
61 if post.Password == "" || s.hashData(password) != post.Password {
62 data.BoardError(w, Get(b, data.Account, "Incorrect password."))
63 return
64 }
65
66 confirm := r.FormValue("confirmation")
67 if confirm != "1" {
68 data.Board = b
69 data.Post = post
70 data.Extra = password
71 data.Template = "board_delete"
72 data.execute(w)
73 return
74 }
75
76 s.deletePost(db, post)
77
78 if post.Parent == 0 {
79 os.Remove(filepath.Join(s.config.Root, b.Dir, "res", fmt.Sprintf("%d.html", post.ID)))
80 } else {
81 s.writeThread(db, b, post.Thread())
82 }
83 s.writeIndexes(db, b)
84
85 data.Template = "board_info"
86 data.Info = fmt.Sprintf("Deleted No.%d", post.ID)
87 data.execute(w)
88 return
89 }
90 data.BoardError(w, Get(b, data.Account, "No post selected."))
91 }
92
View as plain text