Compare commits

..
19 changed files with 1693 additions and 804 deletions

No files matched your search

@@ -1,3 +1,8 @@
import { SongSwipeRow } from "@/components/song-swipe-row"
import {
useSwipeRowGroup,
type SwipeRowGroup,
} from "@/components/ios-swipe-to-remove-row"
import {
CollectionHero,
useCollectionHeroLayout,
@@ -68,6 +73,7 @@ export default function AlbumScreen() {
handleAlbumTrackAction,
isAlbumFavorite: isAlbumFav,
} = useMediaMenuActions()
const swipeGroup = useSwipeRowGroup()
const isFocused = useIsFocused()
const coverUri =
config && typeof id === "string" ? albumCoverUrl(config.serverUrl, id) : ""
@@ -112,6 +118,7 @@ export default function AlbumScreen() {
}
return album ? (
<AlbumTrackRow
swipeGroup={swipeGroup}
track={item.track}
number={item.number}
album={album}
@@ -132,6 +139,7 @@ export default function AlbumScreen() {
palette,
playFromSong,
songIds,
swipeGroup,
]
)
@@ -235,6 +243,7 @@ export default function AlbumScreen() {
}}
/>
<Animated.FlatList
onScrollBeginDrag={swipeGroup.closeOpenRow}
onLayout={onLayout}
{...longListProps}
data={listItems}
@@ -456,6 +465,7 @@ function albumListItemKey(item: AlbumListItem) {
const AlbumTrackRow = memo(function AlbumTrackRow({
track,
swipeGroup,
number,
album,
songIds,
@@ -465,6 +475,7 @@ const AlbumTrackRow = memo(function AlbumTrackRow({
onAction,
}: {
track: BrowseAlbumTrack
swipeGroup: SwipeRowGroup
number: number
album: BrowseAlbumDetail
songIds: Id<"songs">[]
@@ -477,8 +488,22 @@ const AlbumTrackRow = memo(function AlbumTrackRow({
action: MediaAction
) => Promise<void>
}) {
const Row = Platform.OS === "ios" ? SongSwipeRow : Pressable
const { config } = useServerConfig()
return (
<MediaContextMenu
preview={
config
? {
kind: "song",
title: track.title,
artist: album.artist.name,
album: album.title,
artworkUrl: albumCoverUrl(config.serverUrl, album.id),
hasArtwork: album.cover != null,
}
: undefined
}
stationArtistId={album.artist.id}
favorited={favorited}
navigation={{
@@ -494,7 +519,10 @@ const AlbumTrackRow = memo(function AlbumTrackRow({
onAction={(action) => void onAction(track, album, action)}
>
<MusicEntityView kind="song" id={track.id}>
<Pressable
<Row
group={swipeGroup}
backgroundColor={palette.background}
onPlayNext={() => void onAction(track, album, "play-next")}
style={styles.trackRow}
onPress={() => void onPlay(track.id as Id<"songs">, songIds)}
>
@@ -535,7 +563,7 @@ const AlbumTrackRow = memo(function AlbumTrackRow({
{formatDuration(track.durationSeconds)}
</PaletteText>
</View>
</Pressable>
</Row>
</MusicEntityView>
</MediaContextMenu>
)
@@ -141,6 +141,12 @@ const ArtistRow = memo(function ArtistRow({
return (
<MediaContextMenu
preview={{
kind: "artist",
title: artist.name,
artworkUrl: artistCoverUrl(serverUrl, artist.id),
hasArtwork: artist.photo != null,
}}
favorited={favorited}
share={{ type: "artist", artistId: artist.id, title: artist.name }}
onAction={(action) => void onAction(artist, action)}
@@ -160,7 +166,11 @@ const ArtistRow = memo(function ArtistRow({
>
{artist.name}
</Text>
<Ionicons name="chevron-forward" size={16} color={colors.mutedForeground} />
<Ionicons
name="chevron-forward"
size={16}
color={colors.mutedForeground}
/>
</Pressable>
</MediaContextMenu>
)
@@ -39,7 +39,6 @@ import ReanimatedSwipeable from "react-native-gesture-handler/ReanimatedSwipeabl
import { CollectionDivider } from "@/components/collection-list"
import { CoverImage } from "@/components/cover-image"
import {
SwipeToRemoveRow,
useSwipeRowGroup,
type SwipeRowGroup,
} from "@/components/ios-swipe-to-remove-row"
@@ -434,28 +433,24 @@ const PlaylistSongRow = memo(function PlaylistSongRow({
playlistLabel={PLAYLIST_LABEL}
palette={palette}
inset
swipe={
Platform.OS === "ios"
? {
group: swipeGroup,
backgroundColor: palette.background,
onPlayNext: () => onAction(song, "play-next"),
}
: undefined
}
onPlay={onPlay}
onAction={onAction}
/>
)
if (!removable) {
if (Platform.OS === "ios" || !removable) {
return row
}
if (Platform.OS === "ios") {
return (
<SwipeToRemoveRow
group={swipeGroup}
backgroundColor={palette.background}
actionAccessibilityLabel={`Remove ${song.title} from playlist`}
onRemove={handleRemove}
>
{row}
</SwipeToRemoveRow>
)
}
return (
<ReanimatedSwipeable
friction={2}
+17 -7
View File
@@ -44,6 +44,7 @@ import {
NowPlayingQueue,
QUEUE_HORIZONTAL_INSET,
} from "@/components/now-playing-queue"
import { PlayerArtistMenu } from "@/components/player-artist-menu"
import { PlayerSlider } from "@/components/player-slider"
import { PlayerSymbol } from "@/components/player-symbol"
import { useFavorites } from "@/contexts/favorites"
@@ -544,13 +545,15 @@ function SongMeta({
{ color: colors.foreground },
]}
/>
<MarqueeText
text={song.artist.name}
style={[
compact ? styles.compactArtist : styles.artist,
{ color: colors.secondaryForeground },
]}
/>
<PlayerArtistMenu song={song} onAction={handleAction}>
<MarqueeText
text={song.artist.name}
style={[
compact ? styles.compactArtist : styles.artist,
{ color: colors.secondaryForeground },
]}
/>
</PlayerArtistMenu>
</View>
<Pressable
onPress={() => void toggleSongFavorite(song.id)}
@@ -567,6 +570,12 @@ function SongMeta({
/>
</Pressable>
<MediaActionMenu
share={{
type: "song",
songId: song.id,
albumId: song.album.id,
title: song.title,
}}
stationArtistId={song.artist.id}
favorited={favorited}
navigation={{
@@ -574,6 +583,7 @@ function SongMeta({
album: { id: song.album.id },
}}
includeActions={[
"share",
"play-next",
"play-last",
"shuffle",
+1 -1
View File
@@ -134,7 +134,7 @@ function songEntity(song: {
albumId: song.albumId,
duration: String(song.durationSeconds),
genres: song.genres ?? "",
year: song.year == null ? "" : String(song.year),
year: song.year ? String(song.year) : "",
track: song.track == null ? "" : String(song.track),
disk: song.diskNumber == null ? "" : String(song.diskNumber),
favorite: song.favorite ? "1" : "0",
+13
View File
@@ -94,6 +94,13 @@ export const AlbumCard = memo(function AlbumCard({
return (
<MediaContextMenu
preview={{
kind: "album",
title: album.title,
artist: album.artist.name,
artworkUrl: albumCoverUrl(config.serverUrl, album.id),
hasArtwork: album.cover != null,
}}
favorited={isAlbumFavorite(album.id)}
stationArtistId={album.artist.id}
navigation={{ artist: { id: album.artist.id } }}
@@ -149,6 +156,12 @@ export const ArtistCard = memo(function ArtistCard({
return (
<MediaContextMenu
preview={{
kind: "artist",
title: artist.name,
artworkUrl: artistCoverUrl(config.serverUrl, artist.id),
hasArtwork: artist.photo != null,
}}
favorited={isArtistFavorite(artist.id)}
share={{ type: "artist", artistId: artist.id, title: artist.name }}
onAction={(action) => void handleArtistAction(artist, action)}
+119 -4
View File
@@ -4,12 +4,19 @@ import { requireNativeView } from "expo"
import type { ComponentType, ReactNode } from "react"
import {
Platform,
Text,
useWindowDimensions,
View,
type StyleProp,
type ViewProps,
type ViewStyle,
} from "react-native"
import { Ionicons } from "@expo/vector-icons"
import { CoverImage } from "@/components/cover-image"
import { useThemeColors } from "@/lib/theme"
import { useAppNavigation } from "@/lib/app-navigation"
import { useServerConfig } from "@/contexts/server-config"
import type { MediaShareContext } from "@/lib/media-links"
import { shouldShowShare } from "@/lib/media-links"
@@ -32,7 +39,17 @@ export type MediaNavigation = {
album?: { id: string }
}
export type MediaMenuPreview = {
title: string
artist?: string
album?: string
artworkUrl: string
hasArtwork: boolean
kind: "album" | "artist" | "song"
}
export type MediaMenuConfig = {
preview?: MediaMenuPreview
selectedCount?: number
playlistLabel?: string
favorited?: boolean
@@ -89,12 +106,12 @@ const MEDIA_ACTION_ITEMS: {
{
action: "go-artist",
label: () => "Go to Artist",
image: "person",
image: "music.mic",
},
{
action: "go-album",
label: () => "Go to Album",
image: "opticaldisc",
image: "square.stack",
},
{
action: "favorite",
@@ -214,10 +231,95 @@ function MediaMenuBase({
const NativeContextMenuView =
Platform.OS === "ios"
? requireNativeView<
ViewProps & { disableForceFlatten?: boolean; children: ReactNode }
ViewProps & {
disableForceFlatten?: boolean
children: ReactNode
onPreviewTappedAnimationCompleted?: () => void
}
>("ExpoRouterNativeLinkPreview", "NativeLinkPreviewView")
: null
const NativeContextMenuPreview =
Platform.OS === "ios"
? requireNativeView<
ViewProps & {
preferredContentSize: { width: number; height: number }
children: ReactNode
}
>("ExpoRouterNativeLinkPreview", "NativeLinkPreviewContentView")
: null
function MediaPreview({ preview }: { preview: MediaMenuPreview }) {
const colors = useThemeColors()
const { width: windowWidth, fontScale } = useWindowDimensions()
if (!NativeContextMenuPreview) return null
const width = Math.min(360, windowWidth - 32)
const height = Math.max(124, 32 + 76 * fontScale)
const isArtist = preview.kind === "artist"
return (
// Match Expo Router's preview wrapper: UIKit mounts this separately, but
// Yoga must also keep it out of the trigger's normal layout.
<NativeContextMenuPreview
preferredContentSize={{ width, height }}
style={{ position: "absolute", top: 0, left: 0 }}
>
<View
style={{
width,
height,
padding: 16,
flexDirection: "row",
alignItems: "center",
gap: 16,
backgroundColor: colors.card,
}}
>
<CoverImage
uri={preview.artworkUrl}
hasCover={preview.hasArtwork}
style={{ width: 88, height: 88, borderRadius: isArtist ? 44 : 12 }}
/>
<View style={{ flex: 1, gap: 2 }}>
<Text
numberOfLines={2}
style={{
color: colors.foreground,
fontSize: 17,
fontWeight: "600",
}}
>
{preview.title}
</Text>
{preview.artist ? (
<Text
numberOfLines={1}
style={{ color: colors.mutedForeground, fontSize: 15 }}
>
{preview.artist}
</Text>
) : null}
{preview.album ? (
<Text
numberOfLines={1}
style={{ color: colors.mutedForeground, fontSize: 14 }}
>
{preview.album}
</Text>
) : null}
</View>
{preview.kind !== "song" ? (
<Ionicons
name="chevron-forward"
size={20}
color={colors.mutedForeground}
/>
) : null}
</View>
</NativeContextMenuPreview>
)
}
const NativeContextMenuAction: ComponentType<{
identifier: string
title: string
@@ -233,11 +335,13 @@ const NativeContextMenuAction: ComponentType<{
function NativeMediaContextMenu({
children,
preview,
onAction,
share,
...actionConfig
}: MediaMenuWrapperProps) {
const { config } = useServerConfig()
const { push } = useAppNavigation()
if (!NativeContextMenuView || !NativeContextMenuAction) {
return children
@@ -252,10 +356,21 @@ function NativeMediaContextMenu({
}
return (
<NativeContextMenuView style={{ display: "contents" }} disableForceFlatten>
<NativeContextMenuView
style={{ display: "contents" }}
disableForceFlatten
onPreviewTappedAnimationCompleted={() => {
if (share?.type === "artist") push(`/artist/${share.artistId}`)
else if (share?.type === "album" || share?.type === "song")
push(`/album/${share.albumId}`)
}}
>
{/* The native menu retains its first trigger. Keep that view mounted when
focus changes replace the Siri entity wrapper inside it. */}
<View collapsable={false}>{children}</View>
{preview && (actionConfig.selectedCount ?? 1) === 1 ? (
<MediaPreview preview={preview} />
) : null}
{buildMediaMenuActions({ ...actionConfig, share }).map((action) => (
<NativeContextMenuAction
key={action.id}
+45
View File
@@ -0,0 +1,45 @@
import type { MenuComponentProps } from "@expo/ui/community/menu"
import { Button, Host, Menu, RNHostView } from "@expo/ui/swift-ui"
import { disabled, menuOrder } from "@expo/ui/swift-ui/modifiers"
// SwiftUI's automatic order reverses menus that open upward in the player.
// Keep the same top-to-bottom order as the UIKit long-press menus.
export function MenuView({
actions,
children,
style,
testID,
onPressAction,
}: MenuComponentProps) {
return (
<Host matchContents style={style} testID={testID} ignoreSafeArea="all">
<Menu
modifiers={[menuOrder("fixed")]}
label={
<RNHostView matchContents>
<>{children}</>
</RNHostView>
}
>
{actions
.filter((action) => !action.attributes?.hidden)
.map((action) => (
<Button
key={action.id ?? action.title}
label={action.title}
systemImage={
typeof action.image === "string" ? action.image : undefined
}
role={action.attributes?.destructive ? "destructive" : undefined}
modifiers={[disabled(action.attributes?.disabled ?? false)]}
onPress={() =>
onPressAction?.({
nativeEvent: { event: action.id ?? action.title },
})
}
/>
))}
</Menu>
</Host>
)
}
@@ -0,0 +1,56 @@
import { useState, type ReactNode } from "react"
import { StyleSheet, View } from "react-native"
import {
MediaActionMenu,
type MediaAction,
} from "@/components/media-context-menu"
import type { BrowseSong } from "@otunes/convex/types"
export function PlayerArtistMenu({
song,
onAction,
children,
}: {
song: BrowseSong
onAction: (action: MediaAction) => void
children: ReactNode
}) {
const [size, setSize] = useState({ width: 0, height: 0 })
return (
<View
style={styles.container}
onLayout={({ nativeEvent: { layout } }) =>
setSize({ width: layout.width, height: layout.height })
}
>
<View
pointerEvents="none"
accessibilityElementsHidden
importantForAccessibility="no-hide-descendants"
>
{children}
</View>
{size.width > 0 ? (
<MediaActionMenu
style={StyleSheet.absoluteFill}
includeActions={["go-album", "go-artist"]}
onAction={onAction}
>
<View
style={size}
accessible
accessibilityRole="button"
accessibilityLabel={song.artist.name}
accessibilityHint="Opens album and artist options"
/>
</MediaActionMenu>
) : null}
</View>
)
}
const styles = StyleSheet.create({
container: { overflow: "hidden" },
})
+54 -8
View File
@@ -1,3 +1,5 @@
import { SongSwipeRow } from "@/components/song-swipe-row"
import type { SwipeRowGroup } from "@/components/ios-swipe-to-remove-row"
import { PaletteText } from "@/components/palette-text"
import { MusicEntityView } from "@/components/music-entity-view"
import { collectionInset } from "@/components/collection-list"
@@ -22,6 +24,8 @@ import {
type MediaMenuConfig,
} from "@/components/media-context-menu"
function consumeLongPress() {}
export const SongRow = memo(function SongRow({
song,
onPress,
@@ -29,6 +33,7 @@ export const SongRow = memo(function SongRow({
trailing,
palette,
inset = false,
swipe,
}: {
song: BrowseSong
onPress?: () => void
@@ -37,6 +42,11 @@ export const SongRow = memo(function SongRow({
}
trailing?: React.ReactNode
palette?: Pick<CoverPalette, "foreground" | "secondaryForeground" | "divider">
swipe?: {
group: SwipeRowGroup
onPlayNext: () => void
backgroundColor: string
}
/** Album/playlist layout: the divider starts at the title, not the artwork. */
inset?: boolean
}) {
@@ -77,13 +87,8 @@ export const SongRow = memo(function SongRow({
</>
)
const row = (
<Pressable
style={
inset ? styles.insetRow : [styles.row, { borderBottomColor: divider }]
}
onPress={onPress}
>
const rowContent = (
<>
<CoverImage
uri={albumCoverUrl(config.serverUrl, song.album.id)}
hasCover={song.album.cover != null}
@@ -96,8 +101,31 @@ export const SongRow = memo(function SongRow({
) : (
content
)}
</Pressable>
</>
)
const rowStyle = inset
? styles.insetRow
: [styles.row, { borderBottomColor: divider }]
const row =
swipe && Platform.OS === "ios" ? (
<SongSwipeRow
group={swipe.group}
onPlayNext={swipe.onPlayNext}
backgroundColor={swipe.backgroundColor}
onPress={onPress}
style={rowStyle}
>
{rowContent}
</SongSwipeRow>
) : (
<Pressable
style={rowStyle}
onPress={onPress}
onLongPress={contextMenu ? consumeLongPress : undefined}
>
{rowContent}
</Pressable>
)
if (!contextMenu) {
return (
@@ -127,6 +155,7 @@ export const SongListRow = memo(function SongListRow({
playlistLabel,
palette,
inset,
swipe,
onPlay,
onAction,
}: {
@@ -135,9 +164,15 @@ export const SongListRow = memo(function SongListRow({
playlistLabel?: string
palette?: Pick<CoverPalette, "foreground" | "secondaryForeground" | "divider">
inset?: boolean
swipe?: {
group: SwipeRowGroup
onPlayNext: () => void
backgroundColor: string
}
onPlay: (song: BrowseSong) => void
onAction: (song: BrowseSong, action: MediaAction) => void
}) {
const { config } = useServerConfig()
const handlePress = useCallback(() => onPlay(song), [onPlay, song])
return (
@@ -145,8 +180,19 @@ export const SongListRow = memo(function SongListRow({
song={song}
palette={palette}
inset={inset}
swipe={swipe}
onPress={handlePress}
contextMenu={{
preview: config
? {
kind: "song",
title: song.title,
artist: song.artist.name,
album: song.album.title,
artworkUrl: albumCoverUrl(config.serverUrl, song.album.id),
hasArtwork: song.album.cover != null,
}
: undefined,
stationArtistId: song.artist.id,
favorited,
playlistLabel,
+332
View File
@@ -0,0 +1,332 @@
import * as Haptics from "expo-haptics"
import { SymbolView } from "expo-symbols"
import {
useCallback,
useEffect,
useMemo,
useState,
type ReactNode,
} from "react"
import {
Pressable,
StyleSheet,
type StyleProp,
type ViewStyle,
} from "react-native"
import { Gesture, GestureDetector } from "react-native-gesture-handler"
import Animated, {
type SharedValue,
cancelAnimation,
Extrapolation,
interpolate,
useAnimatedStyle,
useSharedValue,
withSpring,
} from "react-native-reanimated"
import { scheduleOnRN } from "react-native-worklets"
import type { SwipeRowGroup } from "@/components/ios-swipe-to-remove-row"
const ACTION_WIDTH = 82
const SPRING = { duration: 450, dampingRatio: 1 }
function impact() {
void Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium)
}
/** A tap competes with the swipe so dragging a moving row never starts playback. */
export function SongSwipeRow({
children,
style,
backgroundColor,
group,
onPress,
onPlayNext,
}: {
children: ReactNode
style?: StyleProp<ViewStyle>
backgroundColor: string
group: SwipeRowGroup
onPress?: () => void
onPlayNext: () => void
}) {
const x = useSharedValue(0)
const startX = useSharedValue(0)
const width = useSharedValue(0)
const height = useSharedValue(0)
const committed = useSharedValue(false)
const commitProgress = useSharedValue(0)
const [open, setOpen] = useState<-1 | 0 | 1>(0)
const close = useCallback(() => {
x.value = withSpring(0, SPRING)
committed.value = false
commitProgress.value = withSpring(0, SPRING)
setOpen(0)
group.clear(close)
}, [commitProgress, committed, group, x])
useEffect(() => () => group.clear(close), [close, group])
const begin = useCallback(() => group.setOpen(close), [close, group])
const settle = useCallback(
(side: -1 | 0 | 1) => {
setOpen(side)
if (!side) group.clear(close)
},
[close, group]
)
const activate = useCallback(
(side: number) => {
close()
if (side > 0) onPlayNext()
// Download is intentionally a placeholder.
},
[close, onPlayNext]
)
const tapRow = useCallback(() => {
if (open) close()
else onPress?.()
}, [close, onPress, open])
const gesture = useMemo(() => {
const pan = Gesture.Pan()
// Leave the screen's leading edge available for native back navigation.
.hitSlop({ left: -24 })
.activeOffsetX([-10, 10])
.failOffsetY([-10, 10])
.onStart(() => {
cancelAnimation(x)
startX.value = x.value
scheduleOnRN(begin)
})
.onUpdate((event) => {
x.value = Math.max(
-width.value,
Math.min(width.value, startX.value + event.translationX)
)
const commit =
Math.abs(x.value) > Math.max(ACTION_WIDTH, width.value * 0.55)
if (commit !== committed.value) {
committed.value = commit
commitProgress.value = withSpring(commit ? 1 : 0, SPRING)
scheduleOnRN(impact)
}
})
.onEnd((event) => {
const side = x.value > 0 ? 1 : -1
if (committed.value) {
x.value = withSpring(0, SPRING)
scheduleOnRN(activate, side)
return
}
const projected = x.value + event.velocityX * 0.12
const target =
Math.abs(x.value) > 10 && projected * side > ACTION_WIDTH / 2
? side
: 0
x.value = withSpring(target * ACTION_WIDTH, {
...SPRING,
velocity: event.velocityX,
})
scheduleOnRN(settle, target)
})
.onFinalize((_event, success) => {
if (!success) {
x.value = withSpring(0, SPRING)
scheduleOnRN(close)
}
})
const tap = Gesture.Tap()
.maxDistance(10)
.maxDuration(500)
.onEnd((_event, success) => {
if (success) scheduleOnRN(tapRow)
})
return Gesture.Exclusive(pan, tap)
}, [
activate,
begin,
close,
commitProgress,
committed,
settle,
startX,
tapRow,
width,
x,
])
const rowStyle = useAnimatedStyle(() => ({
transform: [{ translateX: x.value }],
borderRadius: interpolate(
Math.abs(x.value),
[0, 40],
[0, height.value / 2],
Extrapolation.CLAMP
),
}))
const fillStyle = useAnimatedStyle(() => ({
opacity: interpolate(
Math.abs(x.value),
[0, 40],
[0, 1],
Extrapolation.CLAMP
),
}))
return (
<Animated.View
style={styles.container}
onLayout={(event) => {
width.value = event.nativeEvent.layout.width
height.value = event.nativeEvent.layout.height
}}
>
<SwipeAction
side={1}
x={x}
height={height}
progress={commitProgress}
open={open === 1}
onPress={() => activate(1)}
/>
<SwipeAction
side={-1}
x={x}
height={height}
progress={commitProgress}
open={open === -1}
onPress={() => activate(-1)}
/>
<GestureDetector gesture={gesture}>
<Animated.View
accessible
accessibilityRole="button"
onAccessibilityTap={tapRow}
accessibilityActions={[
{ name: "playNext", label: "Play Next" },
{ name: "download", label: "Download" },
]}
onAccessibilityAction={(event) => {
if (event.nativeEvent.actionName === "playNext") activate(1)
}}
style={[styles.row, { backgroundColor }, rowStyle]}
>
<Animated.View
pointerEvents="none"
style={[StyleSheet.absoluteFill, styles.fill, fillStyle]}
/>
<Animated.View style={style}>{children}</Animated.View>
</Animated.View>
</GestureDetector>
</Animated.View>
)
}
function SwipeAction({
side,
x,
height,
progress,
open,
onPress,
}: {
side: 1 | -1
x: SharedValue<number>
height: SharedValue<number>
progress: SharedValue<number>
open: boolean
onPress: () => void
}) {
const actionStyle = useAnimatedStyle(() => ({
width: Math.max(0, x.value * side),
}))
const pillStyle = useAnimatedStyle(() => {
const revealed = Math.max(0, x.value * side)
const resting = Math.max(
0,
Math.min(
revealed - 20,
interpolate(revealed, [24, ACTION_WIDTH, 220], [0, 62, 200])
)
)
const pillWidth =
resting + (Math.max(0, revealed - 12) - resting) * progress.value
const restingHeight = interpolate(
pillWidth,
[0, 24, 62, 140, 200],
[0, 0.3, 0.77, 0.82, 0.86].map((ratio) => ratio * height.value),
Extrapolation.CLAMP
)
const pillHeight =
restingHeight +
(Math.max(0, height.value - 12) - restingHeight) * progress.value
return {
width: pillWidth,
height: pillHeight,
borderRadius: pillHeight / 2,
}
})
const iconStyle = useAnimatedStyle(() => ({
transform: [
{
scale: interpolate(
x.value * side,
[24, 64],
[0, 1],
Extrapolation.CLAMP
),
},
],
}))
return (
<Animated.View
pointerEvents={open ? "auto" : "none"}
accessibilityElementsHidden={!open}
importantForAccessibility={open ? "auto" : "no-hide-descendants"}
style={[
styles.action,
side > 0 ? styles.leading : styles.trailing,
actionStyle,
]}
>
<Pressable
accessibilityRole="button"
accessibilityLabel={side > 0 ? "Play Next" : "Download"}
onPress={onPress}
style={styles.actionButton}
>
<Animated.View
style={[
styles.pill,
{ backgroundColor: side > 0 ? "#6b5bff" : "#008dff" },
pillStyle,
]}
>
<Animated.View style={iconStyle}>
<SymbolView
name={
side > 0
? "text.line.first.and.arrowtriangle.forward"
: "arrow.down"
}
size={22}
tintColor="white"
/>
</Animated.View>
</Animated.View>
</Pressable>
</Animated.View>
)
}
const styles = StyleSheet.create({
container: { overflow: "hidden" },
row: { overflow: "hidden" },
fill: { backgroundColor: "rgba(128,128,128,0.16)" },
action: { position: "absolute", top: 0, bottom: 0 },
leading: { left: 0 },
trailing: { right: 0 },
actionButton: { flex: 1, alignItems: "center", justifyContent: "center" },
pill: { alignItems: "center", justifyContent: "center", overflow: "hidden" },
})
+1 -1
View File
@@ -1007,7 +1007,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
convex,
cacheScope,
api.library.browseSortedSongIds,
sort
{ ...sort, fromSongId: songId }
)
} catch {
return
+5 -8
View File
@@ -3,15 +3,15 @@
import * as React from "react"
import Link from "next/link"
import {
Disc3,
GalleryVerticalEnd,
ListEnd,
ListPlus,
ListStart,
MicVocal,
MoreHorizontal,
Share2,
Shuffle,
Star,
User,
} from "lucide-react"
import { Button } from "@/components/ui/button"
@@ -22,10 +22,7 @@ import {
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import {
copyMediaShareLink,
type MediaShareContext,
} from "@/lib/media-links"
import { copyMediaShareLink, type MediaShareContext } from "@/lib/media-links"
import { cn } from "@/lib/utils"
export type MediaContextAction =
@@ -141,7 +138,7 @@ export function MediaContextMenuItems({
<DropdownMenuItem asChild className="justify-between">
<Link href={`/artist/${artist.id}`}>
Go to Artist
<User />
<MicVocal />
</Link>
</DropdownMenuItem>
) : null}
@@ -149,7 +146,7 @@ export function MediaContextMenuItems({
<DropdownMenuItem asChild className="justify-between">
<Link href={`/album/${album.id}`}>
Go to Album
<Disc3 />
<GalleryVerticalEnd />
</Link>
</DropdownMenuItem>
) : null}
+1 -1
View File
@@ -1197,7 +1197,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
convex,
cacheScope,
api.library.browseSortedSongIds,
sort
{ ...sort, fromSongId: songId }
)
} catch {
return
+454 -466
View File
@@ -61,512 +61,494 @@ async function cleanHlsStreamsOlderThan(maxIdleMs: number) {
return { checked, removed, failed }
}
async function coverPathIfExists(path: string): Promise<string | null> {
return fs
.access(path)
.then(() => path)
.catch(() => null)
}
const SCAN_PAGE_SIZE = 2000
/** Outlives the 10 minute action timeout so a crashed scan frees the lock. */
const SCAN_LOCK_LEASE_MS = 15 * 60 * 1000
/** New files left unparsed after this are picked up by the next scan. */
const SCAN_PARSE_BUDGET_MS = 6 * 60 * 1000
const SCAN_BATCH_SIZE = 200
const FS_CONCURRENCY = 16
async function syncArtistCover(
ctx: ActionCtx,
codec: string,
dbCover: string | null,
diskPath: string,
id: Id<"artists">
) {
const onDisk = await coverPathIfExists(diskPath)
if (onDisk !== null) {
if (dbCover !== onDisk) {
await ctx.runMutation(internal.library.updateArtistCover, {
id,
cover: onDisk,
})
type Page<T> = { page: T[]; isDone: boolean; continueCursor: string }
/** Reads every page of an internal paginated query. */
async function collectPages<T>(
load: (cursor: string | null) => Promise<Page<T>>
): Promise<T[]> {
const items: T[] = []
let cursor: string | null = null
for (;;) {
const result = await load(cursor)
items.push(...result.page)
if (result.isDone) {
return items
}
} else if (dbCover !== null && dbCover.includes(codec)) {
await ctx.runMutation(internal.library.updateArtistCover, {
id,
cover: null,
})
cursor = result.continueCursor
}
}
async function syncAlbumCover(
ctx: ActionCtx,
codec: string,
dbCover: string | null,
diskPath: string,
id: Id<"albums">
) {
const onDisk = await coverPathIfExists(diskPath)
if (onDisk !== null) {
if (dbCover !== onDisk) {
await ctx.runMutation(internal.library.updateAlbumCover, {
id,
cover: onDisk,
})
/** Like `Promise.all(items.map(fn))` with at most `limit` calls in flight. */
async function mapLimit<T, R>(
items: T[],
limit: number,
fn: (item: T) => Promise<R>
): Promise<R[]> {
const results = new Array<R>(items.length)
let next = 0
const worker = async () => {
while (next < items.length) {
const index = next++
results[index] = await fn(items[index])
}
} else if (dbCover !== null && dbCover.includes(codec)) {
await ctx.runMutation(internal.library.updateAlbumCover, {
id,
cover: null,
})
}
await Promise.all(
Array.from({ length: Math.min(limit, items.length) }, worker)
)
return results
}
function chunk<T>(items: T[], size: number): T[][] {
const chunks: T[][] = []
for (let i = 0; i < items.length; i += size) {
chunks.push(items.slice(i, i + size))
}
return chunks
}
async function readDirents(dir: string): Promise<fsSync.Dirent[]> {
try {
return await fs.readdir(dir, { withFileTypes: true })
} catch (error) {
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
return []
}
throw error
}
}
async function purgeRemovedSongs(ctx: ActionCtx, ids: Id<"songs">[]) {
if (ids.length === 0) {
return
}
await ctx.runMutation(internal.library.purgeSongsFromUsersAndPlaylists, {
ids,
type DiskAlbum = {
codec: string
artistFolder: string
albumFolder: string
files: string[]
}
type DiskLibrary = {
/** Every audio file path. */
files: Set<string>
/** Every `folder.jpg` / `cover.jpg` path. */
covers: Set<string>
albums: DiskAlbum[]
}
/** Lists `DATA_DIR/<codec>/<artist>/<album>/<file>` with one readdir per directory. */
async function walkLibrary(codecs: string[]): Promise<DiskLibrary> {
const files = new Set<string>()
const covers = new Set<string>()
const artistDirs = (
await Promise.all(
codecs.map(async (codec) =>
(await readDirents(`${DATA_DIR}/${codec}`))
.filter((dirent) => dirent.isDirectory())
.map((dirent) => ({ codec, artistFolder: dirent.name }))
)
)
).flat()
const albumDirs = (
await mapLimit(
artistDirs,
FS_CONCURRENCY,
async ({ codec, artistFolder }) => {
const artistPath = `${DATA_DIR}/${codec}/${artistFolder}`
const dirents = await readDirents(artistPath)
if (dirents.some((d) => !d.isDirectory() && d.name === "folder.jpg")) {
covers.add(`${artistPath}/folder.jpg`)
}
return dirents
.filter((dirent) => dirent.isDirectory())
.map((dirent) => ({ codec, artistFolder, albumFolder: dirent.name }))
}
)
).flat()
const albums = await mapLimit(albumDirs, FS_CONCURRENCY, async (album) => {
const albumPath = `${DATA_DIR}/${album.codec}/${album.artistFolder}/${album.albumFolder}`
const albumFiles: string[] = []
for (const dirent of await readDirents(albumPath)) {
if (dirent.isDirectory()) {
continue
}
const filePath = `${albumPath}/${dirent.name}`
if (dirent.name === "cover.jpg") {
covers.add(filePath)
} else {
files.add(filePath)
albumFiles.push(filePath)
}
}
return { ...album, files: albumFiles }
})
return { files, covers, albums }
}
async function purgeRemovedAlbums(ctx: ActionCtx, ids: Id<"albums">[]) {
if (ids.length === 0) {
return
/**
* Keeps the current cover while its file exists, otherwise the first codec's
* cover on disk. Covers outside the data directory are left alone.
*/
function pickCover(
current: string | null,
candidates: string[],
covers: Set<string>
): string | null {
if (
current !== null &&
(covers.has(current) || !current.startsWith(`${DATA_DIR}/`))
) {
return current
}
await ctx.runMutation(internal.library.purgeAlbumsFromUsers, { ids })
return candidates.find((candidate) => covers.has(candidate)) ?? null
}
async function purgeRemovedArtists(ctx: ActionCtx, ids: Id<"artists">[]) {
if (ids.length === 0) {
return
/** Untagged years are stored as 0 / "", which both clients treat as unknown. */
function releaseDate(common: music.ICommonTagsResult) {
if (common.date) {
return common.date
}
await ctx.runMutation(internal.library.purgeArtistsFromUsers, { ids })
return common.year ? `01-01-${common.year}` : ""
}
async function removeCodecFromDatabase(ctx: ActionCtx, codec: string) {
const allSongs = await ctx.runQuery(internal.library.getSongs)
type NewSong = {
name: string
files: Record<string, string>
artist: string | string[]
length: number
diskNumber: number | null
track: number
year: number
date: string
genres: string[]
}
const songsToRemove: Id<"songs">[] = []
const songsToUpdate: { id: Id<"songs">; files: Record<string, string> }[] = []
for (const song of allSongs) {
if (!(codec in song.files)) {
type ImportEntry = {
artist: { name: string; folder: string; cover: string | null }
album: {
name: string
folder: string
cover: string | null
year: number
date: string
}
songs: Map<string, NewSong>
}
/**
* Reconciles the database with the files on disk. Reads the library once in
* pages, walks the disk once, parses only untracked files and writes only
* what changed, so re-scanning an unchanged library makes no writes.
*/
async function scanLibrary(ctx: ActionCtx) {
const startedAt = Date.now()
const codecs = (await fs.readdir(DATA_DIR, { withFileTypes: true }))
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name)
const [artists, albums, songs, disk] = await Promise.all([
collectPages((cursor) =>
ctx.runQuery(internal.library.scanArtistsPage, {
paginationOpts: { numItems: SCAN_PAGE_SIZE, cursor },
})
),
collectPages((cursor) =>
ctx.runQuery(internal.library.scanAlbumsPage, {
paginationOpts: { numItems: SCAN_PAGE_SIZE, cursor },
})
),
collectPages((cursor) =>
ctx.runQuery(internal.library.scanSongsPage, {
paginationOpts: { numItems: SCAN_PAGE_SIZE, cursor },
})
),
walkLibrary(codecs),
])
if (disk.files.size === 0 && songs.length > 0) {
throw new Error(
`No audio files found under ${DATA_DIR}; refusing to empty the library`
)
}
// Drop file paths that are gone from disk (including removed codecs). Songs
// left without files are deleted below unless a new file matches them.
const songFiles = new Map<Id<"songs">, Record<string, string>>()
const changedSongs = new Set<Id<"songs">>()
const songsByAlbum = new Map<Id<"albums">, Map<string, Id<"songs">>>()
const tracked = new Set<string>()
for (const song of songs) {
const entries = Object.entries(song.files)
const kept = entries.filter(([, filePath]) => disk.files.has(filePath))
if (kept.length !== entries.length) {
changedSongs.add(song._id)
}
songFiles.set(song._id, Object.fromEntries(kept))
for (const [, filePath] of kept) {
tracked.add(filePath)
}
const byName = songsByAlbum.get(song.album) ?? new Map()
if (!byName.has(song.name)) {
byName.set(song.name, song._id)
}
songsByAlbum.set(song.album, byName)
}
// Parse untracked files only.
const untracked = disk.albums.flatMap((album) =>
album.files
.filter((filePath) => !tracked.has(filePath))
.map((filePath) => ({ ...album, filePath }))
)
const deadline = startedAt + SCAN_PARSE_BUDGET_MS
const parsed = await mapLimit(untracked, FS_CONCURRENCY, async (file) => {
if (Date.now() > deadline) {
return null
}
try {
const metadata = await music.parseFile(file.filePath, {
skipCovers: true,
})
return { ...file, metadata }
} catch {
return null
}
})
const skipped = parsed.filter((file) => file === null).length
const artistByFolder = new Map(
artists.map((artist) => [artist.folder, artist])
)
const albumByKey = new Map(
albums.map((album) => [`${album.artist}/${album.folder}`, album])
)
const imports = new Map<string, ImportEntry>()
for (const file of parsed) {
if (file === null) {
continue
}
const { codec, artistFolder, albumFolder, filePath, metadata } = file
const common = metadata.common
const title = common.title || path.parse(filePath).name
const dbArtist = artistByFolder.get(artistFolder)
const dbAlbum = dbArtist
? albumByKey.get(`${dbArtist._id}/${albumFolder}`)
: undefined
const existingId = dbAlbum && songsByAlbum.get(dbAlbum._id)?.get(title)
if (existingId) {
// A same-titled sibling must not steal a path that still exists.
const files = songFiles.get(existingId)!
if (!(codec in files)) {
songFiles.set(existingId, { ...files, [codec]: filePath })
changedSongs.add(existingId)
}
continue
}
const { [codec]: _, ...update } = song.files
if (Object.keys(update).length === 0) {
songsToRemove.push(song._id)
} else {
songsToUpdate.push({
id: song._id,
files: update,
})
const key = `${artistFolder}/${albumFolder}`
let entry = imports.get(key)
if (!entry) {
entry = {
artist: {
name: common.albumartist || artistFolder,
folder: artistFolder,
cover: pickCover(
null,
codecs.map((c) => `${DATA_DIR}/${c}/${artistFolder}/folder.jpg`),
disk.covers
),
},
album: {
name: common.album || albumFolder,
folder: albumFolder,
cover: pickCover(
null,
codecs.map(
(c) => `${DATA_DIR}/${c}/${artistFolder}/${albumFolder}/cover.jpg`
),
disk.covers
),
year: common.year || 0,
date: releaseDate(common),
},
songs: new Map(),
}
imports.set(key, entry)
}
const pending = entry.songs.get(title)
if (pending) {
pending.files[codec] ??= filePath
continue
}
entry.songs.set(title, {
name: title,
files: { [codec]: filePath },
artist:
common.artist ?? common.artists ?? dbArtist?.name ?? entry.artist.name,
length: Math.round(metadata.format.duration || 0),
diskNumber: common.disk.of === 1 ? null : common.disk.no,
track: common.track.no || 0,
year: common.year || 0,
date: releaseDate(common),
genres: common.genre || [],
})
}
await ctx.runMutation(internal.library.changeFilesForSongs, {
songs: songsToUpdate,
})
await purgeRemovedSongs(ctx, songsToRemove)
await ctx.runMutation(internal.library.removeSongs, { ids: songsToRemove })
const allAlbums = await ctx.runQuery(internal.library.getAlbums)
const removedSongIds = new Set(songsToRemove)
const albumsWithSongs = new Set(
allSongs.flatMap((song) =>
removedSongIds.has(song._id) ? [] : [song.album]
)
const deletedSongs = songs.flatMap((song) =>
Object.keys(songFiles.get(song._id)!).length === 0 ? [song._id] : []
)
const albumsToRemove = allAlbums
const deletedSongSet = new Set(deletedSongs)
const updatedSongs = [...changedSongs].filter((id) => !deletedSongSet.has(id))
const albumsWithSongs = new Set(
songs.flatMap((song) => (deletedSongSet.has(song._id) ? [] : [song.album]))
)
const emptyAlbums = albums
.filter((album) => !albumsWithSongs.has(album._id))
.map((album) => album._id)
await purgeRemovedAlbums(ctx, albumsToRemove)
await ctx.runMutation(internal.library.removeAlbums, { ids: albumsToRemove })
const allArtists = await ctx.runQuery(internal.library.getArtists)
const removedAlbumIds = new Set(albumsToRemove)
const emptyAlbumSet = new Set(emptyAlbums)
const artistsWithAlbums = new Set(
allAlbums.flatMap((album) =>
removedAlbumIds.has(album._id) ? [] : [album.artist]
albums.flatMap((album) =>
emptyAlbumSet.has(album._id) ? [] : [album.artist]
)
)
const artistsToRemove = allArtists
const emptyArtists = artists
.filter((artist) => !artistsWithAlbums.has(artist._id))
.map((artist) => artist._id)
await purgeRemovedArtists(ctx, artistsToRemove)
await ctx.runMutation(internal.library.removeArtists, {
ids: artistsToRemove,
const emptyArtistSet = new Set(emptyArtists)
// Albums and artists about to be deleted keep their cover.
const artistById = new Map(artists.map((artist) => [artist._id, artist]))
const artistCovers = artists.flatMap((artist) => {
if (emptyArtistSet.has(artist._id)) {
return []
}
const cover = pickCover(
artist.cover,
codecs.map((c) => `${DATA_DIR}/${c}/${artist.folder}/folder.jpg`),
disk.covers
)
return cover === artist.cover ? [] : [{ id: artist._id, cover }]
})
const albumCovers = albums.flatMap((album) => {
const artist = artistById.get(album.artist)
if (!artist || emptyAlbumSet.has(album._id)) {
return []
}
const cover = pickCover(
album.cover,
codecs.map(
(c) => `${DATA_DIR}/${c}/${artist.folder}/${album.folder}/cover.jpg`
),
disk.covers
)
return cover === album.cover ? [] : [{ id: album._id, cover }]
})
// Writes. Imports run before the empty-album/artist sweeps, and those
// mutations re-check emptiness, so nothing that gained songs is deleted.
for (const batch of chunk(updatedSongs, SCAN_BATCH_SIZE)) {
await ctx.runMutation(internal.library.changeFilesForSongs, {
songs: batch.map((id) => ({ id, files: songFiles.get(id)! })),
})
}
for (const batch of chunk(deletedSongs, SCAN_BATCH_SIZE)) {
await ctx.runMutation(internal.library.deleteSongs, { ids: batch })
}
let importBatch: ImportEntry[] = []
let importBatchSongs = 0
const flushImports = async () => {
if (importBatch.length === 0) {
return
}
await ctx.runMutation(internal.library.importAlbums, {
albums: importBatch.map((entry) => ({
...entry,
songs: [...entry.songs.values()],
})),
})
importBatch = []
importBatchSongs = 0
}
for (const entry of imports.values()) {
importBatch.push(entry)
importBatchSongs += entry.songs.size
if (importBatchSongs >= SCAN_BATCH_SIZE) {
await flushImports()
}
}
await flushImports()
for (const batch of chunk(artistCovers, SCAN_BATCH_SIZE)) {
await ctx.runMutation(internal.library.updateArtistCovers, {
artists: batch,
})
}
for (const batch of chunk(albumCovers, SCAN_BATCH_SIZE)) {
await ctx.runMutation(internal.library.updateAlbumCovers, {
albums: batch,
})
}
let removedAlbums = 0
for (const batch of chunk(emptyAlbums, SCAN_BATCH_SIZE)) {
removedAlbums += await ctx.runMutation(internal.library.deleteEmptyAlbums, {
ids: batch,
})
}
let removedArtists = 0
for (const batch of chunk(emptyArtists, SCAN_BATCH_SIZE)) {
removedArtists += await ctx.runMutation(
internal.library.deleteEmptyArtists,
{ ids: batch }
)
}
const importedSongs = [...imports.values()].reduce(
(total, entry) => total + entry.songs.size,
0
)
const summary =
`songs: ${importedSongs} added, ${updatedSongs.length} updated, ` +
`${deletedSongs.length} removed; albums removed: ${removedAlbums}; ` +
`artists removed: ${removedArtists}; covers updated: ` +
`${artistCovers.length + albumCovers.length}; ` +
`files deferred: ${skipped}; ${Date.now() - startedAt}ms`
console.log(`library scan: ${summary}`)
return summary
}
/** Runs one scan under the library scan lock; `null` if one is already running. */
export const scheduledUpdateLibrary = internalAction({
args: {},
handler: async (ctx) => {
const codecs = (await fs.readdir(DATA_DIR, { withFileTypes: true }))
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name)
const dbCodecs = Array.from(
new Map(
(await ctx.runQuery(internal.library.getSongs))
.map((song) => {
return Object.keys(song.files)
})
.flat()
.map((codec) => [codec, codec])
).values()
handler: async (ctx): Promise<string | null> => {
const token = crypto.randomUUID()
const acquired: boolean = await ctx.runMutation(
internal.library.acquireLibraryScanLock,
{ token, leaseMs: SCAN_LOCK_LEASE_MS }
)
for (const dbCodec of dbCodecs) {
if (!codecs.includes(dbCodec)) {
await removeCodecFromDatabase(ctx, dbCodec)
}
if (!acquired) {
console.log("library scan: already running, skipped")
return null
}
// Removals
for (const codec of codecs) {
const dbArtists = await ctx.runQuery(internal.library.getArtists)
const dbAlbums = await ctx.runQuery(internal.library.getAlbums)
const dbSongs = await ctx.runQuery(internal.library.getSongs)
const dirtySongs = dbSongs
.filter((song) => {
return codec in song.files && !fsSync.existsSync(song.files[codec])
})
.map((song) => {
const { [codec]: _, ...update } = song.files
return { id: song._id, files: update }
})
const songsToDelete = dirtySongs.filter(
(song) => Object.keys(song.files).length === 0
)
const songsToUpdate = dirtySongs.filter(
(song) => Object.keys(song.files).length !== 0
)
const songsToDeleteIDs = songsToDelete.map((song) => song.id)
await purgeRemovedSongs(ctx, songsToDeleteIDs)
await ctx.runMutation(internal.library.removeSongs, {
ids: songsToDeleteIDs,
})
await ctx.runMutation(internal.library.changeFilesForSongs, {
songs: songsToUpdate,
})
const deletedSongIds = new Set(songsToDeleteIDs)
const albumsWithSongs = new Set(
dbSongs.flatMap((song) =>
deletedSongIds.has(song._id) ? [] : [song.album]
)
)
const albumsToDelete = dbAlbums
.filter((album) => !albumsWithSongs.has(album._id))
.map((album) => album._id)
await purgeRemovedAlbums(ctx, albumsToDelete)
await ctx.runMutation(internal.library.removeAlbums, {
ids: albumsToDelete,
})
const deletedAlbumIds = new Set(albumsToDelete)
const artistsWithAlbums = new Set(
dbAlbums.flatMap((album) =>
deletedAlbumIds.has(album._id) ? [] : [album.artist]
)
)
const artistsToDelete = dbArtists
.filter((artist) => !artistsWithAlbums.has(artist._id))
.map((artist) => artist._id)
await purgeRemovedArtists(ctx, artistsToDelete)
await ctx.runMutation(internal.library.removeArtists, {
ids: artistsToDelete,
})
try {
return await scanLibrary(ctx)
} finally {
await ctx.runMutation(internal.library.releaseLibraryScanLock, { token })
}
// Additions
for (const codec of codecs) {
// Artists
console.log(`${codec}: artists`)
let dbArtists = await ctx.runQuery(internal.library.getArtists)
const dbArtistFolders = dbArtists.map((artist) => artist.folder)
const artists = (
await fs.readdir(`${DATA_DIR}/${codec}`, { withFileTypes: true })
)
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name)
const potentialArtists = artists.filter(
(artist) => !dbArtistFolders.includes(artist)
)
const newArtists = []
for (const folder of potentialArtists) {
const albums = (
await fs.readdir(`${DATA_DIR}/${codec}/${folder}`, {
withFileTypes: true,
})
)
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name)
if (albums.length === 0) {
continue
}
for (let i = 0; i < albums.length; i++) {
const songs = (
await fs.readdir(`${DATA_DIR}/${codec}/${folder}/${albums[i]}`, {
withFileTypes: true,
})
)
.filter(
(dirent) => !dirent.isDirectory() && dirent.name !== "cover.jpg"
)
.map((dirent) => dirent.name)
if (songs.length === 0) {
continue
}
try {
const metadata = await music.parseFile(
`${DATA_DIR}/${codec}/${folder}/${albums[i]}/${songs[0]}`,
{ skipCovers: true }
)
const artistCoverPath = `${DATA_DIR}/${codec}/${folder}/folder.jpg`
const cover = await coverPathIfExists(artistCoverPath)
newArtists.push({
name: metadata.common.albumartist || folder,
folder,
cover,
})
break
} catch {}
}
}
await ctx.runMutation(internal.library.createArtists, {
artists: newArtists,
})
dbArtists = await ctx.runQuery(internal.library.getArtists)
// Albums
console.log(`${codec}: albums`)
let dbAlbums = await ctx.runQuery(internal.library.getAlbums)
const newAlbums = []
for (const dbArtist of dbArtists) {
const dbAlbumsInArtist = dbAlbums.filter(
(album) => album.artist === dbArtist._id
)
const dbAlbumFolders = dbAlbumsInArtist.map((album) => album.folder)
if (!fsSync.existsSync(`${DATA_DIR}/${codec}/${dbArtist.folder}`)) {
continue
}
const albums = (
await fs.readdir(`${DATA_DIR}/${codec}/${dbArtist.folder}`, {
withFileTypes: true,
})
)
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name)
const potentialAlbums = albums.filter(
(album) => !dbAlbumFolders.includes(album)
)
for (const potentialAlbum of potentialAlbums) {
const potentialAlbumPath = `${DATA_DIR}/${codec}/${dbArtist.folder}/${potentialAlbum}`
if (!fsSync.existsSync(potentialAlbumPath)) {
continue
}
const songs = (
await fs.readdir(potentialAlbumPath, {
withFileTypes: true,
})
)
.filter(
(dirent) => !dirent.isDirectory() && dirent.name !== "cover.jpg"
)
.map((dirent) => dirent.name)
if (songs.length === 0) {
continue
}
try {
const metadata = await music.parseFile(
`${potentialAlbumPath}/${songs[0]}`,
{ skipCovers: true }
)
const albumCoverPath = `${DATA_DIR}/${codec}/${dbArtist.folder}/${potentialAlbum}/cover.jpg`
const cover = await coverPathIfExists(albumCoverPath)
newAlbums.push({
name: metadata.common.album || potentialAlbum,
folder: potentialAlbum,
cover,
year: metadata.common.year || 2026,
date:
metadata.common.date || `01-01-${metadata.common.year || 2026}`,
artist: dbArtist._id,
})
} catch {}
}
}
await ctx.runMutation(internal.library.createAlbums, {
albums: newAlbums,
})
dbAlbums = await ctx.runQuery(internal.library.getAlbums)
const dbSongs = await ctx.runQuery(internal.library.getSongs)
const dbAlbumsArtists = dbAlbums.map((album) => {
return {
...album,
artist: dbArtists.find((artist) => artist._id === album.artist)!,
}
})
// Covers
console.log(`${codec}: covers`)
for (const dbArtist of dbArtists) {
const artistPath = `${DATA_DIR}/${codec}/${dbArtist.folder}`
if (!fsSync.existsSync(artistPath)) {
continue
}
await syncArtistCover(
ctx,
codec,
dbArtist.cover,
`${artistPath}/folder.jpg`,
dbArtist._id
)
}
for (const dbAlbum of dbAlbumsArtists) {
const artistPath = `${DATA_DIR}/${codec}/${dbAlbum.artist.folder}`
const albumPath = `${artistPath}/${dbAlbum.folder}`
if (!fsSync.existsSync(artistPath) || !fsSync.existsSync(albumPath)) {
continue
}
await syncAlbumCover(
ctx,
codec,
dbAlbum.cover,
`${albumPath}/cover.jpg`,
dbAlbum._id
)
}
// Songs
console.log(`${codec}: songs`)
const newSongs = []
const updateSongs = []
for (const dbAlbum of dbAlbumsArtists) {
const dbSongsInAlbum = dbSongs.filter(
(song) => song.album === dbAlbum._id
)
const dbAlreadyTracked = dbSongsInAlbum
.filter((song) => codec in song.files)
.map((song) => song.files[codec])
const artistPath = `${DATA_DIR}/${codec}/${dbAlbum.artist.folder}`
const albumPath = `${artistPath}/${dbAlbum.folder}`
if (!fsSync.existsSync(artistPath) || !fsSync.existsSync(albumPath)) {
continue
}
const songs = (await fs.readdir(albumPath, { withFileTypes: true }))
.filter(
(dirent) => !dirent.isDirectory() && dirent.name !== "cover.jpg"
)
.map((dirent) => dirent.name)
.filter((song) => !dbAlreadyTracked.includes(`${albumPath}/${song}`))
for (const song of songs) {
try {
const path = `${DATA_DIR}/${codec}/${dbAlbum.artist.folder}/${dbAlbum.folder}/${song}`
const metadata = await music.parseFile(path, { skipCovers: true })
if (!metadata.common.title) {
continue
}
const existingSong = dbSongsInAlbum.find(
(dbSong) => dbSong.name === metadata.common.title
)
if (existingSong) {
if (
!(codec in existingSong.files) ||
existingSong.files[codec] !== path
) {
const update = { [codec]: path, ...existingSong.files }
updateSongs.push({
id: existingSong._id,
files: update,
})
}
} else {
let files = { [codec]: path }
if (codec !== "atmos") {
const artistPath = `${DATA_DIR}/atmos/${dbAlbum.artist.folder}`
const albumPath = `${artistPath}/${dbAlbum.folder}`
const path = `${albumPath}/${song}`
if (fsSync.existsSync(path)) {
files = { atmos: path, ...files }
}
}
if (codec !== "lossless") {
const artistPath = `${DATA_DIR}/lossless/${dbAlbum.artist.folder}`
const albumPath = `${artistPath}/${dbAlbum.folder}`
const path = `${albumPath}/${song}`
if (fsSync.existsSync(path)) {
files = { lossless: path, ...files }
}
}
if (codec !== "normal") {
const artistPath = `${DATA_DIR}/normal/${dbAlbum.artist.folder}`
const albumPath = `${artistPath}/${dbAlbum.folder}`
const path = `${albumPath}/${song}`
if (fsSync.existsSync(path)) {
files = { normal: path, ...files }
}
}
newSongs.push({
name: metadata.common.title,
files,
album: dbAlbum._id,
artist:
metadata.common.artist ??
metadata.common.artists ??
dbAlbum.artist.name,
albumArtist: dbAlbum.artist._id,
length: Math.round(metadata.format.duration || 0),
diskNumber:
metadata.common.disk.of === 1
? null
: metadata.common.disk.no,
track: metadata.common.track.no || 0,
year: metadata.common.year || 2026,
date:
metadata.common.date ||
`01-01-${metadata.common.year || 2026}`,
genres: metadata.common.genre || [],
})
}
} catch {}
}
}
await ctx.runMutation(internal.library.createSongs, { songs: newSongs })
await ctx.runMutation(internal.library.changeFilesForSongs, {
songs: updateSongs,
})
}
return "success"
},
})
@@ -574,7 +556,13 @@ export const updateLibrary = action({
args: {},
handler: async (ctx): Promise<string> => {
await requireAdmin(ctx)
return ctx.runAction(internal.actions.scheduledUpdateLibrary)
const result: string | null = await ctx.runAction(
internal.actions.scheduledUpdateLibrary
)
if (result === null) {
throw new ConvexError("A library scan is already running")
}
return result
},
})
+526 -286
View File
@@ -3,7 +3,7 @@ import { getAuthUserId } from "@convex-dev/auth/server"
import { internalMutation, internalQuery, query } from "./_generated/server"
import { ConvexError, v } from "convex/values"
import type { Doc, Id } from "./_generated/dataModel"
import type { QueryCtx } from "./_generated/server"
import type { MutationCtx, QueryCtx } from "./_generated/server"
import type { PaginationOptions, PaginationResult } from "convex/server"
import type {
BrowseAlbum,
@@ -280,24 +280,17 @@ export const browseAlbumsPaginated = query({
},
})
const MAX_BROWSE_SONGS = 500
export const browseSongs = query({
args: {},
handler: async (ctx) => {
const artistMap = await loadArtistMap(ctx)
const albums = await ctx.db.query("albums").collect()
const albumMap = new Map(albums.map((album) => [album._id, album]))
const songs = await ctx.db.query("songs").collect()
return songs
.flatMap((song) => {
const album = albumMap.get(song.album)
const albumArtist = artistMap.get(song.albumArtist)
if (!album || !albumArtist) {
return []
}
return [toBrowseSong(song, album, albumArtist)]
})
.sort((a, b) => a.title.localeCompare(b.title))
const songs = await ctx.db
.query("songs")
.withIndex("by_name")
.order("asc")
.take(MAX_BROWSE_SONGS)
return browseSongDocs(ctx, songs)
},
})
@@ -429,49 +422,121 @@ async function paginateSongsByGroup(
}
}
const MAX_LIBRARY_QUEUE_SONGS = 1000
/**
* Up to {@link MAX_LIBRARY_QUEUE_SONGS} library song ids in the order
* `browseSongsPaginated` lists them, starting at `fromSongId` when given.
*/
export async function getSortedLibrarySongIds(
ctx: QueryCtx,
sortKey: SongsSortKey,
sortDirection: SongsSortDirection
sortDirection: SongsSortDirection,
fromSongId?: Id<"songs">
): Promise<Id<"songs">[]> {
if (sortKey === "title") {
const songs = await ctx.db
.query("songs")
.withIndex("by_name")
.order(sortDirection)
.collect()
return songs.map((song) => song._id)
const from = fromSongId ? await ctx.db.get("songs", fromSongId) : null
if (fromSongId && !from) {
return []
}
const songs = await ctx.db.query("songs").collect()
const groupNames =
if (sortKey === "title") {
const songs = ctx.db
.query("songs")
.withIndex("by_name", (q) =>
from === null
? q
: sortDirection === "asc"
? q.gte("name", from.name)
: q.lte("name", from.name)
)
.order(sortDirection)
const ids: Id<"songs">[] = []
for await (const song of songs) {
if (ids.length === 0 && from !== null && song._id !== from._id) {
continue
}
ids.push(song._id)
if (ids.length >= MAX_LIBRARY_QUEUE_SONGS) {
break
}
}
return ids
}
const fromGroup =
from === null
? null
: sortKey === "artist"
? await ctx.db.get("artists", from.albumArtist)
: await ctx.db.get("albums", from.album)
if (from !== null && fromGroup === null) {
return []
}
const groups =
sortKey === "artist"
? new Map(
[...(await loadArtistMap(ctx)).values()].map((artist) => [
artist._id as string,
artist.name,
])
)
: new Map(
(await ctx.db.query("albums").collect()).map((album) => [
album._id as string,
album.name,
])
)
const validSongs = songs.filter((song) =>
groupNames.has(sortKey === "artist" ? song.albumArtist : song.album)
)
validSongs.sort((a, b) => {
const groupA =
groupNames.get(sortKey === "artist" ? a.albumArtist : a.album) ?? ""
const groupB =
groupNames.get(sortKey === "artist" ? b.albumArtist : b.album) ?? ""
return (
compareSongNames(groupA, groupB, sortDirection) ||
compareSongNames(a.name, b.name, sortDirection)
)
})
return validSongs.map((song) => song._id)
? ctx.db
.query("artists")
.withIndex("by_name", (q) =>
fromGroup === null
? q
: sortDirection === "asc"
? q.gte("name", fromGroup.name)
: q.lte("name", fromGroup.name)
)
.order(sortDirection)
: ctx.db
.query("albums")
.withIndex("by_name", (q) =>
fromGroup === null
? q
: sortDirection === "asc"
? q.gte("name", fromGroup.name)
: q.lte("name", fromGroup.name)
)
.order(sortDirection)
const ids: Id<"songs">[] = []
let started = fromGroup === null
for await (const group of groups) {
if (!started) {
if (group._id !== fromGroup?._id) {
continue
}
started = true
}
const songs =
sortKey === "artist"
? await ctx.db
.query("songs")
.withIndex("by_artist", (q) =>
q.eq("albumArtist", group._id as Id<"artists">)
)
.collect()
: await ctx.db
.query("songs")
.withIndex("by_album", (q) =>
q.eq("album", group._id as Id<"albums">)
)
.collect()
songs.sort((a, b) => compareSongNames(a.name, b.name, sortDirection))
const start =
ids.length === 0 && from !== null
? Math.max(
0,
songs.findIndex((song) => song._id === from._id)
)
: 0
for (const song of songs.slice(start)) {
ids.push(song._id)
if (ids.length >= MAX_LIBRARY_QUEUE_SONGS) {
return ids
}
}
}
return ids
}
export const browseSongsPaginated = query({
@@ -498,9 +563,15 @@ export const browseSortedSongIds = query({
args: {
sortKey: songsSortKeyValidator,
sortDirection: songsSortDirectionValidator,
fromSongId: v.optional(v.id("songs")),
},
handler: async (ctx, args) => {
return getSortedLibrarySongIds(ctx, args.sortKey, args.sortDirection)
return getSortedLibrarySongIds(
ctx,
args.sortKey,
args.sortDirection,
args.fromSongId
)
},
})
@@ -535,7 +606,7 @@ export const browseAlbumDetail = query({
return {
...toBrowseAlbum(album, artist),
year: primarySong?.year ?? null,
year: primarySong?.year || null,
genre: primarySong?.genres[0] ?? null,
releaseDate: album.date,
hasLossless: songs.some((song) => "lossless" in song.files),
@@ -569,7 +640,7 @@ async function browseAlbumSummaryHandler(
return {
...toBrowseAlbum(album, artist),
year: primarySong?.year ?? null,
year: primarySong?.year || null,
genre: primarySong?.genres[0] ?? null,
releaseDate: album.date,
hasLossless: songs.some((song) => "lossless" in song.files),
@@ -647,6 +718,15 @@ function matchScore(text: string, query: string): number {
if (lower.includes(query)) {
return 40
}
// Every query word starts a word of the text, in any order.
const words = lower.split(/\s+/)
if (
query
.split(/\s+/)
.every((term) => words.some((word) => word.startsWith(term)))
) {
return 20
}
return 0
}
@@ -660,6 +740,16 @@ function matchScoreSongArtists(
)
}
const SEARCH_ARTIST_LIMIT = 20
const SEARCH_ALBUM_LIMIT = 40
const SEARCH_SONG_LIMIT = 100
/** Top matched artists/albums whose albums and songs are also returned. */
const SEARCH_RELATED_GROUPS = 3
function uniqueDocs<T extends { _id: string }>(docs: T[]): T[] {
return [...new Map(docs.map((doc) => [doc._id, doc])).values()]
}
export const searchLibrary = query({
args: { query: v.string() },
handler: async (ctx, args) => {
@@ -673,23 +763,87 @@ export const searchLibrary = query({
}
}
const artistMap = await loadArtistMap(ctx)
const albums = await ctx.db.query("albums").collect()
const albumMap = new Map(albums.map((album) => [album._id, album]))
const songs = await ctx.db.query("songs").collect()
const searchText = args.query.trim()
const [matchedArtists, matchedAlbums, matchedSongs] = await Promise.all([
ctx.db
.query("artists")
.withSearchIndex("search_name", (q) => q.search("name", searchText))
.take(SEARCH_ARTIST_LIMIT),
ctx.db
.query("albums")
.withSearchIndex("search_name", (q) => q.search("name", searchText))
.take(SEARCH_ALBUM_LIMIT),
ctx.db
.query("songs")
.withSearchIndex("search_name", (q) => q.search("name", searchText))
.take(SEARCH_SONG_LIMIT),
])
const artistResults: Extract<SearchTopResult, { kind: "artist" }>[] = [
...artistMap.values(),
const relatedArtists = matchedArtists.slice(0, SEARCH_RELATED_GROUPS)
const relatedAlbums = matchedAlbums.slice(0, SEARCH_RELATED_GROUPS)
const [albumsByArtist, songsByArtist, songsByAlbum] = await Promise.all([
Promise.all(
relatedArtists.map((artist) =>
ctx.db
.query("albums")
.withIndex("by_artist", (q) => q.eq("artist", artist._id))
.take(SEARCH_ALBUM_LIMIT)
)
),
Promise.all(
relatedArtists.map((artist) =>
ctx.db
.query("songs")
.withIndex("by_artist", (q) => q.eq("albumArtist", artist._id))
.take(SEARCH_SONG_LIMIT)
)
),
Promise.all(
relatedAlbums.map((album) =>
ctx.db
.query("songs")
.withIndex("by_album", (q) => q.eq("album", album._id))
.take(SEARCH_SONG_LIMIT)
)
),
])
const albums = uniqueDocs([...matchedAlbums, ...albumsByArtist.flat()])
const songs = uniqueDocs([
...matchedSongs,
...songsByAlbum.flat(),
...songsByArtist.flat(),
])
const albumMap = new Map(albums.map((album) => [album._id, album]))
const missingAlbumIds = [
...new Set(
songs.flatMap((song) => (albumMap.has(song.album) ? [] : [song.album]))
),
]
.map((artist) => ({
kind: "artist" as const,
item: toBrowseArtist(artist),
score: matchScore(artist.name, queryText),
}))
.filter((result) => result.score > 0)
.sort(
(a, b) => b.score - a.score || a.item.name.localeCompare(b.item.name)
)
for (const album of await Promise.all(
missingAlbumIds.map((id) => ctx.db.get("albums", id))
)) {
if (album) {
albumMap.set(album._id, album)
}
}
const artistMap = await loadArtistMapForIds(ctx, [
...matchedArtists.map((artist) => artist._id),
...[...albumMap.values()].map((album) => album.artist),
...songs.map((song) => song.albumArtist),
])
const artistResults: Extract<SearchTopResult, { kind: "artist" }>[] =
matchedArtists
.map((artist) => ({
kind: "artist" as const,
item: toBrowseArtist(artist),
score: matchScore(artist.name, queryText),
}))
.filter((result) => result.score > 0)
.sort(
(a, b) => b.score - a.score || a.item.name.localeCompare(b.item.name)
)
const albumResults: Extract<SearchTopResult, { kind: "album" }>[] = albums
.flatMap((album) => {
@@ -715,6 +869,7 @@ export const searchLibrary = query({
.sort(
(a, b) => b.score - a.score || a.item.title.localeCompare(b.item.title)
)
.slice(0, SEARCH_ALBUM_LIMIT)
const songResults: Extract<SearchTopResult, { kind: "song" }>[] = songs
.flatMap((song) => {
@@ -743,6 +898,7 @@ export const searchLibrary = query({
.sort(
(a, b) => b.score - a.score || a.item.title.localeCompare(b.item.title)
)
.slice(0, SEARCH_SONG_LIMIT)
const topResults: SearchTopResultItem[] = [
...songResults,
@@ -894,17 +1050,51 @@ export const browseRecentlyPlayedPaginated = query({
},
})
export const getArtists = internalQuery({
args: {},
handler: async (ctx) => {
return await ctx.db.query("artists").collect()
export const scanArtistsPage = internalQuery({
args: { paginationOpts: paginationOptsValidator },
handler: async (ctx, args) => {
const result = await ctx.db.query("artists").paginate(args.paginationOpts)
return {
...result,
page: result.page.map(({ _id, name, folder, cover }) => ({
_id,
name,
folder,
cover,
})),
}
},
})
export const getAlbums = internalQuery({
args: {},
handler: async (ctx) => {
return await ctx.db.query("albums").collect()
export const scanAlbumsPage = internalQuery({
args: { paginationOpts: paginationOptsValidator },
handler: async (ctx, args) => {
const result = await ctx.db.query("albums").paginate(args.paginationOpts)
return {
...result,
page: result.page.map(({ _id, folder, cover, artist }) => ({
_id,
folder,
cover,
artist,
})),
}
},
})
export const scanSongsPage = internalQuery({
args: { paginationOpts: paginationOptsValidator },
handler: async (ctx, args) => {
const result = await ctx.db.query("songs").paginate(args.paginationOpts)
return {
...result,
page: result.page.map(({ _id, name, files, album }) => ({
_id,
name,
files,
album,
})),
}
},
})
@@ -918,13 +1108,6 @@ export const getAlbumsInArtist = internalQuery({
},
})
export const getSongs = internalQuery({
args: {},
handler: async (ctx) => {
return await ctx.db.query("songs").collect()
},
})
export const getSongsInArtist = internalQuery({
args: { artistID: v.id("artists") },
handler: async (ctx, args) => {
@@ -1098,7 +1281,7 @@ export const intentCatalogPage = query({
albumId: album._id,
duration: String(Math.max(0, Math.round(song.length))),
genres: song.genres.join(", "),
year: String(song.year),
year: song.year ? String(song.year) : "",
track: String(song.track),
disk: String(song.diskNumber ?? ""),
favorite: user.favoriteSongs?.includes(song._id) ? "1" : "0",
@@ -1119,7 +1302,7 @@ export const intentCatalogPage = query({
metadata: {
artistName: artist.name,
artistId: artist._id,
year: String(album.year),
year: album.year ? String(album.year) : "",
favorite: user.favoriteAlbums?.includes(album._id) ? "1" : "0",
},
})
@@ -1218,30 +1401,6 @@ export const createArtist = internalMutation({
},
})
export const createArtists = internalMutation({
args: {
artists: v.array(
v.object({
name: v.string(),
folder: v.string(),
cover: v.nullable(v.string()),
})
),
},
handler: async (ctx, args) => {
for (const artist of args.artists) {
const existing = await ctx.db
.query("artists")
.withIndex("by_folder", (q) => q.eq("folder", artist.folder))
.first()
if (existing) {
continue
}
await ctx.db.insert("artists", artist)
}
},
})
export const createAlbum = internalMutation({
args: {
name: v.string(),
@@ -1265,35 +1424,6 @@ export const createAlbum = internalMutation({
},
})
export const createAlbums = internalMutation({
args: {
albums: v.array(
v.object({
name: v.string(),
folder: v.string(),
cover: v.nullable(v.string()),
year: v.number(),
date: v.string(),
artist: v.id("artists"),
})
),
},
handler: async (ctx, args) => {
for (const album of args.albums) {
const existing = await ctx.db
.query("albums")
.withIndex("by_all", (q) =>
q.eq("artist", album.artist).eq("folder", album.folder)
)
.first()
if (existing) {
continue
}
await ctx.db.insert("albums", album)
}
},
})
export const updateAlbumName = internalMutation({
args: {
id: v.id("albums"),
@@ -1338,153 +1468,242 @@ export const createSong = internalMutation({
},
})
export const createSongs = internalMutation({
/**
* Creates (or reuses, matched by folder) the artist and album for each entry
* together with its songs, so an album never exists without songs.
*/
export const importAlbums = internalMutation({
args: {
songs: v.array(
albums: v.array(
v.object({
name: v.string(),
files: v.record(v.string(), v.string()),
album: v.id("albums"),
artist: v.union(v.string(), v.array(v.string())),
albumArtist: v.id("artists"),
length: v.number(),
diskNumber: v.nullable(v.number()),
track: v.number(),
year: v.number(),
date: v.string(),
genres: v.array(v.string()),
artist: v.object({
name: v.string(),
folder: v.string(),
cover: v.nullable(v.string()),
}),
album: v.object({
name: v.string(),
folder: v.string(),
cover: v.nullable(v.string()),
year: v.number(),
date: v.string(),
}),
songs: v.array(
v.object({
name: v.string(),
files: v.record(v.string(), v.string()),
artist: v.union(v.string(), v.array(v.string())),
length: v.number(),
diskNumber: v.nullable(v.number()),
track: v.number(),
year: v.number(),
date: v.string(),
genres: v.array(v.string()),
})
),
})
),
},
handler: async (ctx, args) => {
for (const song of args.songs) {
const existing = await ctx.db
.query("songs")
.withIndex("by_all", (q) =>
q
.eq("name", song.name)
.eq("albumArtist", song.albumArtist)
.eq("album", song.album)
)
.first()
if (existing) {
await ctx.db.patch(existing._id, {
files: { ...existing.files, ...song.files },
})
for (const entry of args.albums) {
if (entry.songs.length === 0) {
continue
}
await ctx.db.insert("songs", song)
}
return
const artistId =
(
await ctx.db
.query("artists")
.withIndex("by_folder", (q) => q.eq("folder", entry.artist.folder))
.first()
)?._id ?? (await ctx.db.insert("artists", entry.artist))
const albumId =
(
await ctx.db
.query("albums")
.withIndex("by_all", (q) =>
q.eq("artist", artistId).eq("folder", entry.album.folder)
)
.first()
)?._id ??
(await ctx.db.insert("albums", { ...entry.album, artist: artistId }))
for (const song of entry.songs) {
const existing = await ctx.db
.query("songs")
.withIndex("by_all", (q) =>
q
.eq("name", song.name)
.eq("albumArtist", artistId)
.eq("album", albumId)
)
.first()
if (existing) {
await ctx.db.patch(existing._id, {
files: { ...existing.files, ...song.files },
})
continue
}
await ctx.db.insert("songs", {
...song,
album: albumId,
albumArtist: artistId,
})
}
}
},
})
export const purgeSongsFromUsersAndPlaylists = internalMutation({
/** Deletes songs and removes them from every playlist, favorite and queue. */
export const deleteSongs = internalMutation({
args: { ids: v.array(v.id("songs")) },
handler: async (ctx, args) => {
const remove = new Set(args.ids)
if (remove.size === 0) {
return
}
for (const playlist of await ctx.db.query("playlists").collect()) {
if (playlist.songs.some((songId) => remove.has(songId))) {
await ctx.db.patch(playlist._id, {
songs: playlist.songs.filter((songId) => !remove.has(songId)),
})
}
}
for (const user of await ctx.db.query("users").collect()) {
const favoriteSongs = user.favoriteSongs
const songQueue = user.songQueue
const queueHistory = user.queueHistory
const patch: Partial<
Pick<
Doc<"users">,
"favoriteSongs" | "songQueue" | "queueHistory" | "currentSongProgress"
>
> = {}
if (favoriteSongs?.some((songId) => remove.has(songId))) {
patch.favoriteSongs = favoriteSongs.filter(
(songId) => !remove.has(songId)
)
}
if (songQueue?.some((songId) => remove.has(songId))) {
const currentRemoved = remove.has(songQueue[0])
patch.songQueue = songQueue.filter((songId) => !remove.has(songId))
if (currentRemoved) {
patch.currentSongProgress = 0
}
}
if (queueHistory?.some((songId) => remove.has(songId))) {
patch.queueHistory = queueHistory.filter(
(songId) => !remove.has(songId)
)
}
if (Object.keys(patch).length > 0) {
await ctx.db.patch(user._id, patch)
}
for (const id of args.ids) {
await ctx.db.delete("songs", id)
}
await purgeSongsFromUsersAndPlaylists(ctx, new Set(args.ids))
},
})
export const purgeAlbumsFromUsers = internalMutation({
/** Deletes the albums that still have no songs when this mutation runs. */
export const deleteEmptyAlbums = internalMutation({
args: { ids: v.array(v.id("albums")) },
handler: async (ctx, args) => {
const remove = new Set(args.ids)
if (remove.size === 0) {
return
}
for (const user of await ctx.db.query("users").collect()) {
const favoriteAlbums = user.favoriteAlbums
const recentlyPlayed = user.recentlyPlayed
const patch: Partial<
Pick<Doc<"users">, "favoriteAlbums" | "recentlyPlayed">
> = {}
if (favoriteAlbums?.some((albumId) => remove.has(albumId))) {
patch.favoriteAlbums = favoriteAlbums.filter(
(albumId) => !remove.has(albumId)
)
}
if (
recentlyPlayed?.some((itemId) => remove.has(itemId as Id<"albums">))
) {
patch.recentlyPlayed = recentlyPlayed.filter(
(itemId) => !remove.has(itemId as Id<"albums">)
)
}
if (Object.keys(patch).length > 0) {
await ctx.db.patch(user._id, patch)
const deleted = new Set<Id<"albums">>()
for (const id of args.ids) {
const song = await ctx.db
.query("songs")
.withIndex("by_album", (q) => q.eq("album", id))
.first()
if (song === null && (await ctx.db.get("albums", id)) !== null) {
await ctx.db.delete("albums", id)
deleted.add(id)
}
}
await purgeAlbumsFromUsers(ctx, deleted)
return deleted.size
},
})
export const purgeArtistsFromUsers = internalMutation({
/** Deletes the artists that still have no albums when this mutation runs. */
export const deleteEmptyArtists = internalMutation({
args: { ids: v.array(v.id("artists")) },
handler: async (ctx, args) => {
const remove = new Set(args.ids)
if (remove.size === 0) {
return
}
for (const user of await ctx.db.query("users").collect()) {
const favoriteArtists = user.favoriteArtists
if (favoriteArtists?.some((artistId) => remove.has(artistId))) {
await ctx.db.patch(user._id, {
favoriteArtists: favoriteArtists.filter(
(artistId) => !remove.has(artistId)
),
})
const deleted = new Set<Id<"artists">>()
for (const id of args.ids) {
const album = await ctx.db
.query("albums")
.withIndex("by_artist", (q) => q.eq("artist", id))
.first()
if (album === null && (await ctx.db.get("artists", id)) !== null) {
await ctx.db.delete("artists", id)
deleted.add(id)
}
}
await purgeArtistsFromUsers(ctx, deleted)
return deleted.size
},
})
async function purgeSongsFromUsersAndPlaylists(
ctx: MutationCtx,
remove: Set<Id<"songs">>
) {
if (remove.size === 0) {
return
}
for (const playlist of await ctx.db.query("playlists").collect()) {
if (playlist.songs.some((songId) => remove.has(songId))) {
await ctx.db.patch(playlist._id, {
songs: playlist.songs.filter((songId) => !remove.has(songId)),
})
}
}
for (const user of await ctx.db.query("users").collect()) {
const favoriteSongs = user.favoriteSongs
const songQueue = user.songQueue
const queueHistory = user.queueHistory
const patch: Partial<
Pick<
Doc<"users">,
"favoriteSongs" | "songQueue" | "queueHistory" | "currentSongProgress"
>
> = {}
if (favoriteSongs?.some((songId) => remove.has(songId))) {
patch.favoriteSongs = favoriteSongs.filter(
(songId) => !remove.has(songId)
)
}
if (songQueue?.some((songId) => remove.has(songId))) {
const currentRemoved = remove.has(songQueue[0])
patch.songQueue = songQueue.filter((songId) => !remove.has(songId))
if (currentRemoved) {
patch.currentSongProgress = 0
}
}
if (queueHistory?.some((songId) => remove.has(songId))) {
patch.queueHistory = queueHistory.filter((songId) => !remove.has(songId))
}
if (Object.keys(patch).length > 0) {
await ctx.db.patch(user._id, patch)
}
}
}
async function purgeAlbumsFromUsers(
ctx: MutationCtx,
remove: Set<Id<"albums">>
) {
if (remove.size === 0) {
return
}
for (const user of await ctx.db.query("users").collect()) {
const favoriteAlbums = user.favoriteAlbums
const recentlyPlayed = user.recentlyPlayed
const patch: Partial<
Pick<Doc<"users">, "favoriteAlbums" | "recentlyPlayed">
> = {}
if (favoriteAlbums?.some((albumId) => remove.has(albumId))) {
patch.favoriteAlbums = favoriteAlbums.filter(
(albumId) => !remove.has(albumId)
)
}
if (recentlyPlayed?.some((itemId) => remove.has(itemId as Id<"albums">))) {
patch.recentlyPlayed = recentlyPlayed.filter(
(itemId) => !remove.has(itemId as Id<"albums">)
)
}
if (Object.keys(patch).length > 0) {
await ctx.db.patch(user._id, patch)
}
}
}
async function purgeArtistsFromUsers(
ctx: MutationCtx,
remove: Set<Id<"artists">>
) {
if (remove.size === 0) {
return
}
for (const user of await ctx.db.query("users").collect()) {
const favoriteArtists = user.favoriteArtists
if (favoriteArtists?.some((artistId) => remove.has(artistId))) {
await ctx.db.patch(user._id, {
favoriteArtists: favoriteArtists.filter(
(artistId) => !remove.has(artistId)
),
})
}
}
}
export const removeArtist = internalMutation({
args: { id: v.id("artists") },
handler: async (ctx, args) => {
@@ -1492,15 +1711,6 @@ export const removeArtist = internalMutation({
},
})
export const removeArtists = internalMutation({
args: { ids: v.array(v.id("artists")) },
handler: async (ctx, args) => {
for (const id of args.ids) {
await ctx.db.delete("artists", id)
}
},
})
export const removeAlbum = internalMutation({
args: { id: v.id("albums") },
handler: async (ctx, args) => {
@@ -1508,15 +1718,6 @@ export const removeAlbum = internalMutation({
},
})
export const removeAlbums = internalMutation({
args: { ids: v.array(v.id("albums")) },
handler: async (ctx, args) => {
for (const id of args.ids) {
await ctx.db.delete("albums", id)
}
},
})
export const removeSong = internalMutation({
args: { id: v.id("songs") },
handler: async (ctx, args) => {
@@ -1545,25 +1746,64 @@ export const changeFilesForSongs = internalMutation({
},
})
export const updateArtistCover = internalMutation({
args: { id: v.id("artists"), cover: v.nullable(v.string()) },
handler: async (ctx, args) => {
return await ctx.db.patch("artists", args.id, { cover: args.cover })
export const updateArtistCovers = internalMutation({
args: {
artists: v.array(
v.object({ id: v.id("artists"), cover: v.nullable(v.string()) })
),
},
})
export const updateAlbumCover = internalMutation({
args: { id: v.id("albums"), cover: v.nullable(v.string()) },
handler: async (ctx, args) => {
return await ctx.db.patch("albums", args.id, { cover: args.cover })
for (const { id, cover } of args.artists) {
if ((await ctx.db.get("artists", id)) !== null) {
await ctx.db.patch("artists", id, { cover })
}
}
},
})
export const removeSongs = internalMutation({
args: { ids: v.array(v.id("songs")) },
export const updateAlbumCovers = internalMutation({
args: {
albums: v.array(
v.object({ id: v.id("albums"), cover: v.nullable(v.string()) })
),
},
handler: async (ctx, args) => {
for (const { id, cover } of args.albums) {
if ((await ctx.db.get("albums", id)) !== null) {
await ctx.db.patch("albums", id, { cover })
}
}
},
})
/**
* Takes the single library scan lease if it is free or expired. The lease
* outlives the action timeout so a crashed scan cannot hold it forever.
*/
export const acquireLibraryScanLock = internalMutation({
args: { token: v.string(), leaseMs: v.number() },
handler: async (ctx, args) => {
const now = Date.now()
const lock = await ctx.db.query("libraryScanLock").first()
if (lock && lock.token !== args.token && lock.expiresAt > now) {
return false
}
const expiresAt = now + args.leaseMs
if (lock) {
await ctx.db.patch(lock._id, { token: args.token, expiresAt })
} else {
await ctx.db.insert("libraryScanLock", { token: args.token, expiresAt })
}
return true
},
})
export const releaseLibraryScanLock = internalMutation({
args: { token: v.string() },
handler: async (ctx, args) => {
for (const id of args.ids) {
await ctx.db.delete("songs", id)
const lock = await ctx.db.query("libraryScanLock").first()
if (lock?.token === args.token) {
await ctx.db.delete(lock._id)
}
},
})
+6 -1
View File
@@ -590,7 +590,12 @@ export const playFromLibrarySong = mutation({
throw new ConvexError("User not found")
}
const songIds = await getSortedLibrarySongIds(ctx, sortKey, sortDirection)
const songIds = await getSortedLibrarySongIds(
ctx,
sortKey,
sortDirection,
songId
)
const index = songIds.indexOf(songId)
const rest = user.shuffleEnabled
? uniqueSongIds(songIds).filter((id) => id !== songId)
+10 -3
View File
@@ -48,13 +48,18 @@ export default defineSchema({
appSettings: defineTable({
disableAccountCreation: v.boolean(),
}),
libraryScanLock: defineTable({
token: v.string(),
expiresAt: v.number(),
}),
artists: defineTable({
name: v.string(),
folder: v.string(),
cover: v.nullable(v.string()),
})
.index("by_name", ["name"])
.index("by_folder", ["folder"]),
.index("by_folder", ["folder"])
.searchIndex("search_name", { searchField: "name" }),
albums: defineTable({
name: v.string(),
folder: v.string(),
@@ -66,7 +71,8 @@ export default defineSchema({
.index("by_artist", ["artist"])
.index("by_name", ["name"])
.index("by_folder", ["folder"])
.index("by_all", ["artist", "folder"]),
.index("by_all", ["artist", "folder"])
.searchIndex("search_name", { searchField: "name" }),
songs: defineTable({
name: v.string(),
files: v.record(v.string(), v.string()),
@@ -83,7 +89,8 @@ export default defineSchema({
.index("by_name", ["name"])
.index("by_artist", ["albumArtist"])
.index("by_album", ["album"])
.index("by_all", ["name", "albumArtist", "album"]),
.index("by_all", ["name", "albumArtist", "album"])
.searchIndex("search_name", { searchField: "name" }),
playlists: defineTable({
name: v.string(),
owner: v.id("users"),
+2
View File
@@ -33,8 +33,10 @@ export type BrowseAlbumTrack = {
}
export type BrowseAlbumDetail = BrowseAlbum & {
/** `null` when the files have no year tag; clients hide it. */
year: number | null
genre: string | null
/** Empty when unknown; clients hide it. */
releaseDate: string
hasLossless: boolean
hasAtmos: boolean