finish lastfm endpoint and fix clearing spotify now playing status

This commit is contained in:
2026-02-28 02:50:54 -08:00
parent 1af3efd7b4
commit 78712188d2
4 changed files with 157 additions and 61 deletions

View File

@@ -36,7 +36,7 @@ type NowPlaying struct {
UpdatedAt time.Time
}
var CurrentNowPlaying = make(map[int]NowPlaying)
var CurrentNowPlaying = make(map[int]map[string]NowPlaying)
func GenerateAPIKey() (string, error) {
bytes := make([]byte, 16)
@@ -80,6 +80,20 @@ func GetUserByAPIKey(apiKey string) (int, string, error) {
return userId, username, nil
}
func GetUserByUsername(username string) (int, error) {
if username == "" {
return 0, fmt.Errorf("empty username")
}
var userId int
err := db.Pool.QueryRow(context.Background(),
"SELECT pk FROM users WHERE username = $1", username).Scan(&userId)
if err != nil {
return 0, err
}
return userId, nil
}
func GetUserBySessionKey(sessionKey string) (int, string, error) {
if sessionKey == "" {
return 0, "", fmt.Errorf("empty session key")
@@ -167,18 +181,38 @@ func checkDuplicate(userId int, artist, songName string, timestamp time.Time) (b
}
func UpdateNowPlaying(np NowPlaying) {
CurrentNowPlaying[np.UserId] = np
if CurrentNowPlaying[np.UserId] == nil {
CurrentNowPlaying[np.UserId] = make(map[string]NowPlaying)
}
CurrentNowPlaying[np.UserId][np.Platform] = np
}
func GetNowPlaying(userId int) (NowPlaying, bool) {
np, ok := CurrentNowPlaying[userId]
return np, ok
platforms := CurrentNowPlaying[userId]
if platforms == nil {
return NowPlaying{}, false
}
np, ok := platforms["lastfm_api"]
if ok && np.SongName != "" {
return np, true
}
np, ok = platforms["spotify"]
if ok && np.SongName != "" {
return np, true
}
return NowPlaying{}, false
}
func ClearNowPlaying(userId int) {
delete(CurrentNowPlaying, userId)
}
func ClearNowPlayingPlatform(userId int, platform string) {
if CurrentNowPlaying[userId] != nil {
delete(CurrentNowPlaying[userId], platform)
}
}
func GetUserSpotifyCredentials(userId int) (clientId, clientSecret, accessToken, refreshToken string, expiresAt time.Time, err error) {
var clientIdPg, clientSecretPg, accessTokenPg, refreshTokenPg pgtype.Text
var expiresAtPg pgtype.Timestamptz