renders.handler.js 6.9 KB

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