From 36f33b9f7edadf387517d274f9b504663be8ef9b Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Thu, 14 Jan 2021 00:55:03 +1300 Subject: [PATCH] General code cleanup from analysis --- api/video.js | 23 +++++++++++------------ utils/converters.js | 11 ++++------- utils/getuser.js | 2 +- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/api/video.js b/api/video.js index e2d6a0a..bee1df4 100644 --- a/api/video.js +++ b/api/video.js @@ -18,25 +18,25 @@ function formatOrder(format) { // key, max, order, transform // asc: lower number comes first, desc: higher number comes first const spec = [ - ["second__height", 8000, "desc", x => x ? Math.floor(x/96) : 0], - ["fps", 100, "desc", x => x ? Math.floor(x/10) : 0], - ["type", " ".repeat(60), "asc", x => x.length], + {key: "second__height", max: 8000, order: "desc", transform: x => x ? Math.floor(x/96) : 0}, + {key: "fps", max: 100, order: "desc", transform: x => x ? Math.floor(x/10) : 0}, + {key: "type", max: " ".repeat(60), order: "asc", transform: x => x.length} ] let total = 0 for (let i = 0; i < spec.length; i++) { const s = spec[i] - let diff = s[3](format[s[0]]) - if (s[2] === "asc") diff = s[3](s[1]) - diff + let diff = s.transform(format[s.key]) + if (s.order === "asc") diff = s.transform(s.max) - diff total += diff - if (i+1 < spec.length) { - s2 = spec[i+1] - total *= s2[3](s2[1]) + if (i+1 < spec.length) { // not the last spec item? + const s2 = spec[i+1] + total *= s2.transform(s2.key) } } return -total } -async function renderVideo(videoPromise, {user, id, instanceOrigin}) { +async function renderVideo(videoPromise, {user, id, instanceOrigin}, locals) { try { // resolve video const video = await videoPromise @@ -63,7 +63,7 @@ async function renderVideo(videoPromise, {user, id, instanceOrigin}) { rec.watched = watchedVideos.includes(rec.videoId) } } - return render(200, "pug/video.pug", {video, subscribed, instanceOrigin}) + return render(200, "pug/video.pug", Object.assign(locals, {video, subscribed, instanceOrigin})) } catch (e) { // show an appropriate error message // these should probably be split out to their own files @@ -71,9 +71,8 @@ async function renderVideo(videoPromise, {user, id, instanceOrigin}) { if (e instanceof fetch.FetchError) { const template = ` p The selected instance, #[code= instanceOrigin], did not respond correctly. -p Requested URL: #[a(href=url)= url] ` - message = pug.render(template, {instanceOrigin, url: outURL}) + message = pug.render(template, {instanceOrigin}) } else if (e instanceof InstanceError) { if (e.identifier === "RATE_LIMITED_BY_YOUTUBE") { const template = ` diff --git a/utils/converters.js b/utils/converters.js index eab6807..5f54680 100644 --- a/utils/converters.js +++ b/utils/converters.js @@ -8,7 +8,7 @@ function timeToPastText(timestamp) { ["hour", 60 * 60 * 1000], ["minute", 60 * 1000], ["second", 1 * 1000] - ].reduce((acc, [unitName, unitValue]) => { + ].reduce((acc, /** @type {[string, number]} */ [unitName, unitValue]) => { if (acc) return acc if (difference > unitValue) { const number = Math.floor(difference / unitValue) @@ -19,12 +19,9 @@ function timeToPastText(timestamp) { } function lengthSecondsToLengthText(seconds) { - return [Math.floor(seconds/3600), Math.floor(seconds/60)%60, seconds%60] - .reduce((a, c, i, t) => ( - a ? a : c || i == 1 ? t.slice(i) : false - ), false) - .map((x, i) => i === 0 ? x : (x+"").padStart(2, "0")) - .join(":") + let parts = [Math.floor(seconds/3600), Math.floor(seconds/60)%60, seconds%60] + if (parts[0] === 0) parts = parts.slice(1) + return parts.map((x, i) => i === 0 ? x : (x+"").padStart(2, "0")).join(":") } /** diff --git a/utils/getuser.js b/utils/getuser.js index 85dcb82..4d50e80 100644 --- a/utils/getuser.js +++ b/utils/getuser.js @@ -81,7 +81,7 @@ class User { } /** - * @param {any} responseHeaders supply this to create a token + * @param {any} [responseHeaders] supply this to create a token */ function getUser(req, responseHeaders) { const token = getToken(req, responseHeaders)