mirror of
https://git.sr.ht/~cadence/cloudtube
synced 2026-05-26 12:32:25 +00:00
Settings page and instance selection
This commit is contained in:
parent
59a7489545
commit
c573a5ac3e
22 changed files with 587 additions and 71 deletions
43
api/video.js
43
api/video.js
|
|
@ -1,19 +1,48 @@
|
|||
const fetch = require("node-fetch")
|
||||
const {render} = require("pinski/plugins")
|
||||
const db = require("./utils/db")
|
||||
const {getToken} = require("./utils/getuser")
|
||||
const {getToken, getUser} = require("./utils/getuser")
|
||||
const pug = require("pug")
|
||||
|
||||
class InstanceError extends Error {
|
||||
constructor(error, identifier) {
|
||||
super(error)
|
||||
this.identifier = identifier
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = [
|
||||
{
|
||||
route: "/watch", methods: ["GET"], code: async ({req, url}) => {
|
||||
const id = url.searchParams.get("v")
|
||||
const video = await fetch(`http://localhost:3000/api/v1/videos/${id}`).then(res => res.json())
|
||||
let subscribed = false
|
||||
const token = getToken(req)
|
||||
if (token) {
|
||||
subscribed = !!db.prepare("SELECT * FROM Subscriptions WHERE token = ? AND ucid = ?").get([token, video.authorId])
|
||||
const user = getUser(req)
|
||||
const settings = user.getSettingsOrDefaults()
|
||||
const outURL = `${settings.instance}/api/v1/videos/${id}`
|
||||
try {
|
||||
const video = await fetch(outURL).then(res => res.json())
|
||||
if (!video) throw new Error("The instance returned null.")
|
||||
if (video.error) throw new InstanceError(video.error, video.identifier)
|
||||
const subscribed = user.isSubscribed(video.authorId)
|
||||
return render(200, "pug/video.pug", {video, subscribed})
|
||||
} catch (e) {
|
||||
let message = pug.render("pre= error", {error: e.stack || e.toString()})
|
||||
if (e instanceof fetch.FetchError) {
|
||||
const template = `
|
||||
p The selected instance, #[code= instance], did not respond correctly.
|
||||
p Requested URL: #[a(href=url)= url]
|
||||
`
|
||||
message = pug.render(template, {instance: settings.instance, url: outURL})
|
||||
} else if (e instanceof InstanceError) {
|
||||
const template = `
|
||||
p #[strong= error.message]
|
||||
if error.identifier
|
||||
p #[code= error.identifier]
|
||||
p That error was generated by #[code= instance].
|
||||
`
|
||||
message = pug.render(template, {instance: settings.instance, error: e})
|
||||
}
|
||||
return render(500, "pug/video.pug", {video: {videoId: id}, error: true, message})
|
||||
}
|
||||
return render(200, "pug/video.pug", {video, subscribed})
|
||||
}
|
||||
}
|
||||
]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue