diff --git a/.gitignore b/.gitignore index d3e3326..13dc8ce 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ build/bin frontend/dist -frontend/node_modules \ No newline at end of file +frontend/node_modules +frontend/wailsjs/go diff --git a/session.go b/session.go index 5981c80..0a897de 100644 --- a/session.go +++ b/session.go @@ -4,29 +4,25 @@ import ( "errors" "fmt" "log" + "sync" "github.com/go-resty/resty/v2" ) -type AuthSuccess struct { - Id string `json:"id"` - IsAdmin bool `json:"isAdmin"` - Name string `json:"name"` - SubsonicSalt string `json:"subsonicSalt"` - SubsonicToken string `json:"subsonicToken"` - Token string `json:"token"` - Username string `json:"username"` -} - type AuthError struct { Error string `json:"error"` } +var LoggedUser AuthSuccess +var randomAlbumWaitGroup sync.WaitGroup +var randomAlbums []Album + // (Tries to) login to a remote navidrome server func (a *App) Login(server, username, password string) (bool, error) { + log.Print("begin Login to server") client := resty.New() - // TODO: check server for leading https and ending /, normalize + // TODO: check server for leading https and trailing /, normalize successData := AuthSuccess{} errorData := AuthError{} @@ -45,6 +41,13 @@ func (a *App) Login(server, username, password string) (bool, error) { if response.IsSuccess() { log.Printf("%+v", successData) + + // Set global session + LoggedUser = successData + // Begin to load the list of albums on the background + randomAlbumWaitGroup.Add(1) + go loadAlbums(server) + return true, nil } else if response.IsError() { log.Printf("%+v", errorData) @@ -54,3 +57,39 @@ func (a *App) Login(server, username, password string) (bool, error) { return false, errors.New("invalid state") } } + +// Waits for the random albums to be loaded, and returns them. +// This function assumes that the random albums are being loaded in the background. +func (a *App) GetRandomAlbums() ([]Album, error) { + log.Printf("Waiting for loading group...") + randomAlbumWaitGroup.Wait() + log.Printf("Waiting group finished") + + return randomAlbums, nil +} + +// Loads a list of random albums from the server. +func loadAlbums(serverUrl string) { + defer randomAlbumWaitGroup.Done() + log.Print("begin loadAlbums") + client := resty.New() + + var errorData AuthError + response, err := client.R(). + SetHeader("x-nd-authorization", fmt.Sprintf("Bearer %s", LoggedUser.Token)). + SetResult(&randomAlbums). + SetError(&errorData). + Get(fmt.Sprintf("%s/api/album?_end=20&_order=DESC&_sort=random&_start=0", serverUrl)) + + if err != nil { + log.Print("Get albums error") + } + + if response.IsSuccess() { + log.Print("Get albums success") + // TODO: Begin to load album artwork in the background + // Album artwork comes from the url /rest/getCoverArt.view + } + + // TODO: Do the loading +} diff --git a/types.go b/types.go new file mode 100644 index 0000000..aef9aa2 --- /dev/null +++ b/types.go @@ -0,0 +1,54 @@ +package main + +import "time" + +// The result of a Login +type AuthSuccess struct { + Id string `json:"id"` + IsAdmin bool `json:"isAdmin"` + Name string `json:"name"` + SubsonicSalt string `json:"subsonicSalt"` + SubsonicToken string `json:"subsonicToken"` + Token string `json:"token"` + Username string `json:"username"` +} + +// An album as defined by the server +type Album struct { + PlayCount int `json:"playCount"` + PlayDate time.Time `json:"playDate"` + Rating int `json:"rating"` + Starred bool `json:"starred"` + StarredAt time.Time `json:"starredAt"` + ID string `json:"id"` + Name string `json:"name"` + EmbedArtPath string `json:"embedArtPath"` + ArtistID string `json:"artistId"` + Artist string `json:"artist"` + AlbumArtistID string `json:"albumArtistId"` + AlbumArtist string `json:"albumArtist"` + AllArtistIds string `json:"allArtistIds"` + MaxYear int `json:"maxYear"` + MinYear int `json:"minYear"` + Compilation bool `json:"compilation"` + SongCount int `json:"songCount"` + Duration float64 `json:"duration"` + Size int `json:"size"` + Genre string `json:"genre"` + Genres []struct { + ID string `json:"id"` + Name string `json:"name"` + } `json:"genres"` + FullText string `json:"fullText"` + OrderAlbumName string `json:"orderAlbumName"` + OrderAlbumArtistName string `json:"orderAlbumArtistName"` + ImageFiles string `json:"imageFiles"` + Paths string `json:"paths"` + SmallImageURL string `json:"smallImageUrl"` + MediumImageURL string `json:"mediumImageUrl"` + LargeImageURL string `json:"largeImageUrl"` + ExternalURL string `json:"externalUrl"` + ExternalInfoUpdatedAt time.Time `json:"externalInfoUpdatedAt"` + CreatedAt time.Time `json:"createdAt"` + UpdatedAt time.Time `json:"updatedAt"` +}