fix(events): a like made in a Subsonic app reaches the other apps, and an open web tab follows likes and playlist changes (#8077)
The native like endpoints announce a like on the event bus; Subsonic star and unstar announced nothing, so the web and Android apps showed a stale heart until they refetched. Subsonic now publishes the same events, through one shape both surfaces build (eventbus.LikeChanged, eventbus.LedBy). The web listened only for the nightly rebuild and notifications, so an open tab ignored likes and playlist changes made in another tab, on Android or in a Subsonic app. It now refreshes its liked views on any like event and its playlists (and the open one) on playlist.created/updated/deleted/ tracks_changed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/eventbus"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playevents"
|
||||
)
|
||||
|
||||
@@ -49,18 +50,21 @@ func (m *mediaHandlers) handleStar(w http.ResponseWriter, r *http.Request) {
|
||||
if slices.Contains(liked, trackID) {
|
||||
_ = playevents.CaptureContextualLikeIfPlaying(r.Context(), q, user.ID, trackID, m.logger)
|
||||
}
|
||||
m.publishLike(user.ID, "track", true, eventbus.LedBy(trackID, liked)...)
|
||||
}
|
||||
if albumID.Valid {
|
||||
if err := q.LikeAlbum(r.Context(), dbq.LikeAlbumParams{UserID: user.ID, AlbumID: albumID}); err != nil {
|
||||
WriteFail(w, r, ErrGeneric, "Could not star album")
|
||||
return
|
||||
}
|
||||
m.publishLike(user.ID, "album", true, albumID)
|
||||
}
|
||||
if artistID.Valid {
|
||||
if err := q.LikeArtist(r.Context(), dbq.LikeArtistParams{UserID: user.ID, ArtistID: artistID}); err != nil {
|
||||
WriteFail(w, r, ErrGeneric, "Could not star artist")
|
||||
return
|
||||
}
|
||||
m.publishLike(user.ID, "artist", true, artistID)
|
||||
}
|
||||
Write(w, r, struct{ Envelope }{Envelope: NewEnvelope("ok")})
|
||||
}
|
||||
@@ -76,14 +80,21 @@ func (m *mediaHandlers) handleUnstar(w http.ResponseWriter, r *http.Request) {
|
||||
params := r.URL.Query()
|
||||
q := dbq.New(m.pool)
|
||||
if t, ok := parseUUID(params.Get("id")); ok {
|
||||
_, _ = q.UnlikeTrack(r.Context(), dbq.UnlikeTrackParams{UserID: user.ID, TrackID: t})
|
||||
unliked, err := q.UnlikeTrack(r.Context(), dbq.UnlikeTrackParams{UserID: user.ID, TrackID: t})
|
||||
_ = playevents.SoftDeleteContextualLikes(r.Context(), q, user.ID, t)
|
||||
if err == nil {
|
||||
m.publishLike(user.ID, "track", false, eventbus.LedBy(t, unliked)...)
|
||||
}
|
||||
}
|
||||
if a, ok := parseUUID(params.Get("albumId")); ok {
|
||||
_ = q.UnlikeAlbum(r.Context(), dbq.UnlikeAlbumParams{UserID: user.ID, AlbumID: a})
|
||||
if q.UnlikeAlbum(r.Context(), dbq.UnlikeAlbumParams{UserID: user.ID, AlbumID: a}) == nil {
|
||||
m.publishLike(user.ID, "album", false, a)
|
||||
}
|
||||
}
|
||||
if a, ok := parseUUID(params.Get("artistId")); ok {
|
||||
_ = q.UnlikeArtist(r.Context(), dbq.UnlikeArtistParams{UserID: user.ID, ArtistID: a})
|
||||
if q.UnlikeArtist(r.Context(), dbq.UnlikeArtistParams{UserID: user.ID, ArtistID: a}) == nil {
|
||||
m.publishLike(user.ID, "artist", false, a)
|
||||
}
|
||||
}
|
||||
Write(w, r, struct{ Envelope }{Envelope: NewEnvelope("ok")})
|
||||
}
|
||||
@@ -257,3 +268,15 @@ func (m *mediaHandlers) handleSetRating(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
Write(w, r, NewEnvelope("ok"))
|
||||
}
|
||||
|
||||
// publishLike tells the user's web and Android apps that a star changed
|
||||
// here, with the same events the native like endpoints send, so an open
|
||||
// tab's hearts and Liked list follow a like made in a Subsonic app.
|
||||
func (m *mediaHandlers) publishLike(userID pgtype.UUID, entityType string, liked bool, ids ...pgtype.UUID) {
|
||||
if m.bus == nil {
|
||||
return
|
||||
}
|
||||
for _, id := range ids {
|
||||
m.bus.Publish(eventbus.LikeChanged(userID.String(), entityType, id.String(), liked))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user