kutt/server/handlers/locals.handler.js

62 lines
1.2 KiB
JavaScript
Raw Normal View History

2024-09-09 18:43:12 +03:30
const query = require("../queries");
const utils = require("../utils");
const env = require("../env");
function isHTML(req, res, next) {
const accepts = req.accepts(["json", "html"]);
req.isHTML = accepts === "html";
next();
}
2024-09-12 14:26:39 +03:30
function noLayout(req, res, next) {
2024-09-09 18:43:12 +03:30
res.locals.layout = null;
next();
}
function viewTemplate(template) {
return function (req, res, next) {
req.viewTemplate = template;
next();
}
}
2024-09-12 14:26:39 +03:30
function config(req, res, next) {
2024-09-09 18:43:12 +03:30
res.locals.default_domain = env.DEFAULT_DOMAIN;
res.locals.site_name = env.SITE_NAME;
2024-09-12 14:26:39 +03:30
res.locals.server_ip_address = env.SERVER_IP_ADDRESS;
2024-09-09 18:43:12 +03:30
next();
}
2024-09-12 14:26:39 +03:30
async function user(req, res, next) {
2024-09-09 18:43:12 +03:30
const user = req.user;
res.locals.user = user;
res.locals.domains = user && (await query.domain.get({ user_id: user.id })).map(utils.sanitize.domain);
next();
}
2024-08-21 21:22:59 +03:30
function createLink(req, res, next) {
res.locals.show_advanced = !!req.body.show_advanced;
next();
}
function editLink(req, res, next) {
res.locals.id = req.params.id;
res.locals.class = "no-animation";
next();
}
2024-09-08 14:10:02 +03:30
function protected(req, res, next) {
res.locals.id = req.params.id;
next();
}
2024-08-21 21:22:59 +03:30
module.exports = {
2024-09-12 14:26:39 +03:30
config,
2024-08-21 21:22:59 +03:30
createLink,
editLink,
2024-09-09 18:43:12 +03:30
isHTML,
2024-09-12 14:26:39 +03:30
noLayout,
2024-09-08 14:10:02 +03:30
protected,
2024-09-12 14:26:39 +03:30
user,
2024-09-09 18:43:12 +03:30
viewTemplate,
2024-08-21 21:22:59 +03:30
}