-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.go
261 lines (213 loc) · 5.32 KB
/
html.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package main
import (
"math"
"net/http"
"sort"
"strconv"
"strings"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
"gorm.io/gorm"
)
const BoardViewReplies = 3
// Top sticked navbar.
type NavbarInfo struct {
Navbar []Board
CurrentBoard Board
}
// Get all avaible boards from db.
func (nav *NavbarInfo) SetNavbar(tx *gorm.DB) error {
return tx.Find(&nav.Navbar).Error
}
func (nav *NavbarInfo) SetCurrentBoard(tx *gorm.DB, link string) error {
return tx.Where(&Board{Link: link}).
First(&nav.CurrentBoard).
Error
}
func (nav *NavbarInfo) SetHeader(link string) error {
if err := nav.SetCurrentBoard(db, link); err != nil {
return err
}
return nav.SetNavbar(db)
}
// Embed in every view with form.
type FormInfo struct {
FormReply int
}
type MainView struct {
NavbarInfo
}
func serveMain(c echo.Context) error {
view := new(strings.Builder)
mv := MainView{}
init := Maybe{
func() error {
return mv.SetNavbar(db)
},
func() error {
return templates.ExecuteTemplate(view, "main_page.tmpl", mv)
},
}
if err := init.Eval(); err != nil {
log.Error().Msg(err.Error())
return c.JSON(http.StatusInternalServerError, Error{err.Error()})
}
return c.HTML(http.StatusOK, view.String())
}
type Paging struct {
Current uint
Total []uint
}
type BoardView struct {
NavbarInfo
FormInfo
Threads []ThreadView
Pages Paging
}
func (b BoardView) LastId() int {
return len(b.Threads) - 1
}
func serveBoard(c echo.Context) error {
board := c.Param("board")
page, err := strconv.Atoi(c.QueryParam("page"))
if err != nil || page < 0 {
page = 0
}
bv := BoardView{}
if err := bv.SetHeader(board); err != nil {
log.Error().Msg(err.Error())
return c.JSON(http.StatusBadRequest, Error{err.Error()})
}
posts := []Post{}
if err := db.Find(&posts, "board = ? AND parent = 0", board).Error; err != nil {
log.Error().Msg(err.Error())
return c.JSON(http.StatusBadRequest, Error{err.Error()})
}
// Sorting by last bump.
sort.Slice(posts, func(i, j int) bool {
return posts[i].LastBump.After(posts[j].LastBump)
})
// Get total pages on board.
pages := len(posts)/10 + 1
if len(posts)%10 == 0 && len(posts) != 0 {
pages--
}
// Get threads with offset for current page.
lb := page * 10
rb := (page + 1) * 10
rb = int(math.Min(float64(rb), float64(len(posts))))
if rb < lb {
return c.JSON(http.StatusBadRequest, Error{"no such page"})
}
posts = posts[lb:rb]
bv.Pages.Current = uint(page)
bv.Pages.Total = make([]uint, pages)
// Generate total pages indexes.
for i := range bv.Pages.Total {
bv.Pages.Total[i] = uint(i)
}
// Create view entry for every thread on board.
for i := range posts {
tv := ThreadView{
OP: posts[i],
Omitted: 0,
}
if err := tv.BoardEntry(); err != nil {
log.Error().Msg(err.Error())
return c.JSON(http.StatusBadRequest, Error{err.Error()})
}
if err := tv.SetFiles(db); err != nil {
log.Error().Msg(err.Error())
// just ignore and return posts w/o files.
}
tv.Omitted -= int64(len(tv.Replies))
bv.Threads = append(bv.Threads, tv)
}
// Render board page.
view := new(strings.Builder)
if err := templates.ExecuteTemplate(view, "board.tmpl", bv); err != nil {
log.Error().Msg(err.Error())
return c.JSON(http.StatusInternalServerError, Error{err.Error()})
}
return c.HTML(http.StatusOK, view.String())
}
type ThreadView struct {
NavbarInfo
FormInfo
OP Post
Replies []Post
Omitted int64
}
// OP field should be set when called.
func (t *ThreadView) BoardEntry() error {
// Get last 3 replies.
err := db.Where(&Post{
Board: t.OP.Board,
Parent: t.OP.ID,
}).
Order("id desc").
Limit(BoardViewReplies).
Find(&t.Replies).
Error
if err != nil {
return err
}
// Reverse it's order.
sort.Slice(t.Replies, func(i, j int) bool {
return t.Replies[i].ID < t.Replies[j].ID
})
// Get total amount of replies.
return db.Model(&Post{}).
Where("board = ? AND parent = ?", t.OP.Board, t.OP.ID).
Count(&t.Omitted).
Error
}
func (t *ThreadView) ThreadEntry(board string, id uint) error {
t.FormReply = int(id)
// Set all replies.
if err := db.Find(&t.Replies, "board = ? AND parent = ?", board, id).Error; err != nil {
return err
}
// Set op post itself.
return db.Where(&Post{}).
First(&t.OP, "parent = 0 AND board = ? AND id = ?", board, id).
Error
}
func (t *ThreadView) SetFiles(tx *gorm.DB) error {
ps := []*Post{&t.OP}
for i := range t.Replies {
ps = append(ps, &t.Replies[i])
}
for i := range ps {
fs, err := ps[i].GetFiles(tx)
if err != nil {
return err
}
ps[i].Files = fs
}
return nil
}
func serveThread(c echo.Context) error {
board := c.Param("board")
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
return c.JSON(http.StatusBadRequest, Error{"invalid thread id"})
}
tv := ThreadView{}
if err := tv.SetHeader(board); err != nil {
return c.JSON(http.StatusBadRequest, Error{err.Error()})
}
if err := tv.ThreadEntry(board, uint(id)); err != nil {
log.Error().Msg(err.Error())
return c.JSON(http.StatusInternalServerError, Error{err.Error()})
}
if err := tv.SetFiles(db); err != nil {
log.Error().Msg(err.Error())
}
view := new(strings.Builder)
if err := templates.ExecuteTemplate(view, "thread.tmpl", tv); err != nil {
log.Error().Msg(err.Error())
return c.JSON(http.StatusInternalServerError, Error{err.Error()})
}
return c.HTML(http.StatusOK, view.String())
}