From d83a5de095472fc1e859169645e858024c27d5ff Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Wed, 6 May 2020 02:14:11 +1200 Subject: [PATCH] Implement SPA setting (closes #35) --- src/lib/constants.js | 2 +- src/site/api/routes.js | 16 ++++++++++++---- src/site/pug/post.pug | 5 +++-- src/site/pug/settings.pug | 2 +- src/site/pug/user.pug | 7 +++++-- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/lib/constants.js b/src/lib/constants.js index 76f27ea..21a3578 100644 --- a/src/lib/constants.js +++ b/src/lib/constants.js @@ -88,7 +88,7 @@ let constants = { name: "spa", default: "on", boolean: true, - replaceEmptyWithDefault: true + replaceEmptyWithDefault: false },{ name: "theme", default: "classic", diff --git a/src/site/api/routes.js b/src/site/api/routes.js index f42d617..3acab20 100644 --- a/src/site/api/routes.js +++ b/src/site/api/routes.js @@ -3,6 +3,7 @@ const switcher = require("../../lib/utils/torswitcher") const {fetchUser, getOrFetchShortcode, userRequestCache, history} = require("../../lib/collectors") const {render, redirect, getStaticURL} = require("pinski/plugins") const {pugCache} = require("../passthrough") +const {getSettings} = require("./utils/getsettings") /** @param {import("../../lib/structures/TimelineEntry")} post */ function getPageTitle(post) { @@ -57,7 +58,7 @@ module.exports = [ } }, { - route: `/u/(${constants.external.username_regex})`, methods: ["GET"], code: ({url, fill}) => { + route: `/u/(${constants.external.username_regex})`, methods: ["GET"], code: ({req, url, fill}) => { if (fill[0] !== fill[0].toLowerCase()) { // some capital letters return Promise.resolve(redirect(`/u/${fill[0].toLowerCase()}`, 301)) } @@ -69,7 +70,10 @@ module.exports = [ await user.timeline.fetchUpToPage(page - 1) } const followerCountsAvailable = !(user.constructor.name === "ReelUser" && user.following === 0 && user.followedBy === 0) - return render(200, "pug/user.pug", {url, user, followerCountsAvailable, constants}) + + const settings = getSettings(req) + + return render(200, "pug/user.pug", {url, user, followerCountsAvailable, constants, settings}) }).catch(error => { if (error === constants.symbols.NOT_FOUND || error === constants.symbols.ENDPOINT_OVERRIDDEN) { return render(404, "pug/friendlyerror.pug", { @@ -173,15 +177,19 @@ module.exports = [ } }, { - route: `/p/(${constants.external.shortcode_regex})`, methods: ["GET"], code: ({fill}) => { + route: `/p/(${constants.external.shortcode_regex})`, methods: ["GET"], code: ({req, fill}) => { return getOrFetchShortcode(fill[0]).then(async post => { await post.fetchChildren() await post.fetchExtendedOwnerP() // serial await is okay since intermediate fetch result is cached if (post.isVideo()) await post.fetchVideoURL() + + const settings = getSettings(req) + return render(200, "pug/post.pug", { title: getPageTitle(post), post, - website_origin: constants.website_origin + website_origin: constants.website_origin, + settings }) }).catch(error => { if (error === constants.symbols.NOT_FOUND) { diff --git a/src/site/pug/post.pug b/src/site/pug/post.pug index 0f81b96..ff3b34d 100644 --- a/src/site/pug/post.pug +++ b/src/site/pug/post.pug @@ -1,4 +1,4 @@ -//- Needs website_origin, title, post +//- Needs website_origin, title, post, settings include includes/post @@ -7,7 +7,8 @@ html head title= title include includes/head - script(type="module" src=getStaticURL("html", "/static/js/post_overlay.js")) + if settings.spa + script(type="module" src=getStaticURL("html", "/static/js/post_overlay.js")) meta(property="og:url" content=`${website_origin}/p/${post.data.shortcode}`) meta(property="og:type" content="article") - let firstEntry = post.children[0] diff --git a/src/site/pug/settings.pug b/src/site/pug/settings.pug index cb8ee3d..e8162ec 100644 --- a/src/site/pug/settings.pug +++ b/src/site/pug/settings.pug @@ -49,7 +49,7 @@ html +checkbox("link_hashtags", "Clickable hashtags", "Clickable", true) - +checkbox("spa", "Fast navigation", "Enabled", true) + +checkbox("spa", "Fast navigation", "Enabled", false) +fieldset("Appearance") +select("theme", "Theme", true, [ diff --git a/src/site/pug/user.pug b/src/site/pug/user.pug index 92911c9..13353f5 100644 --- a/src/site/pug/user.pug +++ b/src/site/pug/user.pug @@ -1,4 +1,4 @@ -//- Needs user, followerCountsAvailable, url, constants +//- Needs user, followerCountsAvailable, url, constants, settings include includes/timeline_page.pug include includes/next_page_button.pug @@ -15,7 +15,10 @@ html else title= `@${user.data.username} | Bibliogram` include includes/head - script(src=getStaticURL("html", "/static/js/post_overlay.js") type="module") + if settings.spa + script(src=getStaticURL("html", "/static/js/post_overlay.js") type="module") + else + script(src=getStaticURL("html", "/static/js/pagination.js") type="module") meta(property="og:url" content=`${constants.website_origin}/u/${user.data.username}`) meta(property="og:type" content="profile") meta(property="og:title" content=(user.data.full_name || user.data.username))