-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
132 lines (105 loc) · 2.74 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"database/sql"
"fmt"
"github.com/labstack/echo/v4"
"net/http"
"strconv"
)
type Handler struct {
db *sql.DB
baseUrl string
}
func NewHandler(dbPath string, baseUrl string) (*Handler, error) {
if dbPath == "" {
dbPath = "data.sqlite3"
}
db, err := CreateMigratedDb(dbPath)
if err != nil {
return nil, err
}
return &Handler{db, baseUrl}, nil
}
func (h *Handler) HandleGetPosts(c echo.Context) error {
posts, err := QueryPosts(h.db)
if err != nil {
return fmt.Errorf("failed to query posts: %w", err)
}
user := GetUser(c)
TzProfileFor(c).AdjustPosts(posts)
return c.Render(http.StatusOK, "index.html", echo.Map{
"Posts": posts,
"EnablePosting": true,
"User": user,
})
}
func (h *Handler) HandleGetSinglePost(c echo.Context) error {
postId, err := strconv.Atoi(c.Param("id"))
if err != nil {
return echo.ErrNotFound
}
post, err := QuerySinglePost(h.db, postId)
if err != nil {
return fmt.Errorf("failed to query single post (id: %v): %w", postId, err)
}
children, err := QueryChildrenPosts(h.db, post.Id)
if err != nil {
return fmt.Errorf("failed to query children: %w", err)
}
user := GetUser(c)
TzProfileFor(c).AdjustPosts(children).AdjustPost(&post)
return c.Render(http.StatusOK, "index.html", echo.Map{
"Children": children,
"Post": post,
"User": user,
"EnablePosting": false,
})
}
func (h *Handler) HandleCreatePost(c echo.Context) error {
bodyContent := c.FormValue("content")
postIdParam := c.Param("id")
var parentPostId *int64
if postIdParam != "" {
postId, err := strconv.Atoi(postIdParam)
if err != nil {
return echo.ErrNotFound
}
postId64 := int64(postId)
parentPostId = &postId64
}
var username string
if user := GetUser(c); user != nil {
username = user.Username()
}
if len(bodyContent) < 8 {
return echo.ErrBadRequest
}
content, err := CompileContent(bodyContent)
if err != nil {
return fmt.Errorf("failed to compile content: %w", err)
}
createdPostId, err := InsertPost(h.db, content, username, parentPostId, c.FormValue("post_anon") == "on")
if err != nil {
return fmt.Errorf("failed to create post: %w", err)
}
if parentPostId == nil {
return c.Redirect(http.StatusSeeOther, "/")
} else {
return c.Redirect(http.StatusSeeOther, fmt.Sprintf("/posts/%v#reply-%v", *parentPostId, createdPostId))
}
}
func (h *Handler) HandleDeletePost(c echo.Context) error {
postIdParam := c.Param("id")
postId, err := strconv.Atoi(postIdParam)
if err != nil {
return echo.ErrNotFound
}
user := GetUser(c)
if user == nil {
return echo.ErrUnauthorized
}
if err := DeletePost(h.db, int64(postId), user.Username(), user.IsAdmin()); err != nil {
return err
}
return c.Redirect(http.StatusSeeOther, "/")
}