Refactor video fetcher to prefetch data

This commit is contained in:
Lomanic 2021-03-29 00:11:05 +02:00 committed by Cadence Ember
parent 1b580ae0b6
commit dbbe950832
No known key found for this signature in database
GPG Key ID: BC1C2C61CF521B17
1 changed files with 7 additions and 11 deletions

View File

@ -118,10 +118,8 @@ function rewriteVideoDescription(descriptionHtml, id) {
return descriptionHtml
}
async function renderVideo(videoPromise, {user, settings, id, instanceOrigin}, locals = {}) {
async function renderVideo(video, {user, settings, id, instanceOrigin}, locals = {}) {
try {
// resolve video
const video = await videoPromise
if (!video) throw new Error("The instance returned null.")
if (video.error) throw new InstanceError(video.error, video.identifier)
@ -216,19 +214,17 @@ module.exports = [
const t = url.searchParams.get("t")
let mediaFragment = converters.tToMediaFragment(t)
if (req.method === "GET") {
if (!settings.local) {
const instanceOrigin = settings.instance
const outURL = `${instanceOrigin}/api/v1/videos/${id}`
const videoPromise = request(outURL).then(res => res.json())
return renderVideo(videoPromise, {user, settings, id, instanceOrigin}, {mediaFragment})
} else {
if (settings.local) {
return render(200, "pug/local-video.pug", {id})
}
const instanceOrigin = settings.instance
const outURL = `${instanceOrigin}/api/v1/videos/${id}`
const video = await request(outURL).then(res => res.json())
return renderVideo(video, {user, settings, id, instanceOrigin}, {mediaFragment})
} else { // req.method === "POST"
const video = JSON.parse(new URLSearchParams(body.toString()).get("video"))
const videoPromise = Promise.resolve(video)
const instanceOrigin = "http://localhost:3000"
return renderVideo(videoPromise, {user, settings, id, instanceOrigin}, {mediaFragment})
return renderVideo(video, {user, settings, id, instanceOrigin}, {mediaFragment})
}
}
}