link.queries.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. const bcrypt = require("bcryptjs");
  2. // FIXME: circular dependency
  3. const CustomError = require("../utils").CustomError;
  4. const redis = require("../redis");
  5. const knex = require("../knex");
  6. const selectable = [
  7. "links.id",
  8. "links.address",
  9. "links.banned",
  10. "links.created_at",
  11. "links.domain_id",
  12. "links.updated_at",
  13. "links.password",
  14. "links.description",
  15. "links.expire_in",
  16. "links.target",
  17. "links.visit_count",
  18. "links.user_id",
  19. "links.uuid",
  20. "domains.address as domain"
  21. ];
  22. function normalizeMatch(match) {
  23. const newMatch = { ...match };
  24. if (newMatch.address) {
  25. newMatch["links.address"] = newMatch.address;
  26. delete newMatch.address;
  27. }
  28. if (newMatch.user_id) {
  29. newMatch["links.user_id"] = newMatch.user_id;
  30. delete newMatch.user_id;
  31. }
  32. if (newMatch.uuid) {
  33. newMatch["links.uuid"] = newMatch.uuid;
  34. delete newMatch.uuid;
  35. }
  36. return newMatch;
  37. };
  38. async function total(match, params) {
  39. const query = knex("links")
  40. .where(normalizeMatch(match));
  41. if (params?.search) {
  42. query.andWhereRaw(
  43. "concat_ws(' ', description, links.address, target, domains.address) ILIKE '%' || ? || '%'",
  44. [params.search]
  45. );
  46. }
  47. query.leftJoin("domains", "links.domain_id", "domains.id");
  48. query.count("links.id");
  49. const [{ count }] = await query;
  50. return typeof count === "number" ? count : parseInt(count);
  51. }
  52. async function get(match, params) {
  53. const query = knex("links")
  54. .select(...selectable)
  55. .where(normalizeMatch(match))
  56. .offset(params.skip)
  57. .limit(params.limit)
  58. .orderBy("created_at", "desc");
  59. if (params?.search) {
  60. query.andWhereRaw(
  61. "concat_ws(' ', description, links.address, target, domains.address) ILIKE '%' || ? || '%'",
  62. [params.search]
  63. );
  64. }
  65. query.leftJoin("domains", "links.domain_id", "domains.id");
  66. const links = await query;
  67. return links;
  68. }
  69. async function find(match) {
  70. if (match.address && match.domain_id) {
  71. const key = redis.key.link(match.address, match.domain_id);
  72. const cachedLink = await redis.client.get(key);
  73. if (cachedLink) return JSON.parse(cachedLink);
  74. }
  75. const link = await knex("links")
  76. .select(...selectable)
  77. .where(normalizeMatch(match))
  78. .leftJoin("domains", "links.domain_id", "domains.id")
  79. .first();
  80. if (link) {
  81. const key = redis.key.link(link.address, link.domain_id);
  82. redis.client.set(key, JSON.stringify(link), "EX", 60 * 60 * 2);
  83. }
  84. return link;
  85. }
  86. async function create(params) {
  87. let encryptedPassword = null;
  88. if (params.password) {
  89. const salt = await bcrypt.genSalt(12);
  90. encryptedPassword = await bcrypt.hash(params.password, salt);
  91. }
  92. const [link] = await knex(
  93. "links"
  94. ).insert(
  95. {
  96. password: encryptedPassword,
  97. domain_id: params.domain_id || null,
  98. user_id: params.user_id || null,
  99. address: params.address,
  100. description: params.description || null,
  101. expire_in: params.expire_in || null,
  102. target: params.target
  103. },
  104. "*"
  105. );
  106. return link;
  107. }
  108. async function remove(match) {
  109. const link = await knex("links").where(match).first();
  110. if (!link) {
  111. return { isRemoved: false, error: "Could not find the link.", link: null }
  112. };
  113. const deletedLink = await knex("links").where("id", link.id).delete();
  114. redis.remove.link(link);
  115. return { isRemoved: !!deletedLink, link };
  116. }
  117. async function batchRemove(match) {
  118. const deleteQuery = knex("links");
  119. const findQuery = knex("links");
  120. Object.entries(match).forEach(([key, value]) => {
  121. findQuery.andWhere(key, ...(Array.isArray(value) ? value : [value]));
  122. deleteQuery.andWhere(key, ...(Array.isArray(value) ? value : [value]));
  123. });
  124. const links = await findQuery;
  125. links.forEach(redis.remove.link);
  126. await deleteQuery.delete();
  127. }
  128. async function update(match, update) {
  129. if (update.password) {
  130. const salt = await bcrypt.genSalt(12);
  131. update.password = await bcrypt.hash(update.password, salt);
  132. }
  133. const links = await knex("links")
  134. .where(match)
  135. .update({ ...update, updated_at: new Date().toISOString() }, "*");
  136. links.forEach(redis.remove.link);
  137. return links;
  138. }
  139. function incrementVisit(match) {
  140. return knex("links").where(match).increment("visit_count", 1);
  141. }
  142. module.exports = {
  143. normalizeMatch,
  144. batchRemove,
  145. create,
  146. find,
  147. get,
  148. incrementVisit,
  149. remove,
  150. total,
  151. update,
  152. }