mirror of
https://git.sr.ht/~cadence/cloudtube
synced 2026-03-02 10:41:36 +00:00
Add subscriptions page
This commit is contained in:
parent
f24e1bb39c
commit
59a7489545
11 changed files with 159 additions and 6 deletions
30
api/subscriptions.js
Normal file
30
api/subscriptions.js
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
const {render} = require("pinski/plugins")
|
||||
const db = require("./utils/db")
|
||||
const {fetchChannelLatest} = require("./utils/youtube")
|
||||
const {getUser} = require("./utils/getuser")
|
||||
|
||||
module.exports = [
|
||||
{
|
||||
route: `/subscriptions`, methods: ["GET"], code: async ({req}) => {
|
||||
const user = getUser(req)
|
||||
let hasSubscriptions = false
|
||||
let videos = []
|
||||
let channels = []
|
||||
if (user.token) {
|
||||
const subscriptions = user.getSubscriptions()
|
||||
const channelPrepared = db.prepare("SELECT * FROM Channels WHERE ucid = ?")
|
||||
channels = subscriptions.map(id => channelPrepared.get(id)).sort((a, b) => {
|
||||
if (a.name < b.name) return -1
|
||||
else if (b.name > a.name) return 1
|
||||
else return 0
|
||||
})
|
||||
if (subscriptions.length) {
|
||||
hasSubscriptions = true
|
||||
const all = await Promise.all(subscriptions.map(id => fetchChannelLatest(id)))
|
||||
videos = all.flat(1).sort((a, b) => b.published - a.published).slice(0, 60)
|
||||
}
|
||||
}
|
||||
return render(200, "pug/subscriptions.pug", {hasSubscriptions, videos, channels})
|
||||
}
|
||||
}
|
||||
]
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
const crypto = require("crypto")
|
||||
const {parse: parseCookie} = require("cookie")
|
||||
|
||||
const constants = require("./constants")
|
||||
const db = require("./db")
|
||||
|
||||
|
|
@ -26,7 +25,7 @@ class User {
|
|||
|
||||
getSubscriptions() {
|
||||
if (this.token) {
|
||||
return db.prepare("SELECT ucid FROM Subscriptions WHERE token = ?").pluck().all(ucid)
|
||||
return db.prepare("SELECT ucid FROM Subscriptions WHERE token = ?").pluck().all(this.token)
|
||||
} else {
|
||||
return []
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,4 +12,14 @@ async function fetchChannel(ucid) {
|
|||
return channel
|
||||
}
|
||||
|
||||
function fetchChannelLatest(ucid) {
|
||||
return fetch(`http://localhost:3000/api/v1/channels/${ucid}/latest`).then(res => res.json()).then(root => {
|
||||
root.forEach(video => {
|
||||
video.descriptionHtml = video.descriptionHtml.replace(/<a /g, '<a tabindex="-1" ')
|
||||
})
|
||||
return root
|
||||
})
|
||||
}
|
||||
|
||||
module.exports.fetchChannel = fetchChannel
|
||||
module.exports.fetchChannelLatest = fetchChannelLatest
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue