link.queries.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. const bcrypt = require("bcryptjs");
  2. const utils = require("../utils");
  3. const redis = require("../redis");
  4. const knex = require("../knex");
  5. const env = require("../env");
  6. const CustomError = utils.CustomError;
  7. const selectable = [
  8. "links.id",
  9. "links.address",
  10. "links.banned",
  11. "links.created_at",
  12. "links.domain_id",
  13. "links.updated_at",
  14. "links.password",
  15. "links.description",
  16. "links.expire_in",
  17. "links.target",
  18. "links.visit_count",
  19. "links.user_id",
  20. "links.uuid",
  21. "domains.address as domain"
  22. ];
  23. const selectable_admin = [
  24. ...selectable,
  25. "users.email as email"
  26. ];
  27. function normalizeMatch(match) {
  28. const newMatch = { ...match };
  29. if (newMatch.address) {
  30. newMatch["links.address"] = newMatch.address;
  31. delete newMatch.address;
  32. }
  33. if (newMatch.user_id) {
  34. newMatch["links.user_id"] = newMatch.user_id;
  35. delete newMatch.user_id;
  36. }
  37. if (newMatch.uuid) {
  38. newMatch["links.uuid"] = newMatch.uuid;
  39. delete newMatch.uuid;
  40. }
  41. if (newMatch.banned !== undefined) {
  42. newMatch["links.banned"] = newMatch.banned;
  43. delete newMatch.banned;
  44. }
  45. return newMatch;
  46. }
  47. async function total(match, params) {
  48. const normalizedMatch = normalizeMatch(match);
  49. const query = knex("links");
  50. Object.entries(normalizedMatch).forEach(([key, value]) => {
  51. query.andWhere(key, ...(Array.isArray(value) ? value : [value]));
  52. });
  53. if (params?.search) {
  54. query.andWhereRaw(
  55. "concat_ws(' ', description, links.address, target, domains.address) ILIKE '%' || ? || '%'",
  56. [params.search]
  57. );
  58. }
  59. query.leftJoin("domains", "links.domain_id", "domains.id");
  60. query.count("links.id as count");
  61. const [{ count }] = await query;
  62. return typeof count === "number" ? count : parseInt(count);
  63. }
  64. async function totalAdmin(match, params) {
  65. const query = knex("links");
  66. Object.entries(normalizeMatch(match)).forEach(([key, value]) => {
  67. query.andWhere(key, ...(Array.isArray(value) ? value : [value]));
  68. });
  69. if (params?.user) {
  70. const id = parseInt(params?.user);
  71. if (Number.isNaN(id)) {
  72. query.andWhereILike("users.email", "%" + params.user + "%");
  73. } else {
  74. query.andWhere("links.user_id", params.user);
  75. }
  76. }
  77. if (params?.search) {
  78. query.andWhereRaw(
  79. "concat_ws(' ', description, links.address, target) ILIKE '%' || ? || '%'",
  80. [params.search]
  81. );
  82. }
  83. if (params?.domain) {
  84. query.andWhereRaw("domains.address ILIKE '%' || ? || '%'", [params.domain]);
  85. }
  86. query.leftJoin("domains", "links.domain_id", "domains.id");
  87. query.leftJoin("users", "links.user_id", "users.id");
  88. query.count("links.id as count");
  89. const [{ count }] = await query;
  90. return typeof count === "number" ? count : parseInt(count);
  91. }
  92. async function get(match, params) {
  93. const query = knex("links")
  94. .select(...selectable)
  95. .where(normalizeMatch(match))
  96. .offset(params.skip)
  97. .limit(params.limit)
  98. .orderBy("links.id", "desc");
  99. if (params?.search) {
  100. query.andWhereRaw(
  101. "concat_ws(' ', description, links.address, target, domains.address) ILIKE '%' || ? || '%'",
  102. [params.search]
  103. );
  104. }
  105. query.leftJoin("domains", "links.domain_id", "domains.id");
  106. return query;
  107. }
  108. async function getAdmin(match, params) {
  109. const query = knex("links").select(...selectable_admin);
  110. Object.entries(normalizeMatch(match)).forEach(([key, value]) => {
  111. query.andWhere(key, ...(Array.isArray(value) ? value : [value]));
  112. });
  113. query
  114. .orderBy("links.id", "desc")
  115. .offset(params.skip)
  116. .limit(params.limit)
  117. if (params?.user) {
  118. const id = parseInt(params?.user);
  119. if (Number.isNaN(id)) {
  120. query.andWhereILike("users.email", "%" + params.user + "%");
  121. } else {
  122. query.andWhere("links.user_id", params.user);
  123. }
  124. }
  125. if (params?.search) {
  126. query.andWhereRaw(
  127. "concat_ws(' ', description, links.address, target) ILIKE '%' || ? || '%'",
  128. [params.search]
  129. );
  130. }
  131. if (params?.domain) {
  132. query.andWhereRaw("domains.address ILIKE '%' || ? || '%'", [params.domain]);
  133. }
  134. query.leftJoin("domains", "links.domain_id", "domains.id");
  135. query.leftJoin("users", "links.user_id", "users.id");
  136. return query;
  137. }
  138. async function find(match) {
  139. if (match.address && match.domain_id !== undefined && env.REDIS_ENABLED) {
  140. const key = redis.key.link(match.address, match.domain_id);
  141. const cachedLink = await redis.client.get(key);
  142. if (cachedLink) return JSON.parse(cachedLink);
  143. }
  144. const link = await knex("links")
  145. .select(...selectable)
  146. .where(normalizeMatch(match))
  147. .leftJoin("domains", "links.domain_id", "domains.id")
  148. .first();
  149. if (link && env.REDIS_ENABLED) {
  150. const key = redis.key.link(link.address, link.domain_id);
  151. redis.client.set(key, JSON.stringify(link), "EX", 60 * 15);
  152. }
  153. return link;
  154. }
  155. async function create(params) {
  156. let encryptedPassword = null;
  157. if (params.password) {
  158. const salt = await bcrypt.genSalt(12);
  159. encryptedPassword = await bcrypt.hash(params.password, salt);
  160. }
  161. let [link] = await knex(
  162. "links"
  163. ).insert(
  164. {
  165. password: encryptedPassword,
  166. domain_id: params.domain_id || null,
  167. user_id: params.user_id || null,
  168. address: params.address,
  169. description: params.description || null,
  170. expire_in: params.expire_in || null,
  171. target: params.target
  172. },
  173. "*"
  174. );
  175. // mysql doesn't return the whole link, but rather the id number only
  176. // so we need to fetch the link ourselves
  177. if (typeof link === "number") {
  178. link = await knex("links").where("id", link).first();
  179. }
  180. return link;
  181. }
  182. async function remove(match) {
  183. const link = await knex("links").where(match).first();
  184. if (!link) {
  185. return { isRemoved: false, error: "Could not find the link.", link: null }
  186. }
  187. const deletedLink = await knex("links").where("id", link.id).delete();
  188. if (env.REDIS_ENABLED) {
  189. redis.remove.link(link);
  190. }
  191. return { isRemoved: !!deletedLink, link };
  192. }
  193. async function batchRemove(match) {
  194. const query = knex("links");
  195. Object.entries(match).forEach(([key, value]) => {
  196. query.andWhere(key, ...(Array.isArray(value) ? value : [value]));
  197. });
  198. const links = await query.clone();
  199. await query.delete();
  200. if (env.REDIS_ENABLED) {
  201. links.forEach(redis.remove.link);
  202. }
  203. }
  204. async function update(match, update) {
  205. if (update.password) {
  206. const salt = await bcrypt.genSalt(12);
  207. update.password = await bcrypt.hash(update.password, salt);
  208. }
  209. // if the links' adddress or domain is changed,
  210. // make sure to delete the original links from cache
  211. let links = []
  212. if (env.REDIS_ENABLED && (update.address || update.domain_id)) {
  213. links = await knex("links").select('*').where(match);
  214. }
  215. await knex("links")
  216. .where(match)
  217. .update({ ...update, updated_at: utils.dateToUTC(new Date()) });
  218. const updated_links = await knex("links").select('*').where(match);
  219. if (env.REDIS_ENABLED) {
  220. links.forEach(redis.remove.link);
  221. updated_links.forEach(redis.remove.link);
  222. }
  223. return updated_links;
  224. }
  225. function incrementVisit(match) {
  226. return knex("links").where(match).increment("visit_count", 1);
  227. }
  228. module.exports = {
  229. normalizeMatch,
  230. batchRemove,
  231. create,
  232. find,
  233. get,
  234. getAdmin,
  235. incrementVisit,
  236. remove,
  237. total,
  238. totalAdmin,
  239. update,
  240. }