From 8324894b0f1e66a2a52b2752c0f695d37f220360 Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Sat, 3 Apr 2021 14:16:13 +1300 Subject: [PATCH] 0.0.17 - Add check for registration_enabled on /register endpoint - Made songlookup check artist name as well --- .gitlab-ci.yml | 2 +- docs/changelog.md | 4 ++++ internal/goscrobble/server.go | 17 ++++++++++++++++- internal/goscrobble/timers.go | 2 ++ internal/goscrobble/track.go | 25 +++++++++++++++++++++++-- web/src/Api/index.js | 9 --------- web/src/Pages/User.js | 2 +- 7 files changed, 47 insertions(+), 14 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index c6537639..8c76eab8 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -3,7 +3,7 @@ stages: - bundle variables: - VERSION: 0.0.16 + VERSION: 0.0.17 build-go: image: golang:1.16.2 diff --git a/docs/changelog.md b/docs/changelog.md index 338106b5..a6cc9de1 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,3 +1,7 @@ +# 0.0.17 +- Add check for registration_enabled on /register endpoint +- Made songlookup check artist name as well + # 0.0.16 - Add registration_enabled to /api/v1/serverinfo - Add config table caching on save diff --git a/internal/goscrobble/server.go b/internal/goscrobble/server.go index 0a648387..6fabb963 100644 --- a/internal/goscrobble/server.go +++ b/internal/goscrobble/server.go @@ -86,6 +86,21 @@ func HandleRequests(port string) { // API ENDPOINT HANDLING // handleRegister - Does as it says! func handleRegister(w http.ResponseWriter, r *http.Request) { + cachedRegistrationEnabled := getRedisVal("REGISTRATION_ENABLED") + if cachedRegistrationEnabled == "" { + registrationEnabled, err := getConfigValue("REGISTRATION_ENABLED") + if err != nil { + throwOkError(w, "Error checking if registration is enabled") + } + setRedisVal("REGISTRATION_ENABLED", registrationEnabled) + cachedRegistrationEnabled = registrationEnabled + } + + if cachedRegistrationEnabled == "0" { + throwOkError(w, "Registration is currently disabled") + return + } + regReq := RegisterRequest{} decoder := json.NewDecoder(r.Body) err := decoder.Decode(®Req) @@ -462,7 +477,7 @@ func fetchServerInfo(w http.ResponseWriter, r *http.Request) { } info := ServerInfo{ - Version: "0.0.16", + Version: "0.0.17", RegistrationEnabled: cachedRegistrationEnabled, } diff --git a/internal/goscrobble/timers.go b/internal/goscrobble/timers.go index dd242776..52b67b70 100644 --- a/internal/goscrobble/timers.go +++ b/internal/goscrobble/timers.go @@ -8,6 +8,8 @@ import ( var endTicker chan bool func StartBackgroundWorkers() { + updateSpotifyData() + endTicker := make(chan bool) hourTicker := time.NewTicker(time.Hour) diff --git a/internal/goscrobble/track.go b/internal/goscrobble/track.go index a64809cd..1f3566b7 100644 --- a/internal/goscrobble/track.go +++ b/internal/goscrobble/track.go @@ -4,6 +4,7 @@ import ( "database/sql" "errors" "log" + "strings" ) type Track struct { @@ -29,7 +30,8 @@ func insertTrack(name string, legnth int, mbid string, spotifyId string, album s // If it didn't match above, lookup by name if track.Uuid == "" { - track = fetchTrack("name", name, tx) + // TODO: add artist check here too + track = fetchTrackWithArtists(name, artists, tx) } // If we can't find it. Lets add it! @@ -47,7 +49,7 @@ func insertTrack(name string, legnth int, mbid string, spotifyId string, album s } if track.Uuid == "" { - track = fetchTrack("name", name, tx) + track = fetchTrackWithArtists(name, artists, tx) } err = track.linkTrack(album, artists, tx) @@ -78,6 +80,25 @@ func fetchTrack(col string, val string, tx *sql.Tx) Track { return track } +func fetchTrackWithArtists(name string, artists []string, tx *sql.Tx) Track { + var track Track + artistString := strings.Join(artists, "','") + + err := tx.QueryRow( + "SELECT BIN_TO_UUID(`uuid`, true), `name`, `desc`, `img`, `mbid` FROM `tracks` "+ + "LEFT JOIN `track_artist` ON `tracks`.`uuid` = `track_artist`.`track` "+ + "WHERE `name` = ? AND BIN_TO_UUID(`track_artist`.`artist`, true) IN ('`"+artistString+"`')", + name).Scan(&track.Uuid, &track.Name, &track.Desc, &track.Img, &track.MusicBrainzID) + + if err != nil { + if err != sql.ErrNoRows { + log.Printf("Error fetching tracks: %+v", err) + } + } + + return track +} + func insertNewTrack(name string, length int, mbid string, spotifyId string, tx *sql.Tx) error { _, err := tx.Exec("INSERT INTO `tracks` (`uuid`, `name`, `length`, `mbid`, `spotify_id`) "+ "VALUES (UUID_TO_BIN(UUID(), true),?,?,?,?)", name, length, mbid, spotifyId) diff --git a/web/src/Api/index.js b/web/src/Api/index.js index 0e0a53e3..e2cd6455 100644 --- a/web/src/Api/index.js +++ b/web/src/Api/index.js @@ -252,12 +252,3 @@ export const getServerInfo = () => { return handleErrorResp(error) }); } - -export const resetScrobbleToken = () => { - return axios.patch(process.env.REACT_APP_API_URL + "user", { token: "" }, { headers: getHeaders() }) - .then((data) => { - return data.data - }).catch((error) => { - return handleErrorResp(error) - }); -} \ No newline at end of file diff --git a/web/src/Pages/User.js b/web/src/Pages/User.js index 312030c8..c6091f63 100644 --- a/web/src/Pages/User.js +++ b/web/src/Pages/User.js @@ -56,7 +56,7 @@ const User = () => { const resetToken = () => { setLoading(true); - resetScrobbleToken(user.uuid) + patchUser({ token: '' }) .then(() => { getUser() .then(data => {