index.js 6.7 KB

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