Files
cinny/src/app/hooks/useNowPlaying.ts
T
jaredandClaude Opus 5.5 0cfca75f6f
CI / Build & Quality Checks (push) Skipped
CI / Secret scan (gitleaks) (push) Skipped
CI / Playwright smoke (e2e) (push) Skipped
feat(presence): "Listening to" from Navidrome / Dropped Needle (#491)
Discord/Spotify-style now playing, served by the new Lotus now-playing
service (matrix repo, now-playing/):

- Profile popout: a "Listening to" card (cover, track, artist, album,
  live progress); member list: a 🎵 line when the member has no typed
  status. Data comes from GET /now-playing/v1/users, batched across all
  visible users and polled every 20 s while the page is visible.
- Settings → Profile: "Share what I'm listening to", off by default,
  stored by the service (GET/PUT /v1/me). Auth is a Matrix OpenID token.
- Status text: while Lotus is open and sharing, the Matrix status shows
  "🎵 Artist – Track" so other clients see it, but only when the user has
  no typed status, and never in DND/Invisible. The typed status and its
  auto-clear are never touched. The music text uses a thin space after
  the note, and every device reads it as "no typed status"
  (pickHeartbeatStatus, Profile sync), so it can't be adopted as the
  user's status. Clearing or auto-clearing a status falls back to the
  music; closing the window drops it (pagehide sends the typed status).
- config.json: nowPlaying → https://matrix.lotusguild.org/now-playing.

Tests: nowPlaying.test.ts (format, marker, priority matrix, position,
config/cover URLs) and a heartbeat case in ownStatusSync.test.ts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PPmy3tPq869XDW4njjVaKA
2026-11-16 23:28:36 -05:00

80 lines
2.8 KiB
TypeScript

import { useCallback, useEffect, useMemo, useSyncExternalStore } from 'react';
import { useMatrixClient } from './useMatrixClient';
import { useClientConfig } from './useClientConfig';
import { NowPlayingEntry, NowPlayingMe, nowPlayingBaseUrl } from '../utils/nowPlaying';
import {
NowPlayingApi,
getNowPlayingServices,
getOwnNowPlaying,
setOwnNowPlaying,
subscribeOwnNowPlaying,
} from '../utils/nowPlayingClient';
import type { NowPlayingStore } from '../utils/nowPlayingStore';
/** [Gitea #491] The now-playing service for this homeserver, if config.json has one. */
export function useNowPlayingServices():
| { api: NowPlayingApi; store: NowPlayingStore }
| undefined {
const mx = useMatrixClient();
const { nowPlaying } = useClientConfig();
const base = nowPlayingBaseUrl(nowPlaying, mx.getDomain() ?? undefined);
return useMemo(() => (base ? getNowPlayingServices(mx, base) : undefined), [mx, base]);
}
const noop = () => () => undefined;
/** What `userId` is playing right now, if they share it. */
export function useNowPlaying(userId: string | undefined): NowPlayingEntry | undefined {
const services = useNowPlayingServices();
const store = services?.store;
const subscribe = useCallback(
(cb: () => void) => (store && userId ? store.subscribe(userId, cb) : noop()),
[store, userId],
);
const get = useCallback(() => (store && userId ? store.get(userId) : undefined), [store, userId]);
return useSyncExternalStore(subscribe, get);
}
/** The signed-in user's own sharing state (kept fresh by NowPlayingMonitor). */
export function useOwnNowPlaying(): NowPlayingMe | undefined {
return useSyncExternalStore(subscribeOwnNowPlaying, getOwnNowPlaying);
}
const OWN_POLL_MS = 20_000;
/**
* Always mounted (ClientNonUIFeatures): polls the user's own entry so their
* status text can follow the music, including while Lotus is in the background.
*/
export function useNowPlayingMonitor(): void {
const services = useNowPlayingServices();
useEffect(() => {
if (!services) {
setOwnNowPlaying(undefined);
return undefined;
}
let stopped = false;
let timer: ReturnType<typeof setTimeout> | undefined;
let delay = OWN_POLL_MS;
const tick = async () => {
try {
const me = await services.api.me();
if (stopped) return;
setOwnNowPlaying(me);
delay = OWN_POLL_MS;
} catch {
// Keep the last state for a while, then drop the music text so a dead
// service can't leave an old track in the status.
delay = Math.min(delay * 2, 5 * 60_000);
if (delay >= 4 * OWN_POLL_MS) setOwnNowPlaying(undefined);
}
if (!stopped) timer = setTimeout(tick, delay);
};
tick();
return () => {
stopped = true;
clearTimeout(timer);
};
}, [services]);
}