Skip to content

Commit

Permalink
fix: handle 404 in score pagination (fixes Splamy#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lemmmy committed May 8, 2021
1 parent 52c5a05 commit daf46e9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
9 changes: 9 additions & 0 deletions scoresaber.user.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion src/api/scoresaber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ export async function get_user_recent_songs_dynamic(user_id: string, page: numbe

async function get_user_recent_songs_new_api_wrap(user_id: string, page: number): Promise<IUserPageData> {
const recent_songs = await get_user_recent_songs(user_id, page);
if (!recent_songs) {
// We reached the end of pagination unexpectedly (multiple of 8 scores)
return {
meta: { was_last_page: true },
songs: []
};
}

return {
meta: {
Expand All @@ -38,8 +45,15 @@ async function get_user_recent_songs_new_api_wrap(user_id: string, page: number)
};
}

export async function get_user_recent_songs(user_id: string, page: number): Promise<IScoresaberSongList> {
export async function get_user_recent_songs(user_id: string, page: number): Promise<IScoresaberSongList | null> {
const req = await auto_fetch_retry(`${SCORESABER_LINK}/player/${user_id}/scores/recent/${page}`);

// If the user has a multiple of 8 scores, there's no way to know we've
// reached the end of the results until hitting a 404.
if (req.status === 404) {
return null; // End of pagination.
}

const data = await req.json() as IScoresaberSongList;
return sanitize_song_ids(data);
}
Expand Down

0 comments on commit daf46e9

Please sign in to comment.