From fb13852d30766961f7bcc2e9d1edd2393d0b5731 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 5 Aug 2020 13:46:01 +0200 Subject: [PATCH] Load all playlist elements --- client/src/standalone/videos/embed.ts | 31 ++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/client/src/standalone/videos/embed.ts b/client/src/standalone/videos/embed.ts index f12b8c9ac..a2902ce58 100644 --- a/client/src/standalone/videos/embed.ts +++ b/client/src/standalone/videos/embed.ts @@ -143,8 +143,11 @@ export class PeerTubeEmbed { return fetch(this.getPlaylistUrl(playlistId)) } - loadPlaylistElements (playlistId: string): Promise { - return fetch(this.getPlaylistUrl(playlistId) + '/videos') + loadPlaylistElements (playlistId: string, start = 0): Promise { + const url = new URL(this.getPlaylistUrl(playlistId) + '/videos') + url.search = new URLSearchParams({ start: '' + start, count: '100' }).toString() + + return fetch(url.toString()) } loadConfig (): Promise { @@ -257,6 +260,28 @@ export class PeerTubeEmbed { } } + private async loadAllPlaylistVideos (playlistId: string, baseResult: ResultList) { + let elements = baseResult.data + let total = baseResult.total + let i = 0 + + while (total > elements.length && i < 10) { + const result = await this.loadPlaylistElements(playlistId, elements.length) + + const json = await result.json() as ResultList + total = json.total + + elements = elements.concat(json.data) + i++ + } + + if (i === 10) { + console.error('Cannot fetch all playlists elements, there are too many!') + } + + return elements + } + private async loadPlaylist (playlistId: string) { const playlistPromise = this.loadPlaylistInfo(playlistId) const playlistElementsPromise = this.loadPlaylistElements(playlistId) @@ -534,7 +559,7 @@ export class PeerTubeEmbed { this.playlist = await res.playlistResponse.json() const playlistElementResult = await res.videosResponse.json() - this.playlistElements = playlistElementResult.data + this.playlistElements = await this.loadAllPlaylistVideos(playlistId, playlistElementResult) this.currentPlaylistElement = this.playlistElements[0] videoId = this.currentPlaylistElement.video.uuid