index.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. ascii: /^[\x00-\x7F]+$/,
  10. custom: regex => new RegExp(`^[${regex}]+$`)
  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. reconnectInterval: 3000,
  28. reconnectTries: 10
  29. })
  30. .then(() => {
  31. this.schemas = {
  32. song: new mongoose.Schema(require(`./schemas/song`)),
  33. queueSong: new mongoose.Schema(require(`./schemas/queueSong`)),
  34. station: new mongoose.Schema(require(`./schemas/station`)),
  35. user: new mongoose.Schema(require(`./schemas/user`)),
  36. activity: new mongoose.Schema(require(`./schemas/activity`)),
  37. playlist: new mongoose.Schema(require(`./schemas/playlist`)),
  38. news: new mongoose.Schema(require(`./schemas/news`)),
  39. report: new mongoose.Schema(require(`./schemas/report`)),
  40. punishment: new mongoose.Schema(require(`./schemas/punishment`))
  41. };
  42. this.models = {
  43. song: mongoose.model('song', this.schemas.song),
  44. queueSong: mongoose.model('queueSong', this.schemas.queueSong),
  45. station: mongoose.model('station', this.schemas.station),
  46. user: mongoose.model('user', this.schemas.user),
  47. activity: mongoose.model('activity', this.schemas.activity),
  48. playlist: mongoose.model('playlist', this.schemas.playlist),
  49. news: mongoose.model('news', this.schemas.news),
  50. report: mongoose.model('report', this.schemas.report),
  51. punishment: mongoose.model('punishment', this.schemas.punishment)
  52. };
  53. mongoose.connection.on('error', err => {
  54. this.logger.error("DB_MODULE", err);
  55. });
  56. mongoose.connection.on('disconnected', () => {
  57. this.logger.error("DB_MODULE", "Disconnected, going to try to reconnect...");
  58. this.setState("RECONNECTING");
  59. });
  60. mongoose.connection.on('reconnected', () => {
  61. this.logger.success("DB_MODULE", "Reconnected.");
  62. this.setState("INITIALIZED");
  63. });
  64. mongoose.connection.on('reconnectFailed', () => {
  65. this.logger.error("DB_MODULE", "Reconnect failed, stopping reconnecting.");
  66. this.failed = true;
  67. this._lockdown();
  68. });
  69. // User
  70. this.schemas.user.path('username').validate((username) => {
  71. return (isLength(username, 2, 32) && regex.custom("a-zA-Z0-9_-").test(username));
  72. }, 'Invalid username.');
  73. this.schemas.user.path('email.address').validate((email) => {
  74. if (!isLength(email, 3, 254)) return false;
  75. if (email.indexOf('@') !== email.lastIndexOf('@')) return false;
  76. return regex.emailSimple.test(email) && regex.ascii.test(email);
  77. }, 'Invalid email.');
  78. // Station
  79. this.schemas.station.path('name').validate((id) => {
  80. return (isLength(id, 2, 16) && regex.az09_.test(id));
  81. }, 'Invalid station name.');
  82. this.schemas.station.path('displayName').validate((displayName) => {
  83. return (isLength(displayName, 2, 32) && regex.ascii.test(displayName));
  84. }, 'Invalid display name.');
  85. this.schemas.station.path('description').validate((description) => {
  86. if (!isLength(description, 2, 200)) return false;
  87. let characters = description.split("");
  88. return characters.filter((character) => {
  89. return character.charCodeAt(0) === 21328;
  90. }).length === 0;
  91. }, 'Invalid display name.');
  92. this.schemas.station.path('owner').validate({
  93. validator: (owner) => {
  94. return new Promise((resolve, reject) => {
  95. this.models.station.countDocuments({ owner: owner }, (err, c) => {
  96. if (err) reject(new Error("A mongo error happened."));
  97. else if (c >= 3) reject(new Error("User already has 3 stations."));
  98. else resolve();
  99. });
  100. });
  101. },
  102. message: 'User already has 3 stations.'
  103. });
  104. /*
  105. this.schemas.station.path('queue').validate((queue, callback) => { //Callback no longer works, see station max count
  106. let totalDuration = 0;
  107. queue.forEach((song) => {
  108. totalDuration += song.duration;
  109. });
  110. return callback(totalDuration <= 3600 * 3);
  111. }, 'The max length of the queue is 3 hours.');
  112. this.schemas.station.path('queue').validate((queue, callback) => { //Callback no longer works, see station max count
  113. if (queue.length === 0) return callback(true);
  114. let totalDuration = 0;
  115. const userId = queue[queue.length - 1].requestedBy;
  116. queue.forEach((song) => {
  117. if (userId === song.requestedBy) {
  118. totalDuration += song.duration;
  119. }
  120. });
  121. return callback(totalDuration <= 900);
  122. }, 'The max length of songs per user is 15 minutes.');
  123. this.schemas.station.path('queue').validate((queue, callback) => { //Callback no longer works, see station max count
  124. if (queue.length === 0) return callback(true);
  125. let totalSongs = 0;
  126. const userId = queue[queue.length - 1].requestedBy;
  127. queue.forEach((song) => {
  128. if (userId === song.requestedBy) {
  129. totalSongs++;
  130. }
  131. });
  132. if (totalSongs <= 2) return callback(true);
  133. if (totalSongs > 3) return callback(false);
  134. if (queue[queue.length - 2].requestedBy !== userId || queue[queue.length - 3] !== userId) return callback(true);
  135. return callback(false);
  136. }, 'The max amount of songs per user is 3, and only 2 in a row is allowed.');
  137. */
  138. // Song
  139. let songTitle = (title) => {
  140. return isLength(title, 1, 100);
  141. };
  142. this.schemas.song.path('title').validate(songTitle, 'Invalid title.');
  143. this.schemas.queueSong.path('title').validate(songTitle, 'Invalid title.');
  144. this.schemas.song.path('artists').validate((artists) => {
  145. return !(artists.length < 1 || artists.length > 10);
  146. }, 'Invalid artists.');
  147. this.schemas.queueSong.path('artists').validate((artists) => {
  148. return !(artists.length < 0 || artists.length > 10);
  149. }, 'Invalid artists.');
  150. let songArtists = (artists) => {
  151. return artists.filter((artist) => {
  152. return (isLength(artist, 1, 64) && artist !== "NONE");
  153. }).length === artists.length;
  154. };
  155. this.schemas.song.path('artists').validate(songArtists, 'Invalid artists.');
  156. this.schemas.queueSong.path('artists').validate(songArtists, 'Invalid artists.');
  157. let songGenres = (genres) => {
  158. if (genres.length < 1 || genres.length > 16) return false;
  159. return genres.filter((genre) => {
  160. return (isLength(genre, 1, 32) && regex.ascii.test(genre));
  161. }).length === genres.length;
  162. };
  163. this.schemas.song.path('genres').validate(songGenres, 'Invalid genres.');
  164. this.schemas.queueSong.path('genres').validate(songGenres, 'Invalid genres.');
  165. let songThumbnail = (thumbnail) => {
  166. if (!isLength(thumbnail, 1, 256)) return false;
  167. if (config.get("cookie.secure") === true) return thumbnail.startsWith("https://");
  168. else return thumbnail.startsWith("http://") || thumbnail.startsWith("https://");
  169. };
  170. this.schemas.song.path('thumbnail').validate(songThumbnail, 'Invalid thumbnail.');
  171. this.schemas.queueSong.path('thumbnail').validate(songThumbnail, 'Invalid thumbnail.');
  172. // Playlist
  173. this.schemas.playlist.path('displayName').validate((displayName) => {
  174. return (isLength(displayName, 1, 32) && regex.ascii.test(displayName));
  175. }, 'Invalid display name.');
  176. this.schemas.playlist.path('createdBy').validate((createdBy) => {
  177. this.models.playlist.countDocuments({ createdBy: createdBy }, (err, c) => {
  178. return !(err || c >= 10);
  179. });
  180. }, 'Max 10 playlists per user.');
  181. this.schemas.playlist.path('songs').validate((songs) => {
  182. return songs.length <= 5000;
  183. }, 'Max 5000 songs per playlist.');
  184. this.schemas.playlist.path('songs').validate((songs) => {
  185. if (songs.length === 0) return true;
  186. return songs[0].duration <= 10800;
  187. }, 'Max 3 hours per song.');
  188. // Report
  189. this.schemas.report.path('description').validate((description) => {
  190. return (!description || (isLength(description, 0, 400) && regex.ascii.test(description)));
  191. }, 'Invalid description.');
  192. resolve();
  193. })
  194. .catch(err => {
  195. this.logger.error("DB_MODULE", err);
  196. reject(err);
  197. });
  198. })
  199. }
  200. passwordValid(password) {
  201. return isLength(password, 6, 200);
  202. }
  203. }