index.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. import config from "config";
  2. import mongoose from "mongoose";
  3. import bluebird from "bluebird";
  4. import async from "async";
  5. import CoreClass from "../../core";
  6. const REQUIRED_DOCUMENT_VERSIONS = {
  7. activity: 2,
  8. news: 3,
  9. playlist: 6,
  10. punishment: 1,
  11. queueSong: 1,
  12. report: 5,
  13. song: 7,
  14. station: 8,
  15. user: 3
  16. };
  17. const regex = {
  18. azAZ09_: /^[A-Za-z0-9_]+$/,
  19. az09_: /^[a-z0-9_]+$/,
  20. emailSimple: /^[\x00-\x7F]+@[a-z0-9]+\.[a-z0-9]+(\.[a-z0-9]+)?$/,
  21. ascii: /^[\x00-\x7F]+$/,
  22. name: /^[\p{L}0-9 .'_-]+$/u,
  23. custom: regex => new RegExp(`^[${regex}]+$`)
  24. };
  25. const isLength = (string, min, max) => !(typeof string !== "string" || string.length < min || string.length > max);
  26. mongoose.Promise = bluebird;
  27. let DBModule;
  28. class _DBModule extends CoreClass {
  29. // eslint-disable-next-line require-jsdoc
  30. constructor() {
  31. super("db");
  32. DBModule = this;
  33. }
  34. /**
  35. * Initialises the database module
  36. *
  37. * @returns {Promise} - returns promise (reject, resolve)
  38. */
  39. initialize() {
  40. return new Promise((resolve, reject) => {
  41. this.schemas = {};
  42. this.models = {};
  43. const mongoUrl = config.get("mongo").url;
  44. mongoose
  45. .connect(mongoUrl, {
  46. useNewUrlParser: true,
  47. useUnifiedTopology: true
  48. })
  49. .then(async () => {
  50. this.schemas = {
  51. song: {},
  52. queueSong: {},
  53. station: {},
  54. user: {},
  55. dataRequest: {},
  56. activity: {},
  57. playlist: {},
  58. news: {},
  59. report: {},
  60. punishment: {}
  61. };
  62. const importSchema = schemaName =>
  63. new Promise(resolve => {
  64. import(`./schemas/${schemaName}`).then(schema => {
  65. this.schemas[schemaName] = new mongoose.Schema(schema.default);
  66. return resolve();
  67. });
  68. });
  69. await importSchema("song");
  70. await importSchema("queueSong");
  71. await importSchema("station");
  72. await importSchema("user");
  73. await importSchema("dataRequest");
  74. await importSchema("activity");
  75. await importSchema("playlist");
  76. await importSchema("news");
  77. await importSchema("report");
  78. await importSchema("punishment");
  79. this.models = {
  80. song: mongoose.model("song", this.schemas.song),
  81. queueSong: mongoose.model("queueSong", this.schemas.queueSong),
  82. station: mongoose.model("station", this.schemas.station),
  83. user: mongoose.model("user", this.schemas.user),
  84. dataRequest: mongoose.model("dataRequest", this.schemas.dataRequest),
  85. activity: mongoose.model("activity", this.schemas.activity),
  86. playlist: mongoose.model("playlist", this.schemas.playlist),
  87. news: mongoose.model("news", this.schemas.news),
  88. report: mongoose.model("report", this.schemas.report),
  89. punishment: mongoose.model("punishment", this.schemas.punishment)
  90. };
  91. mongoose.connection.on("error", err => {
  92. this.log("ERROR", err);
  93. });
  94. mongoose.connection.on("disconnected", () => {
  95. this.log("ERROR", "Disconnected, going to try to reconnect...");
  96. this.setStatus("RECONNECTING");
  97. });
  98. mongoose.connection.on("reconnected", () => {
  99. this.log("INFO", "Reconnected.");
  100. this.setStatus("READY");
  101. });
  102. mongoose.connection.on("reconnectFailed", () => {
  103. this.log("INFO", "Reconnect failed, stopping reconnecting.");
  104. this.setStatus("FAILED");
  105. });
  106. // User
  107. this.schemas.user
  108. .path("username")
  109. .validate(
  110. username =>
  111. isLength(username, 2, 32) &&
  112. regex.custom("a-zA-Z0-9_-").test(username) &&
  113. username.replaceAll(/[_]/g, "").length > 0,
  114. "Invalid username."
  115. );
  116. this.schemas.user.path("email.address").validate(email => {
  117. if (!isLength(email, 3, 254)) return false;
  118. if (email.indexOf("@") !== email.lastIndexOf("@")) return false;
  119. return regex.emailSimple.test(email) && regex.ascii.test(email);
  120. }, "Invalid email.");
  121. this.schemas.user
  122. .path("name")
  123. .validate(
  124. name =>
  125. isLength(name, 1, 64) &&
  126. regex.name.test(name) &&
  127. name.replaceAll(/[ .'_-]/g, "").length > 0,
  128. "Invalid name."
  129. );
  130. // Station
  131. this.schemas.station
  132. .path("name")
  133. .validate(id => isLength(id, 2, 16) && regex.az09_.test(id), "Invalid station name.");
  134. this.schemas.station
  135. .path("displayName")
  136. .validate(
  137. displayName => isLength(displayName, 2, 32) && regex.ascii.test(displayName),
  138. "Invalid display name."
  139. );
  140. this.schemas.station.path("description").validate(description => {
  141. if (!isLength(description, 2, 200)) return false;
  142. const characters = description.split("");
  143. return characters.filter(character => character.charCodeAt(0) === 21328).length === 0;
  144. }, "Invalid display name.");
  145. this.schemas.station.path("owner").validate({
  146. validator: owner =>
  147. new Promise((resolve, reject) => {
  148. this.models.station.countDocuments({ owner }, (err, c) => {
  149. if (err) reject(new Error("A mongo error happened."));
  150. else if (c >= 25) reject(new Error("User already has 25 stations."));
  151. else resolve();
  152. });
  153. }),
  154. message: "User already has 25 stations."
  155. });
  156. // Song
  157. const songTitle = title => isLength(title, 1, 100);
  158. this.schemas.song.path("title").validate(songTitle, "Invalid title.");
  159. this.schemas.song.path("artists").validate(artists => artists.length <= 10, "Invalid artists.");
  160. const songArtists = artists =>
  161. artists.filter(artist => isLength(artist, 1, 64) && artist !== "NONE").length ===
  162. artists.length;
  163. this.schemas.song.path("artists").validate(songArtists, "Invalid artists.");
  164. const songGenres = genres => {
  165. if (genres.length > 16) return false;
  166. return (
  167. genres.filter(genre => isLength(genre, 1, 32) && regex.ascii.test(genre)).length ===
  168. genres.length
  169. );
  170. };
  171. this.schemas.song.path("genres").validate(songGenres, "Invalid genres.");
  172. const songTags = tags =>
  173. tags.filter(tag => /^[a-zA-Z0-9_]{1,64}$|^[a-zA-Z0-9_]{1,64}\[[a-zA-Z0-9_]{1,64}\]$/.test(tag))
  174. .length === tags.length;
  175. this.schemas.song.path("tags").validate(songTags, "Invalid tags.");
  176. const songThumbnail = thumbnail => {
  177. if (!isLength(thumbnail, 1, 256)) return false;
  178. if (config.get("cookie.secure") === true) return thumbnail.startsWith("https://");
  179. return thumbnail.startsWith("http://") || thumbnail.startsWith("https://");
  180. };
  181. this.schemas.song.path("thumbnail").validate(songThumbnail, "Invalid thumbnail.");
  182. // Playlist
  183. this.schemas.playlist
  184. .path("displayName")
  185. .validate(
  186. displayName => isLength(displayName, 1, 32) && regex.ascii.test(displayName),
  187. "Invalid display name."
  188. );
  189. this.schemas.playlist.path("createdBy").validate(createdBy => {
  190. this.models.playlist.countDocuments({ createdBy }, (err, c) => !(err || c >= 100));
  191. }, "Max 100 playlists per user.");
  192. this.schemas.playlist
  193. .path("songs")
  194. .validate(songs => songs.length <= 10000, "Max 10000 songs per playlist.");
  195. // this.schemas.playlist.path("songs").validate(songs => {
  196. // if (songs.length === 0) return true;
  197. // return songs[0].duration <= 10800;
  198. // }, "Max 3 hours per song.");
  199. this.models.activity.syncIndexes();
  200. this.models.dataRequest.syncIndexes();
  201. this.models.news.syncIndexes();
  202. this.models.playlist.syncIndexes();
  203. this.models.punishment.syncIndexes();
  204. this.models.queueSong.syncIndexes();
  205. this.models.report.syncIndexes();
  206. this.models.song.syncIndexes();
  207. this.models.station.syncIndexes();
  208. this.models.user.syncIndexes();
  209. if (config.get("skipDbDocumentsVersionCheck")) resolve();
  210. else {
  211. this.runJob("CHECK_DOCUMENT_VERSIONS", {}, null, -1)
  212. .then(() => {
  213. resolve();
  214. })
  215. .catch(err => {
  216. reject(err);
  217. });
  218. }
  219. })
  220. .catch(err => {
  221. this.log("ERROR", err);
  222. reject(err);
  223. });
  224. });
  225. }
  226. /**
  227. * Checks if all documents have the correct document version
  228. *
  229. * @returns {Promise} - returns promise (reject, resolve)
  230. */
  231. CHECK_DOCUMENT_VERSIONS() {
  232. return new Promise((resolve, reject) => {
  233. async.each(
  234. Object.keys(REQUIRED_DOCUMENT_VERSIONS),
  235. (modelName, next) => {
  236. const model = DBModule.models[modelName];
  237. const requiredDocumentVersion = REQUIRED_DOCUMENT_VERSIONS[modelName];
  238. model.countDocuments({ documentVersion: { $ne: requiredDocumentVersion } }, (err, count) => {
  239. if (err) next(err);
  240. else if (count > 0)
  241. next(
  242. `Collection "${modelName}" has ${count} documents with a wrong document version. Run migration.`
  243. );
  244. else next();
  245. });
  246. },
  247. err => {
  248. if (err) reject(new Error(err));
  249. else resolve();
  250. }
  251. );
  252. });
  253. }
  254. /**
  255. * Returns a database model
  256. *
  257. * @param {object} payload - object containing the payload
  258. * @param {object} payload.modelName - name of the model to get
  259. * @returns {Promise} - returns promise (reject, resolve)
  260. */
  261. GET_MODEL(payload) {
  262. return new Promise(resolve => {
  263. resolve(DBModule.models[payload.modelName]);
  264. });
  265. }
  266. /**
  267. * Returns a database schema
  268. *
  269. * @param {object} payload - object containing the payload
  270. * @param {object} payload.schemaName - name of the schema to get
  271. * @returns {Promise} - returns promise (reject, resolve)
  272. */
  273. GET_SCHEMA(payload) {
  274. return new Promise(resolve => {
  275. resolve(DBModule.schemas[payload.schemaName]);
  276. });
  277. }
  278. /**
  279. * Gets data
  280. *
  281. * @param {object} payload - object containing the payload
  282. * @param {string} payload.page - the page
  283. * @param {string} payload.pageSize - the page size
  284. * @param {string} payload.properties - the properties to return for each song
  285. * @param {string} payload.sort - the sort object
  286. * @param {string} payload.queries - the queries array
  287. * @param {string} payload.operator - the operator for queries
  288. * @param {string} payload.modelName - the db collection modal name
  289. * @param {string} payload.blacklistedProperties - the properties that are not allowed to be returned, filtered by or sorted by
  290. * @param {string} payload.specialProperties - the special properties
  291. * @param {string} payload.specialQueries - the special queries
  292. * @returns {Promise} - returns a promise (resolve, reject)
  293. */
  294. GET_DATA(payload) {
  295. return new Promise((resolve, reject) => {
  296. async.waterfall(
  297. [
  298. // Creates pipeline array
  299. next => next(null, []),
  300. // If a query filter property or sort property is blacklisted, throw error
  301. (pipeline, next) => {
  302. const { sort, queries, blacklistedProperties } = payload;
  303. if (
  304. queries.some(query =>
  305. blacklistedProperties.some(blacklistedProperty =>
  306. blacklistedProperty.startsWith(query.filter.property)
  307. )
  308. )
  309. )
  310. return next("Unable to filter by blacklisted property.");
  311. if (
  312. Object.keys(sort).some(property =>
  313. blacklistedProperties.some(blacklistedProperty =>
  314. blacklistedProperty.startsWith(property)
  315. )
  316. )
  317. )
  318. return next("Unable to sort by blacklisted property.");
  319. return next(null, pipeline);
  320. },
  321. // If a filter or property exists for a special property, add some custom pipeline steps
  322. (pipeline, next) => {
  323. const { properties, queries, specialProperties } = payload;
  324. async.eachLimit(
  325. Object.entries(specialProperties),
  326. 1,
  327. ([specialProperty, pipelineSteps], next) => {
  328. // Check if a filter with the special property exists
  329. const filterExists =
  330. queries.map(query => query.filter.property).indexOf(specialProperty) !== -1;
  331. // Check if a property with the special property exists
  332. const propertyExists = properties.indexOf(specialProperty) !== -1;
  333. // If no such filter or property exists, skip this function
  334. if (!filterExists && !propertyExists) return next();
  335. // Add the specified pipeline steps into the pipeline
  336. pipeline.push(...pipelineSteps);
  337. return next();
  338. },
  339. err => {
  340. next(err, pipeline);
  341. }
  342. );
  343. },
  344. // Adds the match stage to aggregation pipeline, which is responsible for filtering
  345. (pipeline, next) => {
  346. const { queries, operator, specialQueries } = payload;
  347. let queryError;
  348. const newQueries = queries.flatMap(query => {
  349. const { data, filter, filterType } = query;
  350. const newQuery = {};
  351. if (filterType === "regex") {
  352. newQuery[filter.property] = new RegExp(`${data.slice(1, data.length - 1)}`, "i");
  353. } else if (filterType === "contains") {
  354. newQuery[filter.property] = new RegExp(
  355. `${data.replaceAll(/[.*+?^${}()|[\]\\]/g, "\\$&")}`,
  356. "i"
  357. );
  358. } else if (filterType === "exact") {
  359. newQuery[filter.property] = data.toString();
  360. } else if (filterType === "datetimeBefore") {
  361. newQuery[filter.property] = { $lte: new Date(data) };
  362. } else if (filterType === "datetimeAfter") {
  363. newQuery[filter.property] = { $gte: new Date(data) };
  364. } else if (filterType === "numberLesserEqual") {
  365. newQuery[filter.property] = { $lte: Number(data) };
  366. } else if (filterType === "numberLesser") {
  367. newQuery[filter.property] = { $lt: Number(data) };
  368. } else if (filterType === "numberGreater") {
  369. newQuery[filter.property] = { $gt: Number(data) };
  370. } else if (filterType === "numberGreaterEqual") {
  371. newQuery[filter.property] = { $gte: Number(data) };
  372. } else if (filterType === "numberEquals") {
  373. newQuery[filter.property] = { $eq: Number(data) };
  374. } else if (filterType === "boolean") {
  375. newQuery[filter.property] = { $eq: !!data };
  376. }
  377. if (specialQueries[filter.property]) {
  378. return specialQueries[filter.property](newQuery);
  379. }
  380. return newQuery;
  381. });
  382. if (queryError) next(queryError);
  383. const queryObject = {};
  384. if (newQueries.length > 0) {
  385. if (operator === "and") queryObject.$and = newQueries;
  386. else if (operator === "or") queryObject.$or = newQueries;
  387. else if (operator === "nor") queryObject.$nor = newQueries;
  388. }
  389. pipeline.push({ $match: queryObject });
  390. next(null, pipeline);
  391. },
  392. // Adds sort stage to aggregation pipeline if there is at least one column being sorted, responsible for sorting data
  393. (pipeline, next) => {
  394. const { sort } = payload;
  395. const newSort = Object.fromEntries(
  396. Object.entries(sort).map(([property, direction]) => [
  397. property,
  398. direction === "ascending" ? 1 : -1
  399. ])
  400. );
  401. if (Object.keys(newSort).length > 0) pipeline.push({ $sort: newSort });
  402. next(null, pipeline);
  403. },
  404. // Adds first project stage to aggregation pipeline, responsible for including only the requested properties
  405. (pipeline, next) => {
  406. const { properties } = payload;
  407. pipeline.push({ $project: Object.fromEntries(properties.map(property => [property, 1])) });
  408. next(null, pipeline);
  409. },
  410. // Adds second project stage to aggregation pipeline, responsible for excluding some specific properties
  411. (pipeline, next) => {
  412. const { blacklistedProperties } = payload;
  413. if (blacklistedProperties.length > 0)
  414. pipeline.push({
  415. $project: Object.fromEntries(blacklistedProperties.map(property => [property, 0]))
  416. });
  417. next(null, pipeline);
  418. },
  419. // Adds the facet stage to aggregation pipeline, responsible for returning a total document count, skipping and limitting the documents that will be returned
  420. (pipeline, next) => {
  421. const { page, pageSize } = payload;
  422. pipeline.push({
  423. $facet: {
  424. count: [{ $count: "count" }],
  425. documents: [{ $skip: pageSize * (page - 1) }, { $limit: pageSize }]
  426. }
  427. });
  428. // console.dir(pipeline, { depth: 6 });
  429. next(null, pipeline);
  430. },
  431. (pipeline, next) => {
  432. const { modelName } = payload;
  433. DBModule.runJob("GET_MODEL", { modelName }, this)
  434. .then(model => {
  435. if (!model) return next("Invalid model.");
  436. return next(null, pipeline, model);
  437. })
  438. .catch(err => {
  439. next(err);
  440. });
  441. },
  442. // Executes the aggregation pipeline
  443. (pipeline, model, next) => {
  444. model.aggregate(pipeline).exec((err, result) => {
  445. // console.dir(err);
  446. // console.dir(result, { depth: 6 });
  447. if (err) return next(err);
  448. if (result[0].count.length === 0) return next(null, 0, []);
  449. const { count } = result[0].count[0];
  450. const { documents } = result[0];
  451. // console.log(111, err, result, count, documents[0]);
  452. return next(null, count, documents);
  453. });
  454. }
  455. ],
  456. (err, count, documents) => {
  457. if (err && err !== true) return reject(new Error(err));
  458. return resolve({ data: documents, count });
  459. }
  460. );
  461. });
  462. }
  463. /**
  464. * Checks if a password to be stored in the database has a valid length
  465. *
  466. * @param {object} password - the password itself
  467. * @returns {Promise} - returns promise (reject, resolve)
  468. */
  469. passwordValid(password) {
  470. return isLength(password, 6, 200);
  471. }
  472. }
  473. export default new _DBModule();