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:
2026-11-27 00:02:12 -05:00
co-authored by Claude Opus 5.5
parent 528ea9df0c
commit 8ba1d109c3
11 changed files with 204 additions and 51 deletions
+47
View File
@@ -0,0 +1,47 @@
import { afterEach, expect, test, vi } from 'vitest';
import { flushSync } from 'svelte';
vi.mock('#lib/auth/store.svelte.js', () => ({ user: { value: { id: 'u1' } } }));
vi.mock('#lib/query/client.js', () => ({ queryClient: { invalidateQueries: vi.fn() } }));
import { useServerEvents } from './serverEvents.svelte.js';
import { queryClient } from '#lib/query/client.js';
import { qk } from '#lib/api/queries.js';
// Stands in for the browser's EventSource: events are dispatched by hand.
class FakeEventSource extends EventTarget {
static last: FakeEventSource;
constructor(public url: string) {
super();
FakeEventSource.last = this;
}
close(): void {}
}
function frame(kind: string, data: Record<string, unknown>): MessageEvent {
return new MessageEvent(kind, { data: JSON.stringify({ kind, data }) });
}
afterEach(() => {
vi.unstubAllGlobals();
vi.clearAllMocks();
});
// A like or a playlist change made elsewhere (another tab, the Android app,
// a Subsonic app) refreshes what an open tab shows.
test('like and playlist events refresh the views that show them', () => {
vi.stubGlobal('EventSource', FakeEventSource);
const stop = $effect.root(() => useServerEvents());
flushSync();
FakeEventSource.last.dispatchEvent(frame('track.liked', { entity_type: 'track', entity_id: 't1' }));
expect(queryClient.invalidateQueries).toHaveBeenCalledWith({ queryKey: qk.likedIds() });
expect(queryClient.invalidateQueries).toHaveBeenCalledWith({ queryKey: qk.likedTracks() });
vi.mocked(queryClient.invalidateQueries).mockClear();
FakeEventSource.last.dispatchEvent(frame('playlist.tracks_changed', { playlist_id: 'p1' }));
expect(queryClient.invalidateQueries).toHaveBeenCalledWith({ queryKey: ['playlists'] });
expect(queryClient.invalidateQueries).toHaveBeenCalledWith({ queryKey: qk.playlist('p1') });
stop();
});
+56 -5
View File
@@ -7,16 +7,18 @@ import { user } from '#lib/auth/store.svelte.js';
// It reacts to `playlist.system_rebuilt` — the
// daily 03:00 rebuild or a manual refresh — by invalidating the home +
// system-playlist query caches so a tab left open across the rebuild stops
// serving yesterday's snapshot. Active playback
// serving yesterday's snapshot. It also follows likes and playlist changes
// made anywhere else (another tab, the Android app, a Subsonic app), so an
// open tab's hearts, Liked lists and playlists don't wait for a navigation. Active playback
// self-heals separately on the failure path (store.handleLoadFailure); we
// deliberately do NOT yank a playing queue here — that would interrupt a
// mid-song listen to restart the new mix at track 0.
// Monotonic rebuild counter. Bumped on every playlist.system_rebuilt.
// An open system-playlist DETAIL page can't be invalidated in place (its id
// rotates on rebuild — a refetch would 404), so instead it watches this
// counter and offers a "this mix was refreshed → Refresh" affordance that
// re-resolves the variant to the new playlist.
// An open system-playlist DETAIL page watches this counter and offers a
// "this mix was refreshed → Refresh" affordance that re-resolves the
// variant, rather than swapping the songs under someone reading the list
// (and a mix the rebuild retired has no id left to refetch).
let _systemRebuildCount = $state(0);
export const systemRebuilt = {
get count(): number {
@@ -32,6 +34,51 @@ function onSystemRebuilt(): void {
queryClient.invalidateQueries({ queryKey: qk.systemPlaylistsStatus() });
}
// The events a like or unlike sends, for a track, album or artist.
const LIKE_EVENTS = [
'track.liked',
'track.unliked',
'album.liked',
'album.unliked',
'artist.liked',
'artist.unliked'
] as const;
// A like changed somewhere. Every liked view refetches: a track like changes
// every copy of the song, so patching one id in place would miss the rest.
function onLikeChanged(): void {
for (const queryKey of [qk.likedIds(), qk.likedTracks(), qk.likedAlbums(), qk.likedArtists()]) {
queryClient.invalidateQueries({ queryKey });
}
}
// The events a change to a person's own playlist sends.
const PLAYLIST_EVENTS = [
'playlist.created',
'playlist.updated',
'playlist.deleted',
'playlist.tracks_changed'
] as const;
// playlistIdOf reads data.playlist_id from an event frame, or '' when the
// frame carries none or isn't JSON.
export function playlistIdOf(e: Event): string {
try {
const id = JSON.parse((e as MessageEvent<string>).data)?.data?.playlist_id;
return typeof id === 'string' ? id : '';
} catch {
return '';
}
}
// A playlist changed somewhere: the lists refetch, and so does the one
// playlist if it's open or cached.
function onPlaylistChanged(e: Event): void {
queryClient.invalidateQueries({ queryKey: ['playlists'] });
const id = playlistIdOf(e);
if (id) queryClient.invalidateQueries({ queryKey: qk.playlist(id) });
}
// A new notification for this user. The frame carries no content — it
// is a nudge — so the inbox and badge refetch rather than trusting a payload.
function onNotificationCreated(): void {
@@ -49,9 +96,13 @@ export function useServerEvents(): void {
const es = new EventSource('/api/events/stream');
es.addEventListener('playlist.system_rebuilt', onSystemRebuilt);
es.addEventListener('notification.created', onNotificationCreated);
for (const kind of LIKE_EVENTS) es.addEventListener(kind, onLikeChanged);
for (const kind of PLAYLIST_EVENTS) es.addEventListener(kind, onPlaylistChanged);
return () => {
es.removeEventListener('playlist.system_rebuilt', onSystemRebuilt);
es.removeEventListener('notification.created', onNotificationCreated);
for (const kind of LIKE_EVENTS) es.removeEventListener(kind, onLikeChanged);
for (const kind of PLAYLIST_EVENTS) es.removeEventListener(kind, onPlaylistChanged);
es.close();
};
});