diff --git a/package.json b/package.json index 0485f1b7..ffb49b4a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "podverse-api", - "version": "4.15.11", + "version": "4.15.12", "description": "Data API, database migration scripts, and backend services for all Podverse models.", "contributors": [ "Mitch Downey" diff --git a/src/controllers/episode.ts b/src/controllers/episode.ts index 097eafbd..520d6aab 100644 --- a/src/controllers/episode.ts +++ b/src/controllers/episode.ts @@ -204,7 +204,7 @@ const generateEpisodeSelects = ( } if (podcastsOnly) { - qb.andWhere(`podcast.medium = 'podcast' OR podcast."hasVideo" IS true`) + qb.andWhere(`(podcast.medium = 'podcast' OR podcast."hasVideo" IS true)`) } if (liveItemStatus) { diff --git a/src/controllers/mediaRef.ts b/src/controllers/mediaRef.ts index 970569d7..48b8857b 100644 --- a/src/controllers/mediaRef.ts +++ b/src/controllers/mediaRef.ts @@ -214,7 +214,7 @@ const getMediaRefs = async (query, isFromManticoreSearch?, totalOverride?) => { } if (podcastsOnly) { - qb.andWhere(`podcast.medium = 'podcast' OR podcast."hasVideo" IS true`) + qb.andWhere(`(podcast.medium = 'podcast' OR podcast."hasVideo" IS true)`) } const allowRandom = podcastIds.length > 0 || episodeIds.length > 0 diff --git a/src/controllers/playlist.ts b/src/controllers/playlist.ts index ddbb104b..647e3139 100644 --- a/src/controllers/playlist.ts +++ b/src/controllers/playlist.ts @@ -292,7 +292,7 @@ const addOrRemovePlaylistItem = async (playlistId, mediaRefId, episodeId, logged actionTaken = 'added' const filteredMedium = getPlaylistMedium(mediaRef.episode.podcast.medium) if (playlist.medium !== 'mixed' && playlist.medium !== filteredMedium) { - throw new createError.NotFound('Item can not be added to this type of playlist') + throw new createError.BadRequest('Item can not be added to this type of playlist') } playlist.mediaRefs.push(mediaRef) playlist.itemsOrder.push(mediaRef.id) @@ -314,7 +314,7 @@ const addOrRemovePlaylistItem = async (playlistId, mediaRefId, episodeId, logged actionTaken = 'added' const filteredMedium = getPlaylistMedium(episode.podcast.medium) if (playlist.medium !== 'mixed' && playlist.medium !== filteredMedium) { - throw new createError.NotFound('Item can not be added to this type of playlist') + throw new createError.BadRequest('Item can not be added to this type of playlist') } playlist.episodes.push(episode) playlist.itemsOrder.push(episode.id) diff --git a/src/controllers/podcast.ts b/src/controllers/podcast.ts index 624f34fa..0e2686e5 100644 --- a/src/controllers/podcast.ts +++ b/src/controllers/podcast.ts @@ -271,7 +271,7 @@ const getPodcasts = async (query, countOverride?, isFromManticoreSearch?) => { } if (podcastsOnly) { - qb.andWhere(`podcast.medium = 'podcast' OR podcast."hasVideo" IS true`) + qb.andWhere(`(podcast.medium = 'podcast' OR podcast."hasVideo" IS true)`) } const allowRandom = !!podcastIds diff --git a/src/controllers/secondaryQueue.ts b/src/controllers/secondaryQueue.ts index fbc5c0f3..ffc7e4e9 100644 --- a/src/controllers/secondaryQueue.ts +++ b/src/controllers/secondaryQueue.ts @@ -12,6 +12,73 @@ type SecondaryQueueEpisodesForPodcastIdResponseData = { inheritedPodcast: Podcast } +export const getSecondaryQueueEpisodesForPodcastId2 = async ( + episodeId: string, + podcastId: string +): Promise => { + const repository = getRepository(Episode) + const currentEpisode = await repository.findOne( + { + isPublic: true, + id: episodeId + }, + { relations } + ) + + if (!currentEpisode || currentEpisode.liveItem) { + throw new createError.NotFound('Episode not found') + } + + const inheritedPodcast = currentEpisode.podcast + if (!inheritedPodcast) { + throw new createError.NotFound('Podcast not found') + } + + const { itunesEpisode, pubDate } = currentEpisode + const take = currentEpisode.podcast.medium === 'music' ? 50 : 10 + + const previousEpisodesAndWhere = + currentEpisode.podcast.medium === 'music' + ? { itunesEpisode: LessThan(itunesEpisode) } + : { pubDate: LessThan(pubDate) } + const previousEpisodesOrder = + currentEpisode.podcast.medium === 'music' ? ['itunesEpisode', 'DESC'] : ['pubDate', 'DESC'] + const previousEpisodes = await repository.find({ + where: { + isPublic: true, + podcastId, + ...previousEpisodesAndWhere + }, + order: { + [previousEpisodesOrder[0]]: previousEpisodesOrder[1] + }, + take, + relations: ['authors', 'podcast', 'podcast.authors'] + }) + + const nextEpisodesAndWhere = + currentEpisode.podcast.medium === 'music' + ? { itunesEpisode: MoreThan(itunesEpisode) } + : { pubDate: MoreThan(pubDate) } + const nextEpisodesOrder = currentEpisode.podcast.medium === 'music' ? ['itunesEpisode', 'ASC'] : ['pubDate', 'ASC'] + const nextEpisodes = await repository.find({ + where: { + isPublic: true, + podcastId, + ...nextEpisodesAndWhere + }, + order: { + [nextEpisodesOrder[0]]: nextEpisodesOrder[1] + }, + take, + relations: ['authors', 'podcast', 'podcast.authors'] + }) + + return { previousEpisodes, nextEpisodes, inheritedPodcast } +} + +// THIS HAS SOME CRITICAL BUGS IN PODVERSE-RN 4.15.0 +// I'M DEPRECATING IT AND REPLACING IT WITH THE "2" VERSION export const getSecondaryQueueEpisodesForPodcastId = async ( episodeId: string, podcastId: string @@ -85,7 +152,8 @@ type SecondaryQueueEpisodesForPlaylistIdResponseData = { export const getSecondaryQueueEpisodesForPlaylist = async ( playlistId: string, episodeOrMediaRefId: string, - audioOnly: boolean + audioOnly: boolean, + withFix: boolean ): Promise => { const currentPlaylist = await getPlaylist(playlistId) @@ -114,7 +182,10 @@ export const getSecondaryQueueEpisodesForPlaylist = async ( }) // limit to the nearest 50 ids - const previousEpisodesAndMediaRefs = filteredPlaylistItems.slice(currentItemIndex - 50, currentItemIndex) + let previousEpisodesAndMediaRefs = filteredPlaylistItems.slice(currentItemIndex - 50, currentItemIndex) + if (withFix) { + previousEpisodesAndMediaRefs = previousEpisodesAndMediaRefs.reverse() + } const nextEpisodesAndMediaRefs = filteredPlaylistItems.slice(currentItemIndex + 1, currentItemIndex + 1 + 50) return { previousEpisodesAndMediaRefs, nextEpisodesAndMediaRefs } diff --git a/src/routes/secondaryQueue.ts b/src/routes/secondaryQueue.ts index c374d46b..452f862e 100644 --- a/src/routes/secondaryQueue.ts +++ b/src/routes/secondaryQueue.ts @@ -2,29 +2,26 @@ import * as Router from 'koa-router' import { config } from '~/config' import { getSecondaryQueueEpisodesForPodcastId, - getSecondaryQueueEpisodesForPlaylist + getSecondaryQueueEpisodesForPlaylist, + getSecondaryQueueEpisodesForPodcastId2 } from '~/controllers/secondaryQueue' import { emitRouterError } from '~/lib/errors' import { parseNSFWHeader } from '~/middleware/parseNSFWHeader' const router = new Router({ prefix: `${config.apiPrefix}${config.apiVersion}/secondary-queue` }) -/* TODO: REMOVE THIS AFTER NEXT BETA RELEASE */ -// Get episodes that are adjacent within a podcast -router.get('/episode/:episodeId/podcast/:podcastId', parseNSFWHeader, async (ctx) => { - try { - const data = await getSecondaryQueueEpisodesForPodcastId(ctx.params.episodeId, ctx.params.podcastId) - ctx.body = data - } catch (error) { - emitRouterError(error, ctx) - } -}) - // Get episodes that are adjacent within a podcast router.get('/podcast/:podcastId/episode/:episodeId', parseNSFWHeader, async (ctx) => { try { - const data = await getSecondaryQueueEpisodesForPodcastId(ctx.params.episodeId, ctx.params.podcastId) - ctx.body = data + const { withFix } = ctx.query + if (!!withFix) { + const data = await getSecondaryQueueEpisodesForPodcastId2(ctx.params.episodeId, ctx.params.podcastId) + ctx.body = data + } else { + // DEPRECATED AS OF v4.15.1 PODVERSE MOBILE + const data = await getSecondaryQueueEpisodesForPodcastId(ctx.params.episodeId, ctx.params.podcastId) + ctx.body = data + } } catch (error) { emitRouterError(error, ctx) } @@ -36,7 +33,8 @@ router.get('/playlist/:playlistId/episode-or-media-ref/:episodeOrMediaRef', pars const data = await getSecondaryQueueEpisodesForPlaylist( ctx.params.playlistId, ctx.params.episodeOrMediaRef, - !!ctx.query.audioOnly + !!ctx.query.audioOnly, + !!ctx.query.withFix ) ctx.body = data } catch (error) { diff --git a/src/seeds/qa/podcastIndexIds.ts b/src/seeds/qa/podcastIndexIds.ts index 1c009d77..67a5b48f 100644 --- a/src/seeds/qa/podcastIndexIds.ts +++ b/src/seeds/qa/podcastIndexIds.ts @@ -5,14 +5,14 @@ export const podcastIndexIds = [ 5718023, 387129, 3662287, 160817, 150842, 878147, 487548, 167137, 465231, 767934, 577105, 54545, 650774, 955598, 3758236, 203827, 879740, 393504, 575694, 921030, 41504, 5341434, 757675, 174725, 920666, 1333070, 227573, 5465405, - 5498327, 5495489, 556715, 5485175, 202764, 830124, 66844, 4169501, 6524027 + 5498327, 5495489, 556715, 5485175, 202764, 830124, 66844, 4169501, 6524027, 6569395 ] -export const podcastIndexIdsQuick = [5718023, 387129, 3662287, 160817] +export const podcastIndexIdsQuick = [5718023, 387129, 3662287, 160817, 6569395] export const podcastIndexIdsWithLiveItems = [ 4935828, 5495489, 162612, 5461087, 486332, 480983, 3727160, 5496786, 901876, 5498327, 4207213, 5710520, 5465405, 5485175, 574891, 920666, 540927, 4432692, 5718023, 41504, 3756449, 150842, 937170, 946122, 5373053, 624721, 5700613, - 288180, 955598, 6524027 + 288180, 955598, 6524027, 6569395 ] -export const podcastIndexIdsWithLiveItemsQuick = [4935828, 5495489, 162612, 5461087, 6524027] +export const podcastIndexIdsWithLiveItemsQuick = [4935828, 5495489, 162612, 5461087, 6524027, 6569395]