From 4e6c9ec5454573b2942a47a164a795f66a731404 Mon Sep 17 00:00:00 2001 From: Ryan Cohen Date: Wed, 3 Aug 2022 10:55:00 +0000 Subject: [PATCH] feat: add pagination options to watchlist api request --- server/api/plextv.ts | 9 ++++++++- server/routes/discover.ts | 33 ++++++++++++++------------------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/server/api/plextv.ts b/server/api/plextv.ts index 33a26e730..cede458d1 100644 --- a/server/api/plextv.ts +++ b/server/api/plextv.ts @@ -286,11 +286,18 @@ class PlexTvAPI extends ExternalAPI { return parsedXml; } - public async getWatchlist(): Promise { + public async getWatchlist({ + offset = 0, + size = 20, + }: { offset?: number; size?: number } = {}): Promise { try { const response = await this.axios.get( '/library/sections/watchlist/all', { + params: { + 'X-Plex-Container-Start': offset, + 'X-Plex-Container-Size': size, + }, baseURL: 'https://metadata.provider.plex.tv', } ); diff --git a/server/routes/discover.ts b/server/routes/discover.ts index 789ca844d..a1687a546 100644 --- a/server/routes/discover.ts +++ b/server/routes/discover.ts @@ -709,29 +709,24 @@ discoverRoutes.get<{ language: string }, GenreSliderItem[]>( } ); -discoverRoutes.get( - '/watchlist', - async (req, res, next) => { - const userRepository = getRepository(User); +discoverRoutes.get('/watchlist', async (req, res) => { + const userRepository = getRepository(User); - const activeUser = await userRepository.findOne({ - where: { id: req.user?.id }, - select: ['id', 'plexToken'], - }); + const activeUser = await userRepository.findOne({ + where: { id: req.user?.id }, + select: ['id', 'plexToken'], + }); - if (!activeUser?.plexToken) { - return next({ - status: 500, - message: 'Must be a Plex account to use watchlist feature.', - }); - } + if (!activeUser?.plexToken) { + // We will just return an empty array if the user has no plex token + return res.json([]); + } - const plexTV = new PlexTvAPI(activeUser?.plexToken); + const plexTV = new PlexTvAPI(activeUser?.plexToken); - const watchlist = await plexTV.getWatchlist(); + const watchlist = await plexTV.getWatchlist(); - return res.json(watchlist); - } -); + return res.json(watchlist); +}); export default discoverRoutes;