-
-
Notifications
You must be signed in to change notification settings - Fork 20
sync: v4 with dev #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sync: v4 with dev #164
Changes from all commits
93caf7c
b2f63be
ff65fb8
b6cbfa3
14fa70f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -217,7 +217,12 @@ export default class Spotify implements ISource { | |
| case 'album': | ||
| case 'playlist': { | ||
| const base = link.type === 'album' ? 'albums' : 'playlists'; | ||
| const data = await this.apiRequest(`/${base}/${link.id}`); | ||
| const pageLimit = | ||
| link.type === 'playlist' | ||
| ? this.manager.options.spotify?.limitLoadPlaylistPage ?? 100 | ||
| : this.manager.options.spotify?.limitLoadAlbumPage ?? 50; | ||
|
|
||
| const data = await this.apiRequest(`/${base}/${link.id}?limit=${pageLimit}`); | ||
| if (!data || data.error) { | ||
| return { loadType: 'error', data: { message: `${link.type} not found.` } }; | ||
| } | ||
|
|
@@ -228,14 +233,35 @@ export default class Spotify implements ISource { | |
| : data.tracks.items; | ||
| items = items.filter(Boolean); | ||
|
|
||
| let next = data.tracks.next; | ||
| const max = | ||
| options?.limit ?? (link.type === 'playlist' | ||
| ? this.manager.options.spotify?.limitLoadPlaylist ?? this.manager.options.playlistLoadLimit | ||
| : this.manager.options.spotify?.limitLoadAlbum ?? this.manager.options.playlistLoadLimit); | ||
| options?.limit ?? | ||
| (link.type === 'playlist' | ||
| ? this.manager.options.spotify?.limitLoadPlaylist ?? | ||
| this.manager.options.playlistLoadLimit | ||
| : this.manager.options.spotify?.limitLoadAlbum ?? | ||
| this.manager.options.playlistLoadLimit); | ||
|
|
||
| while (next && (!max || items.length < max)) { | ||
| const nextPage = await this.apiRequest(next); | ||
| if (!nextPage || nextPage.error) break; | ||
| const newItems = | ||
| link.type === 'playlist' | ||
| ? nextPage.items.map((i: any) => i.track) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using Consider defining interfaces for the expected API response structures to leverage TypeScript's type safety. For example: interface SpotifyPlaylistTrack {
track: SpotifyTrackObject;
// ... other fields from Spotify API
}
interface SpotifyTrackObject {
id: string;
name: string;
external_urls?: { spotify?: string };
// ... other fields from Spotify API
}Using these interfaces would make the code more robust and self-documenting. |
||
| : nextPage.items; | ||
| items.push(...newItems.filter(Boolean)); | ||
| next = nextPage.next; | ||
| } | ||
|
|
||
| if (max != null) items = items.slice(0, max); | ||
|
|
||
| const tracks = items.map((item: any) => this.buildTrack(item, item.external_urls.spotify)); | ||
| return { loadType: 'playlist', data: { info: { name: data.name, selectedTrack: 0 }, tracks } }; | ||
| const tracks = items.map((item: any) => | ||
| this.buildTrack(item, item.external_urls?.spotify) | ||
| ); | ||
| return { | ||
| loadType: 'playlist', | ||
| data: { info: { name: data.name, selectedTrack: 0 }, tracks }, | ||
| }; | ||
| } | ||
|
|
||
| default: | ||
|
|
@@ -245,10 +271,10 @@ export default class Spotify implements ISource { | |
|
|
||
| private getLinkType(url: string): { type: string; id: string } | null { | ||
| const regex: Record<string, RegExp> = { | ||
| track: /open\.spotify\.com\/(?:intl-[^/]+\/)?track\/(\w+)/, | ||
| album: /open\.spotify\.com\/(?:intl-[^/]+\/)?album\/(\w+)/, | ||
| playlist: /open\.spotify\.com\/(?:intl-[^/]+\/)?playlist\/(\w+)/, | ||
| artist: /open\.spotify\.com\/(?:intl-[^/]+\/)?artist\/(\w+)/, | ||
| track: /(?:open\.spotify\.com\/(?:intl-[^/]+\/)?track\/|spotify:track:)(\w+)/, | ||
| album: /(?:open\.spotify\.com\/(?:intl-[^/]+\/)?album\/|spotify:album:)(\w+)/, | ||
| playlist: /(?:open\.spotify\.com\/(?:intl-[^/]+\/)?playlist\/|spotify:playlist:)(\w+)/, | ||
| artist: /(?:open\.spotify\.com\/(?:intl-[^/]+\/)?artist\/|spotify:artist:)(\w+)/, | ||
| }; | ||
|
|
||
| for (const type in regex) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
whileloop condition!maxis problematic whenmaxis0. In JavaScript,!0evaluates totrue, which causes the loop to run as if there's no limit, fetching all pages of a playlist/album. This is inefficient and doesn't respect the user's intent to load zero tracks. The loop should not execute ifmaxis 0.A better way to check for an unlimited scenario is
max == null, which is true for bothnullandundefinedbut false for0.