1
0
mirror of https://git.sr.ht/~cadence/cloudtube synced 2024-11-22 07:37:29 +00:00
cloudtube/api/subscriptions.js

46 lines
2.1 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 {getUser} = require("../utils/getuser")
2021-05-11 12:29:44 +00:00
const {timeToPastText, rewriteVideoDescription, applyVideoFilters} = require("../utils/converters")
const {refresher} = require("../background/feed-update")
2020-08-30 15:14:05 +00:00
module.exports = [
{
2021-05-11 12:29:44 +00:00
route: `/subscriptions`, methods: ["GET"], code: async ({req, url}) => {
2020-08-30 15:14:05 +00:00
const user = getUser(req)
let hasSubscriptions = false
let videos = []
let channels = []
2022-01-10 01:18:45 +00:00
let missingChannelCount = 0
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
channels = db.prepare(`SELECT Channels.* FROM Channels INNER JOIN Subscriptions ON Channels.ucid = Subscriptions.ucid WHERE token = ? ORDER BY name`).all(user.token)
2022-01-10 01:18:45 +00:00
missingChannelCount = channels.reduce((a, c) => a + c.missing, 0)
// get refreshed status
refreshed = db.prepare(`SELECT min(refreshed) as min, max(refreshed) as max, count(refreshed) as count FROM Channels INNER JOIN Subscriptions ON Channels.ucid = Subscriptions.ucid WHERE token = ?`).get(user.token)
// get watched videos
const watchedVideos = user.getWatchedVideos()
// get videos
if (channels.length) {
2020-08-30 15:14:05 +00:00
hasSubscriptions = true
videos = db.prepare(`SELECT Videos.* FROM Videos INNER JOIN Subscriptions ON Videos.authorID = Subscriptions.ucid WHERE token = ? ORDER BY published DESC LIMIT 60`).all(user.token)
.map(video => {
video.publishedText = timeToPastText(video.published * 1000)
video.watched = watchedVideos.includes(video.videoId)
2021-04-14 11:02:05 +00:00
video.descriptionHtml = rewriteVideoDescription(video.descriptionHtml, video.videoId)
return video
})
2020-08-30 15:14:05 +00:00
}
2021-05-11 12:29:44 +00:00
const filters = user.getFilters()
;({videos} = applyVideoFilters(videos, filters))
2020-08-30 15:14:05 +00:00
}
const settings = user.getSettingsOrDefaults()
const instanceOrigin = settings.instance
return render(200, "pug/subscriptions.pug", {req, url, settings, hasSubscriptions, videos, channels, missingChannelCount, refreshed, timeToPastText, instanceOrigin})
2020-08-30 15:14:05 +00:00
}
}
]