index.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. 'use strict';
  2. const coreClass = require("../../core");
  3. const mongoose = require('mongoose');
  4. const config = require('config');
  5. const regex = {
  6. azAZ09_: /^[A-Za-z0-9_]+$/,
  7. az09_: /^[a-z0-9_]+$/,
  8. emailSimple: /^[\x00-\x7F]+@[a-z0-9]+\.[a-z0-9]+(\.[a-z0-9]+)?$/,
  9. password: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]/,
  10. ascii: /^[\x00-\x7F]+$/
  11. };
  12. const isLength = (string, min, max) => {
  13. return !(typeof string !== 'string' || string.length < min || string.length > max);
  14. }
  15. const bluebird = require('bluebird');
  16. mongoose.Promise = bluebird;
  17. module.exports = class extends coreClass {
  18. initialize() {
  19. return new Promise((resolve, reject) => {
  20. this.setStage(1);
  21. this.schemas = {};
  22. this.models = {};
  23. const mongoUrl = config.get("mongo").url;
  24. mongoose.connect(mongoUrl, {
  25. useNewUrlParser: true,
  26. useCreateIndex: true
  27. })
  28. .then(() => {
  29. this.schemas = {
  30. song: new mongoose.Schema(require(`./schemas/song`)),
  31. queueSong: new mongoose.Schema(require(`./schemas/queueSong`)),
  32. station: new mongoose.Schema(require(`./schemas/station`)),
  33. user: new mongoose.Schema(require(`./schemas/user`)),
  34. playlist: new mongoose.Schema(require(`./schemas/playlist`)),
  35. news: new mongoose.Schema(require(`./schemas/news`)),
  36. report: new mongoose.Schema(require(`./schemas/report`)),
  37. punishment: new mongoose.Schema(require(`./schemas/punishment`))
  38. };
  39. this.models = {
  40. song: mongoose.model('song', this.schemas.song),
  41. queueSong: mongoose.model('queueSong', this.schemas.queueSong),
  42. station: mongoose.model('station', this.schemas.station),
  43. user: mongoose.model('user', this.schemas.user),
  44. playlist: mongoose.model('playlist', this.schemas.playlist),
  45. news: mongoose.model('news', this.schemas.news),
  46. report: mongoose.model('report', this.schemas.report),
  47. punishment: mongoose.model('punishment', this.schemas.punishment)
  48. };
  49. // this.schemas.user.path('username').validate((username) => {
  50. // return (isLength(username, 2, 32) && regex.azAZ09_.test(username));
  51. // }, 'Invalid username.');
  52. this.schemas.user.path('email.address').validate((email) => {
  53. if (!isLength(email, 3, 254)) return false;
  54. if (email.indexOf('@') !== email.lastIndexOf('@')) return false;
  55. return regex.emailSimple.test(email);
  56. }, 'Invalid email.');
  57. this.schemas.station.path('name').validate((id) => {
  58. return (isLength(id, 2, 16) && regex.az09_.test(id));
  59. }, 'Invalid station name.');
  60. this.schemas.station.path('displayName').validate((displayName) => {
  61. return (isLength(displayName, 2, 32) && regex.azAZ09_.test(displayName));
  62. }, 'Invalid display name.');
  63. this.schemas.station.path('description').validate((description) => {
  64. if (!isLength(description, 2, 200)) return false;
  65. let characters = description.split("");
  66. return characters.filter((character) => {
  67. return character.charCodeAt(0) === 21328;
  68. }).length === 0;
  69. }, 'Invalid display name.');
  70. this.schemas.station.path('owner').validate((owner, callback) => {
  71. this.models.station.countDocuments({ owner: owner }, (err, c) => {
  72. callback(!(err || c >= 3));
  73. });
  74. }, 'User already has 3 stations.');
  75. /*
  76. this.schemas.station.path('queue').validate((queue, callback) => {
  77. let totalDuration = 0;
  78. queue.forEach((song) => {
  79. totalDuration += song.duration;
  80. });
  81. return callback(totalDuration <= 3600 * 3);
  82. }, 'The max length of the queue is 3 hours.');
  83. this.schemas.station.path('queue').validate((queue, callback) => {
  84. if (queue.length === 0) return callback(true);
  85. let totalDuration = 0;
  86. const userId = queue[queue.length - 1].requestedBy;
  87. queue.forEach((song) => {
  88. if (userId === song.requestedBy) {
  89. totalDuration += song.duration;
  90. }
  91. });
  92. return callback(totalDuration <= 900);
  93. }, 'The max length of songs per user is 15 minutes.');
  94. this.schemas.station.path('queue').validate((queue, callback) => {
  95. if (queue.length === 0) return callback(true);
  96. let totalSongs = 0;
  97. const userId = queue[queue.length - 1].requestedBy;
  98. queue.forEach((song) => {
  99. if (userId === song.requestedBy) {
  100. totalSongs++;
  101. }
  102. });
  103. if (totalSongs <= 2) return callback(true);
  104. if (totalSongs > 3) return callback(false);
  105. if (queue[queue.length - 2].requestedBy !== userId || queue[queue.length - 3] !== userId) return callback(true);
  106. return callback(false);
  107. }, 'The max amount of songs per user is 3, and only 2 in a row is allowed.');
  108. */
  109. let songTitle = (title) => {
  110. return isLength(title, 1, 100);
  111. };
  112. this.schemas.song.path('title').validate(songTitle, 'Invalid title.');
  113. this.schemas.queueSong.path('title').validate(songTitle, 'Invalid title.');
  114. this.schemas.song.path('artists').validate((artists) => {
  115. return !(artists.length < 1 || artists.length > 10);
  116. }, 'Invalid artists.');
  117. this.schemas.queueSong.path('artists').validate((artists) => {
  118. return !(artists.length < 0 || artists.length > 10);
  119. }, 'Invalid artists.');
  120. let songArtists = (artists) => {
  121. return artists.filter((artist) => {
  122. return (isLength(artist, 1, 32) && regex.ascii.test(artist) && artist !== "NONE");
  123. }).length === artists.length;
  124. };
  125. this.schemas.song.path('artists').validate(songArtists, 'Invalid artists.');
  126. this.schemas.queueSong.path('artists').validate(songArtists, 'Invalid artists.');
  127. let songGenres = (genres) => {
  128. return genres.filter((genre) => {
  129. return (isLength(genre, 1, 16) && regex.az09_.test(genre));
  130. }).length === genres.length;
  131. };
  132. this.schemas.song.path('genres').validate(songGenres, 'Invalid genres.');
  133. this.schemas.queueSong.path('genres').validate(songGenres, 'Invalid genres.');
  134. this.schemas.song.path('thumbnail').validate((thumbnail) => {
  135. return isLength(thumbnail, 8, 256);
  136. }, 'Invalid thumbnail.');
  137. this.schemas.queueSong.path('thumbnail').validate((thumbnail) => {
  138. return isLength(thumbnail, 0, 256);
  139. }, 'Invalid thumbnail.');
  140. this.schemas.playlist.path('displayName').validate((displayName) => {
  141. return (isLength(displayName, 1, 16) && regex.ascii.test(displayName));
  142. }, 'Invalid display name.');
  143. this.schemas.playlist.path('createdBy').validate((createdBy) => {
  144. this.models.playlist.countDocuments({ createdBy: createdBy }, (err, c) => {
  145. return !(err || c >= 10);
  146. });
  147. }, 'Max 10 playlists per user.');
  148. this.schemas.playlist.path('songs').validate((songs) => {
  149. return songs.length <= 2000;
  150. }, 'Max 2000 songs per playlist.');
  151. this.schemas.playlist.path('songs').validate((songs) => {
  152. if (songs.length === 0) return true;
  153. return songs[0].duration <= 10800;
  154. }, 'Max 3 hours per song.');
  155. this.schemas.report.path('description').validate((description) => {
  156. return (!description || (isLength(description, 0, 400) && regex.ascii.test(description)));
  157. }, 'Invalid description.');
  158. resolve();
  159. })
  160. .catch(err => {
  161. console.error(err);
  162. reject(err);
  163. });
  164. })
  165. }
  166. passwordValid(password) {
  167. if (!isLength(password, 6, 200)) return false;
  168. return regex.password.test(password);
  169. }
  170. }