index.js 6.9 KB

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