- Add check for registration_enabled on /register endpoint
- Made songlookup check artist name as well
This commit is contained in:
Daniel Mason 2021-04-03 14:16:13 +13:00
parent 9cbb94fc56
commit 8324894b0f
Signed by: idanoo
GPG key ID: 387387CDBC02F132
7 changed files with 47 additions and 14 deletions

View file

@ -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(&regReq)
@ -462,7 +477,7 @@ func fetchServerInfo(w http.ResponseWriter, r *http.Request) {
}
info := ServerInfo{
Version: "0.0.16",
Version: "0.0.17",
RegistrationEnabled: cachedRegistrationEnabled,
}

View file

@ -8,6 +8,8 @@ import (
var endTicker chan bool
func StartBackgroundWorkers() {
updateSpotifyData()
endTicker := make(chan bool)
hourTicker := time.NewTicker(time.Hour)

View file

@ -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)