locals.handler.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. const query = require("../queries");
  2. const utils = require("../utils");
  3. const env = require("../env");
  4. function isHTML(req, res, next) {
  5. const accepts = req.accepts(["json", "html"]);
  6. req.isHTML = accepts === "html";
  7. next();
  8. }
  9. function noLayout(req, res, next) {
  10. res.locals.layout = null;
  11. next();
  12. }
  13. function viewTemplate(template) {
  14. return function (req, res, next) {
  15. req.viewTemplate = template;
  16. next();
  17. }
  18. }
  19. function config(req, res, next) {
  20. res.locals.default_domain = env.DEFAULT_DOMAIN;
  21. res.locals.site_name = env.SITE_NAME;
  22. res.locals.contact_email = env.CONTACT_EMAIL;
  23. res.locals.server_ip_address = env.SERVER_IP_ADDRESS;
  24. res.locals.disallow_registration = env.DISALLOW_REGISTRATION;
  25. res.locals.mail_enabled = env.MAIL_ENABLED;
  26. next();
  27. }
  28. async function user(req, res, next) {
  29. const user = req.user;
  30. res.locals.user = user;
  31. res.locals.domains = user && (await query.domain.get({ user_id: user.id })).map(utils.sanitize.domain);
  32. next();
  33. }
  34. function createLink(req, res, next) {
  35. res.locals.show_advanced = !!req.body.show_advanced;
  36. next();
  37. }
  38. function editLink(req, res, next) {
  39. res.locals.id = req.params.id;
  40. res.locals.class = "no-animation";
  41. next();
  42. }
  43. function protected(req, res, next) {
  44. res.locals.id = req.params.id;
  45. next();
  46. }
  47. function adminTable(req, res, next) {
  48. res.locals.query = {
  49. anonymous: req.query.anonymous,
  50. domain: req.query.domain,
  51. domains: req.query.domains,
  52. links: req.query.links,
  53. role: req.query.role,
  54. search: req.query.search,
  55. user: req.query.user,
  56. verified: req.query.verified,
  57. };
  58. next();
  59. }
  60. module.exports = {
  61. adminTable,
  62. config,
  63. createLink,
  64. editLink,
  65. isHTML,
  66. noLayout,
  67. protected,
  68. user,
  69. viewTemplate,
  70. }