locals.handler.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.server_cname_address = env.SERVER_CNAME_ADDRESS;
  25. res.locals.disallow_registration = env.DISALLOW_REGISTRATION;
  26. res.locals.mail_enabled = env.MAIL_ENABLED;
  27. res.locals.report_email = env.REPORT_EMAIL;
  28. next();
  29. }
  30. async function user(req, res, next) {
  31. const user = req.user;
  32. res.locals.user = user;
  33. res.locals.domains = user && (await query.domain.get({ user_id: user.id })).map(utils.sanitize.domain);
  34. next();
  35. }
  36. function newPassword(req, res, next) {
  37. res.locals.reset_password_token = req.body.reset_password_token;
  38. next();
  39. }
  40. function createLink(req, res, next) {
  41. res.locals.show_advanced = !!req.body.show_advanced;
  42. next();
  43. }
  44. function editLink(req, res, next) {
  45. res.locals.id = req.params.id;
  46. res.locals.class = "no-animation";
  47. next();
  48. }
  49. function protected(req, res, next) {
  50. res.locals.id = req.params.id;
  51. next();
  52. }
  53. function adminTable(req, res, next) {
  54. res.locals.query = {
  55. anonymous: req.query.anonymous,
  56. domain: req.query.domain,
  57. domains: req.query.domains,
  58. links: req.query.links,
  59. role: req.query.role,
  60. search: req.query.search,
  61. user: req.query.user,
  62. verified: req.query.verified,
  63. };
  64. next();
  65. }
  66. module.exports = {
  67. adminTable,
  68. config,
  69. createLink,
  70. editLink,
  71. isHTML,
  72. newPassword,
  73. noLayout,
  74. protected,
  75. user,
  76. viewTemplate,
  77. }