1
0
mirror of https://git.sr.ht/~cadence/cloudtube synced 2024-11-26 01:17:30 +00:00
cloudtube/api/channels.js

41 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-08-29 13:21:48 +00:00
const {render} = require("pinski/plugins")
const constants = require("../utils/constants")
const {fetchChannel} = require("../utils/youtube")
const {getUser} = require("../utils/getuser")
const converters = require("../utils/converters")
2020-08-29 13:21:48 +00:00
module.exports = [
{
route: `/channel/(${constants.regex.ucid})`, methods: ["GET"], code: async ({req, fill, url}) => {
2020-08-29 13:21:48 +00:00
const id = fill[0]
2020-08-30 13:54:59 +00:00
const user = getUser(req)
2020-08-31 13:22:16 +00:00
const settings = user.getSettingsOrDefaults()
const data = await fetchChannel(id, settings.instance)
2020-08-30 13:54:59 +00:00
const subscribed = user.isSubscribed(id)
const instanceOrigin = settings.instance
2022-01-10 01:18:45 +00:00
// problem with the channel? fetchChannel has collected the necessary information for us.
// we can render a skeleton page, display the message, and provide the option to unsubscribe.
if (data.error) {
const statusCode = data.missing ? 410 : 500
return render(statusCode, "pug/channel-error.pug", {settings, data, subscribed, instanceOrigin})
}
// everything is fine
// normalise info, apply watched status
if (!data.second__subCountText && data.subCount) {
data.second__subCountText = converters.subscriberCountToText(data.subCount)
}
2020-12-29 10:07:23 +00:00
const watchedVideos = user.getWatchedVideos()
if (data.latestVideos) {
data.latestVideos.forEach(video => {
converters.normaliseVideoInfo(video)
2020-12-29 10:07:23 +00:00
video.watched = watchedVideos.includes(video.videoId)
})
}
2022-01-10 01:18:45 +00:00
return render(200, "pug/channel.pug", {settings, data, subscribed, instanceOrigin})
2020-08-29 13:21:48 +00:00
}
}
]