renders.handler.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. const utils = require("../utils");
  2. const query = require("../queries")
  3. const env = require("../env");
  4. async function homepage(req, res) {
  5. res.render("homepage", {
  6. title: "Modern open source URL shortener",
  7. });
  8. }
  9. function login(req, res) {
  10. if (req.user) {
  11. return res.redirect("/");
  12. }
  13. res.render("login", {
  14. title: "Log in or sign up"
  15. });
  16. }
  17. function logout(req, res) {
  18. res.clearCookie("token", { httpOnly: true, secure: env.isProd });
  19. res.render("logout", {
  20. title: "Logging out.."
  21. });
  22. }
  23. function notFound(req, res) {
  24. res.render("404", {
  25. title: "404 - Not found"
  26. });
  27. }
  28. function settings(req, res) {
  29. // TODO: make this a middelware function, apply it to where it's necessary
  30. if (!req.user) {
  31. return res.redirect("/");
  32. }
  33. res.render("settings", {
  34. title: "Settings"
  35. });
  36. }
  37. function stats(req, res) {
  38. // TODO: make this a middelware function, apply it to where it's necessary
  39. if (!req.user) {
  40. return res.redirect("/");
  41. }
  42. const id = req.query.id;
  43. res.render("stats", {
  44. title: "Stats"
  45. });
  46. }
  47. async function banned(req, res) {
  48. res.render("banned", {
  49. title: "Banned link",
  50. });
  51. }
  52. async function report(req, res) {
  53. res.render("report", {
  54. title: "Report abuse",
  55. });
  56. }
  57. async function resetPassword(req, res) {
  58. res.render("reset_password", {
  59. title: "Reset password",
  60. });
  61. }
  62. async function resetPasswordResult(req, res) {
  63. res.render("reset_password_result", {
  64. title: "Reset password",
  65. });
  66. }
  67. async function verifyChangeEmail(req, res) {
  68. res.render("verify_change_email", {
  69. title: "Verifying email",
  70. });
  71. }
  72. async function verify(req, res) {
  73. res.render("verify", {
  74. title: "Verify",
  75. });
  76. }
  77. async function terms(req, res) {
  78. res.render("terms", {
  79. title: "Terms of Service",
  80. });
  81. }
  82. async function confirmLinkDelete(req, res) {
  83. const link = await query.link.find({
  84. uuid: req.query.id,
  85. ...(!req.user.admin && { user_id: req.user.id })
  86. });
  87. if (!link) {
  88. return res.render("partials/links/dialog/message", {
  89. layout: false,
  90. message: "Could not find the link."
  91. });
  92. }
  93. res.render("partials/links/dialog/delete", {
  94. layout: false,
  95. link: utils.getShortURL(link.address, link.domain).link,
  96. id: link.uuid
  97. });
  98. }
  99. async function confirmLinkBan(req, res) {
  100. const link = await query.link.find({
  101. uuid: req.query.id,
  102. ...(!req.user.admin && { user_id: req.user.id })
  103. });
  104. if (!link) {
  105. return res.render("partials/links/dialog/message", {
  106. message: "Could not find the link."
  107. });
  108. }
  109. res.render("partials/links/dialog/ban", {
  110. link: utils.getShortURL(link.address, link.domain).link,
  111. id: link.uuid
  112. });
  113. }
  114. async function addDomainForm(req, res) {
  115. res.render("partials/settings/domain/add_form");
  116. }
  117. async function confirmDomainDelete(req, res) {
  118. const domain = await query.domain.find({
  119. uuid: req.query.id,
  120. user_id: req.user.id
  121. });
  122. if (!domain) {
  123. throw new utils.CustomError("Could not find the link", 400);
  124. }
  125. res.render("partials/settings/domain/delete", {
  126. ...utils.sanitize.domain(domain)
  127. });
  128. }
  129. async function getReportEmail(req, res) {
  130. if (!env.REPORT_EMAIL) {
  131. throw new utils.CustomError("No report email is available.", 400);
  132. }
  133. res.render("partials/report/email", {
  134. report_email: env.REPORT_EMAIL.replace("@", "[at]")
  135. });
  136. }
  137. async function linkEdit(req, res) {
  138. const link = await query.link.find({
  139. uuid: req.params.id,
  140. ...(!req.user.admin && { user_id: req.user.id })
  141. });
  142. // TODO: handle when no link
  143. // if (!link) {
  144. // return res.render("partials/links/dialog/message", {
  145. // layout: false,
  146. // message: "Could not find the link."
  147. // });
  148. // }
  149. res.render("partials/links/edit", {
  150. ...utils.sanitize.link(link),
  151. });
  152. }
  153. module.exports = {
  154. addDomainForm,
  155. banned,
  156. confirmDomainDelete,
  157. confirmLinkBan,
  158. confirmLinkDelete,
  159. getReportEmail,
  160. homepage,
  161. linkEdit,
  162. login,
  163. logout,
  164. notFound,
  165. report,
  166. resetPassword,
  167. resetPasswordResult,
  168. settings,
  169. stats,
  170. terms,
  171. verifyChangeEmail,
  172. verify,
  173. }