This repository has been archived by the owner on Oct 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
/
db.go
158 lines (137 loc) · 3.84 KB
/
db.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
// Package db handles all core database interactions of the server
package db
import (
"errors"
"github.com/bakape/meguca/auth"
r "github.com/dancannon/gorethink"
)
var (
// ErrUserNameTaken denotes a user name the client is trying to register
// with is already taken
ErrUserNameTaken = errors.New("user name already taken")
)
var postReservationQuery = GetMain("info").
Update(
map[string]r.Term{
"postCtr": r.Row.Field("postCtr").Add(1),
},
r.UpdateOpts{
ReturnChanges: true,
},
).
Field("changes").
AtIndex(0).
Field("new_val").
Field("postCtr")
// Exec executes the query and only returns an error, if any. Do not use for
// write queries.
func Exec(query r.Term) error {
return query.Exec(RSession)
}
// Write executes the inner query and returns an error, if any. Only use this
// function for write queries
func Write(query r.Term) error {
_, err := query.RunWrite(RSession)
return err
}
// WriteAll executes passed write queries in order. Returns on first error.
func WriteAll(qs []r.Term) error {
for _, q := range qs {
if err := Write(q); err != nil {
return err
}
}
return nil
}
// One writes the query result into the target pointer or throws an error
func One(query r.Term, res interface{}) error {
c, err := query.Run(RSession)
if err != nil {
return err
}
return c.One(res)
}
// All writes all responses into target pointer to slice or returns error
func All(query r.Term, res interface{}) error {
c, err := query.Run(RSession)
if err != nil {
return err
}
return c.All(res)
}
// FindPost finds a post only by ID number
func FindPost(id uint64) r.Term {
return r.Table("posts").Get(id)
}
// ValidateOP confirms the specified thread exists on specific board
func ValidateOP(id uint64, board string) (valid bool, err error) {
err = One(FindThread(id).Field("board").Eq(board).Default(false), &valid)
return
}
// FindThread is a shorthand for retrieving a document from the "threads" table
func FindThread(id uint64) r.Term {
return r.Table("threads").Get(id)
}
// GetMain is a shorthand for retrieving a document from the "main" table
func GetMain(id string) r.Term {
return r.Table("main").Get(id)
}
// GetAccount is a shorthand for retrieving a document from the "accounts" table
func GetAccount(id string) r.Term {
return r.Table("accounts").Get(id)
}
// GetImage is a shorthand for retrieving a document from the "images" table
func GetImage(id string) r.Term {
return r.Table("images").Get(id)
}
// Insert is a shorthand for inserting documents or slices of documents into a
// table
func Insert(table string, doc interface{}) error {
return Write(r.Table(table).Insert(doc))
}
// BoardCounter retrieves the history or "progress" counter of a board
func BoardCounter(board string) (counter uint64, err error) {
q := r.
Table("posts").
GetAllByIndex("board", board).
Field("lastUpdated").
Max().
Default(0)
err = One(q, &counter)
return
}
// ThreadCounter retrieves the post counter of a thread to get a rough estimate
// of the thread's progress
func ThreadCounter(id uint64) (counter uint64, err error) {
q := r.
Table("posts").
GetAllByIndex("op", id).
Field("lastUpdated").
Max().
Default(0)
err = One(q, &counter)
return
}
// RegisterAccount writes the ID and password hash of a new user account to the
// database
func RegisterAccount(ID string, hash []byte) error {
err := Insert("accounts", auth.User{
ID: ID,
Password: hash,
})
if r.IsConflictErr(err) {
return ErrUserNameTaken
}
return err
}
// GetLoginHash retrieves the login hash of the registered user account
func GetLoginHash(id string) (hash []byte, err error) {
query := GetAccount(id).Field("password").Default(nil)
err = One(query, &hash)
return
}
// ReservePostID reserves a post ID number for post and thread creation
func ReservePostID() (id uint64, err error) {
err = One(postReservationQuery, &id)
return
}