forked from bakape/shamichan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
440 lines (389 loc) · 8.98 KB
/
reader.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package db
import (
"database/sql"
"meguca/common"
"meguca/config"
"meguca/util"
"github.com/boltdb/bolt"
"github.com/lib/pq"
)
type imageScanner struct {
APNG, Audio, Video, Spoiler sql.NullBool
FileType, ThumbType, Length, Size sql.NullInt64
Name, SHA1, MD5, Title, Artist sql.NullString
Dims pq.Int64Array
}
// Returns and array of pointers to the struct fields for passing to
// rowScanner.Scan()
func (i *imageScanner) ScanArgs() []interface{} {
return []interface{}{
&i.APNG, &i.Audio, &i.Video, &i.FileType, &i.ThumbType, &i.Dims,
&i.Length, &i.Size, &i.MD5, &i.SHA1, &i.Title, &i.Artist,
}
}
// Returns the scanned *common.Image or nil, if none
func (i *imageScanner) Val() *common.Image {
if !i.SHA1.Valid {
return nil
}
var dims [4]uint16
for j := range dims {
dims[j] = uint16(i.Dims[j])
}
return &common.Image{
Spoiler: i.Spoiler.Bool,
ImageCommon: common.ImageCommon{
APNG: i.APNG.Bool,
Audio: i.Audio.Bool,
Video: i.Video.Bool,
FileType: uint8(i.FileType.Int64),
ThumbType: uint8(i.ThumbType.Int64),
Length: uint32(i.Length.Int64),
Dims: dims,
Size: int(i.Size.Int64),
MD5: i.MD5.String,
SHA1: i.SHA1.String,
Title: i.Title.String,
Artist: i.Artist.String,
},
Name: i.Name.String,
}
}
type postScanner struct {
common.Post
banned, spoiler, deleted, sage sql.NullBool
name, trip, auth, imageName, flag sql.NullString
links linkRow
commands commandRow
}
func (p *postScanner) ScanArgs() []interface{} {
return []interface{}{
&p.Editing, &p.banned, &p.spoiler, &p.deleted, &p.sage, &p.ID, &p.Time,
&p.Body, &p.flag, &p.name, &p.trip, &p.auth, &p.links, &p.commands,
&p.imageName,
}
}
func (p postScanner) Val() (common.Post, error) {
p.Banned = p.banned.Bool
p.Deleted = p.deleted.Bool
p.Sage = p.sage.Bool
p.Name = p.name.String
p.Trip = p.trip.String
p.Auth = p.auth.String
p.Flag = p.flag.String
p.Links = [][2]uint64(p.links)
p.Commands = []common.Command(p.commands)
return p.Post, nil
}
// Returns if image is spoiled and it's assigned name, if any
func (p postScanner) Image() (bool, string) {
return p.spoiler.Bool, p.imageName.String
}
// PostStats contains post open status, body and creation time
type PostStats struct {
Editing, HasImage, Spoilered bool
ID uint64
Time int64
Body []byte
}
// GetThread retrieves public thread data from the database
func GetThread(id uint64, lastN int) (t common.Thread, err error) {
tx, err := db.Begin()
if err != nil {
return
}
defer tx.Commit()
err = setReadOnly(tx)
if err != nil {
return
}
// Get thread metadata and OP
t, err = scanOP(tx.Stmt(prepared["get_thread"]).QueryRow(id))
if err != nil {
return
}
t.Abbrev = lastN != 0
// Get replies
var (
cap int
limit *int
)
if lastN != 0 {
cap = lastN
limit = &lastN
} else {
cap = int(t.PostCtr)
}
r, err := tx.Stmt(prepared["get_thread_posts"]).Query(id, limit)
if err != nil {
return
}
defer r.Close()
// Scan replies into []common.Post
var (
post postScanner
img imageScanner
p common.Post
args = append(post.ScanArgs(), img.ScanArgs()...)
)
t.Posts = make([]common.Post, 0, cap)
for r.Next() {
err = r.Scan(args...)
if err != nil {
return
}
p, err = extractPost(post, img)
if err != nil {
return
}
t.Posts = append(t.Posts, p)
}
err = r.Err()
if err != nil {
return
}
// Inject bodies into open posts
open := make([]*common.Post, 0, 32)
if t.Editing {
open = append(open, &t.Post)
}
for i := range t.Posts {
if t.Posts[i].Editing {
open = append(open, &t.Posts[i])
}
}
err = injectOpenBodies(open)
return
}
func scanOP(r rowScanner) (t common.Thread, err error) {
var (
post postScanner
img imageScanner
)
args := make([]interface{}, 0, 35)
args = append(args,
&t.Sticky, &t.Board, &t.PostCtr, &t.ImageCtr, &t.ReplyTime, &t.BumpTime,
&t.Subject, &t.NonLive,
)
args = append(args, post.ScanArgs()...)
args = append(args, img.ScanArgs()...)
err = r.Scan(args...)
if err != nil {
return
}
t.Post, err = extractPost(post, img)
return
}
func extractPost(ps postScanner, is imageScanner) (p common.Post, err error) {
p, err = ps.Val()
if err != nil {
return
}
p.Image = is.Val()
if p.Image != nil {
p.Image.Spoiler, p.Image.Name = ps.Image()
}
return
}
// GetPost reads a single post from the database
func GetPost(id uint64) (res common.StandalonePost, err error) {
var (
args = make([]interface{}, 2, 29)
post postScanner
img imageScanner
)
args[0] = &res.OP
args[1] = &res.Board
args = append(args, post.ScanArgs()...)
args = append(args, img.ScanArgs()...)
err = prepared["get_post"].QueryRow(id).Scan(args...)
if err != nil {
return
}
res.Post, err = post.Val()
if err != nil {
return
}
res.Image = img.Val()
if res.Image != nil {
res.Image.Spoiler, res.Image.Name = post.Image()
}
if res.Editing {
res.Body, err = GetOpenBody(res.ID)
if err != nil {
return
}
}
return
}
// GetBoardCatalog retrieves all OPs of a single board
func GetBoardCatalog(board string) (b common.Board, err error) {
r, err := prepared["get_board"].Query(board)
if err != nil {
return
}
b, err = scanCatalog(r)
if err != nil {
return
}
return
}
// Retrieves all threads IDs on the board in bump order with stickies first
func GetThreadIDs(board string) ([]uint64, error) {
r, err := prepared["get_board_thread_ids"].Query(board)
if err != nil {
return nil, err
}
return scanThreadIDs(r)
}
// GetAllBoardCatalog retrieves all threads for the "/all/" meta-board
func GetAllBoardCatalog() (threads common.Board, err error) {
r, err := prepared["get_all_board"].Query()
if err != nil {
return
}
threads, err = scanCatalog(r)
if err != nil || !config.Get().HideNSFW {
return
}
// Hide threads from NSFW boards, if enabled
filtered := make(common.Board, 0, len(threads))
confs := config.GetAllBoardConfigs()
for _, t := range threads {
if !confs[t.Board].NSFW {
filtered = append(filtered, t)
}
}
threads = filtered
return
}
// Retrieves all threads IDs in bump order
func GetAllThreadsIDs() ([]uint64, error) {
r, err := prepared["get_all_thread_ids"].Query()
if err != nil {
return nil, err
}
return scanThreadIDs(r)
}
// GetRecentPosts retrieves posts created in the thread in the last 15 minutes.
// Posts that are being editted also have their Body property set.
func GetRecentPosts(op uint64) (posts []PostStats, err error) {
r, err := prepared["get_recent_posts"].Query(op)
if err != nil {
return
}
defer r.Close()
posts = make([]PostStats, 0, 64)
var p PostStats
for r.Next() {
err = r.Scan(&p.ID, &p.Time, &p.Editing, &p.HasImage, &p.Spoilered)
if err != nil {
return
}
posts = append(posts, p)
}
err = r.Err()
if err != nil {
return
}
// Get open post bodies
if len(posts) != 0 {
var tx *bolt.Tx
tx, err = boltDB.Begin(false)
if err != nil {
return
}
defer tx.Rollback()
buc := tx.Bucket([]byte("open_bodies"))
for i, p := range posts {
if !p.Editing {
continue
}
// Buffer is only valid for the transaction. Need to copy.
posts[i].Body = util.CloneBytes(buc.Get(encodeUint64Heap(p.ID)))
}
}
return
}
// Retvies mutations, that can happen to posts in a thread, after the post is
// closed
func GetThreadMutations(id uint64) (deleted, banned []uint64, err error) {
deleted = make([]uint64, 0, 16)
banned = make([]uint64, 0, 16)
r, err := prepared["get_thread_mutations"].Query(id)
if err != nil {
return
}
defer r.Close()
var (
isDeleted, isBanned sql.NullBool
postID uint64
)
for r.Next() {
err = r.Scan(&postID, &isDeleted, &isBanned)
if err != nil {
return
}
if isDeleted.Bool {
deleted = append(deleted, postID)
}
if isBanned.Bool {
banned = append(banned, postID)
}
}
err = r.Err()
return
}
func scanCatalog(table tableScanner) (board common.Board, err error) {
defer table.Close()
board = make(common.Board, 0, 32)
var t common.Thread
for table.Next() {
t, err = scanOP(table)
if err != nil {
return
}
board = append(board, t)
}
err = table.Err()
if err != nil {
return
}
open := make([]*common.Post, 0, 16)
for i := range board {
if board[i].Editing {
open = append(open, &board[i].Post)
}
}
err = injectOpenBodies(open)
return
}
func scanThreadIDs(table tableScanner) (ids []uint64, err error) {
defer table.Close()
ids = make([]uint64, 0, 64)
var id uint64
for table.Next() {
err = table.Scan(&id)
if err != nil {
return
}
ids = append(ids, id)
}
err = table.Err()
return
}
// Inject open post bodies from the embedded database into the posts
func injectOpenBodies(posts []*common.Post) error {
if len(posts) == 0 {
return nil
}
tx, err := boltDB.Begin(false)
if err != nil {
return err
}
buc := tx.Bucket([]byte("open_bodies"))
for _, p := range posts {
p.Body = string(buc.Get(encodeUint64Heap(p.ID)))
}
return tx.Rollback()
}