index.js 6.7 KB

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