Normalize API counters in the server mappers instead of route loaders

This commit is contained in:
2026-09-24 14:02:23 +02:00
parent b0163f6a4d
commit ba0a2373b5
6 changed files with 68 additions and 16 deletions
@@ -2,7 +2,7 @@ import type { LayoutServerLoad } from './$types';
import { getAllowAnonymousListening } from '$lib/server/playbackConfig';
import { apiFor } from '$lib/server/api';
import { loginRedirect } from '$lib/server/guards';
import { toLikedTrack, toPlaylist, toTrack } from '$lib/server/mappers';
import { toCount, toLikedTrack, toPlaylist, toTrack } from '$lib/server/mappers';
import { authConfig } from '$lib/server/config';
import { LIKED_TRACKS_PAGE_SIZE, PLAYLIST_PICKER_PAGE_SIZE } from '$lib/config';
@@ -45,7 +45,7 @@ export const load: LayoutServerLoad = async ({ locals, url, fetch }) => {
})
.then(({ data }) => ({
items: (data?.items ?? []).map(toPlaylist),
total: Number(data?.totalItemCount ?? 0)
total: toCount(data?.totalItemCount)
})),
{ items: [], total: 0 }
),
+4 -8
View File
@@ -2,7 +2,7 @@ import { redirect } from '@sveltejs/kit';
import type { PageServerLoad, Actions } from './$types';
import { apiFor } from '$lib/server/api';
import { requireUser } from '$lib/server/guards';
import { toMix, toPlaylist, toTrack } from '$lib/server/mappers';
import { toCount, toListeningStats, toMix, toPlaylist, toTrack } from '$lib/server/mappers';
import { addTrackAction } from '$lib/server/actions/playlist';
import { HOME_MIXES_LIMIT, HOME_SHELF_LIMIT, HOME_SPOTLIGHT_TRACKS_LIMIT } from '$lib/config';
import { pickGreeting } from '$lib/server/greeting';
@@ -24,12 +24,8 @@ export const load: PageServerLoad = async ({ locals, url, fetch }) => {
});
const listeningStatsPromise = api
.GET('/users/{id}/listening-stats', { params: { path: { id: user.sub } } })
.then((res) => ({
tracksThisWeek: Number(res.data?.tracksThisWeek ?? 0),
secondsThisWeek: Number(res.data?.secondsThisWeek ?? 0),
streakDays: Number(res.data?.streakDays ?? 0)
}))
.catch(() => ({ tracksThisWeek: 0, secondsThisWeek: 0, streakDays: 0 }));
.then((res) => toListeningStats(res.data))
.catch(() => toListeningStats(null));
const [playlists, mixes, recentlyPlayed] = await Promise.all([
playlistsPromise,
@@ -49,7 +45,7 @@ export const load: PageServerLoad = async ({ locals, url, fetch }) => {
})
.then((res) => ({
items: (res.data?.items ?? []).map(toTrack),
totalCount: Number(res.data?.totalItemCount ?? 0)
totalCount: toCount(res.data?.totalItemCount)
}))
.catch(() => ({ items: [], totalCount: 0 }))
: Promise.resolve({ items: [], totalCount: 0 });
@@ -1,7 +1,7 @@
import type { PageServerLoad, Actions } from './$types';
import { apiFor, unwrapOrError } from '$lib/server/api';
import { getGenres } from '$lib/server/genres';
import { toAlbum, toPage, toTrack } from '$lib/server/mappers';
import { toAlbum, toCount, toPage, toTrack } from '$lib/server/mappers';
import { addAlbumToPlaylistAction, addTrackAction } from '$lib/server/actions/playlist';
import { EXPLORE_ALBUMS_PAGE_SIZE, EXPLORE_PAGE_SIZE, EXPLORE_USERS_LIMIT } from '$lib/config';
@@ -56,9 +56,9 @@ export const load: PageServerLoad = async ({ url, locals, fetch }) => {
genres,
tracks,
albums: (albumsRes?.data?.items ?? []).map((item) => toAlbum(item.album)),
albumsTotal: Number(albumsRes?.data?.totalItemCount ?? 0),
albumsTotal: toCount(albumsRes?.data?.totalItemCount),
users: usersRes?.data?.items ?? [],
usersTotal: Number(usersRes?.data?.totalItemCount ?? 0),
usersTotal: toCount(usersRes?.data?.totalItemCount),
viewerId: user?.sub ?? null
};
};
@@ -1,7 +1,7 @@
import type { PageServerLoad, Actions } from './$types';
import { apiFor, unwrapOrError } from '$lib/server/api';
import { optionalUser } from '$lib/server/guards';
import { toPlaylist } from '$lib/server/mappers';
import { toCount, toPlaylist } from '$lib/server/mappers';
import { PROFILE_PLAYLISTS_PAGE_SIZE } from '$lib/config';
import { followUserAction, unfollowUserAction } from '$lib/server/actions/follow';
@@ -27,7 +27,7 @@ export const load: PageServerLoad = async ({ params, locals, url, fetch, parent
return {
profile,
followersCount: Number(profile.followersCount),
followersCount: toCount(profile.followersCount),
playlists,
isOwnProfile,
isAnonymous: viewer === null