added AJAX loading for videos
This commit is contained in:
68
src/inc/routes/ajax.mjs
Normal file
68
src/inc/routes/ajax.mjs
Normal file
@@ -0,0 +1,68 @@
|
||||
import f0cklib from "../routeinc/f0cklib.mjs";
|
||||
import url from "url";
|
||||
|
||||
export default (router, tpl) => {
|
||||
router.get(/\/ajax\/item\/(?<itemid>\d+)/, async (req, res) => {
|
||||
let query = {};
|
||||
if (typeof req.url === 'string') {
|
||||
const parsedUrl = url.parse(req.url, true);
|
||||
query = parsedUrl.query;
|
||||
} else {
|
||||
// flummpress uses req.url.qs for query string parameters
|
||||
query = req.url.qs || {};
|
||||
}
|
||||
|
||||
let contextUrl = `/${req.params.itemid}`;
|
||||
if (query.tag) contextUrl = `/tag/${query.tag}/${req.params.itemid}`;
|
||||
if (query.user) contextUrl = `/user/${query.user}/${req.params.itemid}`; // User filter takes precedence if both? usually mutually exclusive
|
||||
|
||||
const data = await f0cklib.getf0ck({
|
||||
itemid: req.params.itemid,
|
||||
mode: req.session.mode,
|
||||
session: !!req.session,
|
||||
url: contextUrl,
|
||||
user: query.user,
|
||||
tag: query.tag,
|
||||
mime: query.mime
|
||||
});
|
||||
|
||||
if (!data.success) {
|
||||
return res.reply({
|
||||
code: 404,
|
||||
body: "<h1>404 - Not f0cked</h1>"
|
||||
});
|
||||
}
|
||||
|
||||
// Inject session into data for the template
|
||||
// We clone session to avoid unintended side effects or collisions
|
||||
if (req.session) {
|
||||
data.session = { ...req.session };
|
||||
// data.user comes from f0cklib (uploader). req.session.user is logged-in user string.
|
||||
// If template engine confuses them, removing session.user from this context might help.
|
||||
// item-partial doesn't use session.user.
|
||||
// Note: If anything fails, it prints literal code, so we ensure no collision.
|
||||
if (data.session.user) delete data.session.user;
|
||||
} else {
|
||||
data.session = false;
|
||||
}
|
||||
|
||||
// Inject missing variables normally provided by req or middleware
|
||||
data.url = { pathname: `/${req.params.itemid}` }; // Template expects url.pathname
|
||||
data.fullscreen = req.cookies.fullscreen || 0; // Index.mjs uses req.cookies.fullscreen
|
||||
|
||||
// Render both the item content and the pagination
|
||||
const itemHtml = tpl.render('ajax-item', data);
|
||||
const paginationHtml = tpl.render('snippets/pagination', data);
|
||||
|
||||
// Return JSON response
|
||||
return res.reply({
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
html: itemHtml,
|
||||
pagination: paginationHtml
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
return router;
|
||||
};
|
||||
Reference in New Issue
Block a user