From 71d9ebc5fd26b287f40546f7e42591aaae0f3a31 Mon Sep 17 00:00:00 2001 From: Dave Nicolson Date: Tue, 19 Nov 2024 08:52:18 +0100 Subject: [PATCH] refactor(PlaylistManager): Optimize playlist video removal (#801) * Handle undefined playlist attributes * Optimize playlist video removal --- src/core/managers/PlaylistManager.ts | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/core/managers/PlaylistManager.ts b/src/core/managers/PlaylistManager.ts index f565640a8..c2be4eaa3 100644 --- a/src/core/managers/PlaylistManager.ts +++ b/src/core/managers/PlaylistManager.ts @@ -110,29 +110,32 @@ export default class PlaylistManager { const playlist = new Playlist(this.#actions, info, true); - if (!playlist.info.is_editable) + if (playlist.info.is_editable === false) throw new InnertubeError('This playlist cannot be edited.', playlist_id); const payload: EditPlaylistEndpointOptions = { playlist_id, actions: [] }; - const getSetVideoIds = async (pl: Feed): Promise => { - const key_id = use_set_video_ids ? 'set_video_id' : 'id'; - const videos = pl.videos.filter((video) => video_ids.includes(video.key(key_id).string())); - + const getSetVideoIds = async (pl: Feed, set_video_ids: string[] = []): Promise => { + const videos = pl.videos.filter((video) => video_ids.includes(video.key('id').string())); videos.forEach((video) => - payload.actions.push({ - action: 'ACTION_REMOVE_VIDEO', - set_video_id: video.key('set_video_id').string() - }) + set_video_ids.push(video.key('set_video_id').string()) ); if (payload.actions.length < video_ids.length) { const next = await pl.getContinuation(); - return getSetVideoIds(next); + return getSetVideoIds(next, set_video_ids); } + + return set_video_ids; }; - await getSetVideoIds(playlist); + const set_video_ids = use_set_video_ids ? video_ids : await getSetVideoIds(playlist); + set_video_ids.forEach((set_video_id: string) => + payload.actions.push({ + action: 'ACTION_REMOVE_VIDEO', + set_video_id + }) + ); if (!payload.actions.length) throw new InnertubeError('Given video ids were not found in this playlist.', video_ids);