/
party.go
478 lines (414 loc) · 11.1 KB
/
party.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
package controllers
import (
"crypto/rand"
"database/sql"
"encoding/json"
"fmt"
"log"
"math/big"
pseudoRand "math/rand"
"net/http"
"time"
null "gopkg.in/nullbio/null.v6"
"github.com/chances/party-server/cache"
e "github.com/chances/party-server/errors"
"github.com/chances/party-server/events"
"github.com/chances/party-server/models"
"github.com/chances/party-server/session"
s "github.com/chances/party-server/spotify"
"github.com/gin-gonic/gin"
"github.com/twinj/uuid"
"github.com/vattle/sqlboiler/queries/qm"
"github.com/vattle/sqlboiler/types"
)
// Party controller
type Party struct {
Controller
}
// NewParty creates a new Party controller
func NewParty() Party {
newParty := Party{}
newParty.Setup()
// Seed room code generator
pseudoRand.Seed(time.Now().UnixNano())
for i := 0; i < (pseudoRand.Intn(150-25) + 25); i++ {
generateRoomCode(pseudoRand.Intn(100-5) + 5)
}
return newParty
}
// Get the current user's party
func (cr *Party) Get() gin.HandlerFunc {
return func(c *gin.Context) {
currentParty, err := session.CurrentParty(c)
if err != nil {
if err != sql.ErrNoRows {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
c.JSON(http.StatusNotFound, models.EmptyRespose)
return
}
publicParty, err := currentParty.Public()
if err != nil {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
cr.respondWithParty(c, publicParty)
}
}
// Join a party as a guest
func (cr *Party) Join() gin.HandlerFunc {
return func(c *gin.Context) {
sesh := session.DefaultSession(c)
var joinParty struct {
Data struct {
RoomCode string `json:"room_code" binding:"required"`
} `json:"data" binding:"required"`
}
if err := c.Bind(&joinParty); err != nil {
c.Error(e.BadRequest.WithDetail("Unexpected request body").CausedBy(err))
c.Abort()
return
}
party, err := models.PartiesG(qm.Where("room_code=?", joinParty.Data.RoomCode)).One()
if err != nil {
if err != sql.ErrNoRows {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
c.JSON(http.StatusNotFound, models.EmptyRespose)
return
}
guests, err := party.Guests()
if err != nil {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
// If the user is fully authenticated skip guest initialization and
// respond with augmented party
if session.IsLoggedIn(c) {
// TODO: Make sure this auth'd user started _this_ party
publicParty := cr.augmentParty(party, guests)
cr.respondWithParty(c, publicParty)
return
}
// TODO: Handle party has ended, respond with some error code, 404 seems wrong...
// TODO: Add party leave endpoint for guests? (Remove them from Redis, especially)
// Or remove guest tokens from Redis when a party ends? (Periodic cleanup of guests)
// "Already joined a party" will error if guest exists in Redis, but not DB
// It is a bad request to try to join a party if the guest has already
// joined a different party
//
// If the user is already a guest and they've joined _this_ party then
// respond with augmented party
// Otherwise, it is a bad request if they have NOT joined _this_ party
if session.IsGuest(c) {
guest := *session.CurrentGuest(c)
if guest["Party"] == party.ID {
publicParty := cr.augmentParty(party, guests)
cr.respondWithParty(c, publicParty)
return
}
c.Error(e.BadRequest.WithDetail("Already joined a party"))
c.Abort()
return
}
origin := c.Request.Header.Get("Origin")
guestToken := uuid.NewV4().String()
c.Set("guest", guestToken)
err = sesh.Set("GUEST", guestToken)
if err != nil {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
err = cr.Cache.Set(guestToken, cache.Expires(
time.Now().Add(time.Minute*time.Duration(30)),
gin.H{
"Origin": origin,
"Token": guestToken,
"Party": party.ID,
},
))
if err != nil {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
guests = append(guests, models.NewGuest("", guestToken))
err = party.UpdateGuestList(guests)
if err != nil {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
publicParty := cr.augmentParty(party, guests)
cr.respondWithParty(c, publicParty)
go events.UpdateParty(publicParty)
}
}
// PruneExpiredGuests from active parties every five seconds
func (cr *Party) PruneExpiredGuests() {
for {
time.Sleep(time.Second * time.Duration(5))
prunedGuests := 0
// Get all parties that haven't ended
parties := models.PartiesG(qm.Where("ended=false")).AllP()
for _, party := range parties {
guests, err := party.Guests()
if err != nil {
continue
}
numGuests := len(guests)
// Remove expired guests
for i := 0; i < len(guests); i++ {
guest := guests[i]
exists, err := cr.Cache.Exists(guest.Token)
if err != nil {
continue
}
if !exists {
// Cut out this guest from the slice of guests
guests = append(guests[:i], guests[i+1:]...)
i--
prunedGuests++
}
}
// If the guest list changed then update the party's guest list and
// broadcast changed party
if numGuests != len(guests) {
party.UpdateGuestList(guests)
publicParty := cr.augmentParty(party, guests)
go events.UpdateParty(publicParty)
}
}
if prunedGuests > 0 {
log.Printf("Pruned %d expired guests\n", prunedGuests)
}
}
}
func (cr *Party) augmentParty(
party *models.Party, guests []models.Guest) models.PublicParty {
response := models.PublicParty{
Location: party.Location,
RoomCode: party.RoomCode,
Ended: party.Ended,
}
if len(guests) > 0 {
guestsPublic := make([]models.PublicGuest, 0)
for _, guest := range guests {
guestsPublic = append(guestsPublic, guest.Public())
}
guestsJSONRaw, _ := json.Marshal(guestsPublic)
guestsJSON := types.JSON(guestsJSONRaw)
response.Guests = &guestsJSON
}
if party.CurrentTrack.Valid {
var track models.PlayingTrack
err := party.CurrentTrack.Unmarshal(&track)
if err == nil {
response.CurrentTrack = &track
}
}
return response
}
func (cr *Party) respondWithParty(c *gin.Context, party models.PublicParty) {
c.JSON(http.StatusOK, models.NewResponse(
party.RoomCode, "party",
cr.RequestURI(c),
party,
))
}
// Start a new party for the current user
func (cr *Party) Start() gin.HandlerFunc {
return func(c *gin.Context) {
var newParty struct {
Data struct {
Host string `json:"host" binding:"required"`
PlaylistID string `json:"playlist_id" binding:"required"`
} `json:"data" binding:"required"`
}
if err := c.Bind(&newParty); err != nil {
c.Error(e.BadRequest.WithDetail("Unexpected request body").CausedBy(err))
c.Abort()
return
}
playlistID := newParty.Data.PlaylistID
currentUser := session.CurrentUser(c)
// End the user's current party if they've already started one
currentParty, err := currentUser.PartyG().One()
if currentParty != nil {
cr.endParty(c, currentUser, currentParty)
}
spotifyClient, err := cr.ClientFromSession(c)
if err != nil {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
playlists, err := s.Playlists(currentUser.Username, *spotifyClient)
if err != nil {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
var currentPlaylist *models.Playlist
for _, playlist := range playlists {
if playlistID == playlist.ID {
currentPlaylist = &playlist
break
}
}
// Playlist doesn't belong to user, bad request
if currentPlaylist == nil {
errMessage := fmt.Sprintf("Invalid playlist id %v. User '%s' does not own or subscribe to given playlist", playlistID, currentUser.Username)
c.Error(e.BadRequest.WithDetail(errMessage))
c.Abort()
return
}
location := gin.H{
"host_name": newParty.Data.Host,
}
roomCode := generateRoomCode(roomCodeLength)
if roomCode == "" {
c.Error(e.Internal.WithDetail("Could not generate room code"))
c.Abort()
return
}
party := models.Party{
RoomCode: roomCode,
CurrentTrack: null.JSONFrom(nil),
}
err = party.Location.Marshal(location)
if err != nil {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
// New party's queue
playlist, err := s.Playlist(currentUser.Username, currentPlaylist.ID, *spotifyClient)
if err != nil {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
queue := models.TrackList{
SpotifyPlaylistID: null.NewString("", false),
}
err = queue.Data.Marshal(&playlist.Tracks)
if err != nil {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
err = party.SetQueueG(true, &queue)
if err != nil {
c.Error(e.Internal.WithDetail("Could not create queue").CausedBy(err))
c.Abort()
return
}
// New party's playback history
history := models.TrackList{
SpotifyPlaylistID: null.NewString("", false),
}
err = history.Data.Marshal(make([]models.Track, 0))
if err != nil {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
err = party.SetHistoryG(true, &history)
if err != nil {
c.Error(e.Internal.WithDetail("Could not create history").CausedBy(err))
c.Abort()
return
}
// New party's guest list
guests := make([]models.Guest, 0)
guestList := models.GuestList{}
err = guestList.Data.Marshal(guests)
if err != nil {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
err = party.SetGuestG(true, &guestList)
if err != nil {
c.Error(e.Internal.WithDetail("Could not create guest list").CausedBy(err))
c.Abort()
return
}
err = currentUser.SetPartyG(true, &party)
if err != nil {
c.Error(e.Internal.WithDetail("Could not create party").CausedBy(err))
c.Abort()
return
}
c.JSON(http.StatusOK, models.NewResponse(
party.RoomCode, "party",
cr.RequestURI(c),
party,
))
}
}
// End the current user's party
func (cr *Party) End() gin.HandlerFunc {
return func(c *gin.Context) {
currentUser := session.CurrentUser(c)
currentParty, err := currentUser.PartyG().One()
if err != nil {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
if currentParty == nil {
c.Error(e.BadRequest.WithDetail("No party exists for current user"))
c.Abort()
return
}
cr.endParty(c, currentUser, currentParty)
c.JSON(http.StatusOK, models.EmptyRespose)
}
}
func (cr *Party) endParty(c *gin.Context, user *models.User, party *models.Party) {
party.Ended = true
err := party.UpdateG()
if err != nil {
c.Error(e.Internal.WithDetail("Could not end current party").CausedBy(err))
c.Abort()
return
}
// Broadcast to clients that the party has ended
publicParty, err := party.Public()
if err != nil {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
go events.UpdateParty(publicParty)
user.PartyID = null.NewInt(0, false)
err = user.UpdateG()
if err != nil {
c.Error(e.Internal.WithDetail("Could not update user").CausedBy(err))
c.Abort()
return
}
}
const roomCodeLength = 4
const letterBytes = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
const letterBytesLength = int64(len(letterBytes))
func generateRoomCode(n int) string {
b := make([]byte, n)
for i := range b {
index, err := rand.Int(rand.Reader, big.NewInt(letterBytesLength))
if err != nil {
panic(err)
}
b[i] = letterBytes[index.Int64()]
}
return string(b)
}