locals.handler.js 1.9 KB

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