index.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. 'use strict';
  2. const coreClass = require("../../core");
  3. const mongoose = require('mongoose');
  4. const config = require('config');
  5. /*const bluebird = require('bluebird');
  6. mongoose.Promise = bluebird;*/
  7. module.exports = class extends coreClass {
  8. initialize() {
  9. return new Promise((resolve, reject) => {
  10. this.setStage(1);
  11. this._schemas = {};
  12. this._models = {};
  13. const mongoUrl = config.get("mongo").url;
  14. mongoose.connect(mongoUrl, {
  15. useNewUrlParser: true,
  16. useUnifiedTopology: true
  17. })
  18. .then(() => {
  19. this._schemas = {
  20. convertSchema: new mongoose.Schema(require(`./schemas/convertSchema`)),
  21. accountSchema: new mongoose.Schema(require(`./schemas/accountSchema`)),
  22. account: new mongoose.Schema(require(`./schemas/account`))
  23. };
  24. this._models = {
  25. convertSchema: mongoose.model('convertSchema', this._schemas.convertSchema),
  26. accountSchema: mongoose.model('accountSchema', this._schemas.accountSchema),
  27. account: mongoose.model('account', this._schemas.account)
  28. };
  29. mongoose.connection.on('error', err => {
  30. this.logger.error("DB_MODULE", err);
  31. });
  32. mongoose.connection.on('disconnected', () => {
  33. this.logger.error("DB_MODULE", "Disconnected, going to try to reconnect...");
  34. this.setState("RECONNECTING");
  35. });
  36. mongoose.connection.on('reconnected', () => {
  37. this.logger.success("DB_MODULE", "Reconnected.");
  38. this.setState("INITIALIZED");
  39. });
  40. mongoose.connection.on('reconnectFailed', () => {
  41. this.logger.error("DB_MODULE", "Reconnect failed, stopping reconnecting.");
  42. this.failed = true;
  43. this._lockdown();
  44. });
  45. /*// User
  46. this.schemas.user.path('username').validate((username) => {
  47. return (isLength(username, 2, 32) && regex.custom("a-zA-Z0-9_-").test(username));
  48. }, 'Invalid username.');
  49. this.schemas.user.path('email.address').validate((email) => {
  50. if (!isLength(email, 3, 254)) return false;
  51. if (email.indexOf('@') !== email.lastIndexOf('@')) return false;
  52. return regex.emailSimple.test(email) && regex.ascii.test(email);
  53. }, 'Invalid email.');
  54. // Station
  55. this.schemas.station.path('name').validate((id) => {
  56. return (isLength(id, 2, 16) && regex.az09_.test(id));
  57. }, 'Invalid station name.');
  58. this.schemas.station.path('displayName').validate((displayName) => {
  59. return (isLength(displayName, 2, 32) && regex.ascii.test(displayName));
  60. }, 'Invalid display name.');
  61. this.schemas.station.path('description').validate((description) => {
  62. if (!isLength(description, 2, 200)) return false;
  63. let characters = description.split("");
  64. return characters.filter((character) => {
  65. return character.charCodeAt(0) === 21328;
  66. }).length === 0;
  67. }, 'Invalid display name.');
  68. this.schemas.station.path('owner').validate({
  69. validator: (owner) => {
  70. return new Promise((resolve, reject) => {
  71. this.models.station.countDocuments({ owner: owner }, (err, c) => {
  72. if (err) reject(new Error("A mongo error happened."));
  73. else if (c >= 3) reject(new Error("User already has 3 stations."));
  74. else resolve();
  75. });
  76. });
  77. },
  78. message: 'User already has 3 stations.'
  79. });
  80. /*
  81. this.schemas.station.path('queue').validate((queue, callback) => { //Callback no longer works, see station max count
  82. let totalDuration = 0;
  83. queue.forEach((song) => {
  84. totalDuration += song.duration;
  85. });
  86. return callback(totalDuration <= 3600 * 3);
  87. }, 'The max length of the queue is 3 hours.');
  88. this.schemas.station.path('queue').validate((queue, callback) => { //Callback no longer works, see station max count
  89. if (queue.length === 0) return callback(true);
  90. let totalDuration = 0;
  91. const userId = queue[queue.length - 1].requestedBy;
  92. queue.forEach((song) => {
  93. if (userId === song.requestedBy) {
  94. totalDuration += song.duration;
  95. }
  96. });
  97. return callback(totalDuration <= 900);
  98. }, 'The max length of songs per user is 15 minutes.');
  99. this.schemas.station.path('queue').validate((queue, callback) => { //Callback no longer works, see station max count
  100. if (queue.length === 0) return callback(true);
  101. let totalSongs = 0;
  102. const userId = queue[queue.length - 1].requestedBy;
  103. queue.forEach((song) => {
  104. if (userId === song.requestedBy) {
  105. totalSongs++;
  106. }
  107. });
  108. if (totalSongs <= 2) return callback(true);
  109. if (totalSongs > 3) return callback(false);
  110. if (queue[queue.length - 2].requestedBy !== userId || queue[queue.length - 3] !== userId) return callback(true);
  111. return callback(false);
  112. }, 'The max amount of songs per user is 3, and only 2 in a row is allowed.');
  113. */
  114. // Song
  115. /*let songTitle = (title) => {
  116. return isLength(title, 1, 100);
  117. };
  118. this.schemas.song.path('title').validate(songTitle, 'Invalid title.');
  119. this.schemas.queueSong.path('title').validate(songTitle, 'Invalid title.');
  120. this.schemas.song.path('artists').validate((artists) => {
  121. return !(artists.length < 1 || artists.length > 10);
  122. }, 'Invalid artists.');
  123. this.schemas.queueSong.path('artists').validate((artists) => {
  124. return !(artists.length < 0 || artists.length > 10);
  125. }, 'Invalid artists.');
  126. let songArtists = (artists) => {
  127. return artists.filter((artist) => {
  128. return (isLength(artist, 1, 64) && artist !== "NONE");
  129. }).length === artists.length;
  130. };
  131. this.schemas.song.path('artists').validate(songArtists, 'Invalid artists.');
  132. this.schemas.queueSong.path('artists').validate(songArtists, 'Invalid artists.');
  133. let songGenres = (genres) => {
  134. if (genres.length < 1 || genres.length > 16) return false;
  135. return genres.filter((genre) => {
  136. return (isLength(genre, 1, 32) && regex.ascii.test(genre));
  137. }).length === genres.length;
  138. };
  139. this.schemas.song.path('genres').validate(songGenres, 'Invalid genres.');
  140. this.schemas.queueSong.path('genres').validate(songGenres, 'Invalid genres.');
  141. let songThumbnail = (thumbnail) => {
  142. if (!isLength(thumbnail, 1, 256)) return false;
  143. if (config.get("cookie.secure") === true) return thumbnail.startsWith("https://");
  144. else return thumbnail.startsWith("http://") || thumbnail.startsWith("https://");
  145. };
  146. this.schemas.song.path('thumbnail').validate(songThumbnail, 'Invalid thumbnail.');
  147. this.schemas.queueSong.path('thumbnail').validate(songThumbnail, 'Invalid thumbnail.');
  148. // Playlist
  149. this.schemas.playlist.path('displayName').validate((displayName) => {
  150. return (isLength(displayName, 1, 32) && regex.ascii.test(displayName));
  151. }, 'Invalid display name.');
  152. this.schemas.playlist.path('createdBy').validate((createdBy) => {
  153. this.models.playlist.countDocuments({ createdBy: createdBy }, (err, c) => {
  154. return !(err || c >= 10);
  155. });
  156. }, 'Max 10 playlists per user.');
  157. this.schemas.playlist.path('songs').validate((songs) => {
  158. return songs.length <= 5000;
  159. }, 'Max 5000 songs per playlist.');
  160. this.schemas.playlist.path('songs').validate((songs) => {
  161. if (songs.length === 0) return true;
  162. return songs[0].duration <= 10800;
  163. }, 'Max 3 hours per song.');
  164. // Report
  165. this.schemas.report.path('description').validate((description) => {
  166. return (!description || (isLength(description, 0, 400) && regex.ascii.test(description)));
  167. }, 'Invalid description.');*/
  168. resolve();
  169. })
  170. .catch(err => {
  171. this.logger.error("DB_MODULE", err);
  172. reject(err);
  173. });
  174. })
  175. }
  176. async schema(name) {
  177. return new Promise(async resolve => {
  178. try { await this._validateHook(); } catch { return; }
  179. resolve(this._schemas[name]);
  180. });
  181. }
  182. async model(name) {
  183. return new Promise(async resolve => {
  184. try { await this._validateHook(); } catch { return; }
  185. resolve(this._models[name]);
  186. });
  187. }
  188. }