1
0
mirror of https://git.sr.ht/~cadence/cloudtube synced 2024-09-20 03:07:28 +00:00
cloudtube/api/subscriptions.js

41 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-08-30 15:14:05 +00:00
const {render} = require("pinski/plugins")
const db = require("../utils/db")
const {fetchChannelLatest} = require("../utils/youtube")
const {getUser} = require("../utils/getuser")
const {timeToPastText} = require("../utils/converters")
const {refresher} = require("../background/feed-update")
2020-08-30 15:14:05 +00:00
module.exports = [
{
route: `/subscriptions`, methods: ["GET"], code: async ({req}) => {
const user = getUser(req)
let hasSubscriptions = false
let videos = []
let channels = []
let refreshed = null
2020-08-30 15:14:05 +00:00
if (user.token) {
// trigger a background refresh, needed if they came back from being inactive
refresher.skipWaiting()
// get channels
2020-08-30 15:14:05 +00:00
const subscriptions = user.getSubscriptions()
const template = Array(subscriptions.length).fill("?").join(", ")
channels = db.prepare(`SELECT * FROM Channels WHERE ucid IN (${template}) ORDER BY name`).all(subscriptions)
// get refreshed status
refreshed = db.prepare(`SELECT min(refreshed) as min, max(refreshed) as max, count(refreshed) as count FROM Channels WHERE ucid IN (${template})`).get(subscriptions)
// get videos
2020-08-30 15:14:05 +00:00
if (subscriptions.length) {
hasSubscriptions = true
2020-09-23 11:45:02 +00:00
const template = Array(subscriptions.length).fill("?").join(", ")
videos = db.prepare(`SELECT * FROM Videos WHERE authorId IN (${template}) ORDER BY published DESC LIMIT 60`).all(subscriptions)
.map(video => {
video.publishedText = timeToPastText(video.published * 1000)
return video
})
2020-08-30 15:14:05 +00:00
}
}
const instanceOrigin = user.getSettingsOrDefaults().instance
return render(200, "pug/subscriptions.pug", {hasSubscriptions, videos, channels, refreshed, timeToPastText, instanceOrigin})
2020-08-30 15:14:05 +00:00
}
}
]