-
Notifications
You must be signed in to change notification settings - Fork 13
/
db.go
338 lines (295 loc) · 9.48 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
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
package user
import (
"context"
"encoding/json"
"github.com/jinzhu/gorm"
"github.com/jinzhu/gorm/dialects/postgres"
"github.com/lithammer/shortuuid"
"github.com/rs/zerolog/log"
"github.com/domino14/liwords/pkg/entity"
)
// DBStore is a postgres-backed store for users.
type DBStore struct {
db *gorm.DB
}
// User should be a minimal object. All information such as user profile,
// awards, ratings, records, etc should be in a profile object that
// joins 1-1 with this User object.
// User is exported as a Game has Foreign Keys to it.
type User struct {
gorm.Model
UUID string `gorm:"type:varchar(24);index"`
Username string `gorm:"type:varchar(32);unique_index"`
Email string `gorm:"type:varchar(100);unique_index"`
// Password will be hashed.
Password string `gorm:"type:varchar(128)"`
}
// A user profile is in a one-to-one relationship with a user. It is the
// profile that should have all the extra data we don't want to / shouldn't stick
// in the user model.
type profile struct {
gorm.Model
// `profile` belongs to `user`, `UserID` is the foreign key.
UserID uint
User User
FirstName string `gorm:"type:varchar(32)"`
LastName string `gorm:"type:varchar(64)"`
CountryCode string `gorm:"type:varchar(3)"`
// Title is some sort of acronym/shorthand for a title. Like GM, EX, SM, UK-GM (UK Grandmaster?)
Title string `gorm:"type:varchar(8)"`
// There will be no avatar URL; a user's avatar will be located at a fixed
// URL based on the user ID.
// About is profile notes.
About string `gorm:"type:varchar(2048)"`
// It's ok to have a few JSON bags here. Postgres makes these easy and fast.
// XXX: Come up with a model for friend list.
Ratings postgres.Jsonb // A complex dictionary of ratings with variants/lexica/etc.
Stats postgres.Jsonb // Profile stats such as average score per variant, bingos, a lot of other simple stats.
// More complex stats might be in a separate place.
}
type following struct {
// Follower follows user; pretty straightforward.
UserID uint
User User
FollowerID uint
Follower User
}
// NewDBStore creates a new DB store
func NewDBStore(dbURL string) (*DBStore, error) {
db, err := gorm.Open("postgres", dbURL)
if err != nil {
return nil, err
}
db.AutoMigrate(&User{}, &profile{}, &following{})
// Can't get GORM to auto create these foreign keys, so do it myself /shrug
db.Model(&profile{}).AddForeignKey("user_id", "users(id)", "RESTRICT", "RESTRICT")
db.Model(&following{}).
AddForeignKey("user_id", "users(id)", "RESTRICT", "RESTRICT").
AddForeignKey("follower_id", "users(id)", "RESTRICT", "RESTRICT").
AddUniqueIndex("user_follower_idx", "user_id", "follower_id")
return &DBStore{db: db}, nil
}
// Get gets a user by username.
func (s *DBStore) Get(ctx context.Context, username string) (*entity.User, error) {
u := &User{}
p := &profile{}
if result := s.db.Where("username = ?", username).First(u); result.Error != nil {
return nil, result.Error
}
if result := s.db.Model(u).Related(p); result.Error != nil {
return nil, result.Error
}
profile, err := dbProfileToProfile(p)
if err != nil {
return nil, err
}
entu := &entity.User{
ID: u.ID,
Username: u.Username,
UUID: u.UUID,
Email: u.Email,
Password: u.Password,
Anonymous: false,
Profile: profile,
}
return entu, nil
}
func dbProfileToProfile(p *profile) (*entity.Profile, error) {
var rdata entity.Ratings
err := json.Unmarshal(p.Ratings.RawMessage, &rdata)
if err != nil {
log.Err(err).Msg("profile had bad rating json, zeroing")
rdata = entity.Ratings{Data: map[entity.VariantKey]entity.SingleRating{}}
}
var sdata entity.ProfileStats
err = json.Unmarshal(p.Stats.RawMessage, &sdata)
if err != nil {
log.Err(err).Msg("profile had bad stats json, zeroing")
sdata = entity.ProfileStats{Data: map[entity.VariantKey]*entity.Stats{}}
}
return &entity.Profile{
FirstName: p.FirstName,
LastName: p.LastName,
CountryCode: p.CountryCode,
Title: p.Title,
About: p.About,
Ratings: rdata,
Stats: sdata,
}, nil
}
// GetByUUID gets user by UUID
func (s *DBStore) GetByUUID(ctx context.Context, uuid string) (*entity.User, error) {
u := &User{}
p := &profile{}
var entu *entity.User
if result := s.db.Where("uuid = ?", uuid).First(u); result.Error != nil {
if gorm.IsRecordNotFoundError(result.Error) {
entu = &entity.User{
Username: entity.DeterministicUsername(uuid),
Anonymous: true,
UUID: uuid,
}
} else {
return nil, result.Error
}
} else {
if result := s.db.Model(u).Related(p); result.Error != nil {
return nil, result.Error
}
profile, err := dbProfileToProfile(p)
if err != nil {
return nil, err
}
entu = &entity.User{
ID: u.ID,
Username: u.Username,
UUID: u.UUID,
Email: u.Email,
Password: u.Password,
Profile: profile,
}
}
return entu, nil
}
// New creates a new user in the DB.
func (s *DBStore) New(ctx context.Context, u *entity.User) error {
if u.UUID == "" {
u.UUID = shortuuid.New()
}
dbu := &User{
UUID: u.UUID,
Username: u.Username,
Email: u.Email,
Password: u.Password,
}
result := s.db.Create(dbu)
if result.Error != nil {
return result.Error
}
// Create profile
rdata := entity.Ratings{}
ratbytes, err := json.Marshal(rdata)
if err != nil {
return err
}
sdata := entity.ProfileStats{}
statbytes, err := json.Marshal(sdata)
if err != nil {
return err
}
dbp := &profile{
User: *dbu,
Ratings: postgres.Jsonb{RawMessage: ratbytes},
Stats: postgres.Jsonb{RawMessage: statbytes},
}
result = s.db.Create(dbp)
return result.Error
}
// SetPassword sets the password for the user. The password is already hashed.
func (s *DBStore) SetPassword(ctx context.Context, uuid string, hashpass string) error {
u := &User{}
if result := s.db.Where("uuid = ?", uuid).First(u); result.Error != nil {
return result.Error
}
return s.db.Model(u).Update("password", hashpass).Error
}
// SetRating sets the specific rating for the given variant.
func (s *DBStore) SetRating(ctx context.Context, uuid string, variant entity.VariantKey,
rating entity.SingleRating) error {
u := &User{}
p := &profile{}
if result := s.db.Where("uuid = ?", uuid).First(u); result.Error != nil {
return result.Error
}
if result := s.db.Model(u).Related(p); result.Error != nil {
return result.Error
}
var existingRatings entity.Ratings
err := json.Unmarshal(p.Ratings.RawMessage, &existingRatings)
if err != nil {
log.Err(err).Msg("existing ratings missing; initializing...")
existingRatings = entity.Ratings{Data: map[entity.VariantKey]entity.SingleRating{}}
}
if existingRatings.Data == nil {
existingRatings.Data = make(map[entity.VariantKey]entity.SingleRating)
}
existingRatings.Data[variant] = rating
bytes, err := json.Marshal(existingRatings)
if err != nil {
return err
}
return s.db.Model(p).Update("ratings", postgres.Jsonb{RawMessage: bytes}).Error
}
func (s *DBStore) SetStats(ctx context.Context, uuid string, variant entity.VariantKey,
stats *entity.Stats) error {
u := &User{}
p := &profile{}
if result := s.db.Where("uuid = ?", uuid).First(u); result.Error != nil {
return result.Error
}
if result := s.db.Model(u).Related(p); result.Error != nil {
return result.Error
}
var existingProfileStats entity.ProfileStats
err := json.Unmarshal(p.Stats.RawMessage, &existingProfileStats)
if err != nil {
log.Err(err).Msg("existing stats missing; initializing...")
existingProfileStats = entity.ProfileStats{Data: map[entity.VariantKey]*entity.Stats{}}
}
if existingProfileStats.Data == nil {
existingProfileStats.Data = make(map[entity.VariantKey]*entity.Stats)
}
existingProfileStats.Data[variant] = stats
bytes, err := json.Marshal(existingProfileStats)
if err != nil {
return err
}
return s.db.Model(p).Update("stats", postgres.Jsonb{RawMessage: bytes}).Error
}
// AddFollower creates a follower -> target follow.
func (s *DBStore) AddFollower(ctx context.Context, targetUser, follower uint) error {
dbf := &following{UserID: targetUser, FollowerID: follower}
result := s.db.Create(dbf)
return result.Error
}
// RemoveFollower removes a follower -> target follow.
func (s *DBStore) RemoveFollower(ctx context.Context, targetUser, follower uint) error {
return s.db.Where("user_id = ? AND follower_id = ?", targetUser, follower).Delete(&following{}).Error
}
// GetFollows gets all the users that the passed-in user DB ID is following.
func (s *DBStore) GetFollows(ctx context.Context, uid uint) ([]*entity.User, error) {
type followed struct {
Username string
Uuid string
}
var users []followed
if result := s.db.Table("followings").Select("u0.username, u0.uuid").
Joins("JOIN users as u0 ON u0.id = user_id").
Where("follower_id = ?", uid).Scan(&users); result.Error != nil {
return nil, result.Error
}
log.Debug().Int("num-followed", len(users)).Msg("found-followed")
entUsers := make([]*entity.User, len(users))
for idx, u := range users {
entUsers[idx] = &entity.User{UUID: u.Uuid, Username: u.Username}
}
return entUsers, nil
}
// Username gets the username from the uuid. If not found, return a deterministic username.
func (s *DBStore) Username(ctx context.Context, uuid string) (string, error) {
type u struct {
Username string
}
var user u
if result := s.db.Table("users").Select("username").
Where("uuid = ?", uuid).Scan(&user); result.Error != nil {
if gorm.IsRecordNotFoundError(result.Error) {
return entity.DeterministicUsername(uuid), nil
}
return "", result.Error
}
return user.Username, nil
}
func (s *DBStore) Disconnect() {
s.db.Close()
}