renders.handler.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. const query = require("../queries");
  2. const utils = require("../utils");
  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. res.redirect("/");
  12. return;
  13. }
  14. res.render("login", {
  15. title: "Log in or sign up"
  16. });
  17. }
  18. function logout(req, res) {
  19. utils.deleteCurrentToken(res);
  20. res.render("logout", {
  21. title: "Logging out.."
  22. });
  23. }
  24. function notFound(req, res) {
  25. res.render("404", {
  26. title: "404 - Not found"
  27. });
  28. }
  29. function settings(req, res) {
  30. res.render("settings", {
  31. title: "Settings"
  32. });
  33. }
  34. function admin(req, res) {
  35. res.render("admin", {
  36. title: "Admin"
  37. });
  38. }
  39. function stats(req, res) {
  40. res.render("stats", {
  41. title: "Stats"
  42. });
  43. }
  44. async function banned(req, res) {
  45. res.render("banned", {
  46. title: "Banned link",
  47. });
  48. }
  49. async function report(req, res) {
  50. res.render("report", {
  51. title: "Report abuse",
  52. });
  53. }
  54. async function resetPassword(req, res) {
  55. res.render("reset_password", {
  56. title: "Reset password",
  57. });
  58. }
  59. async function resetPasswordResult(req, res) {
  60. res.render("reset_password_result", {
  61. title: "Reset password",
  62. });
  63. }
  64. async function verifyChangeEmail(req, res) {
  65. res.render("verify_change_email", {
  66. title: "Verifying email",
  67. });
  68. }
  69. async function verify(req, res) {
  70. res.render("verify", {
  71. title: "Verify",
  72. });
  73. }
  74. async function terms(req, res) {
  75. res.render("terms", {
  76. title: "Terms of Service",
  77. });
  78. }
  79. async function confirmLinkDelete(req, res) {
  80. const link = await query.link.find({
  81. uuid: req.query.id,
  82. ...(!req.user.admin && { user_id: req.user.id })
  83. });
  84. if (!link) {
  85. return res.render("partials/links/dialog/message", {
  86. layout: false,
  87. message: "Could not find the link."
  88. });
  89. }
  90. res.render("partials/links/dialog/delete", {
  91. layout: false,
  92. link: utils.getShortURL(link.address, link.domain).link,
  93. id: link.uuid
  94. });
  95. }
  96. async function confirmLinkBan(req, res) {
  97. const link = await query.link.find({
  98. uuid: req.query.id,
  99. ...(!req.user.admin && { user_id: req.user.id })
  100. });
  101. if (!link) {
  102. return res.render("partials/links/dialog/message", {
  103. message: "Could not find the link."
  104. });
  105. }
  106. res.render("partials/links/dialog/ban", {
  107. link: utils.getShortURL(link.address, link.domain).link,
  108. id: link.uuid
  109. });
  110. }
  111. async function confirmUserDelete(req, res) {
  112. const user = await query.user.find({ id: req.query.id });
  113. if (!user) {
  114. return res.render("partials/admin/dialog/message", {
  115. layout: false,
  116. message: "Could not find the user."
  117. });
  118. }
  119. res.render("partials/admin/dialog/delete_user", {
  120. layout: false,
  121. email: user.email,
  122. id: user.id
  123. });
  124. }
  125. async function confirmUserBan(req, res) {
  126. const user = await query.user.find({ id: req.query.id });
  127. if (!user) {
  128. return res.render("partials/admin/dialog/message", {
  129. layout: false,
  130. message: "Could not find the user."
  131. });
  132. }
  133. res.render("partials/admin/dialog/ban_user", {
  134. layout: false,
  135. email: user.email,
  136. id: user.id
  137. });
  138. }
  139. async function createUser(req, res) {
  140. res.render("partials/admin/dialog/create_user", {
  141. layout: false,
  142. });
  143. }
  144. async function addDomainAdmin(req, res) {
  145. res.render("partials/admin/dialog/add_domain", {
  146. layout: false,
  147. });
  148. }
  149. async function addDomainForm(req, res) {
  150. res.render("partials/settings/domain/add_form");
  151. }
  152. async function confirmDomainDelete(req, res) {
  153. const domain = await query.domain.find({
  154. uuid: req.query.id,
  155. user_id: req.user.id
  156. });
  157. if (!domain) {
  158. throw new utils.CustomError("Could not find the domain.", 400);
  159. }
  160. res.render("partials/settings/domain/delete", {
  161. ...utils.sanitize.domain(domain)
  162. });
  163. }
  164. async function confirmDomainBan(req, res) {
  165. const domain = await query.domain.find({
  166. id: req.query.id
  167. });
  168. if (!domain) {
  169. throw new utils.CustomError("Could not find the domain.", 400);
  170. }
  171. const hasUser = !!domain.user_id;
  172. const hasLink = await query.link.find({ domain_id: domain.id });
  173. res.render("partials/admin/dialog/ban_domain", {
  174. id: domain.id,
  175. address: domain.address,
  176. hasUser,
  177. hasLink,
  178. });
  179. }
  180. async function confirmDomainDeleteAdmin(req, res) {
  181. const domain = await query.domain.find({
  182. id: req.query.id
  183. });
  184. if (!domain) {
  185. throw new utils.CustomError("Could not find the domain.", 400);
  186. }
  187. const hasLink = await query.link.find({ domain_id: domain.id });
  188. res.render("partials/admin/dialog/delete_domain", {
  189. id: domain.id,
  190. address: domain.address,
  191. hasLink,
  192. });
  193. }
  194. async function getReportEmail(req, res) {
  195. if (!env.REPORT_EMAIL) {
  196. throw new utils.CustomError("No report email is available.", 400);
  197. }
  198. res.render("partials/report/email", {
  199. report_email: env.REPORT_EMAIL.replace("@", "[at]")
  200. });
  201. }
  202. async function getSupportEmail(req, res) {
  203. if (!env.CONTACT_EMAIL) {
  204. throw new utils.CustomError("No support email is available.", 400);
  205. }
  206. await utils.sleep(500);
  207. res.render("partials/support_email", {
  208. email: env.CONTACT_EMAIL,
  209. });
  210. }
  211. async function linkEdit(req, res) {
  212. const link = await query.link.find({
  213. uuid: req.params.id,
  214. ...(!req.user.admin && { user_id: req.user.id })
  215. });
  216. res.render("partials/links/edit", {
  217. ...(link && utils.sanitize.link(link)),
  218. });
  219. }
  220. async function linkEditAdmin(req, res) {
  221. const link = await query.link.find({
  222. uuid: req.params.id,
  223. });
  224. res.render("partials/admin/links/edit", {
  225. ...(link && utils.sanitize.link(link)),
  226. });
  227. }
  228. module.exports = {
  229. addDomainAdmin,
  230. addDomainForm,
  231. admin,
  232. banned,
  233. confirmDomainBan,
  234. confirmDomainDelete,
  235. confirmDomainDeleteAdmin,
  236. confirmLinkBan,
  237. confirmLinkDelete,
  238. confirmUserBan,
  239. confirmUserDelete,
  240. createUser,
  241. getReportEmail,
  242. getSupportEmail,
  243. homepage,
  244. linkEdit,
  245. linkEditAdmin,
  246. login,
  247. logout,
  248. notFound,
  249. report,
  250. resetPassword,
  251. resetPasswordResult,
  252. settings,
  253. stats,
  254. terms,
  255. verifyChangeEmail,
  256. verify,
  257. }