renders.handler.js 6.9 KB

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