diff --git a/apps/mobile/app/(app)/(tabs)/home/playlist/favorites.tsx b/apps/mobile/app/(app)/(tabs)/home/playlist/favorites.tsx index 68e5e64..a19835c 100644 --- a/apps/mobile/app/(app)/(tabs)/home/playlist/favorites.tsx +++ b/apps/mobile/app/(app)/(tabs)/home/playlist/favorites.tsx @@ -36,7 +36,7 @@ export default function FavoritesScreen() { const colors = useThemeColors() const { config } = useServerConfig() const favorites = useCachedQuery(api.favorites.browseFavoritesPlaylistDetail) - const songs = favorites?.songs ?? [] + const songs = useMemo(() => favorites?.songs.flat() ?? [], [favorites]) const { playInOrder, playShuffled, playFromSong } = usePlaybackActions() const { isSongFavorite } = useFavorites() const { handleSongAction } = useMediaMenuActions() @@ -45,8 +45,8 @@ export default function FavoritesScreen() { usePlaylistSearch(songs) useEffect(() => { - if (favorites) rememberSongs(favorites.songs) - }, [favorites]) + rememberSongs(songs) + }, [songs]) const handlePlaySong = useCallback( (song: BrowseSong) => void playFromSong(song.id, songIds), diff --git a/apps/mobile/components/app-intents/catalog-sync.ts b/apps/mobile/components/app-intents/catalog-sync.ts index 4d4c4fe..91714ce 100644 --- a/apps/mobile/components/app-intents/catalog-sync.ts +++ b/apps/mobile/components/app-intents/catalog-sync.ts @@ -23,19 +23,16 @@ type LibraryKind = (typeof LIBRARY_KINDS)[number] const PAGE_SIZE = 500 type CatalogPage = FunctionReturnType -type Favorites = { - favoriteSongs: string[] - favoriteAlbums: string[] - favoriteArtists: string[] -} +type Favorites = FunctionReturnType function favoriteIds(favorites: Favorites, kind: LibraryKind) { return new Set( - kind === "song" + (kind === "song" ? favorites.favoriteSongs : kind === "album" ? favorites.favoriteAlbums : favorites.favoriteArtists + ).flat() ) } diff --git a/apps/mobile/components/app-intents/dispatch.ts b/apps/mobile/components/app-intents/dispatch.ts index 67fbe2f..4424809 100644 --- a/apps/mobile/components/app-intents/dispatch.ts +++ b/apps/mobile/components/app-intents/dispatch.ts @@ -214,12 +214,13 @@ export function useIntentDispatch() { ) } if (kind === "artist") { - return fetchCachedQuery( + const chunks = await fetchCachedQuery( convex, cacheScope, api.playlists.getOrderedArtistSongIds, { artistId: id as Id<"artists"> } ) + return chunks.flat() } if (kind === "playlist") { if (id === FAVORITES_PLAYLIST_ID) { @@ -229,7 +230,7 @@ export function useIntentDispatch() { api.favorites.getUserFavorites, {} ) - return favorites.favoriteSongs + return favorites.favoriteSongs.flat() } return fetchCachedQuery( convex, @@ -391,17 +392,18 @@ export function useIntentDispatch() { api.favorites.getUserFavorites, {} ) - if (favorites.favoriteSongs.length === 0) { + const favoriteSongs = favorites.favoriteSongs.flat() + if (favoriteSongs.length === 0) { throw new IntentRejected( "Nothing is queued, and your favorites are empty." ) } if (shuffled) { ensureActive(invocation) - await actions.playShuffled(favorites.favoriteSongs) + await actions.playShuffled(favoriteSongs) } else { ensureActive(invocation) - await actions.playInOrder(favorites.favoriteSongs) + await actions.playInOrder(favoriteSongs) } return } diff --git a/apps/mobile/contexts/add-to-playlist.tsx b/apps/mobile/contexts/add-to-playlist.tsx index 817717f..5abdb6b 100644 --- a/apps/mobile/contexts/add-to-playlist.tsx +++ b/apps/mobile/contexts/add-to-playlist.tsx @@ -150,7 +150,7 @@ export function AddToPlaylistProvider({ children }: { children: ReactNode }) { { artistId } ) if (selectionRequestIdRef.current !== requestId) return - setPendingSongIds(songIds) + setPendingSongIds(songIds.flat()) setOpen(true) } catch (error) { if (selectionRequestIdRef.current !== requestId) return diff --git a/apps/mobile/contexts/favorites.tsx b/apps/mobile/contexts/favorites.tsx index 4befb5b..f1213df 100644 --- a/apps/mobile/contexts/favorites.tsx +++ b/apps/mobile/contexts/favorites.tsx @@ -100,15 +100,15 @@ export function FavoritesProvider({ children }: { children: ReactNode }) { ) const songIds = useMemo( - () => new Set(favorites?.favoriteSongs.map(String) ?? []), + () => new Set(favorites?.favoriteSongs.flat()), [favorites?.favoriteSongs] ) const albumIds = useMemo( - () => new Set(favorites?.favoriteAlbums.map(String) ?? []), + () => new Set(favorites?.favoriteAlbums.flat()), [favorites?.favoriteAlbums] ) const artistIds = useMemo( - () => new Set(favorites?.favoriteArtists.map(String) ?? []), + () => new Set(favorites?.favoriteArtists.flat()), [favorites?.favoriteArtists] ) const idsRef = useRef({ songIds, albumIds, artistIds }) diff --git a/apps/mobile/contexts/playback.tsx b/apps/mobile/contexts/playback.tsx index 43aae8d..40ee2c5 100644 --- a/apps/mobile/contexts/playback.tsx +++ b/apps/mobile/contexts/playback.tsx @@ -401,7 +401,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { cacheScope, api.playlists.getOrderedArtistSongIds, { artistId } - ), + ).then((chunks) => chunks.flat()), recorded: { entity: { kind: "artist", id: artistId } }, })) const playlist = collection( @@ -482,12 +482,14 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { let songIds: Id<"songs">[] try { - songIds = await fetchCachedQuery( - convex, - cacheScope, - api.library.browseSortedSongIds, - sort - ) + songIds = ( + await fetchCachedQuery( + convex, + cacheScope, + api.library.browseSortedSongIds, + sort + ) + ).flat() } catch { return } diff --git a/apps/mobile/hooks/use-media-menu-actions.ts b/apps/mobile/hooks/use-media-menu-actions.ts index b367040..5d26f56 100644 --- a/apps/mobile/hooks/use-media-menu-actions.ts +++ b/apps/mobile/hooks/use-media-menu-actions.ts @@ -173,7 +173,7 @@ export function useMediaMenuActions() { api.playlists.getOrderedArtistSongIds, { artistId: artist.id as Id<"artists"> } ) - await runQueueAction(action, songIds) + await runQueueAction(action, songIds.flat()) break } default: diff --git a/apps/web/app/(app)/(home)/page.tsx b/apps/web/app/(app)/(home)/page.tsx index fa3adf3..6af2750 100644 --- a/apps/web/app/(app)/(home)/page.tsx +++ b/apps/web/app/(app)/(home)/page.tsx @@ -32,7 +32,7 @@ export default function Page() { diff --git a/apps/web/app/(app)/library/albums/page.tsx b/apps/web/app/(app)/library/albums/page.tsx index f4d361f..37759e2 100644 --- a/apps/web/app/(app)/library/albums/page.tsx +++ b/apps/web/app/(app)/library/albums/page.tsx @@ -18,7 +18,7 @@ export default function Page() { ) : (
- {albums.map((album) => ( + {albums.flat().map((album) => ( ))}
diff --git a/apps/web/app/(app)/recently-added/page.tsx b/apps/web/app/(app)/recently-added/page.tsx index 919d85d..cd20d80 100644 --- a/apps/web/app/(app)/recently-added/page.tsx +++ b/apps/web/app/(app)/recently-added/page.tsx @@ -18,7 +18,7 @@ export default function Page() { ) : (
- {albums.map((album) => ( + {albums.flat().map((album) => ( ))}
diff --git a/apps/web/components/add-to-playlist-provider.tsx b/apps/web/components/add-to-playlist-provider.tsx index c215e54..5cc1ad8 100644 --- a/apps/web/components/add-to-playlist-provider.tsx +++ b/apps/web/components/add-to-playlist-provider.tsx @@ -184,7 +184,7 @@ export function AddToPlaylistProvider({ } ) if (requestId === selectionRequestRef.current) { - showAddToPlaylist(songIds) + showAddToPlaylist(songIds.flat()) } } catch (error) { if (requestId === selectionRequestRef.current) { diff --git a/apps/web/components/artists-page.tsx b/apps/web/components/artists-page.tsx index 645a54a..744ab95 100644 --- a/apps/web/components/artists-page.tsx +++ b/apps/web/components/artists-page.tsx @@ -32,8 +32,8 @@ export function ArtistsPage() { ) : ( diff --git a/apps/web/components/favorites-provider.tsx b/apps/web/components/favorites-provider.tsx index cbd60e8..686d9a9 100644 --- a/apps/web/components/favorites-provider.tsx +++ b/apps/web/components/favorites-provider.tsx @@ -86,15 +86,15 @@ export function FavoritesProvider({ children }: { children: React.ReactNode }) { ) const songIds = React.useMemo( - () => new Set(favorites?.favoriteSongs.map(String) ?? []), + () => new Set(favorites?.favoriteSongs.flat()), [favorites?.favoriteSongs] ) const albumIds = React.useMemo( - () => new Set(favorites?.favoriteAlbums.map(String) ?? []), + () => new Set(favorites?.favoriteAlbums.flat()), [favorites?.favoriteAlbums] ) const artistIds = React.useMemo( - () => new Set(favorites?.favoriteArtists.map(String) ?? []), + () => new Set(favorites?.favoriteArtists.flat()), [favorites?.favoriteArtists] ) diff --git a/apps/web/components/playback-provider.tsx b/apps/web/components/playback-provider.tsx index e6582f6..e4e9cf2 100644 --- a/apps/web/components/playback-provider.tsx +++ b/apps/web/components/playback-provider.tsx @@ -1233,12 +1233,14 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { let songIds: Id<"songs">[] try { - songIds = await fetchCachedQuery( - convex, - cacheScope, - api.library.browseSortedSongIds, - sort - ) + songIds = ( + await fetchCachedQuery( + convex, + cacheScope, + api.library.browseSortedSongIds, + sort + ) + ).flat() } catch { return } @@ -1268,7 +1270,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { cacheScope, api.playlists.getOrderedArtistSongIds, { artistId } - ), + ).then((chunks) => chunks.flat()), [convex, cacheScope] ) diff --git a/apps/web/components/playlist-page.tsx b/apps/web/components/playlist-page.tsx index 07398b4..8e26494 100644 --- a/apps/web/components/playlist-page.tsx +++ b/apps/web/components/playlist-page.tsx @@ -34,5 +34,7 @@ export function FavoritesPlaylistPage() { return Sign in to view your favorites. } - return + return ( + + ) } diff --git a/apps/web/hooks/use-media-actions.ts b/apps/web/hooks/use-media-actions.ts index 9439842..07f8bcc 100644 --- a/apps/web/hooks/use-media-actions.ts +++ b/apps/web/hooks/use-media-actions.ts @@ -64,7 +64,7 @@ export function useMediaActions() { api.playlists.getOrderedArtistSongIds, { artistId: artistId as Id<"artists"> } ) - void runQueueAction(action, songIds) + void runQueueAction(action, songIds.flat()) break } } diff --git a/packages/convex/client/favorites.ts b/packages/convex/client/favorites.ts index 69d20eb..a905252 100644 --- a/packages/convex/client/favorites.ts +++ b/packages/convex/client/favorites.ts @@ -1,63 +1,54 @@ import type { OptimisticLocalStore, OptimisticUpdate } from "convex/browser" -import type { FunctionArgs, FunctionReference } from "convex/server" +import type { + FunctionArgs, + FunctionReference, + FunctionReturnType, +} from "convex/server" import { api } from "../convex/_generated/api" -import type { Id } from "../convex/_generated/dataModel" +import { chunk } from "../convex/limits" -type FavoriteIds = { - favoriteSongs: Id<"songs">[] - favoriteAlbums: Id<"albums">[] - favoriteArtists: Id<"artists">[] -} +type UserFavorites = FunctionReturnType +type FavoriteList = keyof UserFavorites -type FavoriteList = keyof FavoriteIds - -function patchFavorites( +/** Rewrites one list of the cached `getUserFavorites` result, if loaded. */ +function patchFavorites( store: OptimisticLocalStore, - list: List, - update: (ids: FavoriteIds[List]) => FavoriteIds[List] + list: FavoriteList, + update: (ids: string[]) => string[] ) { 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, - } - ) + const ids: string[] = current[list].flat() + const next = update(ids) + if (next !== ids) { + // `next` only holds ids of this list's kind, which TS cannot follow here. + store.setQuery(api.favorites.getUserFavorites, {}, { + ...current, + [list]: chunk(next), + } as UserFavorites) } } -function addFavorites( +function addFavorites( store: OptimisticLocalStore, - list: List, - ids: FavoriteIds[List] + list: FavoriteList, + ids: string[] ) { 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]) + const added = ids.filter((id) => !current.includes(id)) + return added.length === 0 ? current : [...new Set([...current, ...added])] }) } -function removeFavorites( +function removeFavorites( store: OptimisticLocalStore, - list: List, - ids: FavoriteIds[List] + list: FavoriteList, + ids: string[] ) { - const removed = new Set(ids) - patchFavorites( - store, - list, - (current) => - (current as string[]).filter( - (id) => !removed.has(id) - ) as FavoriteIds[List] + const removed = new Set(ids) + patchFavorites(store, list, (current) => + current.filter((id) => !removed.has(id)) ) } diff --git a/packages/convex/convex/_generated/api.d.ts b/packages/convex/convex/_generated/api.d.ts index 45b3d77..5298bfa 100644 --- a/packages/convex/convex/_generated/api.d.ts +++ b/packages/convex/convex/_generated/api.d.ts @@ -11,6 +11,7 @@ import type * as actions from "../actions.js"; import type * as auth from "../auth.js"; import type * as crons from "../crons.js"; +import type * as favoriteStore from "../favoriteStore.js"; import type * as favorites from "../favorites.js"; import type * as http from "../http.js"; import type * as intentCatalog from "../intentCatalog.js"; @@ -34,6 +35,7 @@ declare const fullApi: ApiFromModules<{ actions: typeof actions; auth: typeof auth; crons: typeof crons; + favoriteStore: typeof favoriteStore; favorites: typeof favorites; http: typeof http; intentCatalog: typeof intentCatalog; diff --git a/packages/convex/convex/app-intent-playlists.test.cjs b/packages/convex/convex/app-intent-playlists.test.cjs index 701d969..3229c5a 100644 --- a/packages/convex/convex/app-intent-playlists.test.cjs +++ b/packages/convex/convex/app-intent-playlists.test.cjs @@ -29,7 +29,7 @@ function playlistMutation() { mutation: (definition) => definition, query: (definition) => definition, } - if (name === "./library") return {} + if (name === "./library" || name === "./limits") return {} throw new Error(`Unexpected import ${name}`) }, }) diff --git a/packages/convex/convex/favoriteStore.ts b/packages/convex/convex/favoriteStore.ts new file mode 100644 index 0000000..749937a --- /dev/null +++ b/packages/convex/convex/favoriteStore.ts @@ -0,0 +1,274 @@ +import type { WithoutSystemFields } from "convex/server" +import { ConvexError, v } from "convex/values" +import { internal } from "./_generated/api" +import type { Doc, Id } from "./_generated/dataModel" +import type { MutationCtx, QueryCtx } from "./_generated/server" +import { internalMutation } from "./_generated/server" +import { chunk, type Chunked } from "./limits" + +export type FavoriteKind = Doc<"favorites">["kind"] + +type FavoriteEntityId = { + [Kind in FavoriteKind]: Extract, { kind: Kind }>["entityId"] +} + +/** A user's favorites, each list oldest first. */ +export type FavoriteIds = { + favoriteSongs: Id<"songs">[] + favoriteAlbums: Id<"albums">[] + favoriteArtists: Id<"artists">[] +} + +/** `FavoriteIds` as `favorites.getUserFavorites` returns it. */ +export type FavoriteIdChunks = { + [List in keyof FavoriteIds]: Chunked +} + +function legacyFavorites(user: Doc<"users"> | null): FavoriteIds { + return { + favoriteSongs: user?.favoriteSongs ?? [], + favoriteAlbums: user?.favoriteAlbums ?? [], + favoriteArtists: user?.favoriteArtists ?? [], + } +} + +export function hasLegacyFavorites(user: Doc<"users">) { + return ( + user.favoriteSongs !== undefined || + user.favoriteAlbums !== undefined || + user.favoriteArtists !== undefined + ) +} + +/** Favorited ids of one kind, oldest first. */ +async function readKind( + ctx: QueryCtx, + userId: Id<"users">, + kind: Kind +): Promise { + const rows = await ctx.db + .query("favorites") + .withIndex("by_user_kind", (q) => q.eq("userId", userId).eq("kind", kind)) + .collect() + return rows.map((row) => row.entityId as FavoriteEntityId[Kind]) +} + +/** + * Reads every favorite, oldest first. Users without rows fall back to the + * legacy arrays on `users` until `migrations.migrateUsers` moves them, so + * migrated users never read (or rerun on) their user doc here. + */ +export async function readFavorites( + ctx: QueryCtx, + userId: Id<"users"> +): Promise { + const [favoriteSongs, favoriteAlbums, favoriteArtists] = await Promise.all([ + readKind(ctx, userId, "song"), + readKind(ctx, userId, "album"), + readKind(ctx, userId, "artist"), + ]) + if ( + favoriteSongs.length > 0 || + favoriteAlbums.length > 0 || + favoriteArtists.length > 0 + ) { + return { favoriteSongs, favoriteAlbums, favoriteArtists } + } + return legacyFavorites(await ctx.db.get(userId)) +} + +/** + * Above this many ids, one scan of the user's list beats a lookup per id, and + * keeps bulk writes under Convex's 4,096 reads per function. + */ +const MAX_POINT_LOOKUPS = 64 + +/** Maps each of `ids` the user has favorited to its row. */ +async function favoriteRows( + ctx: QueryCtx, + userId: Id<"users">, + kind: Kind, + ids: ReadonlySet +): Promise>> { + const rows = + ids.size <= MAX_POINT_LOOKUPS + ? await Promise.all( + [...ids].map((entityId) => + ctx.db + .query("favorites") + .withIndex("by_user_entity", (q) => + q.eq("userId", userId).eq("entityId", entityId) + ) + .unique() + ) + ) + : await ctx.db + .query("favorites") + .withIndex("by_user_kind", (q) => + q.eq("userId", userId).eq("kind", kind) + ) + .collect() + return new Map( + rows.flatMap((row) => + row && ids.has(row.entityId as FavoriteEntityId[Kind]) + ? [[row.entityId, row._id] as const] + : [] + ) + ) +} + +/** True when `ids` is non-empty and the user has favorited every one. */ +export async function allFavorited( + ctx: QueryCtx, + userId: Id<"users">, + kind: Kind, + ids: FavoriteEntityId[Kind][] +) { + const unique = new Set(ids) + if (unique.size === 0) { + return false + } + const rows = await favoriteRows(ctx, userId, kind, unique) + return rows.size === unique.size +} + +/** Appends ids that are not favorited yet, keeping existing ones in place. */ +export async function addFavorites( + ctx: MutationCtx, + userId: Id<"users">, + kind: Kind, + ids: FavoriteEntityId[Kind][] +) { + const unique = new Set(ids) + const existing = await favoriteRows(ctx, userId, kind, unique) + for (const entityId of unique) { + if (!existing.has(entityId)) { + // TS cannot tie a generic `kind` to its `entityId` union member. + await ctx.db.insert("favorites", { + userId, + kind, + entityId, + } as WithoutSystemFields>) + } + } +} + +export async function removeFavorites( + ctx: MutationCtx, + userId: Id<"users">, + kind: Kind, + ids: FavoriteEntityId[Kind][] +) { + const rows = await favoriteRows(ctx, userId, kind, new Set(ids)) + await deleteFavoriteRows(ctx, [...rows.values()]) +} + +/** + * Rows one call deletes before scheduling the rest. Convex counts each delete + * as a read, so unfavoriting a huge artist cannot happen in one mutation. + */ +const MAX_INLINE_DELETES = 1_000 + +async function deleteFavoriteRows(ctx: MutationCtx, rowIds: Id<"favorites">[]) { + for (const rowId of rowIds.slice(0, MAX_INLINE_DELETES)) { + await ctx.db.delete(rowId) + } + for (const rest of chunk(rowIds.slice(MAX_INLINE_DELETES))) { + await ctx.scheduler.runAfter(0, internal.favoriteStore.deleteRows, { + rowIds: rest, + }) + } +} + +/** Finishes deletes `deleteFavoriteRows` could not fit in one mutation. */ +export const deleteRows = internalMutation({ + args: { rowIds: v.array(v.id("favorites")) }, + handler: async (ctx, { rowIds }) => { + // Rows can disappear in between, e.g. when the scanner purges a song. + for (const rowId of rowIds.slice(0, MAX_INLINE_DELETES)) { + if (await ctx.db.get(rowId)) { + await ctx.db.delete(rowId) + } + } + const rest = rowIds.slice(MAX_INLINE_DELETES) + if (rest.length > 0) { + await ctx.scheduler.runAfter(0, internal.favoriteStore.deleteRows, { + rowIds: rest, + }) + } + }, +}) + +/** + * Moves a user's legacy favorite arrays into `favorites` rows, keeping their + * order, then clears them. Safe to call repeatedly. + */ +export async function migrateUserFavorites( + ctx: MutationCtx, + user: Doc<"users"> +) { + if (!hasLegacyFavorites(user)) { + return + } + const legacy = legacyFavorites(user) + await addFavorites(ctx, user._id, "song", legacy.favoriteSongs) + await addFavorites(ctx, user._id, "album", legacy.favoriteAlbums) + await addFavorites(ctx, user._id, "artist", legacy.favoriteArtists) + await ctx.db.patch(user._id, { + favoriteSongs: undefined, + favoriteAlbums: undefined, + favoriteArtists: undefined, + }) +} + +/** + * Called before every favorites write, so a user toggling a favorite before + * the boot migration reaches them does not end up with half their favorites + * in rows and half on `users`. + */ +export async function requireMigratedFavorites( + ctx: MutationCtx, + userId: Id<"users"> +) { + const user = await ctx.db.get(userId) + if (!user) { + throw new ConvexError("User not found") + } + await migrateUserFavorites(ctx, user) +} + +/** Removes deleted songs, albums or artists from every user's favorites. */ +export async function purgeFavoriteEntities( + ctx: MutationCtx, + ids: Iterable +) { + for (const entityId of ids) { + const rows = await ctx.db + .query("favorites") + .withIndex("by_entity", (q) => q.eq("entityId", entityId)) + .collect() + for (const row of rows) { + await ctx.db.delete(row._id) + } + } +} + +export async function deleteUserFavorites( + ctx: MutationCtx, + userId: Id<"users"> +) { + const rows = await Promise.all( + (["song", "album", "artist"] as const).map((kind) => + ctx.db + .query("favorites") + .withIndex("by_user_kind", (q) => + q.eq("userId", userId).eq("kind", kind) + ) + .collect() + ) + ) + await deleteFavoriteRows( + ctx, + rows.flat().map((row) => row._id) + ) +} diff --git a/packages/convex/convex/favorites.ts b/packages/convex/convex/favorites.ts index d4c0bc9..b1ba701 100644 --- a/packages/convex/convex/favorites.ts +++ b/packages/convex/convex/favorites.ts @@ -3,18 +3,26 @@ import { ConvexError, v } from "convex/values" import type { Doc, Id } from "./_generated/dataModel" import type { MutationCtx, QueryCtx } from "./_generated/server" import { mutation, query } from "./_generated/server" -import type { BrowsePlaylistDetail, BrowsePlaylistSummary } from "./library" +import { + addFavorites, + allFavorited, + readFavorites, + removeFavorites, + requireMigratedFavorites, + type FavoriteIdChunks, +} from "./favoriteStore" +import type { BrowsePlaylistSummary } from "./library" import { browseSongsFromIds } from "./library" +import { chunk } from "./limits" +import type { BrowseFavoritesDetail } from "./types" -function uniqueIds(ids: T[]): T[] { - return [...new Set(ids)] -} - -async function requireAuth(ctx: MutationCtx) { +/** Resolves the signed-in user and moves any legacy favorites into rows. */ +async function requireFavoritesUser(ctx: MutationCtx) { const userId = await getAuthUserId(ctx) if (userId === null) { throw new ConvexError("Not authenticated") } + await requireMigratedFavorites(ctx, userId) return userId } @@ -42,112 +50,46 @@ async function getSongIdsForArtist(ctx: QueryCtx, artistId: Id<"artists">) { return songs.map((song) => song._id) } -function allAlbumSongsFavorited( - albumSongIds: Id<"songs">[], - favoriteSongs: Id<"songs">[] -) { - if (albumSongIds.length === 0) { - return false - } - const favorited = new Set(favoriteSongs) - return albumSongIds.every((id) => favorited.has(id)) -} - -function allArtistAlbumsFavorited( - artistAlbumIds: Id<"albums">[], - favoriteAlbums: Id<"albums">[] -) { - if (artistAlbumIds.length === 0) { - return false - } - const favorited = new Set(favoriteAlbums) - return artistAlbumIds.every((id) => favorited.has(id)) -} - -async function promoteAlbumIfAllSongsFavorited( - ctx: MutationCtx, - favoriteSongs: Id<"songs">[], - favoriteAlbums: Id<"albums">[], - albumId: Id<"albums"> -) { - const albumSongIds = await getSongIdsForAlbum(ctx, albumId) - if (!allAlbumSongsFavorited(albumSongIds, favoriteSongs)) { - return favoriteAlbums - } - return uniqueIds([...favoriteAlbums, albumId]) -} - async function promoteArtistIfAllAlbumsFavorited( ctx: MutationCtx, - favoriteAlbums: Id<"albums">[], - favoriteArtists: Id<"artists">[], + userId: Id<"users">, artistId: Id<"artists"> ) { - const artistAlbumIds = await getAlbumIdsForArtist(ctx, artistId) - if (!allArtistAlbumsFavorited(artistAlbumIds, favoriteAlbums)) { - return favoriteArtists + const albumIds = await getAlbumIdsForArtist(ctx, artistId) + if (await allFavorited(ctx, userId, "album", albumIds)) { + await addFavorites(ctx, userId, "artist", [artistId]) } - return uniqueIds([...favoriteArtists, artistId]) } +/** Favorites albums whose songs are now all favorited, then their artists. */ async function cascadeUpFromSongs( ctx: MutationCtx, - favoriteSongs: Id<"songs">[], - favoriteAlbums: Id<"albums">[], - favoriteArtists: Id<"artists">[], + userId: Id<"users">, songs: Doc<"songs">[] ) { - let albums = favoriteAlbums - let artists = favoriteArtists - - const albumIds = new Set(songs.map((song) => song.album)) - for (const albumId of albumIds) { - albums = await promoteAlbumIfAllSongsFavorited( - ctx, - favoriteSongs, - albums, - albumId - ) + for (const albumId of new Set(songs.map((song) => song.album))) { + const songIds = await getSongIdsForAlbum(ctx, albumId) + if (await allFavorited(ctx, userId, "song", songIds)) { + await addFavorites(ctx, userId, "album", [albumId]) + } } - - const artistIds = new Set(songs.map((song) => song.albumArtist)) - for (const artistId of artistIds) { - artists = await promoteArtistIfAllAlbumsFavorited( - ctx, - albums, - artists, - artistId - ) + for (const artistId of new Set(songs.map((song) => song.albumArtist))) { + await promoteArtistIfAllAlbumsFavorited(ctx, userId, artistId) } - - return { favoriteAlbums: albums, favoriteArtists: artists } } export const getUserFavorites = query({ args: {}, - handler: async (ctx) => { + handler: async (ctx): Promise => { const userId = await getAuthUserId(ctx) if (userId === null) { - return { - favoriteSongs: [] as Id<"songs">[], - favoriteAlbums: [] as Id<"albums">[], - favoriteArtists: [] as Id<"artists">[], - } + return { favoriteSongs: [], favoriteAlbums: [], favoriteArtists: [] } } - - const user = await ctx.db.get("users", userId) - if (!user) { - return { - favoriteSongs: [] as Id<"songs">[], - favoriteAlbums: [] as Id<"albums">[], - favoriteArtists: [] as Id<"artists">[], - } - } - + const favorites = await readFavorites(ctx, userId) return { - favoriteSongs: user.favoriteSongs ?? [], - favoriteAlbums: user.favoriteAlbums ?? [], - favoriteArtists: user.favoriteArtists ?? [], + favoriteSongs: chunk(favorites.favoriteSongs), + favoriteAlbums: chunk(favorites.favoriteAlbums), + favoriteArtists: chunk(favorites.favoriteArtists), } }, }) @@ -165,6 +107,7 @@ async function browseFavoritesSummaryHandler( return null } + const { favoriteSongs } = await readFavorites(ctx, userId) return { id: null, isFavorites: true, @@ -176,13 +119,13 @@ async function browseFavoritesSummaryHandler( cover: null, isOwner: true, isSubscribed: false, - songIds: user.favoriteSongs ?? [], + songIds: favoriteSongs, } } export const browseFavoritesPlaylistDetail = query({ args: {}, - handler: async (ctx): Promise => { + handler: async (ctx): Promise => { const summary = await browseFavoritesSummaryHandler(ctx) if (!summary) { return null @@ -196,7 +139,7 @@ export const browseFavoritesPlaylistDetail = query({ title: summary.title, owner: summary.owner, cover: summary.cover, - songs, + songs: chunk(songs), isOwner: summary.isOwner, isSubscribed: summary.isSubscribed, } @@ -206,69 +149,35 @@ export const browseFavoritesPlaylistDetail = query({ export const favoriteSong = mutation({ args: { songId: v.id("songs") }, handler: async (ctx, { songId }) => { - const userId = await requireAuth(ctx) - const user = await ctx.db.get("users", userId) - if (!user) { - throw new ConvexError("User not found") - } - + const userId = await requireFavoritesUser(ctx) const song = await ctx.db.get("songs", songId) if (!song) { throw new ConvexError("Song not found") } - const favoriteSongs = uniqueIds([...(user.favoriteSongs ?? []), songId]) - const cascaded = await cascadeUpFromSongs( - ctx, - favoriteSongs, - user.favoriteAlbums ?? [], - user.favoriteArtists ?? [], - [song] - ) - - await ctx.db.patch(userId, { - favoriteSongs, - favoriteAlbums: cascaded.favoriteAlbums, - favoriteArtists: cascaded.favoriteArtists, - }) + await addFavorites(ctx, userId, "song", [songId]) + await cascadeUpFromSongs(ctx, userId, [song]) }, }) export const favoriteSongs = mutation({ args: { songIds: v.array(v.id("songs")) }, handler: async (ctx, { songIds }) => { - const userId = await requireAuth(ctx) - const user = await ctx.db.get("users", userId) - if (!user) { - throw new ConvexError("User not found") - } - - const uniqueRequestedIds = uniqueIds(songIds) + const userId = await requireFavoritesUser(ctx) const loadedSongs = await Promise.all( - uniqueRequestedIds.map((id) => ctx.db.get("songs", id)) + [...new Set(songIds)].map((id) => ctx.db.get("songs", id)) ) const songs = loadedSongs.filter( (song): song is Doc<"songs"> => song !== null ) - const validSongIds = songs.map((song) => song._id) - const favoriteSongs = uniqueIds([ - ...(user.favoriteSongs ?? []), - ...validSongIds, - ]) - const cascaded = await cascadeUpFromSongs( + await addFavorites( ctx, - favoriteSongs, - user.favoriteAlbums ?? [], - user.favoriteArtists ?? [], - songs + userId, + "song", + songs.map((song) => song._id) ) - - await ctx.db.patch(userId, { - favoriteSongs, - favoriteAlbums: cascaded.favoriteAlbums, - favoriteArtists: cascaded.favoriteArtists, - }) + await cascadeUpFromSongs(ctx, userId, songs) }, }) @@ -286,159 +195,118 @@ export const unfavoriteSongs = mutation({ }, }) +/** Unfavorites songs along with their albums and artists. */ async function unfavoriteSongsHandler( ctx: MutationCtx, songIds: Id<"songs">[] ) { - const userId = await requireAuth(ctx) - const user = await ctx.db.get("users", userId) - if (!user) { - throw new ConvexError("User not found") - } - - const removeSongs = new Set(songIds) + const userId = await requireFavoritesUser(ctx) const loadedSongs = await Promise.all( - [...removeSongs].map((songId) => ctx.db.get("songs", songId)) + [...new Set(songIds)].map((songId) => ctx.db.get("songs", songId)) ) - const removeAlbums = new Set( - loadedSongs.flatMap((song) => (song ? [song.album] : [])) - ) - const removeArtists = new Set( - loadedSongs.flatMap((song) => (song ? [song.albumArtist] : [])) + const songs = loadedSongs.filter( + (song): song is Doc<"songs"> => song !== null ) - await ctx.db.patch(userId, { - favoriteSongs: (user.favoriteSongs ?? []).filter( - (id) => !removeSongs.has(id) - ), - favoriteAlbums: (user.favoriteAlbums ?? []).filter( - (id) => !removeAlbums.has(id) - ), - favoriteArtists: (user.favoriteArtists ?? []).filter( - (id) => !removeArtists.has(id) - ), - }) + await removeFavorites(ctx, userId, "song", songIds) + await removeFavorites( + ctx, + userId, + "album", + songs.map((song) => song.album) + ) + await removeFavorites( + ctx, + userId, + "artist", + songs.map((song) => song.albumArtist) + ) } export const favoriteAlbum = mutation({ args: { albumId: v.id("albums") }, handler: async (ctx, { albumId }) => { - const userId = await requireAuth(ctx) - const user = await ctx.db.get("users", userId) - if (!user) { - throw new ConvexError("User not found") - } - + const userId = await requireFavoritesUser(ctx) const album = await ctx.db.get("albums", albumId) if (!album) { throw new ConvexError("Album not found") } - const songIds = await getSongIdsForAlbum(ctx, albumId) - - const favoriteAlbums = uniqueIds([...(user.favoriteAlbums ?? []), albumId]) - const favoriteSongs = uniqueIds([...(user.favoriteSongs ?? []), ...songIds]) - const favoriteArtists = await promoteArtistIfAllAlbumsFavorited( + await addFavorites(ctx, userId, "album", [albumId]) + await addFavorites( ctx, - favoriteAlbums, - user.favoriteArtists ?? [], - album.artist + userId, + "song", + await getSongIdsForAlbum(ctx, albumId) ) - - await ctx.db.patch(userId, { - favoriteAlbums, - favoriteSongs, - favoriteArtists, - }) + await promoteArtistIfAllAlbumsFavorited(ctx, userId, album.artist) }, }) +/** Unfavorites an album along with its songs and artist. */ export const unfavoriteAlbum = mutation({ args: { albumId: v.id("albums") }, handler: async (ctx, { albumId }) => { - const userId = await requireAuth(ctx) - const user = await ctx.db.get("users", userId) - if (!user) { - throw new ConvexError("User not found") - } - + const userId = await requireFavoritesUser(ctx) const album = await ctx.db.get("albums", albumId) if (!album) { return } - const albumSongIds = await getSongIdsForAlbum(ctx, albumId) - const removeSongs = new Set(albumSongIds) - - const favoriteAlbums = (user.favoriteAlbums ?? []).filter( - (id) => id !== albumId + await removeFavorites(ctx, userId, "album", [albumId]) + await removeFavorites(ctx, userId, "artist", [album.artist]) + await removeFavorites( + ctx, + userId, + "song", + await getSongIdsForAlbum(ctx, albumId) ) - const favoriteSongs = (user.favoriteSongs ?? []).filter( - (id) => !removeSongs.has(id) - ) - let favoriteArtists = user.favoriteArtists ?? [] - - if (favoriteArtists.includes(album.artist)) { - favoriteArtists = favoriteArtists.filter((id) => id !== album.artist) - } - - await ctx.db.patch(userId, { - favoriteSongs, - favoriteAlbums, - favoriteArtists, - }) }, }) +/** Favorites an artist along with all of their albums and songs. */ export const favoriteArtist = mutation({ args: { artistId: v.id("artists") }, handler: async (ctx, { artistId }) => { - const userId = await requireAuth(ctx) - const user = await ctx.db.get("users", userId) - if (!user) { - throw new ConvexError("User not found") - } - + const userId = await requireFavoritesUser(ctx) const artist = await ctx.db.get("artists", artistId) if (!artist) { throw new ConvexError("Artist not found") } - const albumIds = await getAlbumIdsForArtist(ctx, artistId) - const songIds = await getSongIdsForArtist(ctx, artistId) - - await ctx.db.patch(userId, { - favoriteArtists: uniqueIds([...(user.favoriteArtists ?? []), artistId]), - favoriteAlbums: uniqueIds([...(user.favoriteAlbums ?? []), ...albumIds]), - favoriteSongs: uniqueIds([...(user.favoriteSongs ?? []), ...songIds]), - }) + await addFavorites(ctx, userId, "artist", [artistId]) + await addFavorites( + ctx, + userId, + "album", + await getAlbumIdsForArtist(ctx, artistId) + ) + await addFavorites( + ctx, + userId, + "song", + await getSongIdsForArtist(ctx, artistId) + ) }, }) +/** Unfavorites an artist along with all of their albums and songs. */ export const unfavoriteArtist = mutation({ args: { artistId: v.id("artists") }, handler: async (ctx, { artistId }) => { - const userId = await requireAuth(ctx) - const user = await ctx.db.get("users", userId) - if (!user) { - throw new ConvexError("User not found") - } - - const albumIds = await getAlbumIdsForArtist(ctx, artistId) - const songIds = await getSongIdsForArtist(ctx, artistId) - const removeAlbums = new Set(albumIds) - const removeSongs = new Set(songIds) - - await ctx.db.patch(userId, { - favoriteArtists: (user.favoriteArtists ?? []).filter( - (id) => id !== artistId - ), - favoriteAlbums: (user.favoriteAlbums ?? []).filter( - (id) => !removeAlbums.has(id) - ), - favoriteSongs: (user.favoriteSongs ?? []).filter( - (id) => !removeSongs.has(id) - ), - }) + const userId = await requireFavoritesUser(ctx) + await removeFavorites(ctx, userId, "artist", [artistId]) + await removeFavorites( + ctx, + userId, + "album", + await getAlbumIdsForArtist(ctx, artistId) + ) + await removeFavorites( + ctx, + userId, + "song", + await getSongIdsForArtist(ctx, artistId) + ) }, }) diff --git a/packages/convex/convex/library.ts b/packages/convex/convex/library.ts index d33422f..6477067 100644 --- a/packages/convex/convex/library.ts +++ b/packages/convex/convex/library.ts @@ -21,6 +21,8 @@ import type { SearchTopResultItem, SidebarPlaylists, } from "./types" +import { purgeFavoriteEntities } from "./favoriteStore" +import { chunk, type Chunked } from "./limits" import { recordIntentCatalogChanges } from "./intentCatalogChanges" export type { @@ -154,10 +156,19 @@ function toBrowseAlbum( } } +/** + * Past this many songs, scanning the library takes fewer reads than a get per + * song, album and artist, which would hit Convex's 4,096 reads per function. + */ +const MAX_SONG_GETS = 1_000 + export async function browseSongsFromIds( ctx: QueryCtx, songIds: Id<"songs">[] ): Promise { + if (songIds.length > MAX_SONG_GETS) { + return browseSongsByScan(ctx, songIds) + } const songs = await Promise.all( songIds.map((songId) => ctx.db.get("songs", songId)) ) @@ -167,6 +178,27 @@ export async function browseSongsFromIds( ) } +async function browseSongsByScan( + ctx: QueryCtx, + songIds: Id<"songs">[] +): Promise { + const [songs, albums, artistMap] = await Promise.all([ + ctx.db.query("songs").collect(), + ctx.db.query("albums").collect(), + loadArtistMap(ctx), + ]) + const songMap = new Map(songs.map((song) => [song._id, song])) + const albumMap = new Map(albums.map((album) => [album._id, album])) + return songIds.flatMap((songId) => { + const song = songMap.get(songId) + const album = song && albumMap.get(song.album) + const albumArtist = song && artistMap.get(song.albumArtist) + return song && album && albumArtist + ? [toBrowseSong(song, album, albumArtist)] + : [] + }) +} + async function browseSongDocs( ctx: QueryCtx, songs: Doc<"songs">[] @@ -214,32 +246,34 @@ function toBrowseSong( export const browseAlbums = query({ args: {}, - handler: async (ctx) => { + handler: async (ctx): Promise> => { if (!(await isSignedIn(ctx))) { return [] } const artistMap = await loadArtistMap(ctx) const albums = await ctx.db.query("albums").collect() - return albums - .flatMap((album) => { - const artist = artistMap.get(album.artist) - return artist ? [toBrowseAlbum(album, artist)] : [] - }) - .sort((a, b) => a.title.localeCompare(b.title)) + return chunk( + albums + .flatMap((album) => { + const artist = artistMap.get(album.artist) + return artist ? [toBrowseAlbum(album, artist)] : [] + }) + .sort((a, b) => a.title.localeCompare(b.title)) + ) }, }) export const browseArtists = query({ args: {}, - handler: async (ctx) => { + handler: async (ctx): Promise> => { if (!(await isSignedIn(ctx))) { return [] } const artists = await ctx.db.query("artists").collect() - return artists - .map(toBrowseArtist) - .sort((a, b) => a.name.localeCompare(b.name)) + return chunk( + artists.map(toBrowseArtist).sort((a, b) => a.name.localeCompare(b.name)) + ) }, }) @@ -508,7 +542,9 @@ export const browseSortedSongIds = query({ }, handler: async (ctx, args) => { await requireSignedIn(ctx) - return getSortedLibrarySongIds(ctx, args.sortKey, args.sortDirection) + return chunk( + await getSortedLibrarySongIds(ctx, args.sortKey, args.sortDirection) + ) }, }) @@ -605,6 +641,12 @@ function matchScoreSongArtists( ) } +/** + * Results kept per search section. Broad queries like "a" match most of the + * library, which would overflow Convex's array limit and is too long to scroll. + */ +const MAX_SEARCH_RESULTS = 500 + export const searchLibrary = query({ args: { query: v.string() }, handler: async (ctx, args) => { @@ -700,9 +742,15 @@ export const searchLibrary = query({ const result: LibrarySearchResults = { topResults, - artists: artistResults.map((result) => result.item), - albums: albumResults.map((result) => result.item), - songs: songResults.map((result) => result.item), + artists: artistResults + .slice(0, MAX_SEARCH_RESULTS) + .map((result) => result.item), + albums: albumResults + .slice(0, MAX_SEARCH_RESULTS) + .map((result) => result.item), + songs: songResults + .slice(0, MAX_SEARCH_RESULTS) + .map((result) => result.item), } return result }, @@ -710,7 +758,7 @@ export const searchLibrary = query({ export const browseRecentlyAdded = query({ args: { limit: v.optional(v.number()) }, - handler: async (ctx, args) => { + handler: async (ctx, args): Promise> => { if (!(await isSignedIn(ctx))) { return [] } @@ -723,10 +771,12 @@ export const browseRecentlyAdded = query({ albums.map((album) => album.artist) ) - return albums.flatMap((album) => { - const artist = artistMap.get(album.artist) - return artist ? [toBrowseAlbum(album, artist)] : [] - }) + return chunk( + albums.flatMap((album) => { + const artist = artistMap.get(album.artist) + return artist ? [toBrowseAlbum(album, artist)] : [] + }) + ) }, }) @@ -1161,7 +1211,10 @@ async function purgeSongsFromUsersAndPlaylists( } } - // Legacy queue fields stay until `migrations.migrateUsers` clears them. + await purgeFavoriteEntities(ctx, remove) + + // Legacy favorite and queue fields stay until `migrations.migrateUsers` + // clears them. for (const user of await ctx.db.query("users").collect()) { const favoriteSongs = user.favoriteSongs const songQueue = user.songQueue @@ -1202,6 +1255,9 @@ async function purgeAlbumsFromUsers( return } + await purgeFavoriteEntities(ctx, remove) + + // Legacy `favoriteAlbums` stays until `migrations.migrateUsers` clears it. for (const user of await ctx.db.query("users").collect()) { const favoriteAlbums = user.favoriteAlbums const recentlyPlayed = user.recentlyPlayed @@ -1232,6 +1288,9 @@ async function purgeArtistsFromUsers( return } + await purgeFavoriteEntities(ctx, remove) + + // Legacy `favoriteArtists` stays until `migrations.migrateUsers` clears it. for (const user of await ctx.db.query("users").collect()) { const favoriteArtists = user.favoriteArtists if (favoriteArtists?.some((artistId) => remove.has(artistId))) { diff --git a/packages/convex/convex/limits.ts b/packages/convex/convex/limits.ts index 8adb174..d4ae255 100644 --- a/packages/convex/convex/limits.ts +++ b/packages/convex/convex/limits.ts @@ -8,3 +8,20 @@ export const MAX_QUEUE_LENGTH = 5_000 /** Largest profile photo accepted, in bytes after base64 decoding. */ export const MAX_PROFILE_IMAGE_BYTES = 2 * 1024 * 1024 + +/** Most values Convex accepts in a single array. */ +export const MAX_ARRAY_LENGTH = 8_192 + +/** + * A list split into arrays of at most `MAX_ARRAY_LENGTH` values, so a query can + * return more than one array allows. Read it back with `.flat()`. + */ +export type Chunked = T[][] + +export function chunk(items: readonly T[]): Chunked { + const chunks: Chunked = [] + for (let start = 0; start < items.length; start += MAX_ARRAY_LENGTH) { + chunks.push(items.slice(start, start + MAX_ARRAY_LENGTH)) + } + return chunks +} diff --git a/packages/convex/convex/migrations.ts b/packages/convex/convex/migrations.ts index f8ce5bb..132457e 100644 --- a/packages/convex/convex/migrations.ts +++ b/packages/convex/convex/migrations.ts @@ -1,13 +1,15 @@ import { v } from "convex/values" import { internal } from "./_generated/api" import { internalMutation } from "./_generated/server" +import { hasLegacyFavorites, migrateUserFavorites } from "./favoriteStore" import { hasLegacyPlayback, migrateUserPlayback } from "./playbackState" const BATCH_SIZE = 25 /** - * Moves playback state off `users` into `playbackStates`/`playbackProgress` - * and legacy data URL avatars into file storage. Idempotent; the Docker + * Moves playback state off `users` into `playbackStates`/`playbackProgress`, + * favorite arrays into `favorites` rows, and legacy data URL avatars into + * file storage. Idempotent; the Docker * entrypoint runs it on every boot. Run manually with * `npx convex run migrations:migrateUsers`. */ @@ -22,6 +24,12 @@ export const migrateUsers = internalMutation({ if (hasLegacyPlayback(user)) { await migrateUserPlayback(ctx, user) } + if (hasLegacyFavorites(user)) { + // One mutation per user: a large library can mean thousands of rows. + await ctx.scheduler.runAfter(0, internal.migrations.migrateFavorites, { + userId: user._id, + }) + } if (!user.imageStorageId && user.image?.startsWith("data:")) { await ctx.scheduler.runAfter(0, internal.users.storeProfileImage, { userId: user._id, @@ -37,3 +45,14 @@ export const migrateUsers = internalMutation({ } }, }) + +/** Moves one user's favorites into rows. Scheduled by `migrateUsers`. */ +export const migrateFavorites = internalMutation({ + args: { userId: v.id("users") }, + handler: async (ctx, { userId }) => { + const user = await ctx.db.get(userId) + if (user) { + await migrateUserFavorites(ctx, user) + } + }, +}) diff --git a/packages/convex/convex/playlists.ts b/packages/convex/convex/playlists.ts index dd34f42..ab18f2c 100644 --- a/packages/convex/convex/playlists.ts +++ b/packages/convex/convex/playlists.ts @@ -3,6 +3,7 @@ import { ConvexError, v } from "convex/values" import type { Doc, Id } from "./_generated/dataModel" import type { MutationCtx, QueryCtx } from "./_generated/server" import { mutation, query } from "./_generated/server" +import { chunk } from "./limits" import type { BrowsePlaylistDetail, BrowsePlaylistSummary, @@ -290,7 +291,7 @@ export const getArtistSongIds = query({ .query("songs") .withIndex("by_artist", (q) => q.eq("albumArtist", args.artistId)) .collect() - return songs.map((song) => song._id) + return chunk(songs.map((song) => song._id)) }, }) @@ -321,7 +322,7 @@ export const getOrderedArtistSongIds = query({ return a.name.localeCompare(b.name) }) - return songs.map((song) => song._id) + return chunk(songs.map((song) => song._id)) }, }) diff --git a/packages/convex/convex/schema.ts b/packages/convex/convex/schema.ts index d090aba..1690dcb 100644 --- a/packages/convex/convex/schema.ts +++ b/packages/convex/convex/schema.ts @@ -27,6 +27,8 @@ export default defineSchema({ ), preferAtmos: v.optional(v.boolean()), subscribedPlaylists: v.optional(v.array(v.id("playlists"))), + // Legacy favorites, now rows in `favorites`. `migrations.migrateUsers` + // clears them; drop them once every deployment has run it. favoriteArtists: v.optional(v.array(v.id("artists"))), favoriteAlbums: v.optional(v.array(v.id("albums"))), favoriteSongs: v.optional(v.array(v.id("songs"))), @@ -58,6 +60,31 @@ export default defineSchema({ songId: v.optional(v.id("songs")), progressSeconds: v.number(), }).index("by_user", ["userId"]), + // One row per favorite. `by_user_kind` lists them in the order they were + // favorited (the Favorites playlist order), `by_user_entity` answers "is this + // favorited", and `by_entity` lets the scanner purge deleted media. + favorites: defineTable( + v.union( + v.object({ + userId: v.id("users"), + kind: v.literal("song"), + entityId: v.id("songs"), + }), + v.object({ + userId: v.id("users"), + kind: v.literal("album"), + entityId: v.id("albums"), + }), + v.object({ + userId: v.id("users"), + kind: v.literal("artist"), + entityId: v.id("artists"), + }) + ) + ) + .index("by_user_kind", ["userId", "kind"]) + .index("by_user_entity", ["userId", "entityId"]) + .index("by_entity", ["entityId"]), authVerifiers: defineTable({ sessionId: v.optional(v.id("authSessions")), signature: v.optional(v.string()), diff --git a/packages/convex/convex/types.ts b/packages/convex/convex/types.ts index 6cd39de..160e8f5 100644 --- a/packages/convex/convex/types.ts +++ b/packages/convex/convex/types.ts @@ -1,4 +1,5 @@ import type { Id } from "./_generated/dataModel" +import type { Chunked } from "./limits" export type BrowseArtist = { id: Id<"artists"> @@ -75,6 +76,11 @@ export type BrowsePlaylistDetail = { isSubscribed: boolean } +/** Favorites can outgrow one array, so its songs come back chunked. */ +export type BrowseFavoritesDetail = Omit & { + songs: Chunked +} + export type BrowsePlaylistSummary = Omit & { songIds: Id<"songs">[] } diff --git a/packages/convex/convex/users.ts b/packages/convex/convex/users.ts index 35957b6..21c68e3 100644 --- a/packages/convex/convex/users.ts +++ b/packages/convex/convex/users.ts @@ -16,6 +16,7 @@ import { import type { Doc, Id } from "./_generated/dataModel" import type { ActionCtx, MutationCtx } from "./_generated/server" import { MAX_PROFILE_IMAGE_BYTES } from "./limits" +import { deleteUserFavorites } from "./favoriteStore" import { deleteUserPlayback } from "./playbackState" const playbackQualityValidator = v.union( @@ -400,6 +401,7 @@ export const deleteUser = mutation({ } await deleteOwnedPlaylists(ctx, userId) await deleteUserPlayback(ctx, userId) + await deleteUserFavorites(ctx, userId) await setProfileImage(ctx, userId, null) await deleteUserAuthData(ctx, userId) },