- Feed and profile cards show a progress bar with elapsed/total time, only when the source reports a real position (Plex, Jellyfin, the Android reader). It ticks locally between updates. Apple Music's guessed position is no longer reported. - The Android reader advances MediaSession's stale position to now, stamps when it was read (the server corrects for upload delay), and reports seeks. The server pushes an update to watchers when a position jumps. - When a polled source says where in the song someone is, the next poll is scheduled just after the song ends (never sooner than 5s), instead of waiting out the full interval. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MzFywujuH9TiuXbr9vjoX8
This commit is contained in:
@@ -94,6 +94,8 @@ export async function publishNowPlaying(userId: string) {
|
||||
if (dto) await redis.publish(nowPlayingChannel(userId), JSON.stringify(dto));
|
||||
}
|
||||
|
||||
const SEEK_THRESHOLD_MS = 5_000;
|
||||
|
||||
/** Apply one provider's poll result to the user's now-playing state. Returns whether they're playing. */
|
||||
export async function applyPollResult(
|
||||
userId: string,
|
||||
@@ -119,6 +121,13 @@ export async function applyPollResult(
|
||||
|
||||
const track = await upsertTrack(result.track);
|
||||
const changed = !current || current.trackId !== track.id || !current.isPlaying;
|
||||
// Same song, but not where it should be by now: a seek (or a restart). Tell
|
||||
// watchers, or their progress bars would drift.
|
||||
const jumped =
|
||||
!changed &&
|
||||
current.progressMs != null &&
|
||||
result.progressMs != null &&
|
||||
Math.abs(result.progressMs - (current.progressMs + (now.getTime() - current.updatedAt.getTime()))) > SEEK_THRESHOLD_MS;
|
||||
const newPlay =
|
||||
!current || current.trackId !== track.id || (!current.isPlaying && now.getTime() - current.updatedAt.getTime() > REPLAY_GAP_MS);
|
||||
const values = {
|
||||
@@ -137,6 +146,7 @@ export async function applyPollResult(
|
||||
.onConflictDoUpdate({ target: schema.nowPlaying.userId, set: values });
|
||||
|
||||
if (newPlay) await recordListen(userId, track.id, source, now);
|
||||
if (jumped) await publishNowPlaying(userId);
|
||||
if (changed) {
|
||||
await publishNowPlaying(userId);
|
||||
const [t] = await db
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
* which is what keeps third-party API usage proportional to real engagement.
|
||||
*/
|
||||
import { redis } from '../lib/redis';
|
||||
import type { PollResult } from '../providers/types';
|
||||
|
||||
const DUE_KEY = 'poll:due';
|
||||
const WATCHED_TTL_SECONDS = 90;
|
||||
@@ -31,6 +32,23 @@ export function ensureScheduled(connectionIds: string[]) {
|
||||
return redis.zadd(DUE_KEY, 'NX', ...connectionIds.flatMap((id) => [now, id]));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* When the source says exactly where in the song they are, check again just
|
||||
* after it ends instead of waiting out the full interval: the next song shows
|
||||
* up on time, and the progress bar doesn't sit at the end.
|
||||
*/
|
||||
export function nextPollDelay(intervalMs: number, result: PollResult | null): number {
|
||||
const duration = result?.track.durationMs;
|
||||
if (!result?.isPlaying || result.progressMs == null || !duration) return intervalMs;
|
||||
const remaining = duration - result.progressMs;
|
||||
if (remaining <= 0) return intervalMs;
|
||||
return Math.min(intervalMs, Math.max(remaining + TRACK_END_MARGIN_MS, MIN_POLL_GAP_MS));
|
||||
}
|
||||
|
||||
const TRACK_END_MARGIN_MS = 2_000; // let the player move on to the next song first
|
||||
const MIN_POLL_GAP_MS = 5_000;
|
||||
|
||||
export const unschedulePoll = (connectionId: string) => redis.zrem(DUE_KEY, connectionId);
|
||||
|
||||
const CLAIM_SCRIPT = `
|
||||
|
||||
Reference in New Issue
Block a user