-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement get feeds route Implement put feeds Implement delete feed route Fix tests Force color More color tweaks Add why is node running Remove log Fix tests? Fix? Fix route params Wait for app Remove unneeded ends
- Loading branch information
Showing
26 changed files
with
721 additions
and
355 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
routes/api/bookmarks/__id/put-bookmark.js → routes/api/bookmarks/_id/put-bookmark.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
lib/create-episode.js → routes/api/episodes/create-episode-query.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default async function episodesRoutes (fastify, opts) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* eslint-disable camelcase */ | ||
import SQL from '@nearform/sql' | ||
|
||
export async function deleteFeed (fastify, opts) { | ||
const podcastFeedDeleteCounter = new fastify.metrics.client.Counter({ | ||
name: 'breadcrum_podcast_feed_delete_total', | ||
help: 'The number of times podcast feeds are deleted' | ||
}) | ||
|
||
fastify.delete( | ||
'/', | ||
{ | ||
preHandler: fastify.auth([fastify.basicAuth]), | ||
schema: { | ||
parms: { | ||
type: 'object', | ||
properties: { | ||
feed: { | ||
type: 'string', | ||
format: 'uuid' | ||
} | ||
}, | ||
required: ['feed'] | ||
} | ||
} | ||
}, | ||
async function deleteFeedHandler (request, reply) { | ||
return fastify.pg.transact(async client => { | ||
const userId = request.user.id | ||
const { feed: feedId } = request.params | ||
|
||
const feedQuery = SQL` | ||
select | ||
pf.id, | ||
pf.created_at, | ||
pf.updated_at, | ||
pf.title, | ||
pf.description, | ||
pf.image_url, | ||
pf.explicit, | ||
pf.token, | ||
u.username as owner_name | ||
(pf.id = u.default_podcast_feed_id) as default_feed, | ||
from podcast_feeds pf | ||
join users u | ||
on pf.owner_id = u.id | ||
where pf.id = ${feedId} | ||
and pf.owner_id = ${userId} | ||
fetch first row only; | ||
` | ||
|
||
const feedResults = await client.query(feedQuery) | ||
|
||
const pf = feedResults.rows.pop() | ||
if (!pf) { | ||
return reply.notFound(`podcast feed ${feedId} not found`) | ||
} | ||
|
||
if (pf.default_feed) { | ||
return fastify.httpErrors.badRequest('Can\'t delete the default feed') | ||
} | ||
|
||
const query = SQL` | ||
delete from podcast_feeds | ||
where id = ${feedId} | ||
and owner_id =${userId}; | ||
` | ||
|
||
// TODO: check results | ||
await fastify.pg.query(query) | ||
|
||
reply.status = 202 | ||
podcastFeedDeleteCounter.inc() | ||
return { | ||
status: 'ok' | ||
} | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import SQL from '@nearform/sql' | ||
import { getYTDLPUrl } from '../../../../../../lib/run-yt-dlp.js' | ||
import { cache } from '../../../../../../lib/temp-cache.js' | ||
import { getFileKey } from '../../../../../../lib/file-key.js' | ||
|
||
export default async function podcastFeedsRoutes (fastify, opts) { | ||
fastify.get( | ||
'/', | ||
{ | ||
preHandler: fastify.auth([fastify.basicAuth]), | ||
schema: { | ||
parms: { | ||
type: 'object', | ||
properties: { | ||
feed: { | ||
type: 'string', | ||
format: 'uuid' | ||
}, | ||
episodes: { | ||
type: 'string', | ||
format: 'uuid' | ||
} | ||
}, | ||
required: ['feed', 'episode'] | ||
} | ||
} | ||
}, | ||
async function episodeHandler (request, reply) { | ||
const { userId } = request.feedTokenUser | ||
const { feed: feedId, episode: episodeId } = request.params | ||
if (!userId) throw new Error('missing authenticated feed userId') | ||
|
||
const episodeQuery = SQL` | ||
select | ||
e.id, | ||
e.created_at, | ||
e.updated_at, | ||
e.url as src_url, | ||
e.type, | ||
e.medium, | ||
e.size_in_bytes, | ||
e.duration_in_seconds, | ||
e.mime_type, | ||
e.explicit, | ||
e.author_name, | ||
e.filename, | ||
e.ext, | ||
e.src_type, | ||
e.ready, | ||
bm.id as bookmark_id, | ||
bm.url as bookmark_url, | ||
bm.title, | ||
bm.note | ||
from episodes e | ||
join bookmarks bm | ||
on bm.id = e.bookmark_id | ||
where e.owner_id = ${userId} | ||
and bm.owner_id = ${userId} | ||
and e.podcast_feed_id = ${feedId} | ||
and e.ready = true | ||
and e.error is null | ||
and e.id = ${episodeId} | ||
fetch first 1 rows only; | ||
` | ||
|
||
const results = await fastify.pg.query(episodeQuery) | ||
const episode = results.rows.pop() | ||
|
||
if (!episode) { | ||
return reply.notFound(`episide ${episodeId} not found in feed ${feedId}`) | ||
} | ||
|
||
const cacheKey = getFileKey({ | ||
userId, | ||
episodeId: episode.id, | ||
sourceUrl: episode.src_url, | ||
type: episode.type, | ||
medium: episode.medium | ||
}) | ||
|
||
const cachedUrl = cache.get(cacheKey) | ||
|
||
if (cachedUrl) { | ||
reply.header('fly-cache-status', 'HIT') | ||
return reply.redirect(302, cachedUrl) | ||
} else { | ||
reply.header('fly-cache-status', 'MISS') | ||
} | ||
|
||
const metadata = await fastify.pqueue.add(() => getYTDLPUrl({ url: episode.src_url })) | ||
cache.set(cacheKey, metadata.urls, metadata.urls) | ||
reply.redirect(302, metadata.urls) | ||
} | ||
) | ||
} |
Oops, something went wrong.