domains.handler.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. const { Handler } = require("express");
  2. const { CustomError, sanitize } = require("../utils");
  3. const query = require("../queries");
  4. const redis = require("../redis");
  5. const utils = require("../utils");
  6. const env = require("../env");
  7. async function add(req, res) {
  8. const { address, homepage } = req.body;
  9. const domain = await query.domain.add({
  10. address,
  11. homepage,
  12. user_id: req.user.id
  13. });
  14. if (req.isHTML) {
  15. const domains = (await query.domain.get({ user_id: req.user.id })).map(sanitize.domain);
  16. res.setHeader("HX-Reswap", "none");
  17. res.render("partials/settings/domain/table", {
  18. domains
  19. });
  20. return;
  21. }
  22. return res.status(200).send(sanitize.domain(domain));
  23. };
  24. async function addAdmin(req, res) {
  25. const { address, banned, homepage } = req.body;
  26. const domain = await query.domain.add({
  27. address,
  28. homepage,
  29. banned,
  30. ...(banned && { banned_by_id: req.user.id })
  31. });
  32. if (req.isHTML) {
  33. res.setHeader("HX-Trigger", "reloadMainTable");
  34. res.render("partials/admin/dialog/add_domain_success", {
  35. address: domain.address,
  36. });
  37. return;
  38. }
  39. return res.status(200).send({ message: "The domain has been added successfully." });
  40. };
  41. async function remove(req, res) {
  42. const domain = await query.domain.find({
  43. uuid: req.params.id,
  44. user_id: req.user.id
  45. });
  46. if (!domain) {
  47. throw new CustomError("Could not delete the domain.", 400);
  48. }
  49. const [updatedDomain] = await query.domain.update(
  50. { id: domain.id },
  51. { user_id: null }
  52. );
  53. if (!updatedDomain) {
  54. throw new CustomError("Could not delete the domain.", 500);
  55. }
  56. if (env.REDIS_ENABLED) {
  57. redis.remove.domain(updatedDomain);
  58. }
  59. if (req.isHTML) {
  60. const domains = (await query.domain.get({ user_id: req.user.id })).map(sanitize.domain);
  61. res.setHeader("HX-Reswap", "outerHTML");
  62. res.render("partials/settings/domain/delete_success", {
  63. domains,
  64. address: domain.address,
  65. });
  66. return;
  67. }
  68. return res.status(200).send({ message: "Domain deleted successfully" });
  69. };
  70. async function removeAdmin(req, res) {
  71. const id = req.params.id;
  72. const links = req.query.links
  73. const domain = await query.domain.find({ id });
  74. if (!domain) {
  75. throw new CustomError("Could not find the domain.", 400);
  76. }
  77. if (links) {
  78. await query.link.batchRemove({ domain_id: id });
  79. }
  80. await query.domain.remove(domain);
  81. if (req.isHTML) {
  82. res.setHeader("HX-Reswap", "outerHTML");
  83. res.setHeader("HX-Trigger", "reloadMainTable");
  84. res.render("partials/admin/dialog/delete_domain_success", {
  85. address: domain.address,
  86. });
  87. return;
  88. }
  89. return res.status(200).send({ message: "Domain deleted successfully" });
  90. }
  91. async function getAdmin(req, res) {
  92. const { limit, skip } = req.context;
  93. const search = req.query.search;
  94. const user = req.query.user;
  95. const banned = utils.parseBooleanQuery(req.query.banned);
  96. const owner = utils.parseBooleanQuery(req.query.owner);
  97. const links = utils.parseBooleanQuery(req.query.links);
  98. const match = {
  99. ...(banned !== undefined && { banned }),
  100. ...(owner !== undefined && { user_id: [owner ? "is not" : "is", null] }),
  101. };
  102. const [data, total] = await Promise.all([
  103. query.domain.getAdmin(match, { limit, search, user, links, skip }),
  104. query.domain.totalAdmin(match, { search, user, links })
  105. ]);
  106. const domains = data.map(utils.sanitize.domain_admin);
  107. if (req.isHTML) {
  108. res.render("partials/admin/domains/table", {
  109. total,
  110. total_formatted: total.toLocaleString("en-US"),
  111. limit,
  112. skip,
  113. table_domains: domains,
  114. })
  115. return;
  116. }
  117. return res.send({
  118. total,
  119. limit,
  120. skip,
  121. data: domains,
  122. });
  123. }
  124. async function ban(req, res) {
  125. const { id } = req.params;
  126. const update = {
  127. banned_by_id: req.user.id,
  128. banned: true
  129. };
  130. // 1. check if domain exists
  131. const domain = await query.domain.find({ id });
  132. if (!domain) {
  133. throw new CustomError("No domain has been found.", 400);
  134. }
  135. if (domain.banned) {
  136. throw new CustomError("Domain has been banned already.", 400);
  137. }
  138. const tasks = [];
  139. // 2. ban domain
  140. tasks.push(query.domain.update({ id }, update));
  141. // 3. ban user
  142. if (req.body.user && domain.user_id) {
  143. tasks.push(query.user.update({ id: domain.user_id }, update));
  144. }
  145. // 4. ban links
  146. if (req.body.links) {
  147. tasks.push(query.link.update({ domain_id: id }, update));
  148. }
  149. // 5. wait for all tasks to finish
  150. await Promise.all(tasks).catch((err) => {
  151. throw new CustomError("Couldn't ban entries.");
  152. });
  153. // 6. send response
  154. if (req.isHTML) {
  155. res.setHeader("HX-Reswap", "outerHTML");
  156. res.setHeader("HX-Trigger", "reloadMainTable");
  157. res.render("partials/admin/dialog/ban_domain_success", {
  158. address: domain.address,
  159. });
  160. return;
  161. }
  162. return res.status(200).send({ message: "Banned domain successfully." });
  163. }
  164. module.exports = {
  165. add,
  166. addAdmin,
  167. ban,
  168. getAdmin,
  169. remove,
  170. removeAdmin,
  171. }