-
Notifications
You must be signed in to change notification settings - Fork 3
/
radio.go
1404 lines (1207 loc) · 37.7 KB
/
radio.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package radio
import (
"bytes"
"context"
"crypto/sha1"
"database/sql/driver"
"encoding/hex"
"fmt"
"math"
"strconv"
"strings"
"time"
"github.com/R-a-dio/valkyrie/util/eventstream"
"github.com/rs/xid"
"golang.org/x/crypto/bcrypt"
)
const (
LimitArtistLength = 500
LimitAlbumLength = 200
LimitTitleLength = 200
LimitReasonLength = 120
)
// CalculateRequestDelay returns the delay between two requests of a song
func CalculateRequestDelay(requestCount int) time.Duration {
if requestCount > 30 {
requestCount = 30
}
var dur float64
if requestCount >= 0 && requestCount <= 7 {
dur = -11057*math.Pow(float64(requestCount), 2) +
172954*float64(requestCount) + 81720
} else {
dur = 599955*math.Exp(0.0372*float64(requestCount)) + 0.5
}
return time.Duration(time.Duration(dur/2) * time.Second)
}
// IsRobot indicates if the user has the Robot flag
func IsRobot(user User) bool {
return user.UserPermissions.HasExplicit(PermRobot)
}
// CalculateCooldown sees if the cooldown given has passed since `last` and returns
// the remaining time if any and a bool indicating if it has passed since then or
// not. It always returns true if `last` is zero.
func CalculateCooldown(delay time.Duration, last time.Time) (time.Duration, bool) {
// zero time indicates never requested before
if last.IsZero() {
return 0, true
}
since := time.Since(last)
if since > delay {
return 0, true
}
return delay - since, false
}
type Status struct {
// StreamUser is the user that is currently streaming, will be nil
// if no one is connected on the master mountpoint (even short drops)
StreamUser *User
// User is the user that is currently or was the last to broadcast
// on the stream
User User
// Song is the song that is currently playing on the stream
Song Song
// SongInfo is extra information about the song that is currently playing
SongInfo SongInfo
// StreamerName is the name given to us by the user that is streaming
StreamerName string
// Listeners is the current amount of stream listeners
Listeners Listeners
// Thread is an URL to a third-party platform related to the current stream
Thread string
}
func (s *Status) IsZero() bool {
ok := s.User.ID == 0 &&
s.User.DJ.ID == 0 &&
s.Song.ID == 0 &&
s.SongInfo == (SongInfo{}) &&
s.StreamerName == "" &&
s.Listeners == 0 &&
s.Thread == "" &&
(!s.Song.HasTrack() || s.Song.TrackID == 0)
return ok
}
// UserID is an identifier corresponding to an user
type UserID uint32
func ParseUserID(s string) (UserID, error) {
id, err := strconv.ParseUint(s, 10, 32)
if err != nil {
return 0, err
}
return UserID(id), nil
}
func (id UserID) String() string {
return strconv.FormatUint(uint64(id), 10)
}
// UserPermission is a permission for user authorization
type UserPermission string
func (u UserPermission) String() string {
return string(u)
}
type UserPermissions map[UserPermission]struct{}
// Has returns true if the permissions in UserPermission allow
// access to the permission given
func (up UserPermissions) Has(perm UserPermission) bool {
if !up.has(PermActive) { // not an active user
return false
}
return up.has(perm) || up.has(PermDev)
}
// HasExplicit returns true if the permission given is explicitly in the UserPermissions
func (up UserPermissions) HasExplicit(perm UserPermission) bool {
return up.has(perm)
}
// HasEdit returns true if the UserPermissions is allowed to edit the given permission
func (up UserPermissions) HasEdit(perm UserPermission) bool {
// devs can edit any permission
if up.has(PermDev) {
return true
}
// admins can edit every permission except for PermDev
if up.has(PermAdmin) && perm != PermDev {
return true
}
// no one else can edit permissions
return false
}
func (up UserPermissions) has(perm UserPermission) bool {
if up == nil {
return false
}
_, ok := up[perm]
return ok
}
// Scan implements sql.Scanner
//
// Done in a way that it expects all permissions to be a single string or []byte
// separated by a comma
func (upp *UserPermissions) Scan(src interface{}) error {
if upp == nil {
return fmt.Errorf("nil found in Scan")
}
*upp = make(UserPermissions)
up := *upp
switch perms := src.(type) {
case []byte:
for _, p := range bytes.Split(perms, []byte(",")) {
up[UserPermission(bytes.TrimSpace(p))] = struct{}{}
}
case string:
for _, p := range strings.Split(perms, ",") {
up[UserPermission(strings.TrimSpace(p))] = struct{}{}
}
case nil: // no permissions, we made the map above though
default:
return fmt.Errorf("invalid argument passed to Scan")
}
return nil
}
func AllUserPermissions() []UserPermission {
return []UserPermission{
PermActive,
PermNews,
PermDJ,
PermDev,
PermAdmin,
PermStaff,
PermDatabaseDelete,
PermDatabaseEdit,
PermDatabaseView,
PermPendingEdit,
PermPendingView,
PermQueueEdit,
PermRobot,
PermScheduleEdit,
PermListenerView,
PermListenerKick,
PermProxyKick,
PermGrafanaView,
}
}
// List of permissions, this should be kept in sync with the database version
const (
PermActive = "active" // User is active
PermNews = "news" // User has news creation/editing access
PermDJ = "dj" // User has access to the icecast proxy
PermDev = "dev" // User is a developer
PermAdmin = "admin" // User is an administrator
PermStaff = "staff" // User is staff, only for display purposes on staff page
PermDatabaseDelete = "database_delete" // User can delete from the track database
PermDatabaseEdit = "database_edit" // User can edit the track database
PermDatabaseView = "database_view" // User can view the track database
PermPendingEdit = "pending_edit" // User can edit the pending track queue
PermPendingView = "pending_view" // User can view the pending track queue
PermQueueEdit = "queue_edit" // User can edit the streamer queue
PermRobot = "robot" // User is not human
PermScheduleEdit = "schedule_edit" // User can edit the schedule
PermListenerView = "listener_view" // User can view the listener list
PermListenerKick = "listener_kick" // User can kick listeners
PermProxyKick = "proxy_kick" // User can kick streamers
PermGrafanaView = "grafana_view" // User can view grafana
)
// User is an user account in the database
type User struct {
ID UserID
Username string
Password string
Email string
RememberToken string
IP string
UpdatedAt *time.Time
DeletedAt *time.Time
CreatedAt time.Time
DJ DJ
UserPermissions UserPermissions
}
func (u User) ComparePassword(passwd string) error {
return bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(passwd))
}
func (u *User) IsValid() bool {
return u != nil && u.Username != ""
}
var bcryptCost = 14
func GenerateHashFromPassword(passwd string) (string, error) {
h, err := bcrypt.GenerateFromPassword([]byte(passwd), bcryptCost)
if err != nil {
return "", err
}
return string(h), nil
}
// DJID is an identifier corresponding to a dj
type DJID int32
func ParseDJID(s string) (DJID, error) {
id, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return 0, err
}
return DJID(id), nil
}
func (id DJID) String() string {
return strconv.FormatInt(int64(id), 10)
}
// DJ is someone that has access to streaming
type DJ struct {
ID DJID
Name string
Regex string
Text string
Image string
Visible bool
Priority int
Role string
CSS string
Color string
Theme Theme
}
// TrackState is the state of a Track in storage
type TrackState int
const (
TrackStateUnverified TrackState = iota
TrackStatePlayable
)
// ThemeID is the identifier of a website theme
type ThemeID uint32
// Theme is a website theme
type Theme struct {
ID ThemeID
Name string
DisplayName string
Author string
}
type SongInfo struct {
// Start is the time at which the current song started playing
Start time.Time
// End is the expected time the current song stops playing
End time.Time
}
type SearchService interface {
Search(ctx context.Context, query string, limit int64, offset int64) (*SearchResult, error)
Update(context.Context, ...Song) error
Delete(context.Context, ...TrackID) error
}
type SearchResult struct {
Songs []Song
TotalHits int
}
type SongUpdate struct {
Song
Info SongInfo
}
type Thread = string
// Listeners is a dedicated type for an amount of listeners
type Listeners = int64
// ListenerClientID is an identifier unique to each listener
type ListenerClientID uint64
// ParseListenerClientID parses a string as a ListenerClientID, input
// is expected to be like the output of ListenerClientID.String
func ParseListenerClientID(s string) (ListenerClientID, error) {
id, err := strconv.ParseUint(s, 10, 64)
return ListenerClientID(id), err
}
// String returns the ListenerClientID as a string
func (c ListenerClientID) String() string {
return strconv.FormatUint(uint64(c), 10)
}
// Listener is a listener of the stream
type Listener struct {
ID ListenerClientID
UserAgent string
IP string
Start time.Time
}
type ListenerTrackerService interface {
// ListClients lists all listeners currently connected
// to the stream
ListClients(context.Context) ([]Listener, error)
// RemoveClient kicks a listener from the stream
RemoveClient(context.Context, ListenerClientID) error
}
type ManagerService interface {
UpdateFromStorage(context.Context) error
CurrentUser(context.Context) (eventstream.Stream[*User], error)
UpdateUser(context.Context, *User) error
CurrentSong(context.Context) (eventstream.Stream[*SongUpdate], error)
UpdateSong(context.Context, *SongUpdate) error
CurrentThread(context.Context) (eventstream.Stream[Thread], error)
UpdateThread(context.Context, Thread) error
CurrentListeners(context.Context) (eventstream.Stream[Listeners], error)
UpdateListeners(context.Context, Listeners) error
CurrentStatus(context.Context) (eventstream.Stream[Status], error)
}
type StreamerService interface {
Start(context.Context) error
Stop(ctx context.Context, force bool) error
RequestSong(context.Context, Song, string) error
Queue(context.Context) (Queue, error)
}
type Queue []QueueEntry
// Limit limits the queue size to the maxSize given or
// the whole queue if maxSize < len(queue)
func (q Queue) Limit(maxSize int) Queue {
return q[:min(maxSize, len(q))]
}
// Length returns the length of the queue
func (q Queue) Length() time.Duration {
if len(q) > 0 {
last := q[len(q)-1]
return time.Until(last.ExpectedStartTime) + last.Length
}
return 0
}
// RequestAmount returns the amount of QueueEntries that have
// IsUserRequest set to true
func (q Queue) RequestAmount() int {
var n int
for _, entry := range q {
if entry.IsUserRequest {
n++
}
}
return n
}
func NewQueueID() QueueID {
return QueueID{xid.New()}
}
type QueueID struct {
xid.ID
}
func ParseQueueID(s string) (QueueID, error) {
id, err := xid.FromString(s)
return QueueID{id}, err
}
func (qid QueueID) String() string {
return qid.ID.String()
}
// QueueEntry is a Song used in the QueueService
type QueueEntry struct {
// QueueID is a unique identifier for this queue entry
QueueID QueueID
// Song that is queued
Song
// IsUserRequest should be true if this song was added to the queue
// by a third-party user
IsUserRequest bool
// UserIdentifier should be a way to identify the user that requested the song
UserIdentifier string
// ExpectedStartTime is the expected time this song will be played on stream
ExpectedStartTime time.Time
}
func (qe QueueEntry) String() string {
est := qe.ExpectedStartTime.Format("2006-01-02 15:04:05")
if qe.IsUserRequest {
return fmt.Sprintf("(%s)(R) %s", est, qe.Song.Metadata)
} else {
return fmt.Sprintf("(%s)(P) %s", est, qe.Song.Metadata)
}
}
func (qe *QueueEntry) EqualTo(qe2 QueueEntry) bool {
return qe.QueueID == qe2.QueueID
}
type QueueService interface {
// AddRequest requests the given song to be added to the queue, the string given
// is an identifier of the user that requested it
AddRequest(context.Context, Song, string) error
// ReserveNext returns the next yet-to-be-reserved entry from the queue
ReserveNext(context.Context) (*QueueEntry, error)
// ResetReserved resets the reserved status of all entries returned by ReserveNext
// but not yet removed by Remove
ResetReserved(context.Context) error
// Remove removes the first occurence of the given entry from the queue
Remove(context.Context, QueueID) (bool, error)
// Entries returns all entries in the queue
Entries(context.Context) (Queue, error)
}
type AnnounceService interface {
AnnounceSong(context.Context, Status) error
AnnounceRequest(context.Context, Song) error
AnnounceUser(context.Context, *User) error
}
// SongID is a songs identifier
type SongID uint32
func ParseSongID(s string) (SongID, error) {
id, err := strconv.ParseUint(s, 10, 32)
if err != nil {
return 0, err
}
return SongID(id), nil
}
// Scan implements sql.Scanner
func (s *SongID) Scan(src any) error {
// Scanner is only implemented so that null values are supported
// without introducing an intermediate type
if src == nil {
return nil
}
var err error
switch v := src.(type) {
case int64:
*s = SongID(v)
case uint64: // mysql driver sometimes gives you this
*s = SongID(v)
case float64:
*s = SongID(v)
case []byte: // decimals
*s, err = ParseSongID(string(v))
case string:
*s, err = ParseSongID(v)
}
return err
}
func (s SongID) String() string {
return strconv.FormatUint(uint64(s), 10)
}
// SongHash is a sha1 hash
type SongHash [sha1.Size]byte
// ParseSongHash reverts SongHash.String
func ParseSongHash(s string) (SongHash, error) {
var hash SongHash
_, err := hex.Decode(hash[:], []byte(s))
return hash, err
}
// NewSongHash generates a new SongHash for the metadata passed in
func NewSongHash(metadata string) SongHash {
metadata = strings.TrimSpace(strings.ToLower(metadata))
return SongHash(sha1.Sum([]byte(metadata)))
}
// Value implements sql/driver.Valuer
func (s SongHash) Value() (driver.Value, error) {
return s.String(), nil
}
// Scan implements sql.Scanner
func (s *SongHash) Scan(src interface{}) error {
if src == nil {
return nil
}
var err error
switch v := src.(type) {
case []byte:
_, err = hex.Decode((*s)[:], v)
case string:
_, err = hex.Decode((*s)[:], []byte(v))
default:
err = fmt.Errorf("unsupported type in SongHash.Scan: %t", src)
}
return err
}
// String returns a hexadecimal representation of the song hash
func (s SongHash) String() string {
return fmt.Sprintf("%x", s[:])
}
// MarshalJSON implements encoding/json.Marshaler
func (s SongHash) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%x"`, s[:])), nil
}
// UnmarshalJSON implements encoding/json.Unmarshaler
func (s *SongHash) UnmarshalJSON(b []byte) error {
_, err := hex.Decode((*s)[:], b[1:len(b)-1])
return err
}
var zeroSongHash SongHash
func (s *SongHash) IsZero() bool {
return *s == zeroSongHash
}
// Song is a song we've seen played on the stream
type Song struct {
ID SongID
// Hash is a sha1 of the contents of Metadata
Hash SongHash
// HashLink is the same as Hash but points to another song that we share some data with
HashLink SongHash
// Metadata is simple metadata for this song in the format 'artist - title'
Metadata string
// Length is the length of the song
Length time.Duration
// LastPlayed is the last time this song played on stream
LastPlayed time.Time
// LastPlayedBy is the user that last played this song, can be nil
LastPlayedBy *User
// DatabaseTrack is only available if the song is in our streamer database
*DatabaseTrack
// SyncTime is the time this Song was returned by the database layer
SyncTime time.Time
}
// EqualTo returns s == d based on unique fields
func (s Song) EqualTo(d Song) bool {
// both songs we get are copies so we can hydrate them inside
// and use the hash as unique identifier since it should handle
// any special cases
s.Hydrate()
d.Hydrate()
// equal hash means they're just the same metadata
if s.Hash == d.Hash {
return true
}
// equal hashlink to hash means the metadata was the same at some point
if s.HashLink == d.Hash || s.Hash == d.HashLink {
return true
}
// no tracks to compare, so these are then not equal
if !s.HasTrack() || !d.HasTrack() {
return false
}
// we do have a track, equal if the TrackID matches
return s.TrackID == d.TrackID
}
// TrackID is a database track identifier
type TrackID uint32
func ParseTrackID(s string) (TrackID, error) {
id, err := strconv.ParseUint(s, 10, 32)
if err != nil {
return 0, err
}
return TrackID(id), nil
}
func (t TrackID) String() string {
return strconv.FormatUint(uint64(t), 10)
}
// DatabaseTrack is a song we have the actual audio file for and is available to the
// automated streamer
type DatabaseTrack struct {
TrackID TrackID
Artist string
Title string
Album string
FilePath string
Tags string
Acceptor string
LastEditor string
Priority int
Usable bool
NeedReplacement bool
LastRequested time.Time
RequestCount int
}
// Requestable returns whether this song can be requested by a user
func (s *Song) Requestable() bool {
if s == nil || s.DatabaseTrack == nil {
return false
}
delay := s.RequestDelay()
if delay == 0 {
// unknown song delay
return false
}
if time.Since(s.LastPlayed) < delay {
return false
}
if time.Since(s.LastRequested) < delay {
return false
}
return true
}
var veryFarAway = time.Hour * 24 * 90
func (s *Song) RequestDelay() time.Duration {
if s == nil || s.DatabaseTrack == nil {
return 0
}
return CalculateRequestDelay(s.RequestCount)
}
// UntilRequestable returns the time until this song can be requested again, returns 0
// if song.Requestable() == true
func (s *Song) UntilRequestable() time.Duration {
if s.Requestable() {
return 0
}
delay := s.RequestDelay()
if delay == 0 {
return veryFarAway
}
var furthest time.Time
if s.LastPlayed.After(s.LastRequested) {
furthest = s.LastPlayed
} else {
furthest = s.LastRequested
}
if furthest.IsZero() {
return veryFarAway
}
furthest = furthest.Add(delay)
return time.Until(furthest)
}
// Hydrate tries to fill Song with data from other fields, mostly useful
// for if we have a DatabaseTrack but want to create the Song fields
func (s *Song) Hydrate() {
// trim any whitespace from the metadata
s.Metadata = strings.TrimSpace(s.Metadata)
// if our metadata is empty at this point, and we have a database track
// we lookup the artist and title of the track to create our metadata
if s.Metadata == "" && s.HasTrack() {
s.Metadata = Metadata(s.Artist, s.Title)
}
if s.Metadata == "" {
// no metadata to work with, to avoid a bogus hash creation down below
// we just exit early and don't update anything
return
}
// generate a hash from the metadata
s.Hash = NewSongHash(s.Metadata)
// and if our HashLink isn't set yet update that too
if s.HashLink.IsZero() {
s.HashLink = s.Hash
}
}
func Metadata(artist, title string) string {
artist = strings.TrimSpace(artist)
title = strings.TrimSpace(title)
if artist != "" {
return fmt.Sprintf("%s - %s", artist, title)
}
return title
}
func NewSong(metadata string, length ...time.Duration) Song {
song := Song{
Metadata: metadata,
}
if len(length) > 0 {
song.Length = length[0]
}
song.Hydrate()
return song
}
// HasTrack returns true if t != nil, can be used as Song.HasTrack to check if a track
// was allocated for the embedded field
func (t *DatabaseTrack) HasTrack() bool {
return t != nil
}
type StorageTx interface {
Commit() error
Rollback() error
}
// StorageService is an interface containing all *StorageService interfaces
type StorageService interface {
SessionStorageService
RelayStorageService
QueueStorageService
SongStorageService
TrackStorageService
RequestStorageService
UserStorageService
StatusStorageService
SubmissionStorageService
NewsStorageService
ScheduleStorageService
// Close closes the storage service and cleans up any resources
Close() error
}
// SessionStorageService is a service that supplies a SessionStorage
type SessionStorageService interface {
Sessions(context.Context) SessionStorage
SessionsTx(context.Context, StorageTx) (SessionStorage, StorageTx, error)
}
// SessionStorage stores Session's by a SessionToken
type SessionStorage interface {
Delete(SessionToken) error
Get(SessionToken) (Session, error)
Save(Session) error
}
// SessionToken is the token associated with a singular session
type SessionToken string
// Session is a website user session
type Session struct {
Token SessionToken
Expiry time.Time
Data []byte
}
// QueueStorageService is a service able to supply a QueueStorage
type QueueStorageService interface {
Queue(context.Context) QueueStorage
QueueTx(context.Context, StorageTx) (QueueStorage, StorageTx, error)
}
// QueueStorage stores a queue
type QueueStorage interface {
// Store stores the queue with the name given
Store(name string, queue []QueueEntry) error
// Load returns the queue associated with the name given
Load(name string) ([]QueueEntry, error)
}
// SongStorageService is a service able to supply a SongStorage
type SongStorageService interface {
Song(context.Context) SongStorage
SongTx(context.Context, StorageTx) (SongStorage, StorageTx, error)
}
type LastPlayedKey uint32
const LPKeyLast = LastPlayedKey(math.MaxUint32)
// SongStorage stores information about songs
//
// A song can be anything that plays on stream, unlike a track which is a specific
// kind of song that we have an audio file for and can be played by the automated streamer
type SongStorage interface {
// Create creates a new song with the metadata given
Create(Song) (*Song, error)
// FromMetadata returns the song associated with the metadata given
FromMetadata(metadata string) (*Song, error)
// FromHash returns the song associated with the SongHash given
FromHash(SongHash) (*Song, error)
// LastPlayed returns songs that have recently played, up to amount given after
// applying the offset
LastPlayed(key LastPlayedKey, amountPerPage int) ([]Song, error)
// LastPlayedPagination looks up keys for adjacent pages of key
LastPlayedPagination(key LastPlayedKey, amountPerPage, pageCount int) (prev, next []LastPlayedKey, err error)
// LastPlayedCount returns the amount of plays recorded
LastPlayedCount() (int64, error)
// PlayedCount returns the amount of times the song has been played on stream
PlayedCount(Song) (int64, error)
// AddPlay adds a play to the song. streamer is the dj that played the song.
// If present, ldiff is the difference in amount of listeners between
// song-start and song-end.
AddPlay(song Song, streamer User, ldiff *Listeners) error
// FavoriteCount returns the amount of users that have added this song to
// their favorite list
FavoriteCount(Song) (int64, error)
// Favorites returns all users that have this song on their favorite list
Favorites(Song) ([]string, error)
// FavoritesOf returns all songs that are on a users favorite list
FavoritesOf(nick string, limit, offset int64) ([]Song, int64, error)
// FavoritesOfDatabase returns all songs that are on a users favorite list
// and also have a track database
FavoritesOfDatabase(nick string) ([]Song, error)
// AddFavorite adds the given song to nicks favorite list
AddFavorite(song Song, nick string) (bool, error)
// RemoveFavorite removes the given song from nicks favorite list
RemoveFavorite(song Song, nick string) (bool, error)
// UpdateLength updates the stored length of the song
UpdateLength(Song, time.Duration) error
// UpdateHashLink updates the HashLink of the song
UpdateHashLink(old SongHash, new SongHash) error
}
// TrackStorageService is a service able to supply a TrackStorage
type TrackStorageService interface {
Track(context.Context) TrackStorage
TrackTx(context.Context, StorageTx) (TrackStorage, StorageTx, error)
}
// TrackStorage stores information about tracks
//
// A track is a song that we have the audio file for and can thus be played by
// the automated streaming system
type TrackStorage interface {
// Get returns a single track with the TrackID given
Get(TrackID) (*Song, error)
// All returns all tracks in storage
All() ([]Song, error)
// AllRaw returns all tracks in storage, but without making sure all fields
// are filled. This returns them as-is straight from storage
AllRaw() ([]Song, error)
// Delete removes a track from storage
Delete(TrackID) error
// Unusable returns all tracks that are deemed unusable by the streamer
Unusable() ([]Song, error)
// NeedReplacement returns the song that need a replacement
NeedReplacement() ([]Song, error)
// Insert inserts a new track, errors if ID or TrackID is set
Insert(song Song) (TrackID, error)
// UpdateMetadata updates track metadata only (artist/title/album/tags/filepath/needreplacement)
UpdateMetadata(song Song) error
// UpdateUsable sets usable to the state given
UpdateUsable(song Song, state TrackState) error
// UpdateRequestInfo is called after a track has been requested, this should do any
// necessary book-keeping related to that
UpdateRequestInfo(TrackID) error
// UpdateLastPlayed sets the last time the track was played to the current time
UpdateLastPlayed(TrackID) error
// UpdateLastRequested sets the last time the track was requested to the current time
UpdateLastRequested(TrackID) error
// BeforeLastRequested returns all tracks that have their LastRequested before the
// time given
BeforeLastRequested(before time.Time) ([]Song, error)
// DecrementRequestCount decrements the RequestCount for all tracks that have
// their LastRequested before the time given
DecrementRequestCount(before time.Time) error
// QueueCandidates returns tracks that are candidates to be queue'd by the
// default queue implementation
QueueCandidates() ([]TrackID, error)
}
// RequestStorageService is a service able to supply a RequestStorage
type RequestStorageService interface {
Request(context.Context) RequestStorage
RequestTx(context.Context, StorageTx) (RequestStorage, StorageTx, error)
}
// RequestStorage stores things related to automated streamer song requests
type RequestStorage interface {
// LastRequest returns the time of when the identifier given last requested
// a song from the streamer
LastRequest(identifier string) (time.Time, error)
// UpdateLastRequest updates the LastRequest time to the current time for the
// identifier given
UpdateLastRequest(identifier string) error
}
// UserStorageService is a service able to supply a UserStorage
type UserStorageService interface {
User(context.Context) UserStorage
UserTx(context.Context, StorageTx) (UserStorage, StorageTx, error)
}
// UserStorage stores things related to users with actual accounts on the website
type UserStorage interface {
// All returns all users
All() ([]User, error)
// Create creates a user
Create(User) (UserID, error)
// CreateDJ creates a DJ for the user given
CreateDJ(User, DJ) (DJID, error)
// Get returns the user matching the name given
Get(name string) (*User, error)
// GetByID returns the user associated with the UserID
GetByID(UserID) (*User, error)
// GetByDJID returns the user associated with the DJID
GetByDJID(DJID) (*User, error)
// Update updates the given user
Update(User) (User, error)
// LookupName matches the name given fuzzily to a user
LookupName(name string) (*User, error)
// ByNick returns an user that is associated with the nick given
ByNick(nick string) (*User, error)
// Permissions returns all available permissions
Permissions() ([]UserPermission, error)