Compare commits

..
53 changed files with 1908 additions and 2331 deletions

No files matched your search

+2 -4
View File
@@ -4,9 +4,7 @@ This integration uses Expo 58 inline modules and `expo-app-intents`. Rebuild the
Playback and library changes open oTunes because they need its authenticated React runtime. Swift dispatches a request and waits for an app-owned completion acknowledgment. Playback waits for the native audio player to load the requested track; warmup waits for it to load while paused. Errors reach Siri, and requests that have expired are discarded before starting a side effect. Native catalog reads do not open the app.
The full entity catalog lives in Application Support. Expo's UserDefaults catalog holds at most 100 suggestions per kind, with favorites first, and mirrors those suggestions into Spotlight. Siri's native queries search the full disk catalog.
Sync is incremental. The app subscribes to `intentCatalog.head`, the newest insert, rename or deletion per kind, and fetches a kind only when it moves past the cursor stored in AsyncStorage. `intentCatalog.page` then returns records created after the cursor plus renames and deletions from the `intentCatalogChanges` log. The log keeps 30 days; older cursors, or more than 2,000 changes, rebuild that kind from scratch. Favorites are stamped onto records on device from the favorites subscription, so liking a song costs no catalog queries. Playlists come from the live `intentCatalog.playlists` query. Code lives in `components/app-intents/`: `catalog-sync.ts`, `now-playing.ts`, and `dispatch.ts`.
The full entity catalog lives in Application Support. The backend supplies pages of 200 records. Expo's UserDefaults catalog holds at most 100 suggestions per kind, with favorites first, and mirrors those suggestions into Spotlight. Siri's native queries search the full disk catalog. Catalogs refresh on launch, foreground entry, favorite changes, and every five minutes while foregrounded.
Songs, albums, artists, and playlists have Spotlight open intents and on-screen entity associations. Associations require iOS 18.4 and Xcode 27; Spotlight indexing requires iOS 18. Playback donations are made after the player starts, preserve shuffle, and resolve real catalog entities. Removed records trigger donation cleanup. Sign-out clears catalogs, Spotlight entries, donations, and pending invocations. On iOS 27, the query types also handle system requests to rebuild Spotlight indexes.
@@ -16,7 +14,7 @@ Audio schema intents require Xcode 27 and iOS 27. Custom intents remain availabl
Deploy the companion Convex changes before installing the new mobile build:
- `intentCatalog.head`, `intentCatalog.page` and `intentCatalog.playlists` supply the entity catalog. Library mutations record renames and deletions in `intentCatalogChanges`, pruned daily.
- `library.intentCatalogPage` supplies authenticated, paginated entity records.
- `appIntentPlaylists` records playlist-creation request IDs in the same transaction as creation. Replaying a request returns the original playlist ID.
- `playlists.createPlaylist` accepts an optional `invocationId`; existing callers can omit it.
@@ -89,6 +89,20 @@ export default function AlbumScreen() {
[album]
)
// Older servers do not include album format availability yet.
const formatTracks = useCachedQuery(
api.library.getSongsForPlayback,
album && (album.hasLossless === undefined || album.hasAtmos === undefined)
? { ids: songIds }
: "skip"
)
const hasLossless =
album?.hasLossless ??
formatTracks?.some((track) => track && "lossless" in track.files)
const hasAtmos =
album?.hasAtmos ??
formatTracks?.some((track) => track && "atmos" in track.files)
useEffect(() => {
if (album) {
rememberSongs(albumDetailSongs(album))
@@ -97,12 +111,6 @@ export default function AlbumScreen() {
const listItems = useMemo(() => albumListItems(album?.tracks ?? []), [album])
const playTrack = useCallback(
(songId: Id<"songs">, ids: Id<"songs">[]) =>
playFromSong(songId, ids, `album:${id}`),
[id, playFromSong]
)
const renderItem = useCallback(
({ item }: { item: AlbumListItem }) => {
if (item.type === "disc") {
@@ -117,7 +125,9 @@ export default function AlbumScreen() {
songIds={songIds}
palette={palette}
favorited={isSongFavorite(item.track.id)}
onPlay={playTrack}
onPlay={(songId, ids) =>
playFromSong(songId, ids, `album:${album.id}`)
}
onAction={handleAlbumTrackAction}
/>
) : null
@@ -127,7 +137,7 @@ export default function AlbumScreen() {
handleAlbumTrackAction,
isSongFavorite,
palette,
playTrack,
playFromSong,
songIds,
swipeGroup,
]
@@ -364,7 +374,7 @@ export default function AlbumScreen() {
{metadata}
</PaletteText>
) : null}
{album.hasLossless ? (
{hasLossless ? (
<>
{metadata ? (
<PaletteText
@@ -382,9 +392,9 @@ export default function AlbumScreen() {
/>
</>
) : null}
{album.hasAtmos ? (
{hasAtmos ? (
<>
{metadata || album.hasLossless ? (
{metadata || hasLossless ? (
<PaletteText
tone="secondaryForeground"
style={{ color: palette.secondaryForeground }}
@@ -1,4 +1,4 @@
import { atmosEnabled, dolbyAtmosSupported } from "@/lib/playback-capabilities"
import { dolbyAtmosSupported } from "@/lib/playback-capabilities"
import { Ionicons } from "@expo/vector-icons"
import { useAction, useMutation, useQuery } from "convex/react"
import { Image } from "expo-image"
@@ -30,7 +30,7 @@ import { useProfileImageUrl } from "@/hooks/use-profile-image-url"
import { changeServer, signOut } from "@/lib/session"
import { radius, spacing, useThemeColors } from "@/lib/theme"
import { showError } from "@/lib/toast"
import type { PlaybackQuality } from "@otunes/convex/playback-stream"
import type { PlaybackQuality } from "@/lib/urls"
import { api } from "@otunes/convex/_generated/api"
import type { Id } from "@otunes/convex/_generated/dataModel"
@@ -418,7 +418,7 @@ function QualitySection({ user }: { user: CurrentUser }) {
: "This device does not support Dolby Atmos playback."
}
disabled={!dolbyAtmosSupported}
value={atmosEnabled(user)}
value={dolbyAtmosSupported && (user.preferAtmos ?? false)}
onValueChange={(checked) =>
void updateAtmos({ preferAtmos: checked }).catch((error: Error) =>
showError(error.message)
+1 -1
View File
@@ -73,10 +73,10 @@ export default function AppLayout() {
return (
<ConvexAuthRecovery onInvalidate={invalidateSession}>
<PlaybackProvider>
<AppIntentBridge />
<AppNavigationSync />
<AddToPlaylistProvider>
<FavoritesProvider>
<AppIntentBridge />
<Stack screenOptions={stackOptions}>
<Stack.Screen
name="(tabs)"
+142 -119
View File
@@ -1,12 +1,12 @@
import { useTabletLayout } from "@/hooks/use-tablet-layout"
import { TabletNowPlaying } from "@/components/tablet-now-playing"
import { atmosEnabled } from "@/lib/playback-capabilities"
import { dolbyAtmosSupported } from "@/lib/playback-capabilities"
import { NavigationBar } from "expo-navigation-bar"
import { MusicEntityView } from "@/components/music-entity-view"
import { Stack, useRouter, useLocalSearchParams } from "expo-router"
import * as Haptics from "expo-haptics"
import { StatusBar } from "expo-status-bar"
import { useCallback, useEffect, useMemo, useRef, useState } from "react"
import { useEffect, useRef, useState } from "react"
import { Gesture, GestureDetector } from "react-native-gesture-handler"
import {
Platform,
@@ -45,16 +45,16 @@ import {
QUEUE_HORIZONTAL_INSET,
} from "@/components/now-playing-queue"
import { PlayerArtistMenu } from "@/components/player-artist-menu"
import { PlayerProgress } from "@/components/player-progress"
import { PlayerSlider } from "@/components/player-slider"
import { PlayerSymbol } from "@/components/player-symbol"
import { useFavorites } from "@/contexts/favorites"
import { usePlaybackActions, usePlaybackQueue } from "@/contexts/playback"
import { usePlayback } from "@/contexts/playback"
import { useServerConfig } from "@/contexts/server-config"
import { useCachedQuery } from "@/hooks/use-cached-query"
import { useCoverPalette } from "@/hooks/use-cover-palette"
import { useMediaMenuActions } from "@/hooks/use-media-menu-actions"
import { useSystemVolume } from "@/hooks/use-system-volume"
import { formatDuration, formatRemainingDuration } from "@/lib/format"
import {
nowPlayingColors,
type NowPlayingColors,
@@ -90,13 +90,20 @@ export default function PlayerScreen() {
isPlaying,
shuffleEnabled,
repeatEnabled,
currentTime,
duration,
queueSongIds,
} = usePlaybackQueue()
const { setIsPlaying, skipNext, skipPrevious } = usePlaybackActions()
setIsPlaying,
seekTo,
skipNext,
skipPrevious,
} = usePlayback()
const { volume, setVolume, available: volumeAvailable } = useSystemVolume()
const { queue } = useLocalSearchParams<{ queue?: string }>()
const [showQueue, setShowQueue] = useState(queue === "1")
const [isDismissing, setIsDismissing] = useState(false)
const [stageHeight, setStageHeight] = useState(0)
const [scrubTime, setScrubTime] = useState<number | null>(null)
const user = useCachedQuery(api.auth.currentUser)
const playbackInfo = useCachedQuery(
api.library.getSongPlaybackInfo,
@@ -107,7 +114,7 @@ export default function PlayerScreen() {
? playbackFormat(
playbackInfo.files,
user.playbackQuality,
atmosEnabled(user)
dolbyAtmosSupported && (user.preferAtmos ?? false)
)
: null
@@ -119,7 +126,7 @@ export default function PlayerScreen() {
coverUri,
currentSong ? currentSong.album.cover != null : undefined
)
const colors = useMemo(() => nowPlayingColors(palette), [palette])
const colors = nowPlayingColors(palette)
const background = useSharedValue(colors.background)
useEffect(() => {
@@ -152,65 +159,33 @@ export default function PlayerScreen() {
}))
// The player covers the status bar, so it can't use the native sheet's
// swipe to dismiss. Each GestureDetector needs its own gesture instance.
const makeDismissGesture = useCallback(
() =>
Gesture.Pan()
.activeOffsetY(12)
.failOffsetY(-12)
.failOffsetX([-20, 20])
.onUpdate((event) => {
dismissY.value = Math.max(0, event.translationY)
})
.onEnd((event) => {
if (
event.translationY > DISMISS_DISTANCE ||
event.velocityY > DISMISS_VELOCITY
) {
scheduleOnRN(setIsDismissing, true)
dismissY.value = withTiming(
windowHeight,
{ duration: 220 },
(finished) => {
if (finished) scheduleOnRN(router.back)
}
)
} else {
dismissY.value = withSpring(0, {
duration: 350,
dampingRatio: 0.85,
})
}
}),
[dismissY, router, windowHeight]
)
const handleGesture = useMemo(makeDismissGesture, [makeDismissGesture])
const controlsGesture = useMemo(makeDismissGesture, [makeDismissGesture])
const stageGesture = useMemo(
() => makeDismissGesture().enabled(!showQueue),
[makeDismissGesture, showQueue]
)
const hasCover = currentSong ? currentSong.album.cover != null : false
const queueHeader = useMemo(
() =>
currentSong ? (
<View style={styles.queueHeader}>
<CoverImage
uri={coverUri}
hasCover={hasCover}
style={styles.queueArtwork}
/>
<SongMeta song={currentSong} colors={colors} compact />
</View>
) : null,
[colors, coverUri, currentSong, hasCover]
)
const songMeta = useMemo(
() =>
currentSong ? <SongMeta song={currentSong} colors={colors} /> : null,
[colors, currentSong]
)
// swipe to dismiss.
function dismissGesture() {
return Gesture.Pan()
.activeOffsetY(12)
.failOffsetY(-12)
.failOffsetX([-20, 20])
.onUpdate((event) => {
dismissY.value = Math.max(0, event.translationY)
})
.onEnd((event) => {
if (
event.translationY > DISMISS_DISTANCE ||
event.velocityY > DISMISS_VELOCITY
) {
scheduleOnRN(setIsDismissing, true)
dismissY.value = withTiming(
windowHeight,
{ duration: 220 },
(finished) => {
if (finished) scheduleOnRN(router.back)
}
)
} else {
dismissY.value = withSpring(0, { duration: 350, dampingRatio: 0.85 })
}
})
}
const hasQueue = queueSongIds.length > 0
@@ -229,6 +204,7 @@ export default function PlayerScreen() {
return null
}
const hasCover = currentSong.album.cover != null
const bottomInset = Math.max(insets.bottom, spacing.md)
const artworkSize = Math.max(
0,
@@ -237,6 +213,9 @@ export default function PlayerScreen() {
stageHeight > 0 ? stageHeight - META_HEIGHT - spacing.xl : Infinity
)
)
const displayedTime = scrubTime ?? currentTime
const timeColor =
scrubTime === null ? colors.secondaryForeground : colors.foreground
function togglePlayback() {
void Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light)
@@ -255,10 +234,10 @@ export default function PlayerScreen() {
colors={colors}
backgroundStyle={backgroundStyle}
dismissStyle={dismissStyle}
dismissGestures={{ handle: handleGesture, artwork: controlsGesture }}
dismissGesture={dismissGesture}
showQueue={showQueue}
toggleQueue={toggleQueue}
metadata={songMeta}
metadata={<SongMeta song={currentSong} colors={colors} />}
/>
</MusicEntityView>
)
@@ -285,7 +264,7 @@ export default function PlayerScreen() {
id={currentSong.id}
style={[styles.safe, { paddingTop: insets.top }]}
>
<GestureDetector gesture={handleGesture}>
<GestureDetector gesture={dismissGesture()}>
<Pressable
onPress={() => router.back()}
hitSlop={12}
@@ -299,7 +278,7 @@ export default function PlayerScreen() {
</Pressable>
</GestureDetector>
<GestureDetector gesture={stageGesture}>
<GestureDetector gesture={dismissGesture().enabled(!showQueue)}>
<View
style={styles.stage}
onLayout={(event) =>
@@ -318,7 +297,20 @@ export default function PlayerScreen() {
<NowPlayingQueue
colors={colors}
viewportHeight={stageHeight}
currentHeader={queueHeader}
currentHeader={
<View style={styles.queueHeader}>
<CoverImage
uri={coverUri}
hasCover={hasCover}
style={styles.queueArtwork}
/>
<SongMeta
song={currentSong}
colors={colors}
compact
/>
</View>
}
/>
</Animated.View>
) : null
@@ -342,28 +334,50 @@ export default function PlayerScreen() {
/>
</Animated.View>
</View>
<View style={styles.meta}>{songMeta}</View>
<View style={styles.meta}>
<SongMeta song={currentSong} colors={colors} />
</View>
</Animated.View>
)}
</LayoutAnimationConfig>
</View>
</GestureDetector>
<GestureDetector gesture={controlsGesture}>
<GestureDetector gesture={dismissGesture()}>
<View style={[styles.controls, { paddingBottom: bottomInset }]}>
<PlayerProgress
colors={colors}
timeStyle={styles.time}
center={
format ? (
<PlayerSlider
accessibilityLabel="Playback position"
value={displayedTime}
maximumValue={duration}
fillColor={colors.sliderFill}
activeFillColor={colors.foreground}
trackColor={colors.sliderTrack}
onValueChange={setScrubTime}
onSlidingComplete={(value) => {
void seekTo(value)
setScrubTime(null)
}}
/>
<View style={styles.timeRow}>
<Text style={[styles.time, { color: timeColor }]}>
{formatDuration(displayedTime)}
</Text>
{format ? (
<View style={styles.format} pointerEvents="none">
<AudioFormatBadge
format={format}
color={colors.secondaryForeground}
backgroundColor={colors.background}
/>
) : null
}
/>
</View>
) : null}
<Text style={[styles.time, { color: timeColor }]}>
{formatRemainingDuration(displayedTime, duration).replace(
"-",
"\u2212"
)}
</Text>
</View>
<View style={styles.transport}>
<TransportButton
@@ -405,7 +419,35 @@ export default function PlayerScreen() {
</TransportButton>
</View>
<VolumeSlider colors={colors} />
{volumeAvailable ? (
<PlayerSlider
accessibilityLabel="Volume"
commitHoldMs={0}
animateValueChanges
value={volume}
maximumValue={1}
fillColor={colors.sliderFill}
activeFillColor={colors.foreground}
trackColor={colors.sliderTrack}
onValueChange={setVolume}
leading={
<PlayerSymbol
name="speaker.fill"
fallback="volume-low"
size={14}
color={colors.secondaryForeground}
/>
}
trailing={
<PlayerSymbol
name="speaker.wave.3.fill"
fallback="volume-high"
size={14}
color={colors.secondaryForeground}
/>
}
/>
) : null}
<View style={styles.footer}>
<Pressable
@@ -475,41 +517,6 @@ export default function PlayerScreen() {
)
}
/** Phone volume slider. Owns the volume listener so volume changes stay local. */
function VolumeSlider({ colors }: { colors: NowPlayingColors }) {
const { volume, setVolume, available } = useSystemVolume()
if (!available) return null
return (
<PlayerSlider
accessibilityLabel="Volume"
commitHoldMs={0}
animateValueChanges
value={volume}
maximumValue={1}
fillColor={colors.sliderFill}
activeFillColor={colors.foreground}
trackColor={colors.sliderTrack}
onValueChange={setVolume}
leading={
<PlayerSymbol
name="speaker.fill"
fallback="volume-low"
size={14}
color={colors.secondaryForeground}
/>
}
trailing={
<PlayerSymbol
name="speaker.wave.3.fill"
fallback="volume-high"
size={14}
color={colors.secondaryForeground}
/>
}
/>
)
}
/** Title and artist with the favorite and more buttons. */
function SongMeta({
song,
@@ -732,7 +739,23 @@ const styles = StyleSheet.create({
paddingHorizontal: HORIZONTAL_INSET,
paddingTop: spacing.md,
},
time: { fontWeight: "600" },
timeRow: {
flexDirection: "row",
alignItems: "center",
justifyContent: "space-between",
marginTop: -4,
},
format: {
position: "absolute",
left: 0,
right: 0,
alignItems: "center",
},
time: {
fontSize: 12,
fontWeight: "600",
fontVariant: ["tabular-nums"],
},
transport: {
flexDirection: "row",
alignItems: "center",
+799 -12
View File
@@ -1,21 +1,147 @@
import { useQuery } from "convex/react"
import { useConvex, useQuery } from "convex/react"
import * as AppIntents from "expo-app-intents"
import { useMemo, useSyncExternalStore } from "react"
import type { AppIntentEntity, AppIntentInvocation } from "expo-app-intents"
import { useEffect, useMemo, useRef, useSyncExternalStore } from "react"
import { AppState } from "react-native"
import {
completeMusicInvocation,
publishMusicCatalog,
trackMusicCatalogSync,
} from "@/lib/app-intents"
import { useAppNavigation } from "@/lib/app-navigation"
import { useMusicCatalogSync } from "@/components/app-intents/catalog-sync"
import { useIntentDispatch } from "@/components/app-intents/dispatch"
import { useNowPlayingCatalog } from "@/components/app-intents/now-playing"
import {
ANONYMOUS_CACHE_SCOPE,
useCacheScope,
} from "@/contexts/persistent-cache"
import { usePlayback } from "@/contexts/playback"
import {
FAVORITES_PLAYLIST_ID,
loadHandledIntentIds,
rememberHandledIntentId,
} from "@/lib/app-intent-handled"
import { showError } from "@/lib/toast"
import { fetchCachedQuery } from "@otunes/convex/client/cached-query"
import { getPlaybackStore } from "@otunes/convex/client/playback-store"
import type { RepeatMode } from "@otunes/convex/client/playback-queue"
import { api } from "@otunes/convex/_generated/api"
import type { Id } from "@otunes/convex/_generated/dataModel"
const warmedAudioIds = new Set<string>()
class IntentRejected extends Error {}
const KNOWN_INTENTS = new Set([
"playMusic",
"warmupAudio",
"addToPlaylist",
"addToUpNext",
"clearUpNext",
"createPlaylist",
"createStation",
"updateAffinity",
"transport",
"openMusic",
])
type MusicSource = {
kind: string
id: string
}
function readString(value: unknown) {
return typeof value === "string" && value.trim() ? value : null
}
function readSources(value: unknown): MusicSource[] {
if (!Array.isArray(value)) {
return []
}
return value.flatMap((entry) => {
if (!entry || typeof entry !== "object") {
return []
}
const source = entry as { kind?: unknown; id?: unknown }
if (typeof source.kind !== "string" || typeof source.id !== "string") {
return []
}
if (!source.kind || !source.id) {
return []
}
return [{ kind: source.kind, id: source.id }]
})
}
function shuffleChoice(value: unknown, fallback: boolean) {
if (value === "songs") return true
if (value === "off") return false
if (value == null) return fallback
throw new IntentRejected("Shuffle must be Off or Songs.")
}
function repeatChoice(value: unknown): RepeatMode | null {
if (value == null) return null
if (value === "none") return "none"
if (value === "one") return "once"
if (value === "all") return "always"
throw new IntentRejected("Repeat must be None, One, or All.")
}
function ensureActive(invocation: AppIntentInvocation) {
const deadline = invocation.params.deadline
if (typeof deadline === "number" && Date.now() >= deadline) {
throw new IntentRejected("This request expired. Please try again.")
}
}
function isTransient(error: unknown) {
const message = (
error instanceof Error ? error.message : String(error)
).toLowerCase()
return (
message.includes("not authenticated") ||
message.includes("network") ||
message.includes("fetch") ||
message.includes("connection") ||
message.includes("timed out") ||
message.includes("timeout")
)
}
function songEntity(song: {
id: string
title: string
artistName: string
artistId: string
albumTitle: string
albumId: string
durationSeconds: number
genres?: string
year?: number
track?: number
diskNumber?: number | null
favorite?: boolean
}): AppIntentEntity {
return {
id: song.id,
title: song.title,
subtitle: song.artistName,
synonyms: song.artistName ? [song.artistName] : undefined,
metadata: {
artistName: song.artistName,
artistId: song.artistId,
albumTitle: song.albumTitle,
albumId: song.albumId,
duration: String(song.durationSeconds),
genres: song.genres ?? "",
year: song.year == null ? "" : String(song.year),
track: song.track == null ? "" : String(song.track),
disk: song.diskNumber == null ? "" : String(song.diskNumber),
favorite: song.favorite ? "1" : "0",
},
}
}
/**
* Siri and Shortcuts integration: the on-device entity catalog, the current
* song, and intent dispatch. Must render inside `FavoritesProvider`.
*/
export function AppIntentBridge() {
const scope = useCacheScope()
const store = useMemo(
@@ -28,8 +154,669 @@ export function AppIntentBridge() {
}
function ReadyAppIntentBridge() {
useMusicCatalogSync()
useNowPlayingCatalog()
useIntentDispatch()
const convex = useConvex()
const cacheScope = useCacheScope()
const playback = usePlayback()
const user = useQuery(api.auth.currentUser)
const navigation = useAppNavigation()
const favorites = useQuery(api.favorites.getUserFavorites)
const catalogUserKey = user ? JSON.stringify([user._id, favorites]) : null
const store = useMemo(
() =>
getPlaybackStore(
cacheScope === ANONYMOUS_CACHE_SCOPE ? null : cacheScope
),
[cacheScope]
)
const snapshot = useSyncExternalStore(store.subscribe, store.getSnapshot)
const handledIds = useRef<Set<string> | null>(null)
const publishChain = useRef(Promise.resolve())
const publishGeneration = useRef(0)
const playbackRef = useRef(playback)
playbackRef.current = playback
useEffect(() => {
if (!AppIntents.isAvailable() || !user) return
const generation = ++publishGeneration.current
let stopped = false
let refreshing = false
const refresh = () => {
if (refreshing || stopped) return
refreshing = true
publishChain.current = publishChain.current
.catch(() => undefined)
.then(async () => {
for (const kind of ["artist", "album", "song", "playlist"] as const) {
const entities: AppIntentEntity[] = []
let cursor: string | null = null
do {
const result: {
page: AppIntentEntity[]
isDone: boolean
continueCursor: string
} = await convex.query(api.library.intentCatalogPage, {
kind,
paginationOpts: { cursor, numItems: 200 },
})
if (stopped || generation !== publishGeneration.current) return
entities.push(...result.page)
cursor = result.isDone ? null : result.continueCursor
} while (cursor !== null)
await publishMusicCatalog(kind, entities)
}
})
.catch((error) =>
console.warn("Could not publish music catalog.", error)
)
.finally(() => {
refreshing = false
})
trackMusicCatalogSync(publishChain.current)
}
refresh()
const subscription = AppState.addEventListener("change", (state) => {
if (state === "active") refresh()
})
const timer = setInterval(() => {
if (AppState.currentState === "active") refresh()
}, 5 * 60_000)
return () => {
stopped = true
++publishGeneration.current
subscription.remove()
clearInterval(timer)
}
}, [convex, cacheScope, catalogUserKey])
const currentSongId = playback.queueSongIds[0] ?? null
const currentSong = playback.currentSong
useEffect(() => {
if (!snapshot.hydrated || !AppIntents.isAvailable()) {
return
}
if (!currentSongId) {
void publishMusicCatalog("nowPlaying", []).catch((error) => {
console.warn("Could not clear the current song.", error)
})
return
}
if (!currentSong || currentSong.id !== currentSongId) {
return
}
void publishMusicCatalog("nowPlaying", [
songEntity({
id: currentSong.id,
title: currentSong.title,
artistName: currentSong.artist.name,
artistId: currentSong.artist.id,
albumTitle: currentSong.album.title,
albumId: currentSong.album.id,
durationSeconds: currentSong.durationSeconds,
}),
]).catch((error) => {
console.warn("Could not publish the current song.", error)
})
}, [currentSong, currentSongId, snapshot.hydrated])
AppIntents.useAppIntents(async (pendingIntents) => {
if (!handledIds.current) {
handledIds.current = await loadHandledIntentIds()
}
const handled = handledIds.current
for (const invocation of pendingIntents) {
if (handled.has(invocation.id)) {
const token = readString(invocation.params.completionToken)
if (token) await completeMusicInvocation(token, null)
await AppIntents.removePendingInvocationAsync(invocation.id)
continue
}
if (!KNOWN_INTENTS.has(invocation.name)) {
await AppIntents.removePendingInvocationAsync(invocation.id)
continue
}
const token = readString(invocation.params.completionToken)
try {
ensureActive(invocation)
await performInvocation(invocation)
if (
["playMusic", "warmupAudio", "transport", "createStation"].includes(
invocation.name
)
) {
const id = store.getState().queue[0]
const song = playbackRef.current.currentSong
if (!id) await publishMusicCatalog("nowPlaying", [])
else if (song?.id === id)
await publishMusicCatalog("nowPlaying", [
songEntity({
id: song.id,
title: song.title,
artistName: song.artist.name,
artistId: song.artist.id,
albumTitle: song.album.title,
albumId: song.album.id,
durationSeconds: song.durationSeconds,
}),
])
}
await rememberHandledIntentId(handled, invocation.id)
if (token) await completeMusicInvocation(token, null)
await AppIntents.removePendingInvocationAsync(invocation.id)
} catch (error) {
if (token) {
await completeMusicInvocation(
token,
error instanceof Error ? error.message : "oTunes couldn't do that."
)
}
if (token || error instanceof IntentRejected || !isTransient(error)) {
const message =
error instanceof Error ? error.message : "oTunes couldn't do that."
if (!(error instanceof IntentRejected) || message) {
showError(message)
}
try {
await rememberHandledIntentId(handled, invocation.id)
await AppIntents.removePendingInvocationAsync(invocation.id)
} catch (removeError) {
console.warn(
"App Intent remains pending",
invocation.id,
removeError
)
}
continue
}
console.warn("App Intent remains pending", invocation.id, error)
}
}
async function songIdsFor(kind: string, id: string) {
if (kind === "song") {
return [id as Id<"songs">]
}
if (kind === "album") {
return fetchCachedQuery(
convex,
cacheScope,
api.playlists.getAlbumSongIds,
{
albumId: id as Id<"albums">,
}
)
}
if (kind === "artist") {
return fetchCachedQuery(
convex,
cacheScope,
api.playlists.getOrderedArtistSongIds,
{ artistId: id as Id<"artists"> }
)
}
if (kind === "playlist") {
if (id === FAVORITES_PLAYLIST_ID) {
const favorites = await fetchCachedQuery(
convex,
cacheScope,
api.favorites.getUserFavorites,
{}
)
return favorites.favoriteSongs
}
return fetchCachedQuery(
convex,
cacheScope,
api.playlists.getPlaylistSongIds,
{ playlistId: id as Id<"playlists"> }
)
}
throw new IntentRejected("Choose a song, album, artist, or playlist.")
}
async function uniqueSongIds(sources: MusicSource[]) {
const ids: Id<"songs">[] = []
for (const source of sources) {
ids.push(...(await songIdsFor(source.kind, source.id)))
}
return [...new Set(ids)]
}
async function performInvocation(invocation: AppIntentInvocation) {
const actions = playbackRef.current
switch (invocation.name) {
case "openMusic": {
const kind = readString(invocation.params.kind)
const id = readString(invocation.params.id)
if (!id || !kind) throw new IntentRejected("Choose music to open.")
if (kind === "song") {
const song = await convex.query(api.library.getSong, {
id: id as Id<"songs">,
})
if (!song)
throw new IntentRejected("This song is no longer available.")
navigation.push(`/album/${song.album}`)
} else if (kind === "playlist" && id === FAVORITES_PLAYLIST_ID) {
navigation.push("/playlist/favorites")
} else if (["album", "artist", "playlist"].includes(kind)) {
navigation.push(`/${kind}/${id}`)
} else throw new IntentRejected("Unknown music type.")
return
}
case "playMusic":
await playMusic(invocation)
if (!["next", "tail"].includes(String(invocation.params.queue))) {
ensureActive(invocation)
await playbackRef.current.waitForPlaybackReady(true)
}
return
case "warmupAudio":
await warmup(invocation)
ensureActive(invocation)
await playbackRef.current.waitForPlaybackReady(false)
warmedAudioIds.add(invocation.id)
return
case "addToPlaylist":
await addToPlaylist(invocation)
return
case "addToUpNext":
await addToUpNext(invocation)
return
case "clearUpNext":
ensureActive(invocation)
await actions.clearUpNext()
return
case "createPlaylist":
await createPlaylist(invocation)
return
case "createStation":
await createStation(invocation)
ensureActive(invocation)
await playbackRef.current.waitForPlaybackReady(true)
return
case "updateAffinity":
await updateAffinity(invocation)
return
case "transport":
await transport(invocation)
if (store.getState().queue.length > 0) {
const command = invocation.params.command
await playbackRef.current.waitForPlaybackReady(
command === "resume"
? true
: command === "pause"
? false
: undefined
)
}
return
default:
return
}
}
async function playMusic(invocation: AppIntentInvocation) {
const actions = playbackRef.current
const queue = readString(invocation.params.queue) ?? "now"
const warmupId = readString(invocation.params.warmupId)
const repeat = repeatChoice(invocation.params.repeat)
if (repeat) {
ensureActive(invocation)
await actions.setRepeatMode(repeat)
}
if (queue === "next" || queue === "tail") {
const kind = readString(invocation.params.kind)
const id = readString(invocation.params.id)
if (!kind || !id) {
throw new IntentRejected("Choose music to add to Up Next.")
}
const shuffled = shuffleChoice(
invocation.params.shuffle,
actions.shuffleEnabled
)
if (shuffled !== actions.shuffleEnabled) {
ensureActive(invocation)
await actions.setShuffleEnabled(shuffled)
}
const ids = await songIdsFor(kind, id)
if (ids.length === 0) {
throw new IntentRejected("Nothing to add to Up Next.")
}
if (queue === "next") {
ensureActive(invocation)
await actions.addToQueueNext(ids)
} else {
ensureActive(invocation)
await actions.addToQueueLast(ids)
}
return
}
if (warmupId && warmedAudioIds.has(warmupId)) {
const shuffled = shuffleChoice(
invocation.params.shuffle,
actions.shuffleEnabled
)
if (shuffled !== actions.shuffleEnabled) {
ensureActive(invocation)
await actions.setShuffleEnabled(shuffled)
}
ensureActive(invocation)
actions.setIsPlaying(true)
return
}
const kind = readString(invocation.params.kind) ?? "resume"
if (kind === "resume") {
const shuffled = shuffleChoice(
invocation.params.shuffle,
actions.shuffleEnabled
)
if (shuffled !== actions.shuffleEnabled) {
ensureActive(invocation)
await actions.setShuffleEnabled(shuffled)
}
if (actions.queueSongIds.length === 0) {
const favorites = await fetchCachedQuery(
convex,
cacheScope,
api.favorites.getUserFavorites,
{}
)
if (favorites.favoriteSongs.length === 0) {
throw new IntentRejected(
"Nothing is queued, and your favorites are empty."
)
}
if (shuffled) {
ensureActive(invocation)
await actions.playShuffled(favorites.favoriteSongs)
} else {
ensureActive(invocation)
await actions.playInOrder(favorites.favoriteSongs)
}
return
}
ensureActive(invocation)
actions.setIsPlaying(true)
return
}
const id = readString(invocation.params.id)
if (!id) {
throw new IntentRejected("Choose music to play.")
}
const shuffled = shuffleChoice(
invocation.params.shuffle,
actions.shuffleEnabled
)
if (kind === "song") {
const song = await fetchCachedQuery(
convex,
cacheScope,
api.library.getSong,
{
id: id as Id<"songs">,
}
)
if (!song) {
throw new IntentRejected("That song is no longer in your library.")
}
const albumSongIds = await fetchCachedQuery(
convex,
cacheScope,
api.playlists.getAlbumSongIds,
{ albumId: song.album }
)
ensureActive(invocation)
await actions.startFromSong(
song._id,
albumSongIds.length > 0 ? albumSongIds : [song._id],
shuffled,
true
)
return
}
const ids = await songIdsFor(kind, id)
if (ids.length === 0) {
throw new IntentRejected("Nothing to play.")
}
if (shuffled) {
ensureActive(invocation)
await actions.playShuffled(ids)
} else {
ensureActive(invocation)
await actions.playInOrder(ids)
}
}
async function warmup(invocation: AppIntentInvocation) {
const kind = readString(invocation.params.kind)
const id = readString(invocation.params.id)
if (!kind || !id) {
throw new IntentRejected("Choose music to get ready.")
}
const shuffled = shuffleChoice(invocation.params.shuffle, false)
if (kind === "song") {
const song = await fetchCachedQuery(
convex,
cacheScope,
api.library.getSong,
{
id: id as Id<"songs">,
}
)
if (!song) {
throw new IntentRejected("That song is no longer in your library.")
}
const albumSongIds = await fetchCachedQuery(
convex,
cacheScope,
api.playlists.getAlbumSongIds,
{ albumId: song.album }
)
ensureActive(invocation)
await playbackRef.current.startFromSong(
song._id,
albumSongIds.length > 0 ? albumSongIds : [song._id],
shuffled,
false
)
} else {
const ids = await songIdsFor(kind, id)
if (ids.length === 0) {
throw new IntentRejected("Nothing to get ready.")
}
ensureActive(invocation)
await playbackRef.current.prepareQueue(ids, shuffled)
}
}
async function addToPlaylist(invocation: AppIntentInvocation) {
const playlistId = readString(invocation.params.playlistId)
const sources = readSources(invocation.params.sources)
if (!playlistId || sources.length === 0) {
throw new IntentRejected("Choose music and a playlist.")
}
const songIds = await uniqueSongIds(sources)
if (songIds.length === 0) {
throw new IntentRejected("Those items have no songs to add.")
}
if (playlistId === FAVORITES_PLAYLIST_ID) {
for (const source of sources) {
if (source.kind === "song") {
ensureActive(invocation)
await convex.mutation(api.favorites.favoriteSong, {
songId: source.id as Id<"songs">,
})
} else if (source.kind === "album") {
ensureActive(invocation)
await convex.mutation(api.favorites.favoriteAlbum, {
albumId: source.id as Id<"albums">,
})
} else if (source.kind === "artist") {
ensureActive(invocation)
await convex.mutation(api.favorites.favoriteArtist, {
artistId: source.id as Id<"artists">,
})
} else if (source.kind === "playlist") {
const ids = await songIdsFor(source.kind, source.id)
if (ids.length > 0) {
ensureActive(invocation)
await convex.mutation(api.favorites.favoriteSongs, {
songIds: ids,
})
}
} else {
throw new IntentRejected(
"Choose a song, album, artist, or playlist."
)
}
}
return
}
ensureActive(invocation)
await convex.mutation(api.playlists.addSongsToPlaylist, {
playlistId: playlistId as Id<"playlists">,
songIds,
})
}
async function addToUpNext(invocation: AppIntentInvocation) {
const sources = readSources(invocation.params.sources)
if (sources.length === 0) {
throw new IntentRejected("Choose music to add to Up Next.")
}
const ids = await uniqueSongIds(sources)
if (ids.length === 0) {
throw new IntentRejected("Nothing to add to Up Next.")
}
const position =
readString(invocation.params.position) === "last" ? "last" : "next"
if (position === "last") {
ensureActive(invocation)
await playbackRef.current.addToQueueLast(ids)
} else {
ensureActive(invocation)
await playbackRef.current.addToQueueNext(ids)
}
}
async function createPlaylist(invocation: AppIntentInvocation) {
const name = readString(invocation.params.name)
if (!name) {
throw new IntentRejected("A playlist needs a name.")
}
const songIds = await uniqueSongIds(
readSources(invocation.params.sources)
)
ensureActive(invocation)
await convex.mutation(api.playlists.createPlaylist, {
name,
songIds,
invocationId: invocation.id,
})
}
async function createStation(invocation: AppIntentInvocation) {
const artistId = readString(invocation.params.artistId)
if (!artistId) {
throw new IntentRejected("Nothing is playing in oTunes.")
}
const ids = await songIdsFor("artist", artistId)
if (ids.length === 0)
throw new IntentRejected("This artist has no songs to play.")
ensureActive(invocation)
await playbackRef.current.playShuffled(ids)
}
async function updateAffinity(invocation: AppIntentInvocation) {
const kind = readString(invocation.params.kind)
const id = readString(invocation.params.id)
const state = readString(invocation.params.state)
if (!kind || !id || (state !== "like" && state !== "unset")) {
throw new IntentRejected(
"oTunes can love or unlike a song, album, or artist."
)
}
if (kind === "playlist") {
throw new IntentRejected("oTunes can love songs, albums, and artists.")
}
if (state === "like") {
if (kind === "song") {
ensureActive(invocation)
await convex.mutation(api.favorites.favoriteSong, {
songId: id as Id<"songs">,
})
} else if (kind === "album") {
ensureActive(invocation)
await convex.mutation(api.favorites.favoriteAlbum, {
albumId: id as Id<"albums">,
})
} else if (kind === "artist") {
ensureActive(invocation)
await convex.mutation(api.favorites.favoriteArtist, {
artistId: id as Id<"artists">,
})
} else {
throw new IntentRejected(
"oTunes can love songs, albums, and artists."
)
}
return
}
if (kind === "song") {
ensureActive(invocation)
await convex.mutation(api.favorites.unfavoriteSong, {
songId: id as Id<"songs">,
})
} else if (kind === "album") {
ensureActive(invocation)
await convex.mutation(api.favorites.unfavoriteAlbum, {
albumId: id as Id<"albums">,
})
} else if (kind === "artist") {
ensureActive(invocation)
await convex.mutation(api.favorites.unfavoriteArtist, {
artistId: id as Id<"artists">,
})
} else {
throw new IntentRejected("oTunes can love songs, albums, and artists.")
}
}
async function transport(invocation: AppIntentInvocation) {
const actions = playbackRef.current
const command = readString(invocation.params.command)
if (command === "pause") {
ensureActive(invocation)
actions.setIsPlaying(false)
return
}
if (command === "resume") {
if (actions.queueSongIds.length === 0) {
throw new IntentRejected("Nothing is queued in oTunes.")
}
ensureActive(invocation)
actions.setIsPlaying(true)
return
}
if (command === "next") {
ensureActive(invocation)
await actions.skipNext()
return
}
if (command === "previous") {
ensureActive(invocation)
await actions.skipPrevious()
return
}
throw new IntentRejected("oTunes doesn't know that playback control.")
}
})
return null
}
@@ -1,198 +0,0 @@
import { useConvex, useQuery } from "convex/react"
import * as AppIntents from "expo-app-intents"
import type { AppIntentEntity } from "expo-app-intents"
import { useEffect, useRef } from "react"
import { AppState } from "react-native"
import { useCacheScope } from "@/contexts/persistent-cache"
import {
loadCatalogCursors,
saveCatalogCursors,
type CatalogCursors,
} from "@/lib/app-intent-handled"
import {
publishMusicCatalog,
readMusicCatalog,
trackMusicCatalogSync,
} from "@/lib/app-intents"
import { api } from "@otunes/convex/_generated/api"
import type { FunctionReturnType } from "convex/server"
const LIBRARY_KINDS = ["artist", "album", "song"] as const
type LibraryKind = (typeof LIBRARY_KINDS)[number]
const PAGE_SIZE = 500
type CatalogPage = FunctionReturnType<typeof api.intentCatalog.page>
type Favorites = {
favoriteSongs: string[]
favoriteAlbums: string[]
favoriteArtists: string[]
}
function favoriteIds(favorites: Favorites, kind: LibraryKind) {
return new Set<string>(
kind === "song"
? favorites.favoriteSongs
: kind === "album"
? favorites.favoriteAlbums
: favorites.favoriteArtists
)
}
/** Stamps `metadata.favorite`, which Siri uses to rank suggestions. */
function withFavorite(entity: AppIntentEntity, favorite: boolean) {
const flag = favorite ? "1" : "0"
return entity.metadata?.favorite === flag
? entity
: { ...entity, metadata: { ...entity.metadata, favorite: flag } }
}
/**
* Keeps the on-device Siri catalog in step with the server. `intentCatalog.head`
* is a live subscription, so the library is fetched only when it changes, and
* then only the records changed since the stored cursor. Favorites are applied
* locally from the existing favorites subscription.
*/
export function useMusicCatalogSync() {
const convex = useConvex()
const scope = useCacheScope()
const head = useQuery(api.intentCatalog.head)
const favorites = useQuery(api.favorites.getUserFavorites)
const playlists = useQuery(api.intentCatalog.playlists)
const latest = useRef({ head, favorites, playlists })
latest.current = { head, favorites, playlists }
const schedule = useRef<() => void>(() => undefined)
useEffect(() => {
if (!AppIntents.isAvailable()) return
let stopped = false
let running = false
let dirty = false
let cursors: CatalogCursors | null = null
// What the disk catalog reflects, to skip no-op republishes.
const appliedFavorites = new Map<LibraryKind, Set<string>>()
let publishedPlaylists: string | null = null
async function fetchChanges(kind: LibraryKind, since: number | undefined) {
const upserts: AppIntentEntity[] = []
let removed: string[] = []
let head = 0
let cursor: string | null = null
do {
const result: CatalogPage = await convex.query(api.intentCatalog.page, {
kind,
since,
paginationOpts: { cursor, numItems: PAGE_SIZE },
})
if (stopped) return null
if (cursor === null) {
if (result.reset) return fetchChanges(kind, undefined)
head = result.head ?? 0
removed = result.removed
}
upserts.push(...result.page)
cursor = result.isDone ? null : result.continueCursor
} while (cursor !== null)
return { upserts, removed, head, full: since === undefined }
}
async function syncKind(
kind: LibraryKind,
serverHead: number,
favorites: Favorites
) {
if (!cursors) return
const since = cursors[kind]
const stale = since === undefined || serverHead > since
const liked = favoriteIds(favorites, kind)
const previousLiked = appliedFavorites.get(kind)
const likesChanged =
!previousLiked ||
previousLiked.size !== liked.size ||
[...liked].some((id) => !previousLiked.has(id))
if (!stale && !likesChanged) return
const changes = stale ? await fetchChanges(kind, since) : null
if (stopped) return
const records = new Map<string, AppIntentEntity>()
if (!changes?.full) {
for (const entity of await readMusicCatalog(kind)) {
records.set(entity.id, entity)
}
}
for (const id of changes?.removed ?? []) records.delete(id)
for (const entity of changes?.upserts ?? [])
records.set(entity.id, entity)
let modified = changes !== null
for (const [id, entity] of records) {
const next = withFavorite(entity, liked.has(id))
if (next !== entity) {
records.set(id, next)
modified = true
}
}
if (stopped) return
if (modified) await publishMusicCatalog(kind, [...records.values()])
appliedFavorites.set(kind, liked)
if (changes && !stopped) {
cursors = { ...cursors, [kind]: changes.head }
await saveCatalogCursors(scope, cursors)
}
}
async function syncOnce() {
const { head, favorites, playlists } = latest.current
cursors ??= await loadCatalogCursors(scope)
if (head && favorites) {
for (const kind of LIBRARY_KINDS) {
if (stopped) return
await syncKind(kind, head[kind], favorites)
}
}
if (playlists && !stopped) {
const json = JSON.stringify(playlists)
if (json !== publishedPlaylists) {
await publishMusicCatalog("playlist", playlists)
publishedPlaylists = json
}
}
}
schedule.current = () => {
if (stopped) return
if (running) {
dirty = true
return
}
running = true
const sync = (async () => {
do {
dirty = false
await syncOnce()
} while (dirty && !stopped)
})()
.catch((error) =>
console.warn("Could not publish music catalog.", error)
)
.finally(() => {
running = false
})
trackMusicCatalogSync(sync)
}
schedule.current()
// Retries a failed sync; a no-op without server queries when current.
const subscription = AppState.addEventListener("change", (state) => {
if (state === "active") schedule.current()
})
return () => {
stopped = true
schedule.current = () => undefined
subscription.remove()
}
}, [convex, scope])
useEffect(() => {
schedule.current()
}, [head, favorites, playlists])
}
@@ -1,631 +0,0 @@
import { useConvex } from "convex/react"
import * as AppIntents from "expo-app-intents"
import type { AppIntentInvocation } from "expo-app-intents"
import { useMemo, useRef } from "react"
import { useFavorites, type FavoriteKind } from "@/contexts/favorites"
import {
ANONYMOUS_CACHE_SCOPE,
useCacheScope,
} from "@/contexts/persistent-cache"
import { usePlaybackActions, usePlaybackQueue } from "@/contexts/playback"
import {
FAVORITES_PLAYLIST_ID,
loadHandledIntentIds,
rememberHandledIntentId,
} from "@/lib/app-intent-handled"
import { completeMusicInvocation } from "@/lib/app-intents"
import { useAppNavigation } from "@/lib/app-navigation"
import { showError } from "@/lib/toast"
import { fetchCachedQuery } from "@otunes/convex/client/cached-query"
import { getPlaybackStore } from "@otunes/convex/client/playback-store"
import type { RepeatMode } from "@otunes/convex/client/playback-queue"
import { api } from "@otunes/convex/_generated/api"
import type { Id } from "@otunes/convex/_generated/dataModel"
import { publishNowPlaying } from "./now-playing"
const warmedAudioIds = new Set<string>()
class IntentRejected extends Error {}
const KNOWN_INTENTS = new Set([
"playMusic",
"warmupAudio",
"addToPlaylist",
"addToUpNext",
"clearUpNext",
"createPlaylist",
"createStation",
"updateAffinity",
"transport",
"openMusic",
])
type MusicSource = {
kind: string
id: string
}
function readString(value: unknown) {
return typeof value === "string" && value.trim() ? value : null
}
function readSources(value: unknown): MusicSource[] {
if (!Array.isArray(value)) {
return []
}
return value.flatMap((entry) => {
if (!entry || typeof entry !== "object") {
return []
}
const source = entry as { kind?: unknown; id?: unknown }
if (typeof source.kind !== "string" || typeof source.id !== "string") {
return []
}
if (!source.kind || !source.id) {
return []
}
return [{ kind: source.kind, id: source.id }]
})
}
function isFavoriteKind(kind: string): kind is FavoriteKind {
return kind === "song" || kind === "album" || kind === "artist"
}
function shuffleChoice(value: unknown, fallback: boolean) {
if (value === "songs") return true
if (value === "off") return false
if (value == null) return fallback
throw new IntentRejected("Shuffle must be Off or Songs.")
}
function repeatChoice(value: unknown): RepeatMode | null {
if (value == null) return null
if (value === "none") return "none"
if (value === "one") return "once"
if (value === "all") return "always"
throw new IntentRejected("Repeat must be None, One, or All.")
}
function ensureActive(invocation: AppIntentInvocation) {
const deadline = invocation.params.deadline
if (typeof deadline === "number" && Date.now() >= deadline) {
throw new IntentRejected("This request expired. Please try again.")
}
}
function isTransient(error: unknown) {
const message = (
error instanceof Error ? error.message : String(error)
).toLowerCase()
return (
message.includes("not authenticated") ||
message.includes("network") ||
message.includes("fetch") ||
message.includes("connection") ||
message.includes("timed out") ||
message.includes("timeout")
)
}
/** Runs Siri and Shortcuts invocations queued by the native intents. */
export function useIntentDispatch() {
const convex = useConvex()
const cacheScope = useCacheScope()
const playback = { ...usePlaybackQueue(), ...usePlaybackActions() }
const favorites = useFavorites()
const navigation = useAppNavigation()
const store = useMemo(
() =>
getPlaybackStore(
cacheScope === ANONYMOUS_CACHE_SCOPE ? null : cacheScope
),
[cacheScope]
)
const handledIds = useRef<Set<string> | null>(null)
const playbackRef = useRef(playback)
playbackRef.current = playback
/** Intent ids arrive as strings; the server validates them. */
function setFavorite(kind: FavoriteKind, id: string, favorite: boolean) {
return kind === "song"
? favorites.setFavorite(kind, id as Id<"songs">, favorite)
: kind === "album"
? favorites.setFavorite(kind, id as Id<"albums">, favorite)
: favorites.setFavorite(kind, id as Id<"artists">, favorite)
}
AppIntents.useAppIntents(async (pendingIntents) => {
if (!handledIds.current) {
handledIds.current = await loadHandledIntentIds()
}
const handled = handledIds.current
for (const invocation of pendingIntents) {
if (handled.has(invocation.id)) {
const token = readString(invocation.params.completionToken)
if (token) await completeMusicInvocation(token, null)
await AppIntents.removePendingInvocationAsync(invocation.id)
continue
}
if (!KNOWN_INTENTS.has(invocation.name)) {
await AppIntents.removePendingInvocationAsync(invocation.id)
continue
}
const token = readString(invocation.params.completionToken)
try {
ensureActive(invocation)
await performInvocation(invocation)
if (
["playMusic", "warmupAudio", "transport", "createStation"].includes(
invocation.name
)
) {
await publishNowPlaying(
store.getState().queue[0] ?? null,
playbackRef.current.currentSong
)
}
await rememberHandledIntentId(handled, invocation.id)
if (token) await completeMusicInvocation(token, null)
await AppIntents.removePendingInvocationAsync(invocation.id)
} catch (error) {
if (token) {
await completeMusicInvocation(
token,
error instanceof Error ? error.message : "oTunes couldn't do that."
)
}
if (token || error instanceof IntentRejected || !isTransient(error)) {
const message =
error instanceof Error ? error.message : "oTunes couldn't do that."
if (!(error instanceof IntentRejected) || message) {
showError(message)
}
try {
await rememberHandledIntentId(handled, invocation.id)
await AppIntents.removePendingInvocationAsync(invocation.id)
} catch (removeError) {
console.warn(
"App Intent remains pending",
invocation.id,
removeError
)
}
continue
}
console.warn("App Intent remains pending", invocation.id, error)
}
}
async function songIdsFor(kind: string, id: string) {
if (kind === "song") {
return [id as Id<"songs">]
}
if (kind === "album") {
return fetchCachedQuery(
convex,
cacheScope,
api.playlists.getAlbumSongIds,
{
albumId: id as Id<"albums">,
}
)
}
if (kind === "artist") {
return fetchCachedQuery(
convex,
cacheScope,
api.playlists.getOrderedArtistSongIds,
{ artistId: id as Id<"artists"> }
)
}
if (kind === "playlist") {
if (id === FAVORITES_PLAYLIST_ID) {
const favorites = await fetchCachedQuery(
convex,
cacheScope,
api.favorites.getUserFavorites,
{}
)
return favorites.favoriteSongs
}
return fetchCachedQuery(
convex,
cacheScope,
api.playlists.getPlaylistSongIds,
{ playlistId: id as Id<"playlists"> }
)
}
throw new IntentRejected("Choose a song, album, artist, or playlist.")
}
async function uniqueSongIds(sources: MusicSource[]) {
const ids: Id<"songs">[] = []
for (const source of sources) {
ids.push(...(await songIdsFor(source.kind, source.id)))
}
return [...new Set(ids)]
}
async function performInvocation(invocation: AppIntentInvocation) {
const actions = playbackRef.current
switch (invocation.name) {
case "openMusic": {
const kind = readString(invocation.params.kind)
const id = readString(invocation.params.id)
if (!id || !kind) throw new IntentRejected("Choose music to open.")
if (kind === "song") {
const song = await convex.query(api.library.getSong, {
id: id as Id<"songs">,
})
if (!song)
throw new IntentRejected("This song is no longer available.")
navigation.push(`/album/${song.album}`)
} else if (kind === "playlist" && id === FAVORITES_PLAYLIST_ID) {
navigation.push("/playlist/favorites")
} else if (["album", "artist", "playlist"].includes(kind)) {
navigation.push(`/${kind}/${id}`)
} else throw new IntentRejected("Unknown music type.")
return
}
case "playMusic":
await playMusic(invocation)
if (!["next", "tail"].includes(String(invocation.params.queue))) {
ensureActive(invocation)
await playbackRef.current.waitForPlaybackReady(true)
}
return
case "warmupAudio":
await warmup(invocation)
ensureActive(invocation)
await playbackRef.current.waitForPlaybackReady(false)
warmedAudioIds.add(invocation.id)
return
case "addToPlaylist":
await addToPlaylist(invocation)
return
case "addToUpNext":
await addToUpNext(invocation)
return
case "clearUpNext":
ensureActive(invocation)
await actions.clearUpNext()
return
case "createPlaylist":
await createPlaylist(invocation)
return
case "createStation":
await createStation(invocation)
ensureActive(invocation)
await playbackRef.current.waitForPlaybackReady(true)
return
case "updateAffinity":
await updateAffinity(invocation)
return
case "transport":
await transport(invocation)
if (store.getState().queue.length > 0) {
const command = invocation.params.command
await playbackRef.current.waitForPlaybackReady(
command === "resume"
? true
: command === "pause"
? false
: undefined
)
}
return
default:
return
}
}
async function playMusic(invocation: AppIntentInvocation) {
const actions = playbackRef.current
const queue = readString(invocation.params.queue) ?? "now"
const warmupId = readString(invocation.params.warmupId)
const repeat = repeatChoice(invocation.params.repeat)
if (repeat) {
ensureActive(invocation)
await actions.setRepeatMode(repeat)
}
if (queue === "next" || queue === "tail") {
const kind = readString(invocation.params.kind)
const id = readString(invocation.params.id)
if (!kind || !id) {
throw new IntentRejected("Choose music to add to Up Next.")
}
const shuffled = shuffleChoice(
invocation.params.shuffle,
actions.shuffleEnabled
)
if (shuffled !== actions.shuffleEnabled) {
ensureActive(invocation)
await actions.setShuffleEnabled(shuffled)
}
const ids = await songIdsFor(kind, id)
if (ids.length === 0) {
throw new IntentRejected("Nothing to add to Up Next.")
}
if (queue === "next") {
ensureActive(invocation)
await actions.addToQueueNext(ids)
} else {
ensureActive(invocation)
await actions.addToQueueLast(ids)
}
return
}
if (warmupId && warmedAudioIds.has(warmupId)) {
const shuffled = shuffleChoice(
invocation.params.shuffle,
actions.shuffleEnabled
)
if (shuffled !== actions.shuffleEnabled) {
ensureActive(invocation)
await actions.setShuffleEnabled(shuffled)
}
ensureActive(invocation)
actions.setIsPlaying(true)
return
}
const kind = readString(invocation.params.kind) ?? "resume"
if (kind === "resume") {
const shuffled = shuffleChoice(
invocation.params.shuffle,
actions.shuffleEnabled
)
if (shuffled !== actions.shuffleEnabled) {
ensureActive(invocation)
await actions.setShuffleEnabled(shuffled)
}
if (actions.queueSongIds.length === 0) {
const favorites = await fetchCachedQuery(
convex,
cacheScope,
api.favorites.getUserFavorites,
{}
)
if (favorites.favoriteSongs.length === 0) {
throw new IntentRejected(
"Nothing is queued, and your favorites are empty."
)
}
if (shuffled) {
ensureActive(invocation)
await actions.playShuffled(favorites.favoriteSongs)
} else {
ensureActive(invocation)
await actions.playInOrder(favorites.favoriteSongs)
}
return
}
ensureActive(invocation)
actions.setIsPlaying(true)
return
}
const id = readString(invocation.params.id)
if (!id) {
throw new IntentRejected("Choose music to play.")
}
const shuffled = shuffleChoice(
invocation.params.shuffle,
actions.shuffleEnabled
)
if (kind === "song") {
const song = await fetchCachedQuery(
convex,
cacheScope,
api.library.getSong,
{
id: id as Id<"songs">,
}
)
if (!song) {
throw new IntentRejected("That song is no longer in your library.")
}
const albumSongIds = await fetchCachedQuery(
convex,
cacheScope,
api.playlists.getAlbumSongIds,
{ albumId: song.album }
)
ensureActive(invocation)
await actions.startFromSong(
song._id,
albumSongIds.length > 0 ? albumSongIds : [song._id],
shuffled,
true
)
return
}
const ids = await songIdsFor(kind, id)
if (ids.length === 0) {
throw new IntentRejected("Nothing to play.")
}
if (shuffled) {
ensureActive(invocation)
await actions.playShuffled(ids)
} else {
ensureActive(invocation)
await actions.playInOrder(ids)
}
}
async function warmup(invocation: AppIntentInvocation) {
const kind = readString(invocation.params.kind)
const id = readString(invocation.params.id)
if (!kind || !id) {
throw new IntentRejected("Choose music to get ready.")
}
const shuffled = shuffleChoice(invocation.params.shuffle, false)
if (kind === "song") {
const song = await fetchCachedQuery(
convex,
cacheScope,
api.library.getSong,
{
id: id as Id<"songs">,
}
)
if (!song) {
throw new IntentRejected("That song is no longer in your library.")
}
const albumSongIds = await fetchCachedQuery(
convex,
cacheScope,
api.playlists.getAlbumSongIds,
{ albumId: song.album }
)
ensureActive(invocation)
await playbackRef.current.startFromSong(
song._id,
albumSongIds.length > 0 ? albumSongIds : [song._id],
shuffled,
false
)
} else {
const ids = await songIdsFor(kind, id)
if (ids.length === 0) {
throw new IntentRejected("Nothing to get ready.")
}
ensureActive(invocation)
await playbackRef.current.prepareQueue(ids, shuffled)
}
}
async function addToPlaylist(invocation: AppIntentInvocation) {
const playlistId = readString(invocation.params.playlistId)
const sources = readSources(invocation.params.sources)
if (!playlistId || sources.length === 0) {
throw new IntentRejected("Choose music and a playlist.")
}
const songIds = await uniqueSongIds(sources)
if (songIds.length === 0) {
throw new IntentRejected("Those items have no songs to add.")
}
if (playlistId === FAVORITES_PLAYLIST_ID) {
for (const source of sources) {
if (isFavoriteKind(source.kind)) {
ensureActive(invocation)
await setFavorite(source.kind, source.id, true)
} else if (source.kind === "playlist") {
const ids = await songIdsFor(source.kind, source.id)
ensureActive(invocation)
await favorites.addFavoriteSongs(ids)
} else {
throw new IntentRejected(
"Choose a song, album, artist, or playlist."
)
}
}
return
}
ensureActive(invocation)
await convex.mutation(api.playlists.addSongsToPlaylist, {
playlistId: playlistId as Id<"playlists">,
songIds,
})
}
async function addToUpNext(invocation: AppIntentInvocation) {
const sources = readSources(invocation.params.sources)
if (sources.length === 0) {
throw new IntentRejected("Choose music to add to Up Next.")
}
const ids = await uniqueSongIds(sources)
if (ids.length === 0) {
throw new IntentRejected("Nothing to add to Up Next.")
}
const position =
readString(invocation.params.position) === "last" ? "last" : "next"
if (position === "last") {
ensureActive(invocation)
await playbackRef.current.addToQueueLast(ids)
} else {
ensureActive(invocation)
await playbackRef.current.addToQueueNext(ids)
}
}
async function createPlaylist(invocation: AppIntentInvocation) {
const name = readString(invocation.params.name)
if (!name) {
throw new IntentRejected("A playlist needs a name.")
}
const songIds = await uniqueSongIds(
readSources(invocation.params.sources)
)
ensureActive(invocation)
await convex.mutation(api.playlists.createPlaylist, {
name,
songIds,
invocationId: invocation.id,
})
}
async function createStation(invocation: AppIntentInvocation) {
const artistId = readString(invocation.params.artistId)
if (!artistId) {
throw new IntentRejected("Nothing is playing in oTunes.")
}
const ids = await songIdsFor("artist", artistId)
if (ids.length === 0)
throw new IntentRejected("This artist has no songs to play.")
ensureActive(invocation)
await playbackRef.current.playShuffled(ids)
}
async function updateAffinity(invocation: AppIntentInvocation) {
const kind = readString(invocation.params.kind)
const id = readString(invocation.params.id)
const state = readString(invocation.params.state)
if (!kind || !id || (state !== "like" && state !== "unset")) {
throw new IntentRejected(
"oTunes can love or unlike a song, album, or artist."
)
}
if (!isFavoriteKind(kind)) {
throw new IntentRejected("oTunes can love songs, albums, and artists.")
}
ensureActive(invocation)
await setFavorite(kind, id, state === "like")
}
async function transport(invocation: AppIntentInvocation) {
const actions = playbackRef.current
const command = readString(invocation.params.command)
if (command === "pause") {
ensureActive(invocation)
actions.setIsPlaying(false)
return
}
if (command === "resume") {
if (actions.queueSongIds.length === 0) {
throw new IntentRejected("Nothing is queued in oTunes.")
}
ensureActive(invocation)
actions.setIsPlaying(true)
return
}
if (command === "next") {
ensureActive(invocation)
await actions.skipNext()
return
}
if (command === "previous") {
ensureActive(invocation)
await actions.skipPrevious()
return
}
throw new IntentRejected("oTunes doesn't know that playback control.")
}
})
}
@@ -1,59 +0,0 @@
import * as AppIntents from "expo-app-intents"
import type { AppIntentEntity } from "expo-app-intents"
import { useEffect } from "react"
import { usePlaybackQueue } from "@/contexts/playback"
import { publishMusicCatalog } from "@/lib/app-intents"
type CurrentSong = NonNullable<
ReturnType<typeof usePlaybackQueue>["currentSong"]
>
function nowPlayingEntity(song: CurrentSong): AppIntentEntity {
const artistName = song.artist.name
return {
id: song.id,
title: song.title,
subtitle: artistName,
synonyms: artistName ? [artistName] : undefined,
metadata: {
artistName,
artistId: song.artist.id,
albumTitle: song.album.title,
albumId: song.album.id,
duration: String(song.durationSeconds),
genres: "",
year: "",
track: "",
disk: "",
favorite: "0",
},
}
}
/**
* Publishes the "nowPlaying" catalog Siri reads for the current song. With an
* id but no matching `song` it leaves the catalog alone until the song loads.
*/
export async function publishNowPlaying(
queueHeadId: string | null,
song: CurrentSong | null
) {
if (!queueHeadId) await publishMusicCatalog("nowPlaying", [])
else if (song?.id === queueHeadId)
await publishMusicCatalog("nowPlaying", [nowPlayingEntity(song)])
}
/** Keeps the "nowPlaying" catalog in step with the playback queue. */
export function useNowPlayingCatalog() {
const playback = usePlaybackQueue()
const queueHeadId = playback.queueSongIds[0] ?? null
const song = playback.currentSong
useEffect(() => {
if (!AppIntents.isAvailable()) return
void publishNowPlaying(queueHeadId, song).catch((error) => {
console.warn("Could not publish the current song.", error)
})
}, [queueHeadId, song])
}
+25 -19
View File
@@ -2,33 +2,39 @@ import { PaletteText } from "@/components/palette-text"
import { StyleSheet, View } from "react-native"
import Svg, { G, Path } from "react-native-svg"
import {
DOLBY_PATHS,
DOLBY_VIEW_BOX,
DOLBY_WIDTH,
LOSSLESS_FALL,
LOSSLESS_FALL_GAP,
LOSSLESS_PEAKS,
LOSSLESS_STROKES,
LOSSLESS_THIN_WIDTH,
LOSSLESS_VIEW_BOX,
LOSSLESS_WIDTH,
MARK_HEIGHT,
} from "@otunes/convex/client/audio-format-marks"
import type { PlaybackFormat } from "@otunes/convex/playback-stream"
const LABEL_SIZE = 13
const MARK_HEIGHT = 11
function DolbyMark({ color }: { color: string }) {
return (
<Svg width={DOLBY_WIDTH} height={MARK_HEIGHT} viewBox={DOLBY_VIEW_BOX}>
{DOLBY_PATHS.map((d) => (
<Path key={d} d={d} fill={color} />
))}
<Svg
width={(MARK_HEIGHT * 23) / 16}
height={MARK_HEIGHT}
viewBox="0 0 23 16"
>
<Path d="M0 0H2.5A8 8 0 0 1 2.5 16H0Z" fill={color} />
<Path d="M23 0H20.5A8 8 0 0 0 20.5 16H23Z" fill={color} />
</Svg>
)
}
const LOSSLESS_PEAKS = [110, 215, 320]
const LOSSLESS_THIN_WIDTH = 24
const LOSSLESS_STROKES: [peak: number, d: string][] = [
[110, "M-70 320C-62 245 -38 166 0 166"],
[215, "M-70 320C-62 245 -38 166 0 166"],
[320, "M-30 185C-22 173 -11 166 0 166"],
[110, "M166 413C176 413 186 406 194 395"],
[215, "M166 413C200 413 226 340 240 275"],
[320, "M166 413C200 413 226 340 240 275"],
]
const LOSSLESS_FALL =
"M0 178L4 179L8 181L12 184L16 189L20 196L23 204L27 213L30 223L34 233L39 243L43 254L48 266L53 278L58 289L62 301L66 313L71 325L75 336L79 347L83 357L86 364L89 370L93 377L97 382L101 387L105 392L109 397L113 401L117 405L121 408L126 412L130 415L134 417L139 419L143 421L148 423L152 424L157 424L161 425L166 425L166 401L163 401L161 400L159 400L157 399L154 397L152 396L151 394L149 392L147 389L145 386L143 383L142 380L140 376L138 371L137 367L135 362L134 357L132 351L130 345L127 339L124 330L120 319L116 308L111 297L107 285L102 272L98 260L93 248L88 235L82 223L76 212L69 201L61 192L53 183L45 175L37 168L28 163L19 158L10 155L0 154Z"
const LOSSLESS_FALL_GAP =
"M0 178L4 180L6 184L8 190L9 197L11 203L14 210L17 219L21 228L24 238L29 248L33 259L38 270L43 282L47 293L52 305L56 317L60 328L64 340L68 351L73 361L76 368L79 375L83 381L87 387L91 393L95 398L100 403L104 408L109 412L113 416L118 419L124 422L130 424L135 425L141 426L147 426L152 426L157 425L161 425L166 425L166 401L163 401L161 399L159 398L158 395L157 393L156 390L155 387L155 384L154 381L153 379L152 376L150 373L149 369L148 366L146 361L145 357L143 352L142 347L140 341L137 335L134 326L130 315L126 304L122 293L117 281L113 268L108 256L103 243L98 231L92 219L86 207L79 196L71 186L62 177L53 168L44 161L32 157L21 155L10 154L0 154Z"
/**
* Three overlapping waves. Each thick downstroke is outlined in the
* background color so it reads as passing over the strokes behind it.
@@ -42,9 +48,9 @@ function LosslessMark({
}) {
return (
<Svg
width={LOSSLESS_WIDTH}
width={(MARK_HEIGHT * 544) / 271}
height={MARK_HEIGHT}
viewBox={LOSSLESS_VIEW_BOX}
viewBox="28 154 544 271"
>
{LOSSLESS_STROKES.map(([peak, d], index) => (
<Path
+97 -121
View File
@@ -38,7 +38,7 @@ import {
type SwipeRowGroup,
} from "@/components/ios-swipe-to-remove-row"
import { PlayerSymbol } from "@/components/player-symbol"
import { usePlaybackActions, usePlaybackQueue } from "@/contexts/playback"
import { usePlayback } from "@/contexts/playback"
import { useServerConfig } from "@/contexts/server-config"
import type { Id } from "@otunes/convex/_generated/dataModel"
import type { NowPlayingColors } from "@/lib/now-playing-colors"
@@ -110,16 +110,18 @@ export function NowPlayingQueue({
tablet?: boolean
fadeColor?: string
}) {
const { queueSongIds, queueHistorySongIds, shuffleEnabled, repeatEnabled } =
usePlaybackQueue()
const {
queueSongIds,
queueHistorySongIds,
shuffleEnabled,
repeatEnabled,
toggleShuffle,
cycleRepeat,
clearHistory,
reorderQueue,
skipToQueueIndex,
skipToHistoryIndex,
} = usePlaybackActions()
} = usePlayback()
const [viewportWidth, setViewportWidth] = useState(0)
const swipeGroup = useSwipeRowGroup()
const [scrollEnabled, setScrollEnabled] = useState(true)
@@ -305,126 +307,100 @@ export function NowPlayingQueue({
[drag]
)
const lastIndex = queueSongIds.length - 1
const renderItem = useCallback(
({ item }: { item: QueueListItem }) => {
switch (item.type) {
case "history-header":
return (
<View style={styles.historyHeader}>
<Text style={[styles.sectionTitle, { color: colors.foreground }]}>
History
</Text>
<Pressable onPress={() => void clearHistory()} hitSlop={10}>
<Text
style={[styles.clear, { color: colors.secondaryForeground }]}
>
Clear
</Text>
</Pressable>
</View>
)
case "history":
return (
<HistoryRow
songId={item.songId}
historyIndex={item.historyIndex}
colors={colors}
onPress={skipToHistoryIndex}
/>
)
case "current":
return <View style={styles.current}>{currentHeader}</View>
case "controls":
return (
<View
style={[
styles.controls,
{
height: itemHeight(item),
backgroundColor: tablet ? "transparent" : colors.background,
},
]}
>
{!tablet ? (
<View style={styles.toggles}>
<QueueToggle
colors={colors}
selected={shuffleEnabled}
label="Shuffle"
symbol="shuffle"
fallback="shuffle"
onPress={toggleShuffle}
/>
<QueueToggle
colors={colors}
selected={repeatEnabled !== "none"}
label={repeatEnabled === "once" ? "Repeat one" : "Repeat"}
symbol={repeatEnabled === "once" ? "repeat.1" : "repeat"}
fallback="repeat"
onPress={cycleRepeat}
/>
</View>
) : null}
{item.showTitle ? (
<Text
style={[styles.sectionTitle, { color: colors.foreground }]}
>
Continue Playing
</Text>
) : null}
</View>
)
case "upcoming":
return (
<UpcomingRow
songId={item.songId}
queueIndex={item.queueIndex}
lastIndex={lastIndex}
tablet={tablet}
colors={colors}
drag={drag}
swipeGroup={swipeGroup}
onPress={skipToQueueIndex}
onRemove={handleRemove}
onDragStart={handleDragStart}
onDrop={handleDrop}
/>
)
case "empty":
return (
<View style={[styles.empty, { height: item.height }]}>
const renderItem = ({ item }: { item: QueueListItem }) => {
switch (item.type) {
case "history-header":
return (
<View style={styles.historyHeader}>
<Text style={[styles.sectionTitle, { color: colors.foreground }]}>
History
</Text>
<Pressable onPress={() => void clearHistory()} hitSlop={10}>
<Text
style={[
styles.emptyText,
{ color: colors.secondaryForeground },
]}
style={[styles.clear, { color: colors.secondaryForeground }]}
>
Nothing else is queued.
Clear
</Text>
</View>
)
}
},
[
clearHistory,
colors,
currentHeader,
cycleRepeat,
drag,
handleDragStart,
handleDrop,
handleRemove,
lastIndex,
repeatEnabled,
shuffleEnabled,
skipToHistoryIndex,
skipToQueueIndex,
swipeGroup,
tablet,
toggleShuffle,
]
)
</Pressable>
</View>
)
case "history":
return (
<HistoryRow
songId={item.songId}
historyIndex={item.historyIndex}
colors={colors}
onPress={skipToHistoryIndex}
/>
)
case "current":
return <View style={styles.current}>{currentHeader}</View>
case "controls":
return (
<View
style={[
styles.controls,
{
height: itemHeight(item),
backgroundColor: tablet ? "transparent" : colors.background,
},
]}
>
{!tablet ? (
<View style={styles.toggles}>
<QueueToggle
colors={colors}
selected={shuffleEnabled}
label="Shuffle"
symbol="shuffle"
fallback="shuffle"
onPress={toggleShuffle}
/>
<QueueToggle
colors={colors}
selected={repeatEnabled !== "none"}
label={repeatEnabled === "once" ? "Repeat one" : "Repeat"}
symbol={repeatEnabled === "once" ? "repeat.1" : "repeat"}
fallback="repeat"
onPress={cycleRepeat}
/>
</View>
) : null}
{item.showTitle ? (
<Text style={[styles.sectionTitle, { color: colors.foreground }]}>
Continue Playing
</Text>
) : null}
</View>
)
case "upcoming":
return (
<UpcomingRow
songId={item.songId}
queueIndex={item.queueIndex}
lastIndex={queueSongIds.length - 1}
tablet={tablet}
colors={colors}
drag={drag}
swipeGroup={swipeGroup}
onPress={skipToQueueIndex}
onRemove={handleRemove}
onDragStart={handleDragStart}
onDrop={handleDrop}
/>
)
case "empty":
return (
<View style={[styles.empty, { height: item.height }]}>
<Text
style={[styles.emptyText, { color: colors.secondaryForeground }]}
>
Nothing else is queued.
</Text>
</View>
)
}
}
return (
<View
@@ -1,83 +0,0 @@
import { useState, type ReactNode } from "react"
import {
StyleSheet,
Text,
View,
type StyleProp,
type TextStyle,
} from "react-native"
import { PlayerSlider } from "@/components/player-slider"
import { usePlaybackActions, usePlaybackProgress } from "@/contexts/playback"
import { formatDuration, formatRemainingDuration } from "@/lib/format"
import type { NowPlayingColors } from "@/lib/now-playing-colors"
/**
* Position slider with elapsed and remaining labels. The only player piece
* subscribed to the playback clock, so the rest of the player stays still
* while audio plays.
*/
export function PlayerProgress({
colors,
center,
timeStyle,
}: {
colors: NowPlayingColors
/** Shown centered between the time labels. */
center?: ReactNode
timeStyle?: StyleProp<TextStyle>
}) {
const { currentTime, duration } = usePlaybackProgress()
const { seekTo } = usePlaybackActions()
const [scrubTime, setScrubTime] = useState<number | null>(null)
const time = scrubTime ?? currentTime
const timeColor =
scrubTime === null ? colors.secondaryForeground : colors.foreground
return (
<>
<PlayerSlider
accessibilityLabel="Playback position"
value={time}
maximumValue={duration}
fillColor={colors.sliderFill}
activeFillColor={colors.foreground}
trackColor={colors.sliderTrack}
onValueChange={setScrubTime}
onSlidingComplete={(value) => {
void seekTo(value)
setScrubTime(null)
}}
/>
<View style={styles.row}>
<Text style={[styles.time, timeStyle, { color: timeColor }]}>
{formatDuration(time)}
</Text>
{center ? (
<View style={styles.center} pointerEvents="none">
{center}
</View>
) : null}
<Text style={[styles.time, timeStyle, { color: timeColor }]}>
{formatRemainingDuration(time, duration).replace("-", "−")}
</Text>
</View>
</>
)
}
const styles = StyleSheet.create({
row: {
flexDirection: "row",
alignItems: "center",
justifyContent: "space-between",
marginTop: -4,
},
center: {
position: "absolute",
left: 0,
right: 0,
alignItems: "center",
},
time: { fontSize: 12, fontVariant: ["tabular-nums"] },
})
@@ -4,15 +4,19 @@ import { Stack } from "expo-router"
import { useDebouncedSearch } from "@/hooks/use-debounced-search"
import { useThemeColors } from "@/lib/theme"
import { filterSongs } from "@otunes/convex/client/song-search"
import type { BrowseSong } from "@otunes/convex/types"
export function usePlaylistSearch(songs: BrowseSong[]) {
const search = useDebouncedSearch()
const filteredSongs = useMemo(
() => filterSongs(songs, search.searchQuery),
[songs, search.searchQuery]
)
const filteredSongs = useMemo(() => {
const query = search.searchQuery.toLowerCase()
if (!query) return songs
return songs.filter((song) =>
[song.title, song.artist.name, song.album.title].some((value) =>
value.toLowerCase().includes(query)
)
)
}, [songs, search.searchQuery])
return { ...search, filteredSongs }
}
+61 -16
View File
@@ -1,5 +1,11 @@
import { useEffect, useState, type ComponentProps, type ReactNode } from "react"
import { Pressable, StyleSheet, View, useWindowDimensions } from "react-native"
import {
Pressable,
StyleSheet,
Text,
View,
useWindowDimensions,
} from "react-native"
import Animated, {
Easing,
ReduceMotion,
@@ -8,7 +14,7 @@ import Animated, {
useSharedValue,
withTiming,
} from "react-native-reanimated"
import { GestureDetector, type Gesture } from "react-native-gesture-handler"
import { GestureDetector, Gesture } from "react-native-gesture-handler"
import { useSafeAreaInsets } from "react-native-safe-area-context"
import { useRouter } from "expo-router"
import { StatusBar } from "expo-status-bar"
@@ -16,12 +22,12 @@ import { StatusBar } from "expo-status-bar"
import { AudioOutputButton } from "@/components/audio-output-button"
import { CoverImage } from "@/components/cover-image"
import { NowPlayingQueue } from "@/components/now-playing-queue"
import { PlayerProgress } from "@/components/player-progress"
import { PlayerSlider } from "@/components/player-slider"
import { PlayerSymbol } from "@/components/player-symbol"
import { usePlaybackActions, usePlaybackQueue } from "@/contexts/playback"
import { usePlayback } from "@/contexts/playback"
import { useServerConfig } from "@/contexts/server-config"
import { useSystemVolume } from "@/hooks/use-system-volume"
import { formatDuration, formatRemainingDuration } from "@/lib/format"
import type { NowPlayingColors } from "@/lib/now-playing-colors"
import { albumCoverUrl } from "@/lib/urls"
@@ -36,7 +42,7 @@ export function TabletNowPlaying({
colors,
backgroundStyle,
dismissStyle,
dismissGestures,
dismissGesture,
showQueue,
toggleQueue,
metadata,
@@ -44,10 +50,7 @@ export function TabletNowPlaying({
colors: NowPlayingColors
backgroundStyle: ComponentProps<typeof Animated.View>["style"]
dismissStyle: ComponentProps<typeof Animated.View>["style"]
dismissGestures: {
handle: ReturnType<typeof Gesture.Pan>
artwork: ReturnType<typeof Gesture.Pan>
}
dismissGesture: () => ReturnType<typeof Gesture.Pan>
showQueue: boolean
toggleQueue: () => void
metadata: ReactNode
@@ -56,11 +59,22 @@ export function TabletNowPlaying({
const insets = useSafeAreaInsets()
const { width, height } = useWindowDimensions()
const { config } = useServerConfig()
const { currentSong, isPlaying, shuffleEnabled, repeatEnabled } =
usePlaybackQueue()
const { setIsPlaying, skipNext, skipPrevious, toggleShuffle, cycleRepeat } =
usePlaybackActions()
const {
currentSong,
currentTime,
duration,
isPlaying,
setIsPlaying,
seekTo,
skipNext,
skipPrevious,
shuffleEnabled,
repeatEnabled,
toggleShuffle,
cycleRepeat,
} = usePlayback()
const { volume, setVolume, available: volumeAvailable } = useSystemVolume()
const [scrubTime, setScrubTime] = useState<number | null>(null)
const [queueHeight, setQueueHeight] = useState(0)
const wideLayout = width >= 700
const sideBySide = showQueue && wideLayout
@@ -99,6 +113,7 @@ export function TabletNowPlaying({
opacity: interpolate(progress.value, [0, 0.15, 1], [0, 0, 1]),
transform: [{ translateX: (1 - progress.value) * 48 }],
}))
const time = scrubTime ?? currentTime
if (!currentSong || !config) return null
const control = (
label: string,
@@ -135,7 +150,7 @@ export function TabletNowPlaying({
return (
<Animated.View style={[styles.root, backgroundStyle, dismissStyle]}>
<StatusBar style={colors.isDark ? "light" : "dark"} />
<GestureDetector gesture={dismissGestures.handle}>
<GestureDetector gesture={dismissGesture()}>
<Pressable
onPress={() => router.back()}
accessibilityRole="button"
@@ -149,7 +164,7 @@ export function TabletNowPlaying({
</GestureDetector>
<View style={styles.body}>
<Animated.View style={[styles.main, mainMotion]}>
<GestureDetector gesture={dismissGestures.artwork}>
<GestureDetector gesture={dismissGesture()}>
<View style={styles.artworkArea}>
<Animated.View style={artworkMotion}>
<CoverImage
@@ -162,7 +177,31 @@ export function TabletNowPlaying({
</GestureDetector>
<Animated.View style={[styles.controls, controlsMotion]}>
<View style={styles.meta}>{metadata}</View>
<PlayerProgress colors={colors} />
<PlayerSlider
accessibilityLabel="Playback position"
value={time}
maximumValue={duration}
fillColor={colors.sliderFill}
activeFillColor={colors.foreground}
trackColor={colors.sliderTrack}
onValueChange={setScrubTime}
onSlidingComplete={(value) => {
void seekTo(value)
setScrubTime(null)
}}
/>
<View style={styles.times}>
<Text
style={[styles.time, { color: colors.secondaryForeground }]}
>
{formatDuration(time)}
</Text>
<Text
style={[styles.time, { color: colors.secondaryForeground }]}
>
{formatRemainingDuration(time, duration).replace("-", "−")}
</Text>
</View>
<View style={styles.transport}>
{control(
"Shuffle",
@@ -337,6 +376,12 @@ const styles = StyleSheet.create({
},
controls: { width: "100%", maxWidth: 456, paddingHorizontal: 0 },
meta: { height: 48 },
times: {
flexDirection: "row",
justifyContent: "space-between",
marginTop: -4,
},
time: { fontSize: 12, fontVariant: ["tabular-nums"] },
transport: {
flexDirection: "row",
alignItems: "center",
+142 -89
View File
@@ -12,28 +12,22 @@ import {
import { useCachedQuery } from "@/hooks/use-cached-query"
import { api } from "@otunes/convex/_generated/api"
import { favoriteOptimisticUpdates } from "@otunes/convex/client/favorites"
import type { Id } from "@otunes/convex/_generated/dataModel"
import { showError } from "@/lib/toast"
/**
* `useMutation(...).withOptimisticUpdate(...)` with a stable identity. Pass a
* module-level update so it does not change between renders.
* `useMutation(...).withOptimisticUpdate(...)` with a stable identity. The
* update is captured on first render, so it must not close over render state.
*/
function useOptimisticMutation<Mutation extends FunctionReference<"mutation">>(
mutation: Mutation,
optimisticUpdate: OptimisticUpdate<FunctionArgs<Mutation>>
): ReactMutation<Mutation> {
const base = useMutation(mutation)
return useMemo(
() => base.withOptimisticUpdate(optimisticUpdate),
[base, optimisticUpdate]
)
const updateRef = useRef(optimisticUpdate)
return useMemo(() => base.withOptimisticUpdate(updateRef.current), [base])
}
type FavoriteTables = { song: "songs"; album: "albums"; artist: "artists" }
export type FavoriteKind = keyof FavoriteTables
type FavoritesContextValue = {
isSongFavorite: (songId: string) => boolean
isAlbumFavorite: (albumId: string) => boolean
@@ -42,20 +36,6 @@ type FavoritesContextValue = {
toggleAlbumFavorite: (albumId: Id<"albums">) => Promise<void>
toggleArtistFavorite: (artistId: Id<"artists">) => Promise<void>
favoriteSongs: (songIds: Id<"songs">[]) => Promise<void>
/** Idempotent; throws so callers like Siri can report failures. */
setFavorite: <Kind extends FavoriteKind>(
kind: Kind,
id: Id<FavoriteTables[Kind]>,
favorite: boolean
) => Promise<void>
/** Throws so callers like Siri can report failures. */
addFavoriteSongs: (songIds: Id<"songs">[]) => Promise<void>
}
function showFavoritesError(error: unknown) {
showError(
error instanceof Error ? error.message : "Failed to update favorites"
)
}
const FavoritesContext = createContext<FavoritesContextValue | null>(null)
@@ -72,31 +52,110 @@ export function FavoritesProvider({ children }: { children: ReactNode }) {
const favorites = useCachedQuery(api.favorites.getUserFavorites)
const favoriteSong = useOptimisticMutation(
api.favorites.favoriteSong,
favoriteOptimisticUpdates.favoriteSong
(store, { songId }) => {
const current = store.getQuery(api.favorites.getUserFavorites, {})
if (!current || current.favoriteSongs.includes(songId)) return
store.setQuery(
api.favorites.getUserFavorites,
{},
{
...current,
favoriteSongs: [...current.favoriteSongs, songId],
}
)
}
)
const unfavoriteSong = useOptimisticMutation(
api.favorites.unfavoriteSong,
favoriteOptimisticUpdates.unfavoriteSong
(store, { songId }) => {
const current = store.getQuery(api.favorites.getUserFavorites, {})
if (!current) return
store.setQuery(
api.favorites.getUserFavorites,
{},
{
...current,
favoriteSongs: current.favoriteSongs.filter((id) => id !== songId),
}
)
}
)
const favoriteAlbum = useOptimisticMutation(
api.favorites.favoriteAlbum,
favoriteOptimisticUpdates.favoriteAlbum
(store, { albumId }) => {
const current = store.getQuery(api.favorites.getUserFavorites, {})
if (!current || current.favoriteAlbums.includes(albumId)) return
store.setQuery(
api.favorites.getUserFavorites,
{},
{
...current,
favoriteAlbums: [...current.favoriteAlbums, albumId],
}
)
}
)
const unfavoriteAlbum = useOptimisticMutation(
api.favorites.unfavoriteAlbum,
favoriteOptimisticUpdates.unfavoriteAlbum
(store, { albumId }) => {
const current = store.getQuery(api.favorites.getUserFavorites, {})
if (!current) return
store.setQuery(
api.favorites.getUserFavorites,
{},
{
...current,
favoriteAlbums: current.favoriteAlbums.filter((id) => id !== albumId),
}
)
}
)
const favoriteArtist = useOptimisticMutation(
api.favorites.favoriteArtist,
favoriteOptimisticUpdates.favoriteArtist
(store, { artistId }) => {
const current = store.getQuery(api.favorites.getUserFavorites, {})
if (!current || current.favoriteArtists.includes(artistId)) return
store.setQuery(
api.favorites.getUserFavorites,
{},
{
...current,
favoriteArtists: [...current.favoriteArtists, artistId],
}
)
}
)
const unfavoriteArtist = useOptimisticMutation(
api.favorites.unfavoriteArtist,
favoriteOptimisticUpdates.unfavoriteArtist
(store, { artistId }) => {
const current = store.getQuery(api.favorites.getUserFavorites, {})
if (!current) return
store.setQuery(
api.favorites.getUserFavorites,
{},
{
...current,
favoriteArtists: current.favoriteArtists.filter(
(id) => id !== artistId
),
}
)
}
)
const favoriteSongsMutation = useOptimisticMutation(
api.favorites.favoriteSongs,
favoriteOptimisticUpdates.favoriteSongs
(store, { songIds }) => {
const current = store.getQuery(api.favorites.getUserFavorites, {})
if (!current) return
store.setQuery(
api.favorites.getUserFavorites,
{},
{
...current,
favoriteSongs: [...new Set([...current.favoriteSongs, ...songIds])],
}
)
}
)
const songIds = useMemo(
@@ -114,72 +173,70 @@ export function FavoritesProvider({ children }: { children: ReactNode }) {
const idsRef = useRef({ songIds, albumIds, artistIds })
idsRef.current = { songIds, albumIds, artistIds }
const setFavorite = useCallback(
async <Kind extends FavoriteKind>(
kind: Kind,
id: Id<FavoriteTables[Kind]>,
favorite: boolean
) => {
if (kind === "song") {
const songId = id as Id<"songs">
await (favorite ? favoriteSong : unfavoriteSong)({ songId })
} else if (kind === "album") {
const albumId = id as Id<"albums">
await (favorite ? favoriteAlbum : unfavoriteAlbum)({ albumId })
} else {
const artistId = id as Id<"artists">
await (favorite ? favoriteArtist : unfavoriteArtist)({ artistId })
const toggleSongFavorite = useCallback(
async (songId: Id<"songs">) => {
try {
if (idsRef.current.songIds.has(songId)) {
await unfavoriteSong({ songId })
} else {
await favoriteSong({ songId })
}
} catch (error) {
showError(
error instanceof Error ? error.message : "Failed to update favorites"
)
}
},
[
favoriteSong,
unfavoriteSong,
favoriteAlbum,
unfavoriteAlbum,
favoriteArtist,
unfavoriteArtist,
]
)
const toggleSongFavorite = useCallback(
(songId: Id<"songs">) =>
setFavorite("song", songId, !idsRef.current.songIds.has(songId)).catch(
showFavoritesError
),
[setFavorite]
[favoriteSong, unfavoriteSong]
)
const toggleAlbumFavorite = useCallback(
(albumId: Id<"albums">) =>
setFavorite(
"album",
albumId,
!idsRef.current.albumIds.has(albumId)
).catch(showFavoritesError),
[setFavorite]
async (albumId: Id<"albums">) => {
try {
if (idsRef.current.albumIds.has(albumId)) {
await unfavoriteAlbum({ albumId })
} else {
await favoriteAlbum({ albumId })
}
} catch (error) {
showError(
error instanceof Error ? error.message : "Failed to update favorites"
)
}
},
[favoriteAlbum, unfavoriteAlbum]
)
const toggleArtistFavorite = useCallback(
(artistId: Id<"artists">) =>
setFavorite(
"artist",
artistId,
!idsRef.current.artistIds.has(artistId)
).catch(showFavoritesError),
[setFavorite]
)
const addFavoriteSongs = useCallback(
async (ids: Id<"songs">[]) => {
const unique = [...new Set(ids)]
if (unique.length > 0) await favoriteSongsMutation({ songIds: unique })
async (artistId: Id<"artists">) => {
try {
if (idsRef.current.artistIds.has(artistId)) {
await unfavoriteArtist({ artistId })
} else {
await favoriteArtist({ artistId })
}
} catch (error) {
showError(
error instanceof Error ? error.message : "Failed to update favorites"
)
}
},
[favoriteSongsMutation]
[favoriteArtist, unfavoriteArtist]
)
const favoriteSongs = useCallback(
(ids: Id<"songs">[]) => addFavoriteSongs(ids).catch(showFavoritesError),
[addFavoriteSongs]
async (ids: Id<"songs">[]) => {
const unique = [...new Set(ids)]
if (unique.length === 0) return
try {
await favoriteSongsMutation({ songIds: unique })
} catch (error) {
showError(
error instanceof Error ? error.message : "Failed to update favorites"
)
}
},
[favoriteSongsMutation]
)
const value = useMemo(
@@ -191,8 +248,6 @@ export function FavoritesProvider({ children }: { children: ReactNode }) {
toggleAlbumFavorite,
toggleArtistFavorite,
favoriteSongs,
setFavorite,
addFavoriteSongs,
}),
[
songIds,
@@ -202,8 +257,6 @@ export function FavoritesProvider({ children }: { children: ReactNode }) {
toggleAlbumFavorite,
toggleArtistFavorite,
favoriteSongs,
setFavorite,
addFavoriteSongs,
]
)
+15 -1
View File
@@ -1,5 +1,4 @@
import { useAuthToken } from "@convex-dev/auth/react"
import { userIdFromToken } from "@otunes/convex/client/auth-token"
import { createContext, useContext, useMemo, type ReactNode } from "react"
import { loadPersistentCache } from "@/lib/persistent-cache"
@@ -10,6 +9,21 @@ const CacheScopeContext = createContext(ANONYMOUS_CACHE_SCOPE)
void loadPersistentCache()
function userIdFromToken(token: string | null) {
const payload = token?.split(".")[1]
if (!payload) {
return null
}
try {
const json = atob(payload.replace(/-/g, "+").replace(/_/g, "/"))
const subject = (JSON.parse(json) as { sub?: unknown }).sub
return typeof subject === "string" ? (subject.split("|")[0] ?? null) : null
} catch {
return null
}
}
/** Namespaces persisted client data by server and signed-in user. */
export function PersistentCacheProvider({
serverUrl,
+33 -23
View File
@@ -97,8 +97,6 @@ type PlaybackContextValue = {
type PlaybackActionsValue = Pick<
PlaybackContextValue,
| "seekTo"
| "waitForPlaybackReady"
| "setIsPlaying"
| "playInOrder"
| "playShuffled"
@@ -138,11 +136,6 @@ type PlaybackQueueValue = Pick<
| "repeatEnabled"
>
type PlaybackProgressValue = Pick<
PlaybackContextValue,
"currentTime" | "duration"
>
type CollectionPlayers = Pick<
PlaybackActionsValue,
| "playAlbum"
@@ -164,17 +157,14 @@ type CollectionPlay = {
recorded: RecordedPlay
}
const PlaybackContext = createContext<PlaybackContextValue | null>(null)
const PlaybackActionsContext = createContext<PlaybackActionsValue | null>(null)
const PlaybackQueueContext = createContext<PlaybackQueueValue | null>(null)
const PlaybackProgressContext = createContext<PlaybackProgressValue | null>(
null
)
/** Ticks while audio plays. Keep consumers small (slider and time labels). */
export function usePlaybackProgress() {
const context = useContext(PlaybackProgressContext)
export function usePlayback() {
const context = useContext(PlaybackContext)
if (!context) {
throw new Error("usePlaybackProgress must be used within PlaybackProvider")
throw new Error("usePlayback must be used within PlaybackProvider")
}
return context
}
@@ -641,8 +631,6 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
const actionsValue = useMemo<PlaybackActionsValue>(
() => ({
seekTo,
waitForPlaybackReady,
...collectionPlayers,
setIsPlaying,
playInOrder,
@@ -683,14 +671,12 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
playInOrder,
playShuffled,
reorderQueue,
seekTo,
setIsPlaying,
skipNext,
skipPrevious,
skipToHistoryIndex,
skipToQueueIndex,
toggleShuffle,
waitForPlaybackReady,
]
)
@@ -715,17 +701,41 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
]
)
const progressValue = useMemo<PlaybackProgressValue>(
() => ({ currentTime, duration }),
[currentTime, duration]
const value = useMemo<PlaybackContextValue>(
() => ({
...actionsValue,
waitForPlaybackReady,
queueSongIds,
queueHistorySongIds,
currentSong,
shuffleEnabled: playback.shuffleEnabled,
repeatEnabled,
isPlaying: isPlaybackActive,
currentTime,
duration,
seekTo,
}),
[
actionsValue,
waitForPlaybackReady,
currentSong,
currentTime,
duration,
isPlaybackActive,
playback.shuffleEnabled,
queueHistorySongIds,
queueSongIds,
repeatEnabled,
seekTo,
]
)
return (
<PlaybackActionsContext.Provider value={actionsValue}>
<PlaybackQueueContext.Provider value={queueValue}>
<PlaybackProgressContext.Provider value={progressValue}>
<PlaybackContext.Provider value={value}>
{children}
</PlaybackProgressContext.Provider>
</PlaybackContext.Provider>
</PlaybackQueueContext.Provider>
</PlaybackActionsContext.Provider>
)
+20 -17
View File
@@ -1,4 +1,4 @@
import { atmosEnabled } from "@/lib/playback-capabilities"
import { dolbyAtmosSupported } from "@/lib/playback-capabilities"
import {
createAudioPlaylist,
setAudioModeAsync,
@@ -113,8 +113,6 @@ export function useNativePlaylistSync({
measuredDuration > 0
? measuredDuration
: (currentSong?.durationSeconds ?? 0)
const durationRef = useRef(duration)
durationRef.current = duration
const queueSyncKey = useMemo(
() => JSON.stringify(queueSongIds),
[queueSongIds]
@@ -134,7 +132,11 @@ export function useNativePlaylistSync({
])
: ""
const userPlaybackKey = user
? JSON.stringify([user._id, user.playbackQuality, atmosEnabled(user)])
? JSON.stringify([
user._id,
user.playbackQuality,
dolbyAtmosSupported && (user.preferAtmos ?? false),
])
: ""
const settleWaiters = useCallback(() => {
@@ -411,17 +413,18 @@ export function useNativePlaylistSync({
[startPlayer]
)
// Reads duration from a ref so seekTo, and every action built on it, stays
// stable across track changes.
const seekTo = useCallback(async (seconds: number) => {
const playlist = playlistRef.current
if (!playlist) return
const maxDuration = playlist.duration || durationRef.current || 0
const clamped = Math.max(0, Math.min(seconds, maxDuration))
await playlist.seekTo(clamped)
currentTimeRef.current = clamped
setCurrentTime(clamped)
}, [])
const seekTo = useCallback(
async (seconds: number) => {
const playlist = playlistRef.current
if (!playlist) return
const maxDuration = playlist.duration || duration || 0
const clamped = Math.max(0, Math.min(seconds, maxDuration))
await playlist.seekTo(clamped)
currentTimeRef.current = clamped
setCurrentTime(clamped)
},
[duration]
)
const handlePlaybackLoadFailure = useCallback(() => {
autoPlayNextRef.current = false
@@ -459,7 +462,7 @@ export function useNativePlaylistSync({
}
if (!isCurrentLoad()) return
const loadKey = `${user.playbackQuality}:${atmosEnabled(user)}:${cookieHeader}`
const loadKey = `${user.playbackQuality}:${dolbyAtmosSupported && (user.preferAtmos ?? false)}:${cookieHeader}`
const needsRebuild =
loadedLoadKeyRef.current !== loadKey ||
nativePlaylistNeedsRebuild(
@@ -500,7 +503,7 @@ export function useNativePlaylistSync({
const plan = playbackStreamPlan(
song.files,
user.playbackQuality,
atmosEnabled(user)
dolbyAtmosSupported && (user.preferAtmos ?? false)
)
sources.set(
song.id,
-35
View File
@@ -5,7 +5,6 @@ import { publishMusicCatalog } from "@/lib/app-intents"
export const FAVORITES_PLAYLIST_ID = "otunes:favorites"
const HANDLED_KEY = "otunes:handled-app-intents"
const CATALOG_CURSORS_KEY = "otunes:app-intent-catalog-cursors"
const MAX_HANDLED = 200
const CATALOG_KINDS = [
@@ -21,7 +20,6 @@ export async function clearAppIntentCatalogs() {
return
}
const results = await Promise.allSettled([
AsyncStorage.removeItem(CATALOG_CURSORS_KEY),
AppIntents.deleteDonationsAsync({ intent: "playMusic" }),
AppIntents.clearPendingInvocationsAsync(),
...CATALOG_KINDS.map((kind) => publishMusicCatalog(kind, [])),
@@ -30,39 +28,6 @@ export async function clearAppIntentCatalogs() {
if (failed?.status === "rejected") throw failed.reason
}
/**
* Server `head` values the disk catalog is synced to, per library kind. A
* missing kind means it has never synced and needs a full download.
*/
export type CatalogCursors = Partial<
Record<"song" | "album" | "artist", number>
>
/** Cursors for `scope`; another server or user's cursors read as empty. */
export async function loadCatalogCursors(
scope: string
): Promise<CatalogCursors> {
try {
const raw = await AsyncStorage.getItem(CATALOG_CURSORS_KEY)
const parsed = raw
? (JSON.parse(raw) as { scope?: unknown; cursors?: CatalogCursors })
: null
return parsed?.scope === scope ? (parsed.cursors ?? {}) : {}
} catch {
return {}
}
}
export async function saveCatalogCursors(
scope: string,
cursors: CatalogCursors
) {
await AsyncStorage.setItem(
CATALOG_CURSORS_KEY,
JSON.stringify({ scope, cursors })
)
}
export async function loadHandledIntentIds() {
const raw = await AsyncStorage.getItem(HANDLED_KEY)
if (!raw) {
-7
View File
@@ -50,13 +50,6 @@ export function publishMusicCatalog(kind: string, entities: AppIntentEntity[]) {
return write
}
/** The full disk catalog for `kind`, as last published. */
export async function readMusicCatalog(kind: string) {
await publishing.catch(() => undefined)
if (!native || !AppIntents.isAvailable()) return []
return JSON.parse(await native.catalogJSON(kind)) as AppIntentEntity[]
}
export async function completeMusicInvocation(
token: string,
error: string | null
+38 -7
View File
@@ -1,9 +1,40 @@
import type { MediaShareTarget } from "@otunes/convex/client/media-links"
export type MediaShareContext = (
| { type: "album"; albumId: string }
| { type: "song"; albumId: string; songId: string }
| { type: "playlist"; playlistId: string; isFavorites?: boolean }
| { type: "artist"; artistId: string }
) & { title: string }
export {
mediaShareUrl,
shouldShowShare,
} from "@otunes/convex/client/media-links"
export function shouldShowShare(
share: MediaShareContext | undefined,
selectedCount: number
) {
if (!share) {
return false
}
if (share.type === "song") {
return selectedCount === 1
}
return true
}
/** A share target plus the title shown in the native share sheet. */
export type MediaShareContext = MediaShareTarget & { title: string }
export function mediaShareUrl(
serverUrl: string,
share: MediaShareContext
): string {
const origin = serverUrl.replace(/\/+$/, "")
switch (share.type) {
case "album":
return `${origin}/album/${share.albumId}`
case "song":
return `${origin}/album/${share.albumId}?s=${share.songId}`
case "playlist":
if (share.isFavorites) {
return `${origin}/playlist/favorites`
}
return `${origin}/playlist/${share.playlistId}`
case "artist":
return `${origin}/artist/${share.artistId}`
}
}
-5
View File
@@ -2,8 +2,3 @@ import { Platform } from "react-native"
// Android Dolby decoder reports do not reliably indicate working Atmos playback.
export const dolbyAtmosSupported = Platform.OS === "ios"
/** Whether to stream Atmos for this user: they opted in and the device can play it. */
export function atmosEnabled(user: { preferAtmos?: boolean }) {
return dolbyAtmosSupported && (user.preferAtmos ?? false)
}
+4 -4
View File
@@ -5,10 +5,8 @@ import type { Id } from "@otunes/convex/_generated/dataModel"
import type { BrowseSong } from "@otunes/convex/types"
import {
getCachedSong,
getSongSnapshot,
getSongsVersion,
requestSongs,
songFromSnapshot,
subscribeSongs,
} from "@otunes/convex/client/song-cache"
import { isPersistentCacheLoaded } from "@/lib/persistent-cache"
@@ -52,10 +50,12 @@ export function useSongs(songIds: readonly Id<"songs">[]) {
*/
export function useSong(songId: Id<"songs"> | null): BrowseSong | null {
const convex = useConvex()
// While the song is missing the snapshot is the cache version, so every
// cache change re-renders and retries the fetch below.
const snapshot = useSyncExternalStore(subscribeSongs, () =>
getSongSnapshot(songId)
songId ? (getCachedSong(songId) ?? getSongsVersion()) : null
)
const song = songFromSnapshot(snapshot)
const song = typeof snapshot === "object" ? snapshot : null
useEffect(() => {
if (songId && !song && isPersistentCacheLoaded()) {
+33 -11
View File
@@ -1,11 +1,9 @@
import {
type PlaybackQuality,
songHlsPlaylistPath,
songPlayPath,
} from "@otunes/convex/playback-stream"
import { dolbyAtmosSupported } from "@/lib/playback-capabilities"
import { Platform } from "react-native"
import type { AudioSource } from "expo-audio"
export type PlaybackQuality = "low" | "medium" | "high" | "lossless"
export const coverImageSizes = [64, 128, 192, 256, 384, 512, 768, 1024] as const
export type CoverImageSize = (typeof coverImageSizes)[number]
@@ -47,6 +45,33 @@ export function playlistCoverUrl(
return sizedCoverUrl(`${serverUrl}/api/playlist/${playlistId}/cover`, size)
}
export function songPlayUrl(
serverUrl: string,
songId: string,
quality: PlaybackQuality | undefined,
preferAtmos: boolean | undefined
) {
const params = new URLSearchParams()
if (Platform.OS === "ios") params.set("losslessFormat", "alac")
if (quality) params.set("quality", quality)
if (dolbyAtmosSupported && preferAtmos) params.set("preferAtmos", "true")
const query = params.toString()
return `${serverUrl}/api/song/${songId}/play${query ? `?${query}` : ""}`
}
export function songHlsPlaylistUrl(
serverUrl: string,
songId: string,
quality: PlaybackQuality | undefined,
preferAtmos: boolean | undefined
) {
const params = new URLSearchParams()
if (quality) params.set("quality", quality)
if (dolbyAtmosSupported && preferAtmos) params.set("preferAtmos", "true")
const query = params.toString()
return `${serverUrl}/api/song/${songId}/play/hls${query ? `?${query}` : ""}`
}
export function songAudioSource(
serverUrl: string,
songId: string,
@@ -55,12 +80,9 @@ export function songAudioSource(
useHls: boolean,
options?: { cookieHeader?: string | null; songTitle?: string }
): AudioSource {
const path = useHls
? songHlsPlaylistPath(songId, quality, preferAtmos)
: songPlayPath(songId, quality, preferAtmos, {
losslessFormat: Platform.OS === "ios" ? "alac" : undefined,
})
const uri = `${serverUrl}${path}`
const uri = useHls
? songHlsPlaylistUrl(serverUrl, songId, quality, preferAtmos)
: songPlayUrl(serverUrl, songId, quality, preferAtmos)
const headers = options?.cookieHeader
? { Cookie: options.cookieHeader }
+27 -14
View File
@@ -1,7 +1,3 @@
import {
parsePlaybackQuality,
playbackStreamPlan,
} from "@otunes/convex/playback-stream"
import { audioContentTypeForPath } from "@/lib/audio-content-type"
import { seekableLosslessFile } from "@/lib/server-lossless"
import { getSongSource } from "@/lib/server-song"
@@ -122,17 +118,34 @@ export async function GET(
try {
const song = await getSongSource(id)
const {
source: originalCodec,
kbps,
transcodeNeeded,
} = playbackStreamPlan(
song.files,
// Without a quality, serve the original file as this route always has.
parsePlaybackQuality(request.nextUrl.searchParams.get("quality")) ??
"lossless",
const quality = request.nextUrl.searchParams.get("quality") as
"low" | "medium" | "high" | "lossless" | null
const preferAtmos =
request.nextUrl.searchParams.get("preferAtmos") === "true"
)
let kbps = 0
if (quality === "low") {
kbps = 64
} else if (quality === "medium") {
kbps = 128
} else if (quality === "high") {
kbps = 256
}
let originalCodec = "lossless" in song.files ? "lossless" : "normal"
let transcodeNeeded = true
if (preferAtmos && "atmos" in song.files) {
originalCodec = "atmos"
transcodeNeeded = false
} else if (kbps === 0 && "lossless" in song.files) {
originalCodec = "lossless"
transcodeNeeded = false
} else if (kbps === 0 || (kbps === 256 && "normal" in song.files)) {
originalCodec = "normal"
transcodeNeeded = false
}
if (!transcodeNeeded && originalCodec === "lossless") {
// Native iOS playback does not identify itself as Safari.
+19 -3
View File
@@ -24,6 +24,8 @@ import {
import { TrackRow } from "@/components/track-row"
import { Button } from "@/components/ui/button"
import { ScrollArea } from "@/components/ui/scroll-area"
import { useCachedQuery } from "@/hooks/use-cached-query"
import { api } from "@otunes/convex/_generated/api"
import {
useMediaActions,
useTrackListMediaAction,
@@ -114,6 +116,20 @@ export function AlbumView({
[album.tracks]
)
// Older servers do not include album format availability yet.
const formatTracks = useCachedQuery(
api.library.getSongsForPlayback,
album.hasLossless === undefined || album.hasAtmos === undefined
? { ids: albumSongIds }
: "skip"
)
const hasLossless =
album.hasLossless ??
formatTracks?.some((track) => track && "lossless" in track.files)
const hasAtmos =
album.hasAtmos ??
formatTracks?.some((track) => track && "atmos" in track.files)
React.useEffect(() => {
rememberSongs(albumDetailSongs(album))
}, [album])
@@ -244,15 +260,15 @@ export function AlbumView({
</Link>
<div className="flex flex-wrap items-center justify-center gap-2 text-sm text-muted-foreground sm:justify-start">
{metadata ? <span>{metadata}</span> : null}
{album.hasLossless ? (
{hasLossless ? (
<>
{metadata ? <span aria-hidden>·</span> : null}
<AudioFormatBadge format="lossless" />
</>
) : null}
{album.hasAtmos ? (
{hasAtmos ? (
<>
{metadata || album.hasLossless ? (
{metadata || hasLossless ? (
<span aria-hidden>·</span>
) : null}
<AudioFormatBadge format="atmos" />
+26 -19
View File
@@ -1,28 +1,35 @@
import {
DOLBY_PATHS,
DOLBY_VIEW_BOX,
DOLBY_WIDTH,
LOSSLESS_FALL,
LOSSLESS_FALL_GAP,
LOSSLESS_PEAKS,
LOSSLESS_STROKES,
LOSSLESS_THIN_WIDTH,
LOSSLESS_VIEW_BOX,
LOSSLESS_WIDTH,
MARK_HEIGHT,
} from "@otunes/convex/client/audio-format-marks"
import type { PlaybackFormat } from "@otunes/convex/playback-stream"
const MARK_HEIGHT = 11
function DolbyMark({ color }: { color: string }) {
return (
<svg width={DOLBY_WIDTH} height={MARK_HEIGHT} viewBox={DOLBY_VIEW_BOX}>
{DOLBY_PATHS.map((d) => (
<path key={d} d={d} fill={color} />
))}
<svg
width={(MARK_HEIGHT * 23) / 16}
height={MARK_HEIGHT}
viewBox="0 0 23 16"
>
<path d="M0 0H2.5A8 8 0 0 1 2.5 16H0Z" fill={color} />
<path d="M23 0H20.5A8 8 0 0 0 20.5 16H23Z" fill={color} />
</svg>
)
}
const LOSSLESS_PEAKS = [110, 215, 320]
const LOSSLESS_THIN_WIDTH = 24
const LOSSLESS_STROKES: [peak: number, d: string][] = [
[110, "M-70 320C-62 245 -38 166 0 166"],
[215, "M-70 320C-62 245 -38 166 0 166"],
[320, "M-30 185C-22 173 -11 166 0 166"],
[110, "M166 413C176 413 186 406 194 395"],
[215, "M166 413C200 413 226 340 240 275"],
[320, "M166 413C200 413 226 340 240 275"],
]
const LOSSLESS_FALL =
"M0 178L4 179L8 181L12 184L16 189L20 196L23 204L27 213L30 223L34 233L39 243L43 254L48 266L53 278L58 289L62 301L66 313L71 325L75 336L79 347L83 357L86 364L89 370L93 377L97 382L101 387L105 392L109 397L113 401L117 405L121 408L126 412L130 415L134 417L139 419L143 421L148 423L152 424L157 424L161 425L166 425L166 401L163 401L161 400L159 400L157 399L154 397L152 396L151 394L149 392L147 389L145 386L143 383L142 380L140 376L138 371L137 367L135 362L134 357L132 351L130 345L127 339L124 330L120 319L116 308L111 297L107 285L102 272L98 260L93 248L88 235L82 223L76 212L69 201L61 192L53 183L45 175L37 168L28 163L19 158L10 155L0 154Z"
const LOSSLESS_FALL_GAP =
"M0 178L4 180L6 184L8 190L9 197L11 203L14 210L17 219L21 228L24 238L29 248L33 259L38 270L43 282L47 293L52 305L56 317L60 328L64 340L68 351L73 361L76 368L79 375L83 381L87 387L91 393L95 398L100 403L104 408L109 412L113 416L118 419L124 422L130 424L135 425L141 426L147 426L152 426L157 425L161 425L166 425L166 401L163 401L161 399L159 398L158 395L157 393L156 390L155 387L155 384L154 381L153 379L152 376L150 373L149 369L148 366L146 361L145 357L143 352L142 347L140 341L137 335L134 326L130 315L126 304L122 293L117 281L113 268L108 256L103 243L98 231L92 219L86 207L79 196L71 186L62 177L53 168L44 161L32 157L21 155L10 154L0 154Z"
/**
* Three overlapping waves. Each thick downstroke is outlined in the
* background color so it reads as passing over the strokes behind it.
@@ -36,9 +43,9 @@ function LosslessMark({
}) {
return (
<svg
width={LOSSLESS_WIDTH}
width={(MARK_HEIGHT * 544) / 271}
height={MARK_HEIGHT}
viewBox={LOSSLESS_VIEW_BOX}
viewBox="28 154 544 271"
>
{LOSSLESS_STROKES.map(([peak, d], index) => (
<path
+92 -53
View File
@@ -1,31 +1,13 @@
"use client"
import * as React from "react"
import type { OptimisticUpdate } from "convex/browser"
import { useMutation, type ReactMutation } from "convex/react"
import type { FunctionArgs, FunctionReference } from "convex/server"
import { useMutation } from "convex/react"
import { toast } from "sonner"
import { useCachedQuery } from "@/hooks/use-cached-query"
import { api } from "@otunes/convex/_generated/api"
import { favoriteOptimisticUpdates } from "@otunes/convex/client/favorites"
import type { Id } from "@otunes/convex/_generated/dataModel"
/**
* `useMutation(...).withOptimisticUpdate(...)` with a stable identity. Pass a
* module-level update so it does not change between renders.
*/
function useOptimisticMutation<Mutation extends FunctionReference<"mutation">>(
mutation: Mutation,
optimisticUpdate: OptimisticUpdate<FunctionArgs<Mutation>>
): ReactMutation<Mutation> {
const base = useMutation(mutation)
return React.useMemo(
() => base.withOptimisticUpdate(optimisticUpdate),
[base, optimisticUpdate]
)
}
type FavoritesContextValue = {
isSongFavorite: (songId: string) => boolean
isAlbumFavorite: (albumId: string) => boolean
@@ -52,38 +34,97 @@ export function useFavorites() {
export function FavoritesProvider({ children }: { children: React.ReactNode }) {
const favorites = useCachedQuery(api.favorites.getUserFavorites)
const favoriteSong = useOptimisticMutation(
api.favorites.favoriteSong,
favoriteOptimisticUpdates.favoriteSong
)
const favoriteSongsMutation = useOptimisticMutation(
api.favorites.favoriteSongs,
favoriteOptimisticUpdates.favoriteSongs
)
const unfavoriteSong = useOptimisticMutation(
api.favorites.unfavoriteSong,
favoriteOptimisticUpdates.unfavoriteSong
)
const unfavoriteSongs = useOptimisticMutation(
api.favorites.unfavoriteSongs,
favoriteOptimisticUpdates.unfavoriteSongs
)
const favoriteAlbum = useOptimisticMutation(
api.favorites.favoriteAlbum,
favoriteOptimisticUpdates.favoriteAlbum
)
const unfavoriteAlbum = useOptimisticMutation(
api.favorites.unfavoriteAlbum,
favoriteOptimisticUpdates.unfavoriteAlbum
)
const favoriteArtist = useOptimisticMutation(
api.favorites.favoriteArtist,
favoriteOptimisticUpdates.favoriteArtist
)
const unfavoriteArtist = useOptimisticMutation(
api.favorites.unfavoriteArtist,
favoriteOptimisticUpdates.unfavoriteArtist
const favoriteSong = useMutation(api.favorites.favoriteSong).withOptimisticUpdate(
(store, { songId }) => {
const current = store.getQuery(api.favorites.getUserFavorites, {})
if (current && !current.favoriteSongs.includes(songId)) {
store.setQuery(api.favorites.getUserFavorites, {}, {
...current,
favoriteSongs: [...current.favoriteSongs, songId],
})
}
}
)
const favoriteSongsMutation = useMutation(
api.favorites.favoriteSongs
).withOptimisticUpdate((store, { songIds }) => {
const current = store.getQuery(api.favorites.getUserFavorites, {})
if (current) {
store.setQuery(api.favorites.getUserFavorites, {}, {
...current,
favoriteSongs: [...new Set([...current.favoriteSongs, ...songIds])],
})
}
})
const unfavoriteSong = useMutation(
api.favorites.unfavoriteSong
).withOptimisticUpdate((store, { songId }) => {
const current = store.getQuery(api.favorites.getUserFavorites, {})
if (current) {
store.setQuery(api.favorites.getUserFavorites, {}, {
...current,
favoriteSongs: current.favoriteSongs.filter((id) => id !== songId),
})
}
})
const unfavoriteSongs = useMutation(
api.favorites.unfavoriteSongs
).withOptimisticUpdate((store, { songIds }) => {
const current = store.getQuery(api.favorites.getUserFavorites, {})
if (current) {
const removed = new Set(songIds)
store.setQuery(api.favorites.getUserFavorites, {}, {
...current,
favoriteSongs: current.favoriteSongs.filter((id) => !removed.has(id)),
})
}
})
const favoriteAlbum = useMutation(
api.favorites.favoriteAlbum
).withOptimisticUpdate((store, { albumId }) => {
const current = store.getQuery(api.favorites.getUserFavorites, {})
if (current && !current.favoriteAlbums.includes(albumId)) {
store.setQuery(api.favorites.getUserFavorites, {}, {
...current,
favoriteAlbums: [...current.favoriteAlbums, albumId],
})
}
})
const unfavoriteAlbum = useMutation(
api.favorites.unfavoriteAlbum
).withOptimisticUpdate((store, { albumId }) => {
const current = store.getQuery(api.favorites.getUserFavorites, {})
if (current) {
store.setQuery(api.favorites.getUserFavorites, {}, {
...current,
favoriteAlbums: current.favoriteAlbums.filter((id) => id !== albumId),
})
}
})
const favoriteArtist = useMutation(
api.favorites.favoriteArtist
).withOptimisticUpdate((store, { artistId }) => {
const current = store.getQuery(api.favorites.getUserFavorites, {})
if (current && !current.favoriteArtists.includes(artistId)) {
store.setQuery(api.favorites.getUserFavorites, {}, {
...current,
favoriteArtists: [...current.favoriteArtists, artistId],
})
}
})
const unfavoriteArtist = useMutation(
api.favorites.unfavoriteArtist
).withOptimisticUpdate((store, { artistId }) => {
const current = store.getQuery(api.favorites.getUserFavorites, {})
if (current) {
store.setQuery(api.favorites.getUserFavorites, {}, {
...current,
favoriteArtists: current.favoriteArtists.filter(
(id) => id !== artistId
),
})
}
})
const songIds = React.useMemo(
() => new Set(favorites?.favoriteSongs.map(String) ?? []),
@@ -247,8 +288,6 @@ export function FavoritesProvider({ children }: { children: React.ReactNode }) {
)
return (
<FavoritesContext.Provider value={value}>
{children}
</FavoritesContext.Provider>
<FavoritesContext.Provider value={value}>{children}</FavoritesContext.Provider>
)
}
+13 -1
View File
@@ -30,7 +30,6 @@ import {
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import { copyMediaShareLink, type MediaShareContext } from "@/lib/media-links"
import { shouldShowShare } from "@otunes/convex/client/media-links"
import { cn } from "@/lib/utils"
export type MediaContextAction =
@@ -48,6 +47,19 @@ export type MediaContextNavigation = {
export type { MediaShareContext } from "@/lib/media-links"
function shouldShowShare(
share: MediaShareContext | undefined,
selectedCount: number
) {
if (!share) {
return false
}
if (share.type === "song") {
return selectedCount === 1
}
return true
}
// Lets the same items render inside a dropdown or a right-click menu.
const MenuKindContext = React.createContext<"dropdown" | "context">("dropdown")
+2 -1
View File
@@ -47,12 +47,13 @@ import {
import { usePlayback } from "@/components/playback-provider"
import { api } from "@otunes/convex/_generated/api"
import type { Id } from "@otunes/convex/_generated/dataModel"
import type { PlaybackQuality } from "@otunes/convex/playback-stream"
import { resizeProfileImageToDataUrl } from "@/lib/resize-profile-image"
import { cn } from "@/lib/utils"
type Section = "profile" | "quality" | "users" | "library"
type PlaybackQuality = "low" | "medium" | "high" | "lossless"
type CurrentUser = {
_id: Id<"users">
name: string
@@ -1,7 +1,6 @@
"use client"
import { useAuthToken } from "@convex-dev/auth/react"
import { userIdFromToken } from "@otunes/convex/client/auth-token"
import { createContext, ReactNode, useContext, useEffect, useMemo } from "react"
import { loadPersistentCache } from "@/lib/persistent-cache"
@@ -14,6 +13,21 @@ if (typeof window !== "undefined") {
void loadPersistentCache()
}
function userIdFromToken(token: string | null) {
const payload = token?.split(".")[1]
if (!payload) {
return null
}
try {
const json = atob(payload.replace(/-/g, "+").replace(/_/g, "/"))
const subject = (JSON.parse(json) as { sub?: unknown }).sub
return typeof subject === "string" ? (subject.split("|")[0] ?? null) : null
} catch {
return null
}
}
/** Namespaces persisted client data by the signed-in user. */
export function PersistentCacheProvider({ children }: { children: ReactNode }) {
const token = useAuthToken()
+5 -7
View File
@@ -22,10 +22,7 @@ import type { BrowseSong } from "@otunes/convex/types"
import {
playbackFormat,
type PlaybackFormat,
type PlaybackQuality,
playbackStreamPlan,
songHlsPlaylistPath,
songPlayPath,
} from "@otunes/convex/playback-stream"
import {
ANONYMOUS_CACHE_SCOPE,
@@ -43,6 +40,7 @@ import {
getServerPlaybackSnapshot,
} from "@otunes/convex/client/playback-store"
import { rememberSongs, useSong } from "@/lib/song-cache"
import { songHlsPlaylistUrl, songPlayUrl } from "@/lib/song-play-url"
const PREVIOUS_TRACK_RESTART_THRESHOLD_SECONDS = 5
const PROGRESS_SAVE_INTERVAL_MS = 5000
@@ -185,14 +183,14 @@ function readStoredVolume(): number {
type PlaybackLoadSettings = {
preferAtmos: boolean
playbackQuality: PlaybackQuality
playbackQuality: "low" | "medium" | "high" | "lossless"
useHls: boolean
}
function playbackLoadSettings(
user: {
preferAtmos?: boolean
playbackQuality?: PlaybackQuality
playbackQuality?: "low" | "medium" | "high" | "lossless"
},
dolbyAtmosSupported: boolean | null,
files: Record<string, unknown>
@@ -247,12 +245,12 @@ function loadAudioSource(
): AudioSourceCleanup {
let disposed = false
let hlsInstance: Hls | null = null
const directUrl = songPlayPath(
const directUrl = songPlayUrl(
songId,
settings.playbackQuality,
settings.preferAtmos
)
const hlsUrl = songHlsPlaylistPath(
const hlsUrl = songHlsPlaylistUrl(
songId,
settings.playbackQuality,
settings.preferAtmos
+12 -6
View File
@@ -40,7 +40,6 @@ import { playlistCoverUrl } from "@/lib/cover-urls"
import { copyPlaylistLink } from "@/lib/media-links"
import { sumDurationSeconds } from "@/lib/media-summary"
import { rememberSongs } from "@/lib/song-cache"
import { filterSongs } from "@otunes/convex/client/song-search"
import { getSelectionRangePositions } from "@/lib/selection-range-styles"
const ADD_TO_ANOTHER_PLAYLIST = "Add to Another Playlist"
@@ -80,10 +79,17 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) {
? playlist.songs.filter((song) => !hiddenFavoriteIds.has(song.id))
: playlistSongs
const [searchQuery, setSearchQuery] = React.useState("")
const isSearching = searchQuery.trim() !== ""
const normalizedQuery = searchQuery.trim().toLowerCase()
const filteredSongs = React.useMemo(
() => filterSongs(songs, searchQuery),
[songs, searchQuery]
() =>
normalizedQuery
? songs.filter((song) =>
[song.title, song.artist.name, song.album.title].some((value) =>
value.toLowerCase().includes(normalizedQuery)
)
)
: songs,
[songs, normalizedQuery]
)
const playlistSongIds = React.useMemo(
() => songs.map((song) => song.id as Id<"songs">),
@@ -150,7 +156,7 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) {
runSongsAction(action, playlistSongIds)
}
const canReorder = isOwner && !isFavorites && !isSearching
const canReorder = isOwner && !isFavorites && !normalizedQuery
function handleRowSelect(index: number, event: React.MouseEvent) {
const songId = filteredSongs[index].id
@@ -464,7 +470,7 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) {
/>
) : (
<p className="px-6 py-8 text-sm text-muted-foreground">
{isSearching
{normalizedQuery
? "No songs found."
: "This playlist has no songs yet."}
</p>
+55 -9
View File
@@ -1,16 +1,62 @@
import { toast } from "sonner"
import {
mediaShareUrl,
type MediaShareTarget,
} from "@otunes/convex/client/media-links"
export type MediaShareContext = MediaShareTarget
export type MediaShareContext =
| { type: "album"; albumId: string }
| { type: "song"; albumId: string; songId: string }
| { type: "playlist"; playlistId: string; isFavorites?: boolean }
| { type: "artist"; artistId: string }
function mediaOrigin() {
if (typeof window === "undefined") {
return ""
}
return window.location.origin
}
function albumShareUrl(albumId: string) {
return `${mediaOrigin()}/album/${albumId}`
}
function songShareUrl(albumId: string, songId: string) {
return `${mediaOrigin()}/album/${albumId}?s=${songId}`
}
function playlistShareUrl(
playlistId: string,
options?: { isFavorites?: boolean }
) {
if (options?.isFavorites) {
return `${mediaOrigin()}/playlist/favorites`
}
return `${mediaOrigin()}/playlist/${playlistId}`
}
function artistShareUrl(artistId: string) {
return `${mediaOrigin()}/artist/${artistId}`
}
function mediaShareUrl(share: MediaShareContext) {
switch (share.type) {
case "album":
return albumShareUrl(share.albumId)
case "song":
return songShareUrl(share.albumId, share.songId)
case "playlist":
return playlistShareUrl(share.playlistId, {
isFavorites: share.isFavorites,
})
case "artist":
return artistShareUrl(share.artistId)
}
}
async function copyTextToClipboard(text: string) {
await navigator.clipboard.writeText(text)
toast.success("Link Copied")
}
export async function copyMediaShareLink(share: MediaShareContext) {
await navigator.clipboard.writeText(
mediaShareUrl(window.location.origin, share)
)
toast.success("Link Copied")
await copyTextToClipboard(mediaShareUrl(share))
}
export async function copyAlbumLink(albumId: string) {
+30 -13
View File
@@ -1,9 +1,4 @@
import type { Doc } from "@otunes/convex/_generated/dataModel"
import {
parsePlaybackQuality,
playbackStreamPlan,
qualityKbps,
} from "@otunes/convex/playback-stream"
import crypto from "node:crypto"
import fs from "node:fs/promises"
import path from "node:path"
@@ -55,6 +50,33 @@ const state = (globalState.__otunesHls ??= {
lastCleanupAt: 0,
})
export function qualityKbps(quality: string | null) {
if (quality === "low") {
return 64
}
if (quality === "high") {
return 256
}
return 128
}
export function selectHlsSourceCodec(
song: SongSource,
quality: string | null,
preferAtmos: boolean
) {
let sourceCodec = "lossless" in song.files ? "lossless" : "normal"
if (preferAtmos && "atmos" in song.files) {
sourceCodec = "atmos"
} else if (quality === "high" && "normal" in song.files) {
sourceCodec = "normal"
}
return sourceCodec
}
function hlsLookupKey(
songId: string,
quality: string | null,
@@ -283,14 +305,9 @@ export async function ensureHlsJob(
): Promise<HlsJob> {
await cleanupIdleHlsJobs()
const plan = playbackStreamPlan(
song.files,
parsePlaybackQuality(quality),
preferAtmos
)
// HLS always transcodes, and lossless quality has no target bitrate.
const kbps = plan.kbps || qualityKbps("medium")
const sourcePath = song.files[plan.source]
const kbps = qualityKbps(quality)
const sourceCodec = selectHlsSourceCodec(song, quality, preferAtmos)
const sourcePath = song.files[sourceCodec]
const key = await hlsJobKey(song, sourcePath, kbps)
const lookupKey = hlsLookupKey(song._id, quality, preferAtmos)
+3 -23
View File
@@ -2,13 +2,10 @@ import { useConvex } from "convex/react"
import { useEffect, useMemo, useSyncExternalStore } from "react"
import type { Id } from "@otunes/convex/_generated/dataModel"
import type { BrowseSong } from "@otunes/convex/types"
import {
getCachedSong,
getSongSnapshot,
getSongsVersion,
requestSongs,
songFromSnapshot,
subscribeSongs,
} from "@otunes/convex/client/song-cache"
import { isPersistentCacheLoaded } from "@/lib/persistent-cache"
@@ -51,24 +48,7 @@ export function useSongs(songIds: readonly Id<"songs">[]) {
return songs
}
/**
* Like `useSongs` for one song, but only re-renders when that song changes
* once it is cached.
*/
export function useSong(songId: Id<"songs"> | null): BrowseSong | null {
const convex = useConvex()
const snapshot = useSyncExternalStore(
subscribeSongs,
() => getSongSnapshot(songId),
() => null
)
const song = songFromSnapshot(snapshot)
useEffect(() => {
if (songId && !song && isPersistentCacheLoaded()) {
requestSongs(convex, [songId])
}
}, [convex, songId, song, snapshot])
return song
export function useSong(songId: Id<"songs"> | null) {
const songIds = useMemo(() => (songId ? [songId] : []), [songId])
return useSongs(songIds)[0] ?? null
}
+33
View File
@@ -0,0 +1,33 @@
export type PlaybackQuality = "low" | "medium" | "high" | "lossless"
export function songPlayUrl(
songId: string,
quality: PlaybackQuality | undefined,
preferAtmos: boolean | undefined
) {
const params = new URLSearchParams()
if (quality) {
params.set("quality", quality)
}
if (preferAtmos) {
params.set("preferAtmos", "true")
}
const query = params.toString()
return `/api/song/${songId}/play${query ? `?${query}` : ""}`
}
export function songHlsPlaylistUrl(
songId: string,
quality: PlaybackQuality | undefined,
preferAtmos: boolean | undefined
) {
const params = new URLSearchParams()
if (quality) {
params.set("quality", quality)
}
if (preferAtmos) {
params.set("preferAtmos", "true")
}
const query = params.toString()
return `/api/song/${songId}/play/hls${query ? `?${query}` : ""}`
}
@@ -1,30 +0,0 @@
/**
* Path data for the Dolby Atmos and Lossless badge marks, drawn with each
* platform's own SVG primitives.
*/
export const MARK_HEIGHT = 11
export const DOLBY_VIEW_BOX = "0 0 23 16"
export const DOLBY_WIDTH = (MARK_HEIGHT * 23) / 16
export const DOLBY_PATHS = [
"M0 0H2.5A8 8 0 0 1 2.5 16H0Z",
"M23 0H20.5A8 8 0 0 0 20.5 16H23Z",
]
export const LOSSLESS_VIEW_BOX = "28 154 544 271"
export const LOSSLESS_WIDTH = (MARK_HEIGHT * 544) / 271
export const LOSSLESS_PEAKS = [110, 215, 320]
export const LOSSLESS_THIN_WIDTH = 24
export const LOSSLESS_STROKES: [peak: number, d: string][] = [
[110, "M-70 320C-62 245 -38 166 0 166"],
[215, "M-70 320C-62 245 -38 166 0 166"],
[320, "M-30 185C-22 173 -11 166 0 166"],
[110, "M166 413C176 413 186 406 194 395"],
[215, "M166 413C200 413 226 340 240 275"],
[320, "M166 413C200 413 226 340 240 275"],
]
export const LOSSLESS_FALL =
"M0 178L4 179L8 181L12 184L16 189L20 196L23 204L27 213L30 223L34 233L39 243L43 254L48 266L53 278L58 289L62 301L66 313L71 325L75 336L79 347L83 357L86 364L89 370L93 377L97 382L101 387L105 392L109 397L113 401L117 405L121 408L126 412L130 415L134 417L139 419L143 421L148 423L152 424L157 424L161 425L166 425L166 401L163 401L161 400L159 400L157 399L154 397L152 396L151 394L149 392L147 389L145 386L143 383L142 380L140 376L138 371L137 367L135 362L134 357L132 351L130 345L127 339L124 330L120 319L116 308L111 297L107 285L102 272L98 260L93 248L88 235L82 223L76 212L69 201L61 192L53 183L45 175L37 168L28 163L19 158L10 155L0 154Z"
export const LOSSLESS_FALL_GAP =
"M0 178L4 180L6 184L8 190L9 197L11 203L14 210L17 219L21 228L24 238L29 248L33 259L38 270L43 282L47 293L52 305L56 317L60 328L64 340L68 351L73 361L76 368L79 375L83 381L87 387L91 393L95 398L100 403L104 408L109 412L113 416L118 419L124 422L130 424L135 425L141 426L147 426L152 426L157 425L161 425L166 425L166 401L163 401L161 399L159 398L158 395L157 393L156 390L155 387L155 384L154 381L153 379L152 376L150 373L149 369L148 366L146 361L145 357L143 352L142 347L140 341L137 335L134 326L130 315L126 304L122 293L117 281L113 268L108 256L103 243L98 231L92 219L86 207L79 196L71 186L62 177L53 168L44 161L32 157L21 155L10 154L0 154Z"
-15
View File
@@ -1,15 +0,0 @@
/** Reads the user id from a Convex Auth JWT without verifying it, for scoping client caches. */
export function userIdFromToken(token: string | null | undefined) {
const payload = token?.split(".")[1]
if (!payload) {
return null
}
try {
const json = atob(payload.replace(/-/g, "+").replace(/_/g, "/"))
const subject = (JSON.parse(json) as { sub?: unknown }).sub
return typeof subject === "string" ? (subject.split("|")[0] ?? null) : null
} catch {
return null
}
}
-96
View File
@@ -1,96 +0,0 @@
import type { OptimisticLocalStore, OptimisticUpdate } from "convex/browser"
import type { FunctionArgs, FunctionReference } from "convex/server"
import { api } from "../convex/_generated/api"
import type { Id } from "../convex/_generated/dataModel"
type FavoriteIds = {
favoriteSongs: Id<"songs">[]
favoriteAlbums: Id<"albums">[]
favoriteArtists: Id<"artists">[]
}
type FavoriteList = keyof FavoriteIds
function patchFavorites<List extends FavoriteList>(
store: OptimisticLocalStore,
list: List,
update: (ids: FavoriteIds[List]) => FavoriteIds[List]
) {
const current = store.getQuery(api.favorites.getUserFavorites, {})
if (!current) return
const next = update(current[list])
if (next !== current[list]) {
store.setQuery(
api.favorites.getUserFavorites,
{},
{
...current,
[list]: next,
}
)
}
}
function addFavorites<List extends FavoriteList>(
store: OptimisticLocalStore,
list: List,
ids: FavoriteIds[List]
) {
patchFavorites(store, list, (current) => {
const added = ids.filter((id) => !(current as string[]).includes(id))
return added.length === 0
? current
: ([...new Set([...current, ...added])] as FavoriteIds[List])
})
}
function removeFavorites<List extends FavoriteList>(
store: OptimisticLocalStore,
list: List,
ids: FavoriteIds[List]
) {
const removed = new Set<string>(ids)
patchFavorites(
store,
list,
(current) =>
(current as string[]).filter(
(id) => !removed.has(id)
) as FavoriteIds[List]
)
}
type FavoritesApi = typeof api.favorites
type FavoriteMutation = {
[
Name in keyof FavoritesApi
]: FavoritesApi[Name] extends FunctionReference<"mutation"> ? Name : never
}[keyof FavoritesApi]
/**
* Optimistic updates for the favorites mutations, so toggles show instantly.
* Pass them to `useMutation(...).withOptimisticUpdate(...)`.
*/
export const favoriteOptimisticUpdates = {
favoriteSong: (store, { songId }) =>
addFavorites(store, "favoriteSongs", [songId]),
favoriteSongs: (store, { songIds }) =>
addFavorites(store, "favoriteSongs", songIds),
unfavoriteSong: (store, { songId }) =>
removeFavorites(store, "favoriteSongs", [songId]),
unfavoriteSongs: (store, { songIds }) =>
removeFavorites(store, "favoriteSongs", songIds),
favoriteAlbum: (store, { albumId }) =>
addFavorites(store, "favoriteAlbums", [albumId]),
unfavoriteAlbum: (store, { albumId }) =>
removeFavorites(store, "favoriteAlbums", [albumId]),
favoriteArtist: (store, { artistId }) =>
addFavorites(store, "favoriteArtists", [artistId]),
unfavoriteArtist: (store, { artistId }) =>
removeFavorites(store, "favoriteArtists", [artistId]),
} satisfies {
[Name in FavoriteMutation]?: OptimisticUpdate<
FunctionArgs<FavoritesApi[Name]>
>
}
-34
View File
@@ -1,34 +0,0 @@
/** A piece of media that has a shareable page on the web app. */
export type MediaShareTarget =
| { type: "album"; albumId: string }
| { type: "song"; albumId: string; songId: string }
| { type: "playlist"; playlistId: string; isFavorites?: boolean }
| { type: "artist"; artistId: string }
/** Builds the web app URL for `share`, relative to `origin` (no trailing slash needed). */
export function mediaShareUrl(origin: string, share: MediaShareTarget) {
const base = origin.replace(/\/+$/, "")
switch (share.type) {
case "album":
return `${base}/album/${share.albumId}`
case "song":
return `${base}/album/${share.albumId}?s=${share.songId}`
case "playlist":
return share.isFavorites
? `${base}/playlist/favorites`
: `${base}/playlist/${share.playlistId}`
case "artist":
return `${base}/artist/${share.artistId}`
}
}
/** Songs are shared one at a time; collections are shared whatever is selected. */
export function shouldShowShare(
share: MediaShareTarget | undefined,
selectedCount: number
) {
if (!share) {
return false
}
return share.type !== "song" || selectedCount === 1
}
+2 -5
View File
@@ -1,9 +1,6 @@
import type { Id } from "../convex/_generated/dataModel"
/**
* Queue rules shared by the clients, which apply them locally, and the Convex
* playback mutations.
*/
/** Mirrors the queue rules in `packages/convex/convex/playback.ts`. */
function shuffleArray<T>(items: T[]): T[] {
const next = [...items]
@@ -33,7 +30,7 @@ export const EMPTY_PLAYBACK_QUEUE: PlaybackQueueState = {
progressSeconds: 0,
}
export const MAX_QUEUE_HISTORY = 30
const MAX_QUEUE_HISTORY = 30
function uniqueSongIds(songIds: Id<"songs">[]) {
return [...new Set(songIds)]
-13
View File
@@ -59,19 +59,6 @@ export function getCachedSong(songId: string) {
return readCache<BrowseSong>(songKey(songId))
}
/**
* `useSyncExternalStore` snapshot for one song: the song once cached, else the
* cache version, so a missing song re-renders on every cache change (to retry
* the fetch) and a cached one only when it changes.
*/
export function getSongSnapshot(songId: string | null) {
return songId ? (getCachedSong(songId) ?? version) : null
}
export function songFromSnapshot(snapshot: ReturnType<typeof getSongSnapshot>) {
return typeof snapshot === "object" ? snapshot : null
}
/** Stores song metadata so the player and queue can render it offline. */
export function rememberSongs(songs: readonly BrowseSong[]) {
let changed = false
-21
View File
@@ -1,21 +0,0 @@
type SearchableSong = {
title: string
artist: { name: string }
album: { title: string }
}
/** Case-insensitive match on title, artist, or album. A blank query keeps every song. */
export function filterSongs<T extends SearchableSong>(
songs: T[],
query: string
) {
const normalized = query.trim().toLowerCase()
if (!normalized) {
return songs
}
return songs.filter((song) =>
[song.title, song.artist.name, song.album.title].some((value) =>
value.toLowerCase().includes(normalized)
)
)
}
-4
View File
@@ -13,8 +13,6 @@ import type * as auth from "../auth.js";
import type * as crons from "../crons.js";
import type * as favorites from "../favorites.js";
import type * as http from "../http.js";
import type * as intentCatalog from "../intentCatalog.js";
import type * as intentCatalogChanges from "../intentCatalogChanges.js";
import type * as library from "../library.js";
import type * as limits from "../limits.js";
import type * as migrations from "../migrations.js";
@@ -36,8 +34,6 @@ declare const fullApi: ApiFromModules<{
crons: typeof crons;
favorites: typeof favorites;
http: typeof http;
intentCatalog: typeof intentCatalog;
intentCatalogChanges: typeof intentCatalogChanges;
library: typeof library;
limits: typeof limits;
migrations: typeof migrations;
-6
View File
@@ -9,10 +9,4 @@ crons.interval(
internal.actions.scheduledUpdateLibrary
)
crons.daily(
"prune Siri catalog change log",
{ hourUTC: 9, minuteUTC: 0 },
internal.intentCatalogChanges.pruneIntentCatalogChanges
)
export default crons
-337
View File
@@ -1,337 +0,0 @@
import { getAuthUserId } from "@convex-dev/auth/server"
import { paginationOptsValidator } from "convex/server"
import { ConvexError, v } from "convex/values"
import type { Doc, Id } from "./_generated/dataModel"
import { query, type QueryCtx } from "./_generated/server"
import {
INTENT_CATALOG_CHANGE_RETENTION_MS,
type LibraryCatalogKind,
} from "./intentCatalogChanges"
import { getSongArtistNames, songArtistsMatchAlbum } from "./library"
/**
* Siri/App Intents entity catalog. Library records carry no per-user state, so
* clients sync them incrementally and overlay favorites locally.
*/
const FAVORITES_PLAYLIST_ID = "otunes:favorites"
/** More changes than this since a client's cursor and it rebuilds instead. */
const MAX_CHANGES_PER_SYNC = 2000
const libraryKindValidator = v.union(
v.literal("song"),
v.literal("album"),
v.literal("artist")
)
type IntentEntity = {
id: string
title: string
subtitle?: string
synonyms?: string[]
metadata: Record<string, string>
}
async function requireUser(ctx: QueryCtx) {
const userId = await getAuthUserId(ctx)
if (!userId) throw new ConvexError("Not authenticated")
return userId
}
function unique<T>(values: T[]) {
return [...new Set(values)]
}
async function getMany<Table extends "albums" | "artists" | "users">(
ctx: QueryCtx,
table: Table,
ids: Id<Table>[]
) {
const docs = await Promise.all(unique(ids).map((id) => ctx.db.get(table, id)))
return new Map(
docs.flatMap((doc) => (doc ? [[doc._id as Id<Table>, doc] as const] : []))
)
}
async function songEntities(ctx: QueryCtx, songs: Doc<"songs">[]) {
const [albums, artists] = await Promise.all([
getMany(
ctx,
"albums",
songs.map((song) => song.album)
),
getMany(
ctx,
"artists",
songs.map((song) => song.albumArtist)
),
])
return songs.flatMap((song): IntentEntity[] => {
const album = albums.get(song.album)
const artist = artists.get(song.albumArtist)
if (!album || !artist || !song.name.trim()) return []
const credits = getSongArtistNames(song.artist)
const artistName = songArtistsMatchAlbum(credits, artist.name)
? artist.name
: credits.join(", ") || artist.name
return [
{
id: song._id,
title: song.name,
subtitle: artistName,
metadata: {
artistName,
artistId: artist._id,
albumTitle: album.name,
albumId: album._id,
duration: String(Math.max(0, Math.round(song.length))),
genres: song.genres.join(", "),
year: String(song.year),
track: String(song.track),
disk: String(song.diskNumber ?? ""),
},
},
]
})
}
async function albumEntities(ctx: QueryCtx, albums: Doc<"albums">[]) {
const artists = await getMany(
ctx,
"artists",
albums.map((album) => album.artist)
)
return albums.flatMap((album): IntentEntity[] => {
const artist = artists.get(album.artist)
if (!artist || !album.name.trim()) return []
return [
{
id: album._id,
title: album.name,
subtitle: artist.name,
metadata: {
artistName: artist.name,
artistId: artist._id,
year: String(album.year),
},
},
]
})
}
function artistEntities(artists: Doc<"artists">[]) {
return artists.flatMap((artist): IntentEntity[] =>
artist.name.trim()
? [{ id: artist._id, title: artist.name, metadata: {} }]
: []
)
}
/** Rows in `ids` not already in `rows`, so renamed records are re-sent. */
async function withUpdatedRows<Row extends { _id: string }>(
rows: Row[],
ids: string[],
load: (id: string) => Promise<Row | null>
) {
const seen = new Set(rows.map((row) => row._id))
const updated: (Row | null)[] = await Promise.all(
ids.filter((id) => !seen.has(id)).map(load)
)
return [...rows, ...updated.flatMap((row) => (row ? [row] : []))]
}
async function newestCreationTime(ctx: QueryCtx, kind: LibraryCatalogKind) {
const table =
kind === "song" ? "songs" : kind === "album" ? "albums" : "artists"
const [row, change] = await Promise.all([
ctx.db.query(table).order("desc").first(),
ctx.db
.query("intentCatalogChanges")
.withIndex("by_kind", (q) => q.eq("kind", kind))
.order("desc")
.first(),
])
return Math.max(row?._creationTime ?? 0, change?._creationTime ?? 0)
}
/**
* Newest insert, rename or deletion per kind. Clients subscribe to this and
* fetch a kind only after it moves past their cursor.
*/
export const head = query({
args: {},
handler: async (ctx) => {
await requireUser(ctx)
const [song, album, artist] = await Promise.all([
newestCreationTime(ctx, "song"),
newestCreationTime(ctx, "album"),
newestCreationTime(ctx, "artist"),
])
return { song, album, artist }
},
})
/**
* One page of catalog records. Without `since` it walks the whole library;
* with it, only records created, renamed or removed after `since`. The first
* page reports `head`, the cursor to resume from once every page is read, and
* `reset` when the change log no longer covers `since` (stale or too many
* changes); the client then rebuilds without `since`.
*/
export const page = query({
args: {
kind: libraryKindValidator,
since: v.optional(v.number()),
paginationOpts: paginationOptsValidator,
},
handler: async (ctx, { kind, since, paginationOpts }) => {
await requireUser(ctx)
const firstPage = paginationOpts.cursor === null
let removed: string[] = []
let updatedIds: string[] = []
if (firstPage && since !== undefined) {
const changes = await ctx.db
.query("intentCatalogChanges")
.withIndex("by_kind", (q) =>
q.eq("kind", kind).gt("_creationTime", since)
)
.take(MAX_CHANGES_PER_SYNC + 1)
// Pruning may have dropped deletions older than the retention window.
const expired = since < Date.now() - INTENT_CATALOG_CHANGE_RETENTION_MS
if (expired || changes.length > MAX_CHANGES_PER_SYNC) {
return {
page: [],
isDone: true,
continueCursor: "",
head: await newestCreationTime(ctx, kind),
removed,
reset: true,
}
}
removed = unique(
changes.filter((change) => change.removed).map((c) => c.entityId)
)
const removedIds = new Set(removed)
updatedIds = unique(
changes
.filter((change) => !removedIds.has(change.entityId))
.map((change) => change.entityId)
)
}
const head = firstPage ? await newestCreationTime(ctx, kind) : undefined
const respond = (
rows: { isDone: boolean; continueCursor: string },
page: IntentEntity[]
) => ({
page,
isDone: rows.isDone,
continueCursor: rows.continueCursor,
head,
removed,
reset: false,
})
// Rows created after `since`, or every row when it is omitted.
if (kind === "song") {
const query = ctx.db.query("songs")
const rows = await (
since === undefined
? query
: query.withIndex("by_creation_time", (q) =>
q.gt("_creationTime", since)
)
).paginate(paginationOpts)
const docs = await withUpdatedRows(rows.page, updatedIds, (id) => {
const songId = ctx.db.normalizeId("songs", id)
return songId ? ctx.db.get("songs", songId) : Promise.resolve(null)
})
return respond(rows, await songEntities(ctx, docs))
}
if (kind === "album") {
const query = ctx.db.query("albums")
const rows = await (
since === undefined
? query
: query.withIndex("by_creation_time", (q) =>
q.gt("_creationTime", since)
)
).paginate(paginationOpts)
const docs = await withUpdatedRows(rows.page, updatedIds, (id) => {
const albumId = ctx.db.normalizeId("albums", id)
return albumId ? ctx.db.get("albums", albumId) : Promise.resolve(null)
})
return respond(rows, await albumEntities(ctx, docs))
}
const query = ctx.db.query("artists")
const rows = await (
since === undefined
? query
: query.withIndex("by_creation_time", (q) =>
q.gt("_creationTime", since)
)
).paginate(paginationOpts)
const docs = await withUpdatedRows(rows.page, updatedIds, (id) => {
const artistId = ctx.db.normalizeId("artists", id)
return artistId ? ctx.db.get("artists", artistId) : Promise.resolve(null)
})
return respond(rows, artistEntities(docs))
},
})
/** The signed-in user's own and subscribed playlists, plus Favorites. */
export const playlists = query({
args: {},
handler: async (ctx) => {
const userId = await requireUser(ctx)
const user = await ctx.db.get("users", userId)
if (!user) throw new ConvexError("Not authenticated")
const [owned, subscribed] = await Promise.all([
ctx.db
.query("playlists")
.withIndex("by_owner", (q) => q.eq("owner", userId))
.collect(),
Promise.all(
unique(user.subscribedPlaylists ?? []).map((id) =>
ctx.db.get("playlists", id)
)
),
])
const others = subscribed.filter(
(playlist): playlist is Doc<"playlists"> =>
playlist !== null && playlist.owner !== userId
)
const owners = await getMany(
ctx,
"users",
others.map((playlist) => playlist.owner)
)
owners.set(userId, user)
const entities: IntentEntity[] = [
{
id: FAVORITES_PLAYLIST_ID,
title: "Favorites",
synonyms: [
"favorites",
"favourite songs",
"liked songs",
"loved songs",
],
metadata: { ownerName: user.name, createdByMe: "1" },
},
]
for (const playlist of [...owned, ...others]) {
if (!playlist.name.trim()) continue
const ownerName = owners.get(playlist.owner)?.name ?? ""
entities.push({
id: playlist._id,
title: playlist.name,
subtitle: ownerName,
metadata: {
ownerName,
createdByMe: playlist.owner === userId ? "1" : "0",
},
})
}
return entities
},
})
@@ -1,40 +0,0 @@
import { internal } from "./_generated/api"
import { internalMutation, type MutationCtx } from "./_generated/server"
export type LibraryCatalogKind = "song" | "album" | "artist"
/** Clients that last synced before this must rebuild their catalog from scratch. */
export const INTENT_CATALOG_CHANGE_RETENTION_MS = 30 * 24 * 60 * 60_000
const PRUNE_BATCH = 500
export async function recordIntentCatalogChanges(
ctx: MutationCtx,
kind: LibraryCatalogKind,
entityIds: string[],
removed: boolean
) {
for (const entityId of entityIds) {
await ctx.db.insert("intentCatalogChanges", { kind, entityId, removed })
}
}
export const pruneIntentCatalogChanges = internalMutation({
args: {},
handler: async (ctx) => {
const cutoff = Date.now() - INTENT_CATALOG_CHANGE_RETENTION_MS
const stale = await ctx.db
.query("intentCatalogChanges")
.withIndex("by_creation_time", (q) => q.lt("_creationTime", cutoff))
.take(PRUNE_BATCH)
for (const change of stale) {
await ctx.db.delete("intentCatalogChanges", change._id)
}
if (stale.length === PRUNE_BATCH) {
await ctx.scheduler.runAfter(
0,
internal.intentCatalogChanges.pruneIntentCatalogChanges,
{}
)
}
},
})
+126 -6
View File
@@ -21,7 +21,6 @@ import type {
SearchTopResultItem,
SidebarPlaylists,
} from "./types"
import { recordIntentCatalogChanges } from "./intentCatalogChanges"
export type {
BrowseAlbum,
@@ -54,7 +53,7 @@ export async function requireSignedIn(ctx: QueryCtx) {
}
}
export function getSongArtistNames(artist: string | string[]): string[] {
function getSongArtistNames(artist: string | string[]): string[] {
if (Array.isArray(artist)) {
return artist.map((name) => name.trim()).filter((name) => name.length > 0)
}
@@ -62,7 +61,7 @@ export function getSongArtistNames(artist: string | string[]): string[] {
return trimmed.length > 0 ? [trimmed] : []
}
export function songArtistsMatchAlbum(
function songArtistsMatchAlbum(
songArtists: string[],
albumArtistName: string
): boolean {
@@ -962,6 +961,130 @@ export const getSongsForPlayback = query({
},
})
const FAVORITES_PLAYLIST_ID = "otunes:favorites"
/** Bounded pages for the device's disk-backed Siri catalog. */
export const intentCatalogPage = query({
args: {
kind: v.union(
v.literal("song"),
v.literal("album"),
v.literal("artist"),
v.literal("playlist")
),
paginationOpts: paginationOptsValidator,
},
handler: async (ctx, { kind, paginationOpts }) => {
const userId = await getAuthUserId(ctx)
if (!userId) throw new ConvexError("Not authenticated")
const user = await ctx.db.get("users", userId)
if (!user) throw new ConvexError("Not authenticated")
type Entity = {
id: string
title: string
subtitle?: string
synonyms?: string[]
metadata: Record<string, string>
}
const page: Entity[] = []
if (kind === "song") {
const result = await ctx.db.query("songs").paginate(paginationOpts)
for (const song of result.page) {
const album = await ctx.db.get("albums", song.album)
const artist = await ctx.db.get("artists", song.albumArtist)
if (!album || !artist || !song.name.trim()) continue
const credits = getSongArtistNames(song.artist)
const artistName = songArtistsMatchAlbum(credits, artist.name)
? artist.name
: credits.join(", ") || artist.name
page.push({
id: song._id,
title: song.name,
subtitle: artistName,
metadata: {
artistName,
artistId: artist._id,
albumTitle: album.name,
albumId: album._id,
duration: String(Math.max(0, Math.round(song.length))),
genres: song.genres.join(", "),
year: String(song.year),
track: String(song.track),
disk: String(song.diskNumber ?? ""),
favorite: user.favoriteSongs?.includes(song._id) ? "1" : "0",
},
})
}
return { ...result, page }
}
if (kind === "album") {
const result = await ctx.db.query("albums").paginate(paginationOpts)
for (const album of result.page) {
const artist = await ctx.db.get("artists", album.artist)
if (!artist || !album.name.trim()) continue
page.push({
id: album._id,
title: album.name,
subtitle: artist.name,
metadata: {
artistName: artist.name,
artistId: artist._id,
year: String(album.year),
favorite: user.favoriteAlbums?.includes(album._id) ? "1" : "0",
},
})
}
return { ...result, page }
}
if (kind === "artist") {
const result = await ctx.db.query("artists").paginate(paginationOpts)
for (const artist of result.page) {
if (artist.name.trim())
page.push({
id: artist._id,
title: artist.name,
metadata: {
favorite: user.favoriteArtists?.includes(artist._id) ? "1" : "0",
},
})
}
return { ...result, page }
}
const result = await ctx.db.query("playlists").paginate(paginationOpts)
for (const playlist of result.page) {
if (
playlist.owner !== userId &&
!user.subscribedPlaylists?.includes(playlist._id)
)
continue
if (!playlist.name.trim()) continue
const owner = await ctx.db.get("users", playlist.owner)
page.push({
id: playlist._id,
title: playlist.name,
subtitle: owner?.name ?? "",
metadata: {
ownerName: owner?.name ?? "",
createdByMe: playlist.owner === userId ? "1" : "0",
},
})
}
if (paginationOpts.cursor === null)
page.unshift({
id: FAVORITES_PLAYLIST_ID,
title: "Favorites",
synonyms: [
"favorites",
"favourite songs",
"liked songs",
"loved songs",
],
metadata: { ownerName: user.name, createdByMe: "1" },
})
return { ...result, page }
},
})
export const createArtists = internalMutation({
args: {
artists: v.array(
@@ -1168,7 +1291,6 @@ export const removeArtists = internalMutation({
for (const id of args.ids) {
await ctx.db.delete("artists", id)
}
await recordIntentCatalogChanges(ctx, "artist", args.ids, true)
},
})
@@ -1178,7 +1300,6 @@ export const removeAlbums = internalMutation({
for (const id of args.ids) {
await ctx.db.delete("albums", id)
}
await recordIntentCatalogChanges(ctx, "album", args.ids, true)
},
})
@@ -1216,6 +1337,5 @@ export const removeSongs = internalMutation({
for (const id of args.ids) {
await ctx.db.delete("songs", id)
}
await recordIntentCatalogChanges(ctx, "song", args.ids, true)
},
})
+2 -1
View File
@@ -1,9 +1,10 @@
import { ConvexError } from "convex/values"
import type { Doc, Id } from "./_generated/dataModel"
import type { MutationCtx, QueryCtx } from "./_generated/server"
import { MAX_QUEUE_HISTORY } from "../client/playback-queue"
import { MAX_QUEUE_LENGTH } from "./limits"
const MAX_QUEUE_HISTORY = 30
export type RepeatMode = Doc<"playbackStates">["repeatEnabled"]
export type PlaybackState = Pick<
-6
View File
@@ -69,12 +69,6 @@ export default defineSchema({
invocationId: v.string(),
playlistId: v.id("playlists"),
}).index("by_user_invocation", ["userId", "invocationId"]),
/** Renames and deletions; Siri catalog clients find inserts by `_creationTime`. */
intentCatalogChanges: defineTable({
kind: v.union(v.literal("song"), v.literal("album"), v.literal("artist")),
entityId: v.string(),
removed: v.boolean(),
}).index("by_kind", ["kind"]),
appSettings: defineTable({
disableAccountCreation: v.boolean(),
}),
-20
View File
@@ -23,26 +23,10 @@
"types": "./server-url.ts",
"default": "./server-url.ts"
},
"./client/audio-format-marks": {
"types": "./client/audio-format-marks.ts",
"default": "./client/audio-format-marks.ts"
},
"./client/auth-token": {
"types": "./client/auth-token.ts",
"default": "./client/auth-token.ts"
},
"./client/cached-query": {
"types": "./client/cached-query.ts",
"default": "./client/cached-query.ts"
},
"./client/favorites": {
"types": "./client/favorites.ts",
"default": "./client/favorites.ts"
},
"./client/media-links": {
"types": "./client/media-links.ts",
"default": "./client/media-links.ts"
},
"./client/persistent-cache": {
"types": "./client/persistent-cache.ts",
"default": "./client/persistent-cache.ts"
@@ -58,10 +42,6 @@
"./client/song-cache": {
"types": "./client/song-cache.ts",
"default": "./client/song-cache.ts"
},
"./client/song-search": {
"types": "./client/song-search.ts",
"default": "./client/song-search.ts"
}
},
"scripts": {
+1 -66
View File
@@ -1,21 +1,5 @@
export type PlaybackQuality = "low" | "medium" | "high" | "lossless"
const PLAYBACK_QUALITIES: readonly string[] = [
"low",
"medium",
"high",
"lossless",
] satisfies PlaybackQuality[]
/** Reads a `quality` request parameter; anything unknown means the default. */
export function parsePlaybackQuality(
value: string | null | undefined
): PlaybackQuality | undefined {
return value && PLAYBACK_QUALITIES.includes(value)
? (value as PlaybackQuality)
: undefined
}
export function qualityKbps(quality: PlaybackQuality | null | undefined) {
if (quality === "low") {
return 64
@@ -32,15 +16,9 @@ export function qualityKbps(quality: PlaybackQuality | null | undefined) {
return 0
}
export type PlaybackSource = "atmos" | "lossless" | "normal"
export type PlaybackStreamPlan = {
playbackQuality: PlaybackQuality
preferAtmos: boolean
/** The file that is streamed as-is, or transcoded when `transcodeNeeded`. */
source: PlaybackSource
/** AAC bitrate to transcode to; 0 for lossless quality. */
kbps: number
transcodeNeeded: boolean
useHls: boolean
}
@@ -51,70 +29,27 @@ export function playbackStreamPlan(
preferAtmos: boolean
): PlaybackStreamPlan {
const playbackQuality = quality ?? "medium"
const kbps = qualityKbps(playbackQuality)
const kbps = playbackQuality === "lossless" ? 0 : qualityKbps(playbackQuality)
const effectivePreferAtmos = preferAtmos && "atmos" in files
let source: PlaybackSource = "lossless" in files ? "lossless" : "normal"
let transcodeNeeded = true
if (effectivePreferAtmos) {
source = "atmos"
transcodeNeeded = false
} else if (kbps === 0 && "lossless" in files) {
source = "lossless"
transcodeNeeded = false
} else if (kbps === 0 || (kbps === 256 && "normal" in files)) {
source = "normal"
transcodeNeeded = false
}
return {
playbackQuality,
preferAtmos: effectivePreferAtmos,
source,
kbps,
transcodeNeeded,
useHls: transcodeNeeded,
}
}
function songStreamQuery(
params: URLSearchParams,
quality: PlaybackQuality | undefined,
preferAtmos: boolean | undefined
) {
if (quality) {
params.set("quality", quality)
}
if (preferAtmos) {
params.set("preferAtmos", "true")
}
const query = params.toString()
return query ? `?${query}` : ""
}
export function songPlayPath(
songId: string,
quality: PlaybackQuality | undefined,
preferAtmos: boolean | undefined,
options?: { losslessFormat?: "alac" }
) {
const params = new URLSearchParams()
if (options?.losslessFormat) {
params.set("losslessFormat", options.losslessFormat)
}
return `/api/song/${songId}/play${songStreamQuery(params, quality, preferAtmos)}`
}
export function songHlsPlaylistPath(
songId: string,
quality: PlaybackQuality | undefined,
preferAtmos: boolean | undefined
) {
const query = songStreamQuery(new URLSearchParams(), quality, preferAtmos)
return `/api/song/${songId}/play/hls${query}`
}
export type PlaybackFormat = "atmos" | "lossless"
/** The labeled format a song streams in under the plan, if any. */