Skip to content

Commit

Permalink
Add compress and work on feed route
Browse files Browse the repository at this point in the history
  • Loading branch information
bcomnes committed Jul 15, 2022
1 parent 3929a6e commit d63c9af
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 6 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"url": "https://github.com/hifiwi-fi/breadcrum.net/issues"
},
"dependencies": {
"@fastify/accepts": "^4.0.0",
"@fastify/auth": "^3.0.0",
"@fastify/autoload": "^5.0.0",
"@fastify/basic-auth": "^4.0.0",
Expand Down Expand Up @@ -36,6 +37,7 @@
"format-duration": "^2.0.0",
"fragmentions": "^1.1.3",
"highlight.js": "^11.5.0",
"jsonfeed-to-rss": "^3.0.6",
"local-storage-proxy": "^4.0.3",
"mine.css": "^7.0.0",
"p-queue": "^7.2.0",
Expand Down
13 changes: 13 additions & 0 deletions plugins/accepts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import fp from 'fastify-plugin'

/**
* This plugins adds fastify-auth
*
* @see https://github.com/fastify/fastify-accepts
*/
export default fp(async function (fastify, opts) {
fastify.register(import('@fastify/accepts'))
}, {
name: 'accepts',
dependencies: []
})
2 changes: 1 addition & 1 deletion plugins/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ export default fp(async function (fastify, opts) {
})
}, {
name: 'static',
dependencies: ['env']
dependencies: ['env', 'compress']
})
157 changes: 152 additions & 5 deletions routes/api/podcast-feeds/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// import SQL from '@nearform/sql'
import SQL from '@nearform/sql'
import jsonfeedToRSS from 'jsonfeed-to-rss'
import cleanDeep from 'clean-deep'

export default async function podcastFeedsRoutes (fastify, opts) {
fastify.get(
Expand All @@ -15,13 +17,158 @@ export default async function podcastFeedsRoutes (fastify, opts) {
}
},
required: ['feed']
},
querystring: {
type: 'object',
properties: {
format: {
enum: ['json', 'rss']
}
}
}
}
},
async function (request, reply) {
const { userId } = request.feedTokenUser
const { feed: feedId } = request.params
if (!userId) throw new Error('missing authenticated feed userId')
async function getFeedHandler (request, reply) {
return fastify.pg.transact(async client => {
const { userId } = request.feedTokenUser
const { feed: feedId } = request.params
const { format } = request.query
const accept = request.accepts()
if (!userId) throw new Error('missing authenticated feed userId')

const episodesQuery = 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,
b.id as bookmark_id,
b.url as bookmark_url,
b.title,
b.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
order by e.created_at desc, bm.title desc, e.filename desc
fetch first 100 rows only;
`

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
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 [episodesResults, feedResults] = await Promise.all([
client.query(episodesQuery),
client.query(feedQuery)
])

const pf = feedResults.rows.pop()
if (!pf) {
reply.code(404)
return {
status: `podcast feed ${feedId} not found`
}
}

const episodes = episodesResults.rows

const jsonfeed = {
version: 'https://jsonfeed.org/version/1',
title: pf.title || `${pf.owner_name}'s breadcrum feed`,
home_page_url: `https://breadcrum.net/podcast_feeds?id=${feedId}`,
description: pf.description || `This is ${pf.owner_name}'s default podcast feed. Customize this description on the feed's home page.`,
icon: pf.image_url,
favicon: pf.image_url,
author: {
name: pf.username,
url: 'https://breadcrum.net/bookmarks',
avatar: pf.image_url
},
_itunes: {
explicit: pf.explicit
},
items: episodes.map(ep => {
const redirectUrl = `https://${userId}:${pf.token}@breadcrum.net/api/podcast_feeds/${pf.id}/episodes/${ep.id}`

return {
id: ep.id,
url: `https://breadcrum.net/bookmarks/view/?id=${ep.bookmark_id}`,
title: ep.title,
content_text: ep.note,
attachments: cleanDeep([{
url: redirectUrl,
mime_type: `${ep.src_type}/${ep.ext}`,
title: ep.filename,
duration_in_seconds: ep.duration_in_seconds
}])
}
})
}

// TODO: caching
// Querystring overrides accept header
if (format) {
switch (format) {
case 'rss': {
reply.type('application/rss+xml')
const rss = jsonfeedToRSS(jsonfeed, {
itunes: true
})
return rss
}
case 'json':
default: {
return jsonfeed
}
}
}

switch (accept.type(['json', 'rss'])) {
case 'json': {
reply.type('application/json')
return jsonfeed
}
case 'rss':
default: {
reply.type('application/rss+xml')
const rss = jsonfeedToRSS(jsonfeed, {
itunes: true
})
return rss
}
}
})
}
)
}

0 comments on commit d63c9af

Please sign in to comment.