2020-08-29 13:21:48 +00:00
|
|
|
const {render} = require("pinski/plugins")
|
2020-09-23 12:05:02 +00:00
|
|
|
const {fetchChannel} = require("../utils/youtube")
|
|
|
|
const {getUser} = require("../utils/getuser")
|
2021-01-12 01:53:30 +00:00
|
|
|
const converters = require("../utils/converters")
|
2020-08-29 13:21:48 +00:00
|
|
|
|
|
|
|
module.exports = [
|
|
|
|
{
|
2021-11-13 01:17:07 +00:00
|
|
|
route: `/(c|channel|user)/(.+)`, methods: ["GET"], code: async ({req, fill, url}) => {
|
|
|
|
const path = fill[0]
|
|
|
|
const id = fill[1]
|
2020-08-30 13:54:59 +00:00
|
|
|
const user = getUser(req)
|
2020-08-31 13:22:16 +00:00
|
|
|
const settings = user.getSettingsOrDefaults()
|
2022-08-22 22:37:43 +00:00
|
|
|
const data = await fetchChannel(path, id, settings.instance)
|
2020-10-18 08:51:09 +00:00
|
|
|
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
|
2022-09-16 05:18:51 +00:00
|
|
|
const subscribed = user.isSubscribed(id)
|
2022-01-10 01:18:45 +00:00
|
|
|
return render(statusCode, "pug/channel-error.pug", {settings, data, subscribed, instanceOrigin})
|
|
|
|
}
|
|
|
|
|
|
|
|
// everything is fine
|
|
|
|
|
2021-01-12 01:53:30 +00:00
|
|
|
// normalise info, apply watched status
|
2021-02-10 11:14:37 +00:00
|
|
|
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 => {
|
2021-01-12 01:53:30 +00:00
|
|
|
converters.normaliseVideoInfo(video)
|
2020-12-29 10:07:23 +00:00
|
|
|
video.watched = watchedVideos.includes(video.videoId)
|
|
|
|
})
|
|
|
|
}
|
2022-09-16 05:18:51 +00:00
|
|
|
const subscribed = user.isSubscribed(data.authorId)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|