Skip to content

Commit

Permalink
Reorganize routes folder some more
Browse files Browse the repository at this point in the history
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
bcomnes committed Aug 14, 2022
1 parent 76b886d commit 6df80db
Show file tree
Hide file tree
Showing 26 changed files with 721 additions and 355 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: tests
on: [pull_request, push]

env:
FORCE_COLOR: 2
FORCE_COLOR: 1

jobs:
test:
Expand All @@ -22,7 +22,7 @@ jobs:
node-version: ${{ matrix.node-version }}
- run: npm i
- run: node ./scripts/bootstrap-yt-dlp.js
- run: npm test --color=always -- -c
- run: npm test
- name: Coveralls
uses: coverallsapp/github-action@1.1.3
with:
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"snazzy": "^9.0.0",
"sodium-native": "^3.2.1",
"standard": "^17.0.0",
"tap": "^16.0.0"
"tap": "^16.0.0",
"why-is-node-running": "^2.2.2"
},
"engines": {
"node": "^18.0.0 || ^17.0.0",
Expand Down Expand Up @@ -93,7 +94,7 @@
"test": "run-s test:*",
"test:deptree": "depcruise --validate .dependency-cruiser.json app.js web routes plugins",
"test:standard": "standard --verbose | snazzy",
"test:tap": "METRICS=0 c8 tap --no-coverage",
"test:tap": "METRICS=0 c8 tap --no-coverage --color",
"version": "run-s version:*",
"version:changelog": "auto-changelog -p --template keepachangelog auto-changelog --breaking-pattern 'BREAKING CHANGE:'",
"version:git": "git add CHANGELOG.md",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export async function deleteBookmark (fastify, opts) {
AND owner_id =${userId};
`

// TODO: check results
await fastify.pg.query(query)

reply.status = 202
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable camelcase */
import { getBookmarksQuery } from '../get-bookmark-query.js'
import { getBookmarksQuery } from '../get-bookmarks-query.js'
import { fullBookmarkProps } from '../bookmark-props.js'

export async function getBookmark (fastify, opts) {
Expand Down Expand Up @@ -34,8 +34,6 @@ export async function getBookmark (fastify, opts) {
perPage: 1
})

console.log(query)

const results = await fastify.pg.query(query)
const bookmark = results.rows[0]
if (!bookmark) {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable camelcase */
import SQL from '@nearform/sql'
import { createEpisode } from '../../../../lib/create-episode.js'
import { createEpisode } from '../../episodes/create-episode-query.js'
import { runYTDLP } from '../../../../lib/run-yt-dlp.js'
import { commnonBookmarkProps } from '../bookmark-props.js'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ export const getBookmarksQuery = ({
sensitive,
perPage
}) => {
console.log({
tag,
ownerId,
bookmarkId,
before,
url,
sensitive,
perPage
})
return SQL`
with bookmark_page as (
select bm.*
Expand Down
4 changes: 2 additions & 2 deletions routes/api/bookmarks/get-bookmarks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fullBookmarkProps } from './bookmark-props.js'
import { getBookmarksQuery, afterToBeforeQuery } from './get-bookmark-query.js'
import { getBookmarksQuery, afterToBeforeQuery } from './get-bookmarks-query.js'

export async function getBookmarks (fastify, opts) {
fastify.get(
Expand Down Expand Up @@ -70,7 +70,7 @@ export async function getBookmarks (fastify, opts) {
}
},
// Get Bookmarks
async function getBookmarks (request, reply) {
async function getBookmarksHandler (request, reply) {
const id = request.user.id
let {
before,
Expand Down
2 changes: 1 addition & 1 deletion routes/api/bookmarks/put-bookmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
commnonBookmarkProps,
createEpisodeProp
} from './bookmark-props.js'
import { createEpisode } from '../../../lib/create-episode.js'
import { createEpisode } from '../episodes/create-episode-query.js'
import { runYTDLP } from '../../../lib/run-yt-dlp.js'

export async function putBookmarks (fastify, opts) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable camelcase */
import SQL from '@nearform/sql'
import { getOrCreateDefaultFeed } from './get-or-create-default-feed.js'
import { getOrCreateDefaultFeed } from '../feeds/default-feed/get-or-create-default-feed-query.js'

export async function createEpisode ({
client,
Expand Down
1 change: 1 addition & 0 deletions routes/api/episodes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default async function episodesRoutes (fastify, opts) {}
79 changes: 79 additions & 0 deletions routes/api/feeds/_feed/delete-feed.js
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'
}
})
})
}
95 changes: 95 additions & 0 deletions routes/api/feeds/_feed/episodes/_episode/index.js
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)
}
)
}

0 comments on commit 6df80db

Please sign in to comment.