From 116d9ceebf1f39b092c0c38d9f01f797993550d6 Mon Sep 17 00:00:00 2001 From: glenneth Date: Sat, 27 Dec 2025 20:06:37 +0300 Subject: [PATCH] fix: Favorites star UI and track-id lookup - Fix find-track-by-title to parse 'Artist - Title' format from Icecast and search both artist and title columns in tracks table - Fix favorites API alist key mismatch (TRACK-TITLE not TRACK_TITLE) - Fix favorites cache to update UI after loading - Fix race condition where star reverted after clicking - Add aget-profile helper for Postmodern uppercase key lookup --- frontend-partials.lisp | 35 +++++++++++++++++++++++++--------- parenscript/front-page.lisp | 13 +++++++++---- parenscript/stream-player.lisp | 32 ++++++++++++++++++------------- user-profile.lisp | 18 ++++++++++------- 4 files changed, 65 insertions(+), 33 deletions(-) diff --git a/frontend-partials.lisp b/frontend-partials.lisp index d7ebdb1..a54c0db 100644 --- a/frontend-partials.lisp +++ b/frontend-partials.lisp @@ -1,18 +1,35 @@ (in-package :asteroid) (defun find-track-by-title (title) - "Find a track in the database by its title. Returns track ID or nil." + "Find a track in the database by its title. Returns track ID or nil. + Handles 'Artist - Title' format from Icecast metadata." (when (and title (not (string= title "Unknown"))) (handler-case (with-db - (let* ((search-pattern (format nil "%~a%" title)) - (result (postmodern:query - (:limit - (:select '_id - :from 'tracks - :where (:ilike 'title search-pattern)) - 1) - :single))) + ;; Parse 'Artist - Title' format if present + (let* ((parts (cl-ppcre:split " - " title :limit 2)) + (has-artist (> (length parts) 1)) + (artist-part (when has-artist (first parts))) + (title-part (if has-artist (second parts) title)) + (result + (if has-artist + ;; Search by both artist and title + (postmodern:query + (:limit + (:select '_id + :from 'tracks + :where (:and (:ilike 'artist (format nil "%~a%" artist-part)) + (:ilike 'title (format nil "%~a%" title-part)))) + 1) + :single) + ;; Fallback: search by title only + (postmodern:query + (:limit + (:select '_id + :from 'tracks + :where (:ilike 'title (format nil "%~a%" title-part))) + 1) + :single)))) result)) (error (e) (declare (ignore e)) diff --git a/parenscript/front-page.lisp b/parenscript/front-page.lisp index 3477a7e..487999d 100644 --- a/parenscript/front-page.lisp +++ b/parenscript/front-page.lisp @@ -183,7 +183,9 @@ (when (and data (ps:@ data data) (ps:@ data data favorites)) (setf *user-favorites-cache* (ps:chain (ps:@ data data favorites) - (map (lambda (f) (ps:@ f title)))))))) + (map (lambda (f) (ps:@ f title))))) + ;; Update UI after cache is loaded + (check-favorite-status)))) (catch (lambda (error) nil)))) ;; Check if current track is in favorites and update UI @@ -699,8 +701,9 @@ (= (ps:@ data data status) "success"))) (ps:chain btn class-list (remove "favorited")) (setf (ps:@ (ps:chain btn (query-selector ".star-icon")) text-content) "☆") - ;; Refresh now playing to update favorite count - (update-now-playing)))) + ;; Reload cache (don't call update-now-playing as it would + ;; check the old cache before reload completes) + (load-favorites-cache)))) (catch (lambda (error) (ps:chain console (error "Error removing favorite:" error))))) ;; Add favorite @@ -718,7 +721,9 @@ (= (ps:@ data data status) "success"))) (ps:chain btn class-list (add "favorited")) (setf (ps:@ (ps:chain btn (query-selector ".star-icon")) text-content) "★") - (update-now-playing)))) + ;; Reload cache (don't call update-now-playing as it would + ;; check the old cache before reload completes) + (load-favorites-cache)))) (catch (lambda (error) (ps:chain console (error "Error adding favorite:" error))))))))))) diff --git a/parenscript/stream-player.lisp b/parenscript/stream-player.lisp index cad3fb2..491daef 100644 --- a/parenscript/stream-player.lisp +++ b/parenscript/stream-player.lisp @@ -213,10 +213,15 @@ (ps:chain response (json)) nil))) (then (lambda (data) - (when (and data (ps:@ data data) (ps:@ data data favorites)) - (setf *user-favorites-cache-mini* - (ps:chain (ps:@ data data favorites) - (map (lambda (f) (ps:@ f title)))))))) + (when data + ;; Handle both wrapped (data.data.favorites) and unwrapped (data.favorites) responses + (let ((favorites (or (and (ps:@ data data) (ps:@ data data favorites)) + (ps:@ data favorites)))) + (when favorites + (setf *user-favorites-cache-mini* + (ps:chain favorites (map (lambda (f) (ps:@ f title))))) + ;; Update UI after cache is loaded + (check-favorite-status-mini)))))) (catch (lambda (error) nil)))) ;; Check if current track is in favorites and update mini player UI @@ -224,9 +229,10 @@ (let ((title-el (ps:chain document (get-element-by-id "mini-now-playing"))) (btn (ps:chain document (get-element-by-id "favorite-btn-mini")))) (when (and title-el btn) - (let ((title (ps:@ title-el text-content)) - (star-icon (ps:chain btn (query-selector ".star-icon")))) - (if (ps:chain *user-favorites-cache-mini* (includes title)) + (let* ((track-title (ps:@ title-el text-content)) + (star-icon (ps:chain btn (query-selector ".star-icon"))) + (is-in-cache (ps:chain *user-favorites-cache-mini* (includes track-title)))) + (if is-in-cache (progn (ps:chain btn class-list (add "favorited")) (when star-icon (setf (ps:@ star-icon text-content) "★"))) @@ -310,9 +316,9 @@ (= (ps:@ data data status) "success"))) (ps:chain btn class-list (remove "favorited")) (setf (ps:@ (ps:chain btn (query-selector ".star-icon")) text-content) "☆") - ;; Reload cache and refresh display to update favorite count - (load-favorites-cache-mini) - (update-mini-now-playing)))) + ;; Reload cache to update favorite count (don't call update-mini-now-playing + ;; as it would check the old cache before reload completes) + (load-favorites-cache-mini)))) (catch (lambda (error) (ps:chain console (error "Error removing favorite:" error))))) ;; Add favorite @@ -330,9 +336,9 @@ (= (ps:@ data data status) "success"))) (ps:chain btn class-list (add "favorited")) (setf (ps:@ (ps:chain btn (query-selector ".star-icon")) text-content) "★") - ;; Reload cache and refresh display to update favorite count - (load-favorites-cache-mini) - (update-mini-now-playing)))) + ;; Reload cache to update favorite count (don't call update-mini-now-playing + ;; as it would check the old cache before reload completes) + (load-favorites-cache-mini)))) (catch (lambda (error) (ps:chain console (error "Error adding favorite:" error))))))))))) diff --git a/user-profile.lisp b/user-profile.lisp index 38ef32d..9ceac54 100644 --- a/user-profile.lisp +++ b/user-profile.lisp @@ -161,6 +161,10 @@ ;;; API Endpoints for User Favorites ;;; ========================================================================== +(defun aget-profile (key alist) + "Get value from alist using string-equal comparison for key (Postmodern returns uppercase keys)" + (cdr (assoc key alist :test (lambda (a b) (string-equal (string a) (string b)))))) + (define-api asteroid/user/favorites () () "Get current user's favorite tracks" (require-authentication) @@ -168,13 +172,13 @@ (let* ((user-id (session:field "user-id")) (favorites (get-user-favorites user-id))) (api-output `(("status" . "success") - ("favorites" . ,(mapcar (lambda (fav) - `(("id" . ,(cdr (assoc :_id fav))) - ("track_id" . ,(cdr (assoc :track-id fav))) - ("title" . ,(or (cdr (assoc :track-title fav)) - (cdr (assoc :track_title fav)))) - ("rating" . ,(cdr (assoc :rating fav))))) - favorites)) + ("favorites" . ,(or (mapcar (lambda (fav) + `(("id" . ,(aget-profile "-ID" fav)) + ("track_id" . ,(aget-profile "TRACK-ID" fav)) + ("title" . ,(aget-profile "TRACK-TITLE" fav)) + ("rating" . ,(aget-profile "RATING" fav)))) + favorites) + (list))) ; Return empty list instead of null ("count" . ,(get-favorites-count user-id))))))) (define-api asteroid/user/favorites/add (&optional track-id rating title) ()