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:
@@ -19,7 +19,7 @@ import (
|
||||
// against it here never reach the database.
|
||||
func mountedRest() http.Handler {
|
||||
r := chi.NewRouter()
|
||||
Mount(r, nil, slog.New(slog.NewTextHandler(io.Discard, nil)), Config{}, nil, func() int { return 0 }, nil, nil, nil)
|
||||
Mount(r, nil, slog.New(slog.NewTextHandler(io.Discard, nil)), Config{}, nil, nil, func() int { return 0 }, nil, nil, nil)
|
||||
return r
|
||||
}
|
||||
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/eventbus"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playevents"
|
||||
)
|
||||
|
||||
@@ -322,3 +323,28 @@ func TestHandleGetStarred2_LeavesOutMissingAndQuarantinedSongs(t *testing.T) {
|
||||
t.Fatalf("the web's liked list has %d rows (err %v), want all 3", len(all), err)
|
||||
}
|
||||
}
|
||||
|
||||
// A star made in a Subsonic app is announced as the native like endpoint
|
||||
// announces one, so an open web tab or the Android app follows it.
|
||||
func TestHandleStar_TellsTheOtherApps(t *testing.T) {
|
||||
pool, user, track, _, _ := testStarPool(t)
|
||||
m := newStarHandlers(pool)
|
||||
m.bus = eventbus.New()
|
||||
events, cancel := m.bus.Subscribe(8)
|
||||
defer cancel()
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/rest/star?id="+uuidToID(track.ID), nil)
|
||||
w := httptest.NewRecorder()
|
||||
m.handleStar(w, req.WithContext(context.WithValue(req.Context(), userCtxKey, user)))
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
select {
|
||||
case e := <-events:
|
||||
if e.Kind != "track.liked" || e.UserID != user.ID.String() || e.Data["entity_id"] != track.ID.String() {
|
||||
t.Fatalf("event = %+v, want track.liked for the starred track", e)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("no event after a star")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/coverart"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/eventbus"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playevents"
|
||||
)
|
||||
|
||||
@@ -31,6 +32,9 @@ type mediaHandlers struct {
|
||||
// recall credits a scrobble to the daily mix the app last opened
|
||||
// (see mixRecall); nil credits nothing.
|
||||
recall *mixRecall
|
||||
// bus tells the web and Android apps about a star or unstar made here;
|
||||
// nil (tests) tells no one.
|
||||
bus *eventbus.Bus
|
||||
}
|
||||
|
||||
func newMediaHandlers(pool *pgxpool.Pool, events *playevents.Writer, logger *slog.Logger) *mediaHandlers {
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/auth"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/eventbus"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playevents"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/recommendation"
|
||||
@@ -34,11 +35,15 @@ import (
|
||||
//
|
||||
// startScan starts a library scan for an admin's startScan; nil leaves the
|
||||
// method answering that scanning is not available.
|
||||
func Mount(r chi.Router, pool *pgxpool.Pool, logger *slog.Logger, cfg Config, events *playevents.Writer, hops func() int, picker *recommendation.Picker, lists *playlists.Service, startScan StartScan) {
|
||||
//
|
||||
// bus carries a star or unstar made here to the web and Android apps, as
|
||||
// the native like endpoints do; nil tells no one.
|
||||
func Mount(r chi.Router, pool *pgxpool.Pool, logger *slog.Logger, cfg Config, events *playevents.Writer, bus *eventbus.Bus, hops func() int, picker *recommendation.Picker, lists *playlists.Service, startScan StartScan) {
|
||||
b := &browseHandlers{pool: pool}
|
||||
recall := newMixRecall()
|
||||
m := newMediaHandlers(pool, events, logger)
|
||||
m.recall = recall
|
||||
m.bus = bus
|
||||
u := &userHandlers{lookup: dbq.New(pool).GetUserByUsername, list: dbq.New(pool).ListUsers}
|
||||
sc := &scanHandlers{pool: pool, start: startScan}
|
||||
p := &pickHandlers{pool: pool, picker: picker, playlists: lists, logger: logger, recall: recall}
|
||||
|
||||
Reference in New Issue
Block a user