diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index e092f188..ed8e18b1 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -2,7 +2,7 @@ stages: - build variables: - VERSION: 0.1.2 + VERSION: 0.1.3 build-go: image: golang:1.17 diff --git a/docs/changelog.md b/docs/changelog.md index b6129e35..57cbcfd5 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,3 +1,6 @@ +# 0.1.3 +- + # 0.1.2 - Add docker-compose file for local dev - Implemented top listeners for artist/album endpoints to match track diff --git a/init/deploy.sh b/init/deploy.sh index 9ab148c7..22213223 100755 --- a/init/deploy.sh +++ b/init/deploy.sh @@ -6,9 +6,13 @@ cd /var/www/goscrobble-api git pull echo 'Building backend' -go build -o goscrobble cmd/go-scrobble/*.go +go build -o goscrobble cmd/goscrobble/*.go + +echo 'Fetching lastest frontend commit' cd /var/www/goscrobble-web +git pull + echo 'Installing frontend packages' npm install --production diff --git a/internal/goscrobble/artist.go b/internal/goscrobble/artist.go index 9b33d9ba..4ad703d5 100644 --- a/internal/goscrobble/artist.go +++ b/internal/goscrobble/artist.go @@ -126,15 +126,21 @@ func getArtistByUUID(uuid string) (Artist, error) { return artist, nil } -func getTopArtists(userUuid string) (TopArtists, error) { +func getTopArtists(userUuid string, dayRange string) (TopArtists, error) { var topArtist TopArtists + dateClause := "" + if dayRange != "" { + dateClause = " AND DATE(created_at) > SUBDATE(CURRENT_DATE, " + dayRange + ") " + } + rows, err := db.Query("SELECT BIN_TO_UUID(`artists`.`uuid`, true), `artists`.`name`, IFNULL(BIN_TO_UUID(`artists`.`uuid`, true),''), count(*) "+ "FROM `scrobbles` "+ "JOIN `tracks` ON `tracks`.`uuid` = `scrobbles`.`track` "+ "JOIN track_artist ON track_artist.track = tracks.uuid "+ "JOIN artists ON track_artist.artist = artists.uuid "+ "WHERE `scrobbles`.`user` = UUID_TO_BIN(?, true) "+ + dateClause+ "GROUP BY `artists`.`uuid` "+ "ORDER BY count(*) DESC "+ "LIMIT 14;", diff --git a/internal/goscrobble/server.go b/internal/goscrobble/server.go index 9ebb0029..bba6aef6 100644 --- a/internal/goscrobble/server.go +++ b/internal/goscrobble/server.go @@ -77,17 +77,18 @@ func HandleRequests(port string) { v1.HandleFunc("/profile/{username}", limitMiddleware(getProfile, lightLimiter)).Methods("GET") v1.HandleFunc("/artists/top/{uuid}", limitMiddleware(getArtists, lightLimiter)).Methods("GET") + v1.HandleFunc("/artists/top/{uuid}/{days:[0-9]+}", limitMiddleware(getArtists, lightLimiter)).Methods("GET") v1.HandleFunc("/artists/{uuid}", limitMiddleware(getArtist, lightLimiter)).Methods("GET") v1.HandleFunc("/artists/{uuid}/top", limitMiddleware(getTopUsersForArtist, lightLimiter)).Methods("GET") - v1.HandleFunc("/albums/top/{uuid}", limitMiddleware(getArtists, lightLimiter)).Methods("GET") + v1.HandleFunc("/tracks/top/{uuid}", limitMiddleware(getTracks, lightLimiter)).Methods("GET") // User UUID - Top Tracks + v1.HandleFunc("/tracks/top/{uuid}/{days:[0-9]+}", limitMiddleware(getTracks, lightLimiter)).Methods("GET") // User UUID - Top Tracks (With Date) + v1.HandleFunc("/tracks/{uuid}", limitMiddleware(getTrack, lightLimiter)).Methods("GET") // Track UUID + v1.HandleFunc("/tracks/{uuid}/top", limitMiddleware(getTopUsersForTrack, lightLimiter)).Methods("GET") // TrackUUID - Top Listeners + v1.HandleFunc("/albums/{uuid}", limitMiddleware(getAlbum, lightLimiter)).Methods("GET") v1.HandleFunc("/albums/{uuid}/top", limitMiddleware(getTopUsersForAlbum, lightLimiter)).Methods("GET") - v1.HandleFunc("/tracks/top/{uuid}", limitMiddleware(getTracks, lightLimiter)).Methods("GET") // User UUID - Top Tracks - v1.HandleFunc("/tracks/{uuid}", limitMiddleware(getTrack, lightLimiter)).Methods("GET") // Track UUID - v1.HandleFunc("/tracks/{uuid}/top", limitMiddleware(getTopUsersForTrack, lightLimiter)).Methods("GET") // TrackUUID - Top Listeners - v1.HandleFunc("/register", limitMiddleware(handleRegister, heavyLimiter)).Methods("POST") v1.HandleFunc("/login", limitMiddleware(handleLogin, standardLimiter)).Methods("POST") v1.HandleFunc("/sendreset", limitMiddleware(handleSendReset, heavyLimiter)).Methods("POST") @@ -594,18 +595,21 @@ func getTrack(w http.ResponseWriter, r *http.Request) { // getArtists - Returns artist data for a user func getArtists(w http.ResponseWriter, r *http.Request) { var uuid string + + dayRange := "" for k, v := range mux.Vars(r) { if k == "uuid" { uuid = v + } else if k == "days" { + dayRange = v } } - if uuid == "" { throwOkError(w, "Invalid UUID") return } - track, err := getTopArtists(uuid) + track, err := getTopArtists(uuid, dayRange) if err != nil { throwOkError(w, err.Error()) return @@ -616,37 +620,16 @@ func getArtists(w http.ResponseWriter, r *http.Request) { w.Write(json) } -// getAlbums - Returns album data for a user -func getAlbums(w http.ResponseWriter, r *http.Request) { - var uuid string - for k, v := range mux.Vars(r) { - if k == "uuid" { - uuid = v - } - } - - if uuid == "" { - throwOkError(w, "Invalid UUID") - return - } - - album, err := getAlbumByUUID(uuid) - if err != nil { - throwOkError(w, err.Error()) - return - } - - json, _ := json.Marshal(&album) - w.WriteHeader(http.StatusOK) - w.Write(json) -} - // getTracks - Returns track data for a user func getTracks(w http.ResponseWriter, r *http.Request) { var uuid string + + dayRange := "" for k, v := range mux.Vars(r) { if k == "uuid" { uuid = v + } else if k == "days" { + dayRange = v } } @@ -655,7 +638,7 @@ func getTracks(w http.ResponseWriter, r *http.Request) { return } - track, err := getTopTracks(uuid) + track, err := getTopTracks(uuid, dayRange) if err != nil { throwOkError(w, err.Error()) return @@ -838,7 +821,7 @@ func getServerInfo(w http.ResponseWriter, r *http.Request) { } info := ServerInfo{ - Version: "0.1.2", + Version: "0.1.3", RegistrationEnabled: registrationEnabled, } diff --git a/internal/goscrobble/track.go b/internal/goscrobble/track.go index 3e942673..b4527df3 100644 --- a/internal/goscrobble/track.go +++ b/internal/goscrobble/track.go @@ -184,19 +184,26 @@ func getTrackByUUID(uuid string) (Track, error) { return track, nil } -func getTopTracks(userUuid string) (TopTracks, error) { +func getTopTracks(userUuid string, dayRange string) (TopTracks, error) { var topTracks TopTracks + dateClause := "" + if dayRange != "" { + dateClause = " AND DATE(created_at) > SUBDATE(CURRENT_DATE, " + dayRange + ") " + } + rows, err := db.Query("SELECT BIN_TO_UUID(`tracks`.`uuid`, true), `tracks`.`name`, IFNULL(BIN_TO_UUID(`albums`.`uuid`, true),''), count(*) "+ "FROM `scrobbles` "+ "JOIN `tracks` ON `tracks`.`uuid` = `scrobbles`.`track` "+ "JOIN track_album ON track_album.track = tracks.uuid "+ "JOIN albums ON track_album.album = albums.uuid "+ "WHERE `user` = UUID_TO_BIN(?, true) "+ + dateClause+ "GROUP BY `scrobbles`.`track` "+ "ORDER BY count(*) DESC "+ "LIMIT 14", userUuid) + if err != nil { log.Printf("Failed to fetch top tracks: %+v", err) return topTracks, errors.New("Failed to fetch top tracks")