Skip to content

Commit

Permalink
Release v0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Nov 4, 2021
2 parents 12bf117 + f690ff1 commit 096aa05
Show file tree
Hide file tree
Showing 27 changed files with 627 additions and 318 deletions.
31 changes: 31 additions & 0 deletions .idea/runConfigurations/dev_database.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion database/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"gorm.io/gorm"
)

const targetDatabaseVersion = 35
const targetDatabaseVersion = 41

func migrateIfNecessary(db *gorm.DB) {
logger.Info("Connection acquired. Checking database version")
Expand Down
21 changes: 7 additions & 14 deletions database/model/songRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@ package model

type SongRequest struct {
BaseModel
SessionId uint `json:"session_id"`
UserId *uint `json:"user_id"`
SpotifyTrackId string `json:"spotify_track_id"`
TrackMetadata TrackMetadata `gorm:"foreignKey:spotify_track_id;references:spotify_track_id" json:"track_metadata"`
Status SongRequestStatus `json:"status"`
SessionId uint `json:"session_id"`
SpotifyTrackId string `json:"spotify_track_id"`
TrackMetadata TrackMetadata `gorm:"foreignKey:spotify_track_id;references:spotify_track_id" json:"track_metadata"`
Played bool `json:"-"`
Locked bool `json:"-"`
RequestedBy string `json:"requested_by"`
Weight int64 `json:"-"`
}

type SongRequestStatus string

const (
StatusPlayed SongRequestStatus = "PLAYED"
StatusCurrentlyPlaying SongRequestStatus = "CURRENTLY_PLAYING"
StatusUpNext SongRequestStatus = "UP_NEXT"
StatusInQueue SongRequestStatus = "IN_QUEUE"
)
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ require (
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lib/pq v1.10.3 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/mitchellh/mapstructure v1.4.2 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.8.1
github.com/spf13/viper v1.9.0
github.com/zmb3/spotify v1.3.0
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/net v0.0.0-20210908191846-a5e095526f91 // indirect
golang.org/x/crypto v0.0.0-20210920023735-84f357641f63 // indirect
golang.org/x/net v0.0.0-20210917221730-978cfadd31cf // indirect
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f
golang.org/x/sys v0.0.0-20210910150752-751e447fb3d0 // indirect
golang.org/x/sys v0.0.0-20210917161153-d61c044b1678 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/ini.v1 v1.63.0 // indirect
gopkg.in/ini.v1 v1.63.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gorm.io/driver/postgres v1.1.1
gorm.io/gorm v1.21.15
Expand Down
130 changes: 92 additions & 38 deletions go.sum

Large diffs are not rendered by default.

66 changes: 45 additions & 21 deletions listeningSession/fallbackPlaylist.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,38 +58,41 @@ func SetFallbackPlaylistShuffle(session model.SimpleListeningSession, user model
return nil
}

func addFallbackTrackIfNecessary(session model.FullListeningSession, upNextRequest *model.SongRequest) (trackAdded bool, error *SpotifeteError) {
func addFallbackTrackIfNecessary(session model.FullListeningSession, queue []model.SongRequest) (updatedQueue []model.SongRequest, error *SpotifeteError) {

if session.FallbackPlaylistId == nil {
return false, nil
return queue, nil
}

if upNextRequest != nil {
return false, nil
}
for i := len(queue); i < 2; i++ {
addedRequest, spotifeteError := addFallbackTrack(session)
if spotifeteError != nil {
return queue, spotifeteError
}

spotifeteError := addFallbackTrack(session)
if spotifeteError != nil {
return false, spotifeteError
queue = append(queue, addedRequest)
}

return true, nil
return queue, nil
}

func addFallbackTrack(session model.FullListeningSession) (error *SpotifeteError) {
func addFallbackTrack(session model.FullListeningSession) (addedRequest model.SongRequest, error *SpotifeteError) {

fallbackTrackId, spotifeteError := findNextFallbackTrack(session)
if spotifeteError != nil {
return spotifeteError
return model.SongRequest{}, spotifeteError
}

_, spotifeteError = RequestSong(session, fallbackTrackId)
addedRequest, spotifeteError = RequestSong(session, fallbackTrackId, "Fallback-Playlist")
if spotifeteError != nil {
return spotifeteError
return model.SongRequest{}, spotifeteError
}

return nil
return addedRequest, nil
}

func findNextFallbackTrack(session model.FullListeningSession) (nextFallbackTrackId string, spotifeteError *SpotifeteError) {

playableTracks, spotifeteError := getPlayablePlaylistTracks(*session.FallbackPlaylistId, session.Owner)
if spotifeteError != nil {
return "", spotifeteError
Expand All @@ -109,10 +112,17 @@ func findNextFallbackTrack(session model.FullListeningSession) (nextFallbackTrac
}

func doFindNextFallbackTrack(playableTracks *[]spotify.FullTrack, session model.FullListeningSession) (nextFallbackTrackId string, spotifeteError *SpotifeteError) {
currentlyPlayingRequest := GetCurrentlyPlayingRequest(session.SimpleListeningSession)

queue, err := GetFullQueue(session.SimpleListeningSession)
if err != nil {
return "", nil
}

for i := 0; i < 10_000; i++ {
fallbackTrack := findPossibleFallbackTrackFromPlayableTracks(*playableTracks, session.SimpleListeningSession, currentlyPlayingRequest, 0)
fallbackTrack, err := findPossibleFallbackTrackFromPlayableTracks(*playableTracks, session.SimpleListeningSession, queue, 0)
if err != nil {
return "", NewInternalError("could not find possible fallback tracks", err)
}
if fallbackTrack != nil {
return *fallbackTrack, nil
}
Expand All @@ -124,7 +134,7 @@ func doFindNextFallbackTrack(playableTracks *[]spotify.FullTrack, session model.
return "", NewInternalError(fmt.Sprintf("No track found in fallback playlist for session %d that has been played less than 10,000 times. Aborting and removing fallback playlist.", session.ID), nil)
}

func findPossibleFallbackTrackFromPlayableTracks(playableTracks []spotify.FullTrack, session model.SimpleListeningSession, currentlyPlayingRequest *model.SongRequest, maximumPlays int64) (possibleFallbackTrackId *string) {
func findPossibleFallbackTrackFromPlayableTracks(playableTracks []spotify.FullTrack, session model.SimpleListeningSession, queue []model.SongRequest, maximumPlays int64) (possibleFallbackTrackId *string, err error) {
if session.FallbackPlaylistShuffle {
rand.Seed(time.Now().UnixNano())
rand.Shuffle(
Expand All @@ -136,14 +146,28 @@ func findPossibleFallbackTrackFromPlayableTracks(playableTracks []spotify.FullTr

for _, track := range playableTracks {
trackId := track.ID.String()
if currentlyPlayingRequest == nil || currentlyPlayingRequest.SpotifyTrackId != trackId {
playCount := getTrackPlayCount(session, trackId)
if !queueContainsTrack(queue, trackId) {
playCount, err := getTrackPlayCount(session, trackId)
if err != nil {
return nil, err
}

if playCount <= maximumPlays {
return &trackId
return &trackId, nil
}
}
}

return nil
return nil, nil
}

func queueContainsTrack(queue []model.SongRequest, trackId string) bool {

for _, trackInQueue := range queue {
if trackInQueue.SpotifyTrackId == trackId {
return true
}
}

return false
}
Loading

0 comments on commit 096aa05

Please sign in to comment.