index.js 6.8 KB

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