index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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]+[0-9]+[^a-zA-Z0-9]+/,
  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.schemas.user.path('username').validate((username) => {
  36. return (isLength(username, 2, 32) && regex.azAZ09_.test(username));
  37. }, 'Invalid username.');
  38. lib.schemas.user.path('email.address').validate((email) => {
  39. if (!isLength(email, 3, 254)) return false;
  40. if (email.indexOf('@') !== email.lastIndexOf('@')) return false;
  41. return regex.emailSimple.test(email);
  42. }, 'Invalid email.');
  43. lib.schemas.station.path('name').validate((id) => {
  44. return (isLength(id, 2, 16) && regex.az09_.test(id));
  45. }, 'Invalid station name.');
  46. lib.schemas.station.path('displayName').validate((displayName) => {
  47. return (isLength(displayName, 2, 32) && regex.azAZ09_.test(displayName));
  48. }, 'Invalid display name.');
  49. lib.schemas.station.path('description').validate((description) => {
  50. if (!isLength(description, 2, 200)) return false;
  51. let characters = description.split("");
  52. return characters.filter((character) => {
  53. console.log(character.charCodeAt(0), character.charCodeAt(0) === 21328);
  54. return character.charCodeAt(0) === 21328;
  55. }).length === 0;
  56. }, 'Invalid display name.');
  57. let songTitle = (title) => {
  58. return (isLength(title, 1, 64) && regex.ascii.test(title));
  59. };
  60. lib.schemas.song.path('title').validate(songTitle, 'Invalid title.');
  61. lib.schemas.queueSong.path('title').validate(songTitle, 'Invalid title.');
  62. let songArtists = (artists) => {
  63. if (artists.length < 1 || artists.length > 10) return false;
  64. return artists.filter((artist) => {
  65. return (isLength(artist, 1, 32) && regex.ascii.test(artist) && artist !== "NONE");
  66. }).length === artists.length;
  67. };
  68. lib.schemas.song.path('artists').validate(songArtists, 'Invalid artists.');
  69. lib.schemas.queueSong.path('artists').validate(songArtists, 'Invalid artists.');
  70. let songGenres = (genres) => {
  71. return genres.filter((genre) => {
  72. return (isLength(genre, 1, 16) && regex.az09_.test(genre));
  73. }).length === genres.length;
  74. };
  75. lib.schemas.song.path('genres').validate(songGenres, 'Invalid genres.');
  76. lib.schemas.queueSong.path('genres').validate(songGenres, 'Invalid genres.');
  77. let songThumbnail = (thumbnail) => {
  78. return isLength(thumbnail, 8, 256);
  79. };
  80. lib.schemas.song.path('thumbnail').validate(songThumbnail, 'Invalid thumbnail.');
  81. lib.schemas.queueSong.path('thumbnail').validate(songThumbnail, 'Invalid thumbnail.');
  82. lib.schemas.playlist.path('displayName').validate((displayName) => {
  83. return (isLength(displayName, 1, 16) && regex.ascii.test(displayName));
  84. }, 'Invalid display name.');
  85. lib.schemas.report.path('description').validate((description) => {
  86. return (!description || (isLength(description, 0, 400) && regex.ascii.test(description)));
  87. }, 'Invalid description.');
  88. lib.models = {
  89. song: mongoose.model('song', lib.schemas.song),
  90. queueSong: mongoose.model('queueSong', lib.schemas.queueSong),
  91. station: mongoose.model('station', lib.schemas.station),
  92. user: mongoose.model('user', lib.schemas.user),
  93. playlist: mongoose.model('playlist', lib.schemas.playlist),
  94. news: mongoose.model('news', lib.schemas.news),
  95. report: mongoose.model('report', lib.schemas.report)
  96. };
  97. cb();
  98. });
  99. },
  100. passwordValid: (password) => {
  101. if (!isLength(password, 6, 200)) return false;
  102. return regex.password.test(password);
  103. }
  104. };
  105. module.exports = lib;