index.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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({
  71. isAsync: true,
  72. validator: (owner, callback) => {
  73. this.models.station.countDocuments({ owner: owner }, (err, c) => {
  74. callback(!(err || c >= 3))
  75. });
  76. },
  77. message: 'User already has 3 stations.'
  78. });
  79. /*
  80. this.schemas.station.path('queue').validate((queue, callback) => { //Callback no longer works, see station max count
  81. let totalDuration = 0;
  82. queue.forEach((song) => {
  83. totalDuration += song.duration;
  84. });
  85. return callback(totalDuration <= 3600 * 3);
  86. }, 'The max length of the queue is 3 hours.');
  87. this.schemas.station.path('queue').validate((queue, callback) => { //Callback no longer works, see station max count
  88. if (queue.length === 0) return callback(true);
  89. let totalDuration = 0;
  90. const userId = queue[queue.length - 1].requestedBy;
  91. queue.forEach((song) => {
  92. if (userId === song.requestedBy) {
  93. totalDuration += song.duration;
  94. }
  95. });
  96. return callback(totalDuration <= 900);
  97. }, 'The max length of songs per user is 15 minutes.');
  98. this.schemas.station.path('queue').validate((queue, callback) => { //Callback no longer works, see station max count
  99. if (queue.length === 0) return callback(true);
  100. let totalSongs = 0;
  101. const userId = queue[queue.length - 1].requestedBy;
  102. queue.forEach((song) => {
  103. if (userId === song.requestedBy) {
  104. totalSongs++;
  105. }
  106. });
  107. if (totalSongs <= 2) return callback(true);
  108. if (totalSongs > 3) return callback(false);
  109. if (queue[queue.length - 2].requestedBy !== userId || queue[queue.length - 3] !== userId) return callback(true);
  110. return callback(false);
  111. }, 'The max amount of songs per user is 3, and only 2 in a row is allowed.');
  112. */
  113. let songTitle = (title) => {
  114. return isLength(title, 1, 100);
  115. };
  116. this.schemas.song.path('title').validate(songTitle, 'Invalid title.');
  117. this.schemas.queueSong.path('title').validate(songTitle, 'Invalid title.');
  118. this.schemas.song.path('artists').validate((artists) => {
  119. return !(artists.length < 1 || artists.length > 10);
  120. }, 'Invalid artists.');
  121. this.schemas.queueSong.path('artists').validate((artists) => {
  122. return !(artists.length < 0 || artists.length > 10);
  123. }, 'Invalid artists.');
  124. let songArtists = (artists) => {
  125. return artists.filter((artist) => {
  126. return (isLength(artist, 1, 32) && regex.ascii.test(artist) && artist !== "NONE");
  127. }).length === artists.length;
  128. };
  129. this.schemas.song.path('artists').validate(songArtists, 'Invalid artists.');
  130. this.schemas.queueSong.path('artists').validate(songArtists, 'Invalid artists.');
  131. /*let songGenres = (genres) => {
  132. return genres.filter((genre) => {
  133. return (isLength(genre, 1, 16) && regex.azAZ09_.test(genre));
  134. }).length === genres.length;
  135. };
  136. this.schemas.song.path('genres').validate(songGenres, 'Invalid genres.');
  137. this.schemas.queueSong.path('genres').validate(songGenres, 'Invalid genres.');*/
  138. this.schemas.song.path('thumbnail').validate((thumbnail) => {
  139. return isLength(thumbnail, 8, 256);
  140. }, 'Invalid thumbnail.');
  141. this.schemas.queueSong.path('thumbnail').validate((thumbnail) => {
  142. return isLength(thumbnail, 0, 256);
  143. }, 'Invalid thumbnail.');
  144. this.schemas.playlist.path('displayName').validate((displayName) => {
  145. return (isLength(displayName, 1, 16) && regex.ascii.test(displayName));
  146. }, 'Invalid display name.');
  147. this.schemas.playlist.path('createdBy').validate((createdBy) => {
  148. this.models.playlist.countDocuments({ createdBy: createdBy }, (err, c) => {
  149. return !(err || c >= 10);
  150. });
  151. }, 'Max 10 playlists per user.');
  152. this.schemas.playlist.path('songs').validate((songs) => {
  153. return songs.length <= 2000;
  154. }, 'Max 2000 songs per playlist.');
  155. this.schemas.playlist.path('songs').validate((songs) => {
  156. if (songs.length === 0) return true;
  157. return songs[0].duration <= 10800;
  158. }, 'Max 3 hours per song.');
  159. this.schemas.report.path('description').validate((description) => {
  160. return (!description || (isLength(description, 0, 400) && regex.ascii.test(description)));
  161. }, 'Invalid description.');
  162. resolve();
  163. })
  164. .catch(err => {
  165. console.error(err);
  166. reject(err);
  167. });
  168. })
  169. }
  170. passwordValid(password) {
  171. if (!isLength(password, 6, 200)) return false;
  172. return regex.password.test(password);
  173. }
  174. }