forked from osuripple/api
/
self.go
324 lines (294 loc) · 8.33 KB
/
self.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
package v1
import (
"strings"
emoji "github.com/tmdvs/Go-Emoji-Utils"
"zxq.co/ripple/playstyle"
"github.com/ripple-shit/api/common"
semanticiconsugc "zxq.co/ripple/semantic-icons-ugc"
)
type donorInfoResponse struct {
common.ResponseBase
HasDonor bool `json:"has_donor"`
Expiration common.UnixTimestamp `json:"expiration"`
}
// UsersSelfDonorInfoGET returns information about the users' donor status
func UsersSelfDonorInfoGET(md common.MethodData) common.CodeMessager {
var r donorInfoResponse
var privileges uint64
err := md.DB.QueryRow("SELECT privileges, donor_expire FROM users WHERE id = ?", md.ID()).
Scan(&privileges, &r.Expiration)
if err != nil {
md.Err(err)
return Err500
}
r.HasDonor = common.UserPrivileges(privileges)&common.UserPrivilegeDonor > 0
r.Code = 200
return r
}
type favouriteModeResponse struct {
common.ResponseBase
FavouriteMode int `json:"favourite_mode"`
FavouriteRelax int `json:"favourite_relax"`
}
// UsersSelfFavouriteModeGET gets the current user's favourite mode
func UsersSelfFavouriteModeGET(md common.MethodData) common.CodeMessager {
var f favouriteModeResponse
f.Code = 200
if md.ID() == 0 {
return f
}
err := md.DB.QueryRow("SELECT users_stats.favourite_mode, users_stats.favourite_relax FROM users_stats WHERE id = ?", md.ID()).
Scan(&f.FavouriteMode, &f.FavouriteRelax)
if err != nil {
md.Err(err)
return Err500
}
return f
}
type userSettingsData struct {
UsernameAKA *string `json:"username_aka"`
FavouriteMode *int `json:"favourite_mode"`
FavouriteRelaxMode *int `json:"favourite_relax"`
CustomBadge struct {
singleBadge
Show *bool `json:"show"`
} `json:"custom_badge"`
PlayStyle *int `json:"play_style"`
}
// UsersSelfSettingsPOST allows to modify information about the current user.
func UsersSelfSettingsPOST(md common.MethodData) common.CodeMessager {
var d userSettingsData
md.Unmarshal(&d)
// input sanitisation
*d.UsernameAKA = common.SanitiseString(*d.UsernameAKA)
if md.User.UserPrivileges&common.UserPrivilegeDonor > 0 {
d.CustomBadge.Name = common.SanitiseString(d.CustomBadge.Name)
emoji, err := emoji.LookupEmoji(d.CustomBadge.Icon)
if err != nil {
return common.SimpleResponse(400, "Invalid emoji")
}
d.CustomBadge.Icon = emoji.Value
} else {
d.CustomBadge.singleBadge = singleBadge{}
d.CustomBadge.Show = nil
}
d.FavouriteMode = intPtrIn(0, d.FavouriteMode, 3)
if d.FavouriteMode != nil && *d.FavouriteMode == 3 {
v := 0
d.FavouriteRelaxMode = &v
}
d.FavouriteRelaxMode = intPtrIn(0, d.FavouriteRelaxMode, 1)
q := new(common.UpdateQuery).
Add("s.username_aka", d.UsernameAKA).
Add("s.favourite_mode", d.FavouriteMode).
Add("s.custom_badge_name", d.CustomBadge.Name).
Add("s.custom_badge_icon", d.CustomBadge.Icon).
Add("s.show_custom_badge", d.CustomBadge.Show).
Add("s.play_style", *d.PlayStyle&^(playstyle.Spoon|playstyle.LeapMotion|playstyle.OculusRift|playstyle.Dick|playstyle.Eggplant)).
Add("s.favourite_relax", d.FavouriteRelaxMode)
_, err := md.DB.Exec("UPDATE users u, users_stats s SET "+q.Fields()+" WHERE s.id = u.id AND u.id = ?", append(q.Parameters, md.ID())...)
if err != nil {
md.Err(err)
return Err500
}
return UsersSelfSettingsGET(md)
}
func sanitiseIconName(s string) string {
classes := strings.Split(s, " ")
n := make([]string, 0, len(classes))
for _, c := range classes {
if !in(c, n) && in(c, semanticiconsugc.SaneIcons) {
n = append(n, c)
}
}
return strings.Join(n, " ")
}
func in(a string, b []string) bool {
for _, x := range b {
if x == a {
return true
}
}
return false
}
type userSettingsResponse struct {
common.ResponseBase
ID int `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
Flags uint `json:"flags"`
userSettingsData
}
// UsersSelfSettingsGET allows to get "sensitive" information about the current user.
func UsersSelfSettingsGET(md common.MethodData) common.CodeMessager {
var r userSettingsResponse
var ccb bool
r.Code = 200
err := md.DB.QueryRow(`
SELECT
u.id, u.username,
u.email, s.username_aka, s.favourite_mode,
s.favourite_relax,
s.show_custom_badge, s.custom_badge_icon,
s.custom_badge_name, s.can_custom_badge,
s.play_style, u.flags
FROM users u
LEFT JOIN users_stats s ON u.id = s.id
WHERE u.id = ?`, md.ID()).Scan(
&r.ID, &r.Username,
&r.Email, &r.UsernameAKA, &r.FavouriteMode,
&r.FavouriteRelaxMode,
&r.CustomBadge.Show, &r.CustomBadge.Icon,
&r.CustomBadge.Name, &ccb,
&r.PlayStyle, &r.Flags,
)
if err != nil {
md.Err(err)
return Err500
}
if !ccb {
r.CustomBadge = struct {
singleBadge
Show *bool `json:"show"`
}{}
}
return r
}
const (
overwritePP = 0
overwriteScore = 1
modeClassic = 0
modeRelax = 1
autoLastOff = 0
autoLastMessage = 1
autoLastNotification = 2
displayScore = 0
displayPP = 1
)
var validOverwrites = []int{overwritePP, overwriteScore}
var validModes = []int{modeClassic, modeRelax}
var validAutoLast = []int{autoLastOff, autoLastMessage, autoLastNotification}
var validDisplayModes = []int{displayScore, displayPP}
type scoreOverwrite struct {
Std *int `json:"std"`
Taiko *int `json:"taiko"`
Ctb *int `json:"ctb"`
Mania *int `json:"mania"`
}
type scoreboardDisplay struct {
Classic *int `json:"classic"`
Relax *int `json:"relax"`
}
type autoLast struct {
Classic *int `json:"classic"`
Relax *int `json:"relax"`
}
type scoreboard struct {
Mode *int `json:"mode"`
Display *scoreboardDisplay `json:"display"`
}
type scoreboardData struct {
common.ResponseBase
Scoreboard *scoreboard `json:"scoreboard"`
Overwrite *scoreOverwrite `json:"overwrite"`
AutoLast *autoLast `json:"auto_last"`
}
type scoreboardResponse struct {
common.ResponseBase
scoreboardData
}
// UserSelfScoreboardGET returns the in-game scoreboard perferences
// for the current uesr
func UserSelfScoreboardGET(md common.MethodData) common.CodeMessager {
var r scoreboardResponse
r.Code = 200
r.Scoreboard = &scoreboard{Display: &scoreboardDisplay{}}
r.AutoLast = &autoLast{}
r.Overwrite = &scoreOverwrite{}
err := md.DB.QueryRow(`
SELECT
u.is_relax,
score_overwrite_std, score_overwrite_taiko,
score_overwrite_ctb, score_overwrite_mania,
scoreboard_display_classic, scoreboard_display_relax,
auto_last_classic, auto_last_relax
FROM users_preferences JOIN users AS u USING(id)
WHERE id = ? LIMIT 1`, md.ID()).Scan(
&r.Scoreboard.Mode,
&r.Overwrite.Std, &r.Overwrite.Taiko,
&r.Overwrite.Ctb, &r.Overwrite.Mania,
&r.Scoreboard.Display.Classic, &r.Scoreboard.Display.Relax,
&r.AutoLast.Classic, &r.AutoLast.Relax,
)
if err != nil {
md.Err(err)
return Err500
}
return r
}
// UserSelfScoreboardPOST allows users to change their in-game
// scoreboard preferences
func UserSelfScoreboardPOST(md common.MethodData) common.CodeMessager {
var d scoreboardData
err := md.Unmarshal(&d)
if err != nil {
return ErrBadJSON
}
q := new(common.UpdateQuery)
type scoreboardDataField struct {
value *int
allowedValues []int
column string
}
for _, field := range []scoreboardDataField{
{d.Scoreboard.Mode, []int{modeClassic, modeRelax}, "u.is_relax"},
{d.Scoreboard.Display.Classic, validDisplayModes, "scoreboard_display_classic"},
{d.Scoreboard.Display.Relax, validDisplayModes, "scoreboard_display_relax"},
{d.Overwrite.Std, validOverwrites, "score_overwrite_std"},
{d.Overwrite.Taiko, validOverwrites, "score_overwrite_taiko"},
{d.Overwrite.Ctb, validOverwrites, "score_overwrite_ctb"},
{d.Overwrite.Mania, validOverwrites, "score_overwrite_mania"},
{d.AutoLast.Classic, validAutoLast, "auto_last_classic"},
{d.AutoLast.Relax, validAutoLast, "auto_last_relax"},
} {
if field.value == nil {
continue
}
if !contains(*field.value, field.allowedValues) {
return ErrBadField(field.column)
}
q.Add(field.column, field.value)
}
_, err = md.DB.Exec(
`UPDATE users_preferences, users AS u
SET `+q.Fields()+`
WHERE users_preferences.id = ? AND u.id = ?
LIMIT 1`,
append(q.Parameters, md.ID(), md.ID())...,
)
if err != nil {
md.Err(err)
return Err500
}
return UserSelfScoreboardGET(md)
}
func contains(needle int, haystack []int) bool {
for _, x := range haystack {
if x == needle {
return true
}
}
return false
}
func intPtrIn(x int, y *int, z int) *int {
if y == nil {
return nil
}
if *y > z {
return nil
}
if *y < x {
return nil
}
return y
}