renders.handler.js 6.5 KB

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