Skip to content

Commit

Permalink
Merge pull request #678 from podverse/develop
Browse files Browse the repository at this point in the history
Release v4.14.6
  • Loading branch information
mitchdowney authored Oct 21, 2023
2 parents 143e970 + 66b911c commit 6b877b2
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "podverse-api",
"version": "4.14.5",
"version": "4.14.6",
"description": "Data API, database migration scripts, and backend services for all Podverse models.",
"contributors": [
"Mitch Downey"
Expand Down
4 changes: 4 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
podcastIndexRouter,
podpingRouter,
upDeviceRouter,
urlResolverRouter,
userHistoryItemRouter,
userNowPlayingItemRouter,
userQueueItemRouter,
Expand Down Expand Up @@ -202,6 +203,9 @@ export const createApp = async (conn: Connection) => {
app.use(upDeviceRouter.routes())
app.use(upDeviceRouter.allowedMethods())

app.use(urlResolverRouter.routes())
app.use(urlResolverRouter.allowedMethods())

app.use(userHistoryItemRouter.routes())
app.use(userHistoryItemRouter.allowedMethods())

Expand Down
49 changes: 48 additions & 1 deletion src/controllers/episode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,52 @@ const getEpisodesWithLiveItemsWithoutMatchingGuids = async (podcastId: string, e
return episodes
}

type GetEpisodeWebUrl = {
podcastGuid?: string
podcastIndexId?: string
episodeGuid?: string
}

const getEpisodeWebUrl = async ({ podcastGuid, podcastIndexId, episodeGuid }: GetEpisodeWebUrl) => {
if (episodeGuid && podcastGuid) {
const episode = await getRepository(Episode)
.createQueryBuilder('episode')
.innerJoin('episode.podcast', 'podcast')
.addSelect('episode.id')
.where(`podcast.podcastGuid = :podcastGuid`, { podcastGuid })
.andWhere('episode.guid = :episodeGuid', { episodeGuid })
.andWhere('episode.isPublic IS true')
.getOne()

if (!episode) {
throw new createError.NotFound('Episode not found')
}

return {
webUrl: `${config.websiteProtocol}://${config.websiteDomain}/episode/${episode.id}`
}
} else if (episodeGuid && podcastIndexId) {
const episode = await getRepository(Episode)
.createQueryBuilder('episode')
.innerJoin('episode.podcast', 'podcast')
.addSelect('episode.id')
.where(`podcast.podcastIndexId = :podcastIndexId`, { podcastIndexId })
.andWhere('episode.guid = :episodeGuid', { episodeGuid })
.andWhere('episode.isPublic IS true')
.getOne()

if (!episode) {
throw new createError.NotFound('Episode not found')
}

return {
webUrl: `${config.websiteProtocol}://${config.websiteDomain}/episode/${episode.id}`
}
} else {
throw new createError.NotFound('Episode not found')
}
}

export {
dropAndRecreateEpisodesMostRecentMaterializedView,
getEpisode,
Expand All @@ -741,5 +787,6 @@ export {
getEpisodesWithLiveItemsWithoutMatchingGuids,
refreshEpisodesMostRecentMaterializedView,
removeDeadEpisodes,
retrieveLatestChapters
retrieveLatestChapters,
getEpisodeWebUrl
}
25 changes: 24 additions & 1 deletion src/controllers/podcast.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getRepository, In } from 'typeorm'
import { config } from '~/config'
import { getUserSubscribedPodcastIds } from '~/controllers/user'
import { FeedUrl, Podcast, User } from '~/entities'
import { addOrderByToQuery, getManticoreOrderByColumnName, removeAllSpaces } from '~/lib/utility'
Expand Down Expand Up @@ -448,6 +449,27 @@ const updateHasPodcastIndexValueTags = async (podcastIndexIds: string[]) => {
console.log('newPodcastsToUpdate finished')
}

type GetPodcastWebUrl = {
podcastGuid?: string
podcastIndexId?: string
}

const getPodcastWebUrl = async ({ podcastGuid, podcastIndexId }: GetPodcastWebUrl) => {
if (podcastGuid) {
const podcast = await getPodcastByPodcastGuid(podcastGuid)
return {
webUrl: `${config.websiteProtocol}://${config.websiteDomain}/podcast/${podcast.id}`
}
} else if (podcastIndexId) {
const podcast = await getPodcastByPodcastIndexId(podcastIndexId)
return {
webUrl: `${config.websiteProtocol}://${config.websiteDomain}/podcast/${podcast.id}`
}
} else {
throw new createError.NotFound('Podcast not found')
}
}

export {
findPodcastsByFeedUrls,
getPodcast,
Expand All @@ -460,5 +482,6 @@ export {
getSubscribedPodcasts,
subscribeToPodcast,
toggleSubscribeToPodcast,
updateHasPodcastIndexValueTags
updateHasPodcastIndexValueTags,
getPodcastWebUrl
}
23 changes: 23 additions & 0 deletions src/middleware/queryValidation/urlResolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const Joi = require('joi')
import { validateBaseQuery } from './base'

const validateUrlResolverPodcast = async (ctx, next) => {
const schema = Joi.object().keys({
podcastGuid: Joi.string(),
podcastIndexId: Joi.string()
})

await validateBaseQuery(schema, ctx, next)
}

const validateUrlResolverEpisode = async (ctx, next) => {
const schema = Joi.object().keys({
podcastGuid: Joi.string(),
podcastIndexId: Joi.string(),
episodeGuid: Joi.string()
})

await validateBaseQuery(schema, ctx, next)
}

export { validateUrlResolverPodcast, validateUrlResolverEpisode }
1 change: 1 addition & 0 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export { podpingRouter } from '~/routes/podping'
export { toolsRouter } from '~/routes/tools'
export { upDeviceRouter } from '~/routes/upDevice'
export { userRouter } from '~/routes/user'
export { urlResolverRouter } from '~/routes/urlResolver'
export { userHistoryItemRouter } from '~/routes/userHistoryItem'
export { userNowPlayingItemRouter } from '~/routes/userNowPlayingItem'
export { userQueueItemRouter } from '~/routes/userQueueItem'
41 changes: 41 additions & 0 deletions src/routes/urlResolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as Router from 'koa-router'
import { config } from '~/config'
import { getEpisodeWebUrl } from '~/controllers/episode'
import { getPodcastWebUrl } from '~/controllers/podcast'
import { emitRouterError } from '~/lib/errors'
import { validateUrlResolverEpisode, validateUrlResolverPodcast } from '~/middleware/queryValidation/urlResolver'

const router = new Router({ prefix: `${config.apiPrefix}${config.apiVersion}/url-resolver` })

type UrlResolverPodcastQueryParams = {
podcastGuid?: string
podcastIndexId?: string
}

router.get('/podcast', validateUrlResolverPodcast, async (ctx) => {
try {
const { podcastGuid, podcastIndexId } = ctx.request.query as UrlResolverPodcastQueryParams
const body = await getPodcastWebUrl({ podcastGuid, podcastIndexId })
ctx.body = body
} catch (error) {
emitRouterError(error, ctx)
}
})

type UrlResolverEpisodeQueryParams = {
podcastGuid?: string
podcastIndexId?: string
episodeGuid?: string
}

router.get('/episode', validateUrlResolverEpisode, async (ctx) => {
try {
const { podcastGuid, podcastIndexId, episodeGuid } = ctx.request.query as UrlResolverEpisodeQueryParams
const body = await getEpisodeWebUrl({ podcastGuid, podcastIndexId, episodeGuid })
ctx.body = body
} catch (error) {
emitRouterError(error, ctx)
}
})

export const urlResolverRouter = router

0 comments on commit 6b877b2

Please sign in to comment.