songs.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  1. import async from "async";
  2. import mongoose from "mongoose";
  3. import CoreClass from "../core";
  4. let SongsModule;
  5. let CacheModule;
  6. let DBModule;
  7. let UtilsModule;
  8. let YouTubeModule;
  9. let StationsModule;
  10. let PlaylistsModule;
  11. class ErrorWithData extends Error {
  12. /**
  13. * @param {string} message - the error message
  14. * @param {object} data - the error data
  15. */
  16. constructor(message, data) {
  17. super(message);
  18. this.data = data;
  19. }
  20. }
  21. class _SongsModule extends CoreClass {
  22. // eslint-disable-next-line require-jsdoc
  23. constructor() {
  24. super("songs");
  25. SongsModule = this;
  26. }
  27. /**
  28. * Initialises the songs module
  29. *
  30. * @returns {Promise} - returns promise (reject, resolve)
  31. */
  32. async initialize() {
  33. this.setStage(1);
  34. CacheModule = this.moduleManager.modules.cache;
  35. DBModule = this.moduleManager.modules.db;
  36. UtilsModule = this.moduleManager.modules.utils;
  37. YouTubeModule = this.moduleManager.modules.youtube;
  38. StationsModule = this.moduleManager.modules.stations;
  39. PlaylistsModule = this.moduleManager.modules.playlists;
  40. this.SongModel = await DBModule.runJob("GET_MODEL", { modelName: "song" });
  41. this.SongSchemaCache = await CacheModule.runJob("GET_SCHEMA", { schemaName: "song" });
  42. this.setStage(2);
  43. return new Promise((resolve, reject) => {
  44. async.waterfall(
  45. [
  46. next => {
  47. this.setStage(2);
  48. CacheModule.runJob("HGETALL", { table: "songs" })
  49. .then(songs => {
  50. next(null, songs);
  51. })
  52. .catch(next);
  53. },
  54. (songs, next) => {
  55. this.setStage(3);
  56. if (!songs) return next();
  57. const youtubeIds = Object.keys(songs);
  58. return async.each(
  59. youtubeIds,
  60. (youtubeId, next) => {
  61. SongsModule.SongModel.findOne({ youtubeId }, (err, song) => {
  62. if (err) next(err);
  63. else if (!song)
  64. CacheModule.runJob("HDEL", {
  65. table: "songs",
  66. key: youtubeId
  67. })
  68. .then(() => next())
  69. .catch(next);
  70. else next();
  71. });
  72. },
  73. next
  74. );
  75. },
  76. next => {
  77. this.setStage(4);
  78. SongsModule.SongModel.find({}, next);
  79. },
  80. (songs, next) => {
  81. this.setStage(5);
  82. async.each(
  83. songs,
  84. (song, next) => {
  85. CacheModule.runJob("HSET", {
  86. table: "songs",
  87. key: song.youtubeId,
  88. value: SongsModule.SongSchemaCache(song)
  89. })
  90. .then(() => next())
  91. .catch(next);
  92. },
  93. next
  94. );
  95. }
  96. ],
  97. async err => {
  98. if (err) {
  99. err = await UtilsModule.runJob("GET_ERROR", { error: err });
  100. reject(new Error(err));
  101. } else resolve();
  102. }
  103. );
  104. });
  105. }
  106. /**
  107. * Gets a song by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  108. *
  109. * @param {object} payload - object containing the payload
  110. * @param {string} payload.songId - the id of the song we are trying to get
  111. * @returns {Promise} - returns a promise (resolve, reject)
  112. */
  113. GET_SONG(payload) {
  114. return new Promise((resolve, reject) => {
  115. async.waterfall(
  116. [
  117. next => {
  118. if (!mongoose.Types.ObjectId.isValid(payload.songId))
  119. return next("songId is not a valid ObjectId.");
  120. return CacheModule.runJob("HGET", { table: "songs", key: payload.songId }, this)
  121. .then(song => next(null, song))
  122. .catch(next);
  123. },
  124. (song, next) => {
  125. if (song) return next(true, song);
  126. return SongsModule.SongModel.findOne({ _id: payload.songId }, next);
  127. },
  128. (song, next) => {
  129. if (song) {
  130. CacheModule.runJob(
  131. "HSET",
  132. {
  133. table: "songs",
  134. key: payload.songId,
  135. value: song
  136. },
  137. this
  138. ).then(song => next(null, song));
  139. } else next("Song not found.");
  140. }
  141. ],
  142. (err, song) => {
  143. if (err && err !== true) return reject(new Error(err));
  144. return resolve({ song });
  145. }
  146. );
  147. });
  148. }
  149. /**
  150. * Gets songs by id from Mongo
  151. *
  152. * @param {object} payload - object containing the payload
  153. * @param {string} payload.songIds - the ids of the songs we are trying to get
  154. * @param {string} payload.properties - the properties to return
  155. * @returns {Promise} - returns a promise (resolve, reject)
  156. */
  157. GET_SONGS(payload) {
  158. return new Promise((resolve, reject) => {
  159. async.waterfall(
  160. [
  161. next => {
  162. if (!payload.songIds.every(songId => mongoose.Types.ObjectId.isValid(songId)))
  163. next("One or more songIds are not a valid ObjectId.");
  164. else next();
  165. },
  166. next => {
  167. const includeProperties = {};
  168. payload.properties.forEach(property => {
  169. includeProperties[property] = true;
  170. });
  171. return SongsModule.SongModel.find(
  172. {
  173. _id: { $in: payload.songIds }
  174. },
  175. includeProperties,
  176. next
  177. );
  178. }
  179. ],
  180. (err, songs) => {
  181. if (err && err !== true) return reject(new Error(err));
  182. return resolve({ songs });
  183. }
  184. );
  185. });
  186. }
  187. /**
  188. * Makes sure that if a song is not currently in the songs db, to add it
  189. *
  190. * @param {object} payload - an object containing the payload
  191. * @param {string} payload.youtubeId - the youtube song id of the song we are trying to ensure is in the songs db
  192. * @param {string} payload.userId - the youtube song id of the song we are trying to ensure is in the songs db
  193. * @param {string} payload.automaticallyRequested - whether the song was automatically requested or not
  194. * @returns {Promise} - returns a promise (resolve, reject)
  195. */
  196. ENSURE_SONG_EXISTS_BY_YOUTUBE_ID(payload) {
  197. return new Promise((resolve, reject) => {
  198. async.waterfall(
  199. [
  200. next => {
  201. SongsModule.SongModel.findOne({ youtubeId: payload.youtubeId }, next);
  202. },
  203. (song, next) => {
  204. if (song && song.duration > 0) next(true, song);
  205. else {
  206. YouTubeModule.runJob("GET_SONG", { youtubeId: payload.youtubeId }, this)
  207. .then(response => {
  208. next(null, song, response.song);
  209. })
  210. .catch(next);
  211. }
  212. },
  213. (song, youtubeSong, next) => {
  214. if (song && song.duration <= 0) {
  215. song.duration = youtubeSong.duration;
  216. song.save({ validateBeforeSave: true }, err => {
  217. if (err) return next(err, song);
  218. return next(null, song);
  219. });
  220. } else {
  221. const song = new SongsModule.SongModel({
  222. ...youtubeSong,
  223. requestedBy: payload.userId,
  224. requestedAt: Date.now()
  225. });
  226. song.save({ validateBeforeSave: true }, err => {
  227. if (err) return next(err, song);
  228. return next(null, song);
  229. });
  230. }
  231. }
  232. ],
  233. (err, song) => {
  234. if (err && err !== true) return reject(new Error(err));
  235. return resolve({ song });
  236. }
  237. );
  238. });
  239. }
  240. /**
  241. * Gets a song by youtube id
  242. *
  243. * @param {object} payload - an object containing the payload
  244. * @param {string} payload.youtubeId - the youtube id of the song we are trying to get
  245. * @returns {Promise} - returns a promise (resolve, reject)
  246. */
  247. GET_SONG_FROM_YOUTUBE_ID(payload) {
  248. return new Promise((resolve, reject) => {
  249. async.waterfall(
  250. [
  251. next => {
  252. SongsModule.SongModel.findOne({ youtubeId: payload.youtubeId }, next);
  253. }
  254. ],
  255. (err, song) => {
  256. if (err && err !== true) return reject(new Error(err));
  257. return resolve({ song });
  258. }
  259. );
  260. });
  261. }
  262. /**
  263. * Create song
  264. *
  265. * @param {object} payload - an object containing the payload
  266. * @param {string} payload.song - the song object
  267. * @param {string} payload.userId - the user id of the person requesting the song
  268. * @returns {Promise} - returns a promise (resolve, reject)
  269. */
  270. CREATE_SONG(payload) {
  271. return new Promise((resolve, reject) => {
  272. async.waterfall(
  273. [
  274. next => {
  275. DBModule.runJob("GET_MODEL", { modelName: "user" }, this)
  276. .then(UserModel => {
  277. UserModel.findOne(
  278. { _id: payload.userId },
  279. { "preferences.anonymousSongRequests": 1 },
  280. next
  281. );
  282. })
  283. .catch(next);
  284. },
  285. (user, next) => {
  286. const song = new SongsModule.SongModel({
  287. ...payload.song,
  288. requestedBy: user.preferences.anonymousSongRequests ? null : payload.userId,
  289. requestedAt: Date.now()
  290. });
  291. if (song.verified) {
  292. song.verifiedBy = payload.userId;
  293. song.verifiedAt = Date.now();
  294. }
  295. song.save({ validateBeforeSave: true }, err => {
  296. if (err) return next(err, song);
  297. return next(null, song);
  298. });
  299. },
  300. (song, next) => {
  301. SongsModule.runJob("UPDATE_SONG", { songId: song._id });
  302. return next(null, song);
  303. }
  304. ],
  305. (err, song) => {
  306. if (err && err !== true) return reject(new Error(err));
  307. return resolve({ song });
  308. }
  309. );
  310. });
  311. }
  312. /**
  313. * Gets a song from id from Mongo and updates the cache with it
  314. *
  315. * @param {object} payload - an object containing the payload
  316. * @param {string} payload.songId - the id of the song we are trying to update
  317. * @param {string} payload.oldStatus - old status of song being updated (optional)
  318. * @returns {Promise} - returns a promise (resolve, reject)
  319. */
  320. UPDATE_SONG(payload) {
  321. return new Promise((resolve, reject) => {
  322. async.waterfall(
  323. [
  324. next => {
  325. SongsModule.SongModel.findOne({ _id: payload.songId }, next);
  326. },
  327. (song, next) => {
  328. if (!song) {
  329. CacheModule.runJob("HDEL", {
  330. table: "songs",
  331. key: payload.songId
  332. });
  333. return next("Song not found.");
  334. }
  335. return CacheModule.runJob(
  336. "HSET",
  337. {
  338. table: "songs",
  339. key: payload.songId,
  340. value: song
  341. },
  342. this
  343. )
  344. .then(song => {
  345. next(null, song);
  346. })
  347. .catch(next);
  348. },
  349. (song, next) => {
  350. const { _id, youtubeId, title, artists, thumbnail, duration, verified } = song;
  351. const trimmedSong = {
  352. _id,
  353. youtubeId,
  354. title,
  355. artists,
  356. thumbnail,
  357. duration,
  358. verified
  359. };
  360. this.log("INFO", `Going to update playlists now for song ${_id}`);
  361. DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this)
  362. .then(playlistModel => {
  363. playlistModel.updateMany(
  364. { "songs._id": song._id },
  365. { $set: { "songs.$": trimmedSong } },
  366. err => {
  367. if (err) next(err);
  368. else
  369. playlistModel.find({ "songs._id": song._id }, (err, playlists) => {
  370. if (err) next(err);
  371. else {
  372. async.eachLimit(
  373. playlists,
  374. 1,
  375. (playlist, next) => {
  376. PlaylistsModule.runJob(
  377. "UPDATE_PLAYLIST",
  378. {
  379. playlistId: playlist._id
  380. },
  381. this
  382. )
  383. .then(() => {
  384. next();
  385. })
  386. .catch(err => {
  387. next(err);
  388. });
  389. },
  390. err => {
  391. if (err) next(err);
  392. else next(null, song);
  393. }
  394. );
  395. }
  396. });
  397. }
  398. );
  399. })
  400. .catch(err => {
  401. next(err);
  402. });
  403. },
  404. (song, next) => {
  405. const { _id, youtubeId, title, artists, thumbnail, duration, verified } = song;
  406. this.log("INFO", `Going to update stations now for song ${_id}`);
  407. DBModule.runJob("GET_MODEL", { modelName: "station" }, this)
  408. .then(stationModel => {
  409. stationModel.updateMany(
  410. { "queue._id": song._id },
  411. {
  412. $set: {
  413. "queue.$.youtubeId": youtubeId,
  414. "queue.$.title": title,
  415. "queue.$.artists": artists,
  416. "queue.$.thumbnail": thumbnail,
  417. "queue.$.duration": duration,
  418. "queue.$.verified": verified
  419. }
  420. },
  421. err => {
  422. if (err) this.log("ERROR", err);
  423. else
  424. stationModel.find({ "queue._id": song._id }, (err, stations) => {
  425. if (err) next(err);
  426. else {
  427. async.eachLimit(
  428. stations,
  429. 1,
  430. (station, next) => {
  431. StationsModule.runJob(
  432. "UPDATE_STATION",
  433. { stationId: station._id },
  434. this
  435. )
  436. .then(() => {
  437. next();
  438. })
  439. .catch(err => {
  440. next(err);
  441. });
  442. },
  443. err => {
  444. if (err) next(err);
  445. else next(null, song);
  446. }
  447. );
  448. }
  449. });
  450. }
  451. );
  452. })
  453. .catch(err => {
  454. next(err);
  455. });
  456. },
  457. (song, next) => {
  458. async.eachLimit(
  459. song.genres,
  460. 1,
  461. (genre, next) => {
  462. PlaylistsModule.runJob(
  463. "AUTOFILL_GENRE_PLAYLIST",
  464. { genre, createPlaylist: song.verified },
  465. this
  466. )
  467. .then(() => {
  468. next();
  469. })
  470. .catch(err => next(err));
  471. },
  472. err => {
  473. next(err, song);
  474. }
  475. );
  476. async.eachLimit(
  477. song.artists,
  478. 1,
  479. (artist, next) => {
  480. PlaylistsModule.runJob("AUTOFILL_ARTIST_PLAYLIST", { artist }, this)
  481. .then(() => {
  482. next();
  483. })
  484. .catch(err => next(err));
  485. },
  486. err => {
  487. next(err, song);
  488. }
  489. );
  490. }
  491. ],
  492. (err, song) => {
  493. if (err && err !== true) return reject(new Error(err));
  494. if (!payload.oldStatus) payload.oldStatus = null;
  495. CacheModule.runJob("PUB", {
  496. channel: "song.updated",
  497. value: { songId: song._id, oldStatus: payload.oldStatus }
  498. });
  499. return resolve(song);
  500. }
  501. );
  502. });
  503. }
  504. /**
  505. * Gets multiple songs from id from Mongo and updates the cache with it
  506. *
  507. * @param {object} payload - an object containing the payload
  508. * @param {Array} payload.songIds - the ids of the songs we are trying to update
  509. * @param {string} payload.oldStatus - old status of song being updated (optional)
  510. * @returns {Promise} - returns a promise (resolve, reject)
  511. */
  512. async UPDATE_SONGS(payload) {
  513. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  514. const stationModel = await DBModule.runJob("GET_MODEL", { modelName: "station" }, this);
  515. return new Promise((resolve, reject) => {
  516. async.waterfall(
  517. [
  518. // Get songs from Mongo
  519. next => {
  520. const { songIds } = payload;
  521. SongsModule.SongModel.find({ _id: songIds }, next);
  522. },
  523. // Any songs that were not in Mongo, remove from cache, if they're in the cache
  524. (songs, next) => {
  525. const { songIds } = payload;
  526. async.eachLimit(
  527. songIds,
  528. 1,
  529. (songId, next) => {
  530. if (songs.findIndex(song => song._id.toString() === songId) === -1) {
  531. // NOTE: could be made lower priority
  532. CacheModule.runJob("HDEL", {
  533. table: "songs",
  534. key: songId
  535. });
  536. next();
  537. } else next();
  538. },
  539. () => {
  540. next(null, songs);
  541. }
  542. );
  543. },
  544. // Adds/updates all songs in the cache
  545. (songs, next) => {
  546. async.eachLimit(
  547. songs,
  548. 1,
  549. (song, next) => {
  550. CacheModule.runJob(
  551. "HSET",
  552. {
  553. table: "songs",
  554. key: song._id,
  555. value: song
  556. },
  557. this
  558. )
  559. .then(() => {
  560. next();
  561. })
  562. .catch(next);
  563. },
  564. () => {
  565. next(null, songs);
  566. }
  567. );
  568. },
  569. // Updates all playlists that the songs are in by setting the new trimmed song
  570. (songs, next) => {
  571. const trimmedSongs = songs.map(song => {
  572. const { _id, youtubeId, title, artists, thumbnail, duration, verified } = song;
  573. return {
  574. _id,
  575. youtubeId,
  576. title,
  577. artists,
  578. thumbnail,
  579. duration,
  580. verified
  581. };
  582. });
  583. const playlistsToUpdate = new Set();
  584. async.eachLimit(
  585. trimmedSongs,
  586. 1,
  587. (trimmedSong, next) => {
  588. async.waterfall(
  589. [
  590. next => {
  591. playlistModel.updateMany(
  592. { "songs._id": trimmedSong._id },
  593. { $set: { "songs.$": trimmedSong } },
  594. next
  595. );
  596. },
  597. (res, next) => {
  598. playlistModel.find({ "songs._id": trimmedSong._id }, next);
  599. },
  600. (playlists, next) => {
  601. playlists.forEach(playlist => {
  602. playlistsToUpdate.add(playlist._id.toString());
  603. });
  604. next();
  605. }
  606. ],
  607. next
  608. );
  609. },
  610. err => {
  611. next(err, songs, playlistsToUpdate);
  612. }
  613. );
  614. },
  615. // Updates all playlists that the songs are in
  616. (songs, playlistsToUpdate, next) => {
  617. async.eachLimit(
  618. playlistsToUpdate,
  619. 1,
  620. (playlistId, next) => {
  621. PlaylistsModule.runJob(
  622. "UPDATE_PLAYLIST",
  623. {
  624. playlistId
  625. },
  626. this
  627. )
  628. .then(() => {
  629. next();
  630. })
  631. .catch(err => {
  632. next(err);
  633. });
  634. },
  635. err => {
  636. next(err, songs);
  637. }
  638. );
  639. },
  640. // Updates all station queues that the songs are in by setting the new trimmed song
  641. (songs, next) => {
  642. const stationsToUpdate = new Set();
  643. async.eachLimit(
  644. songs,
  645. 1,
  646. (song, next) => {
  647. async.waterfall(
  648. [
  649. next => {
  650. const { youtubeId, title, artists, thumbnail, duration, verified } = song;
  651. stationModel.updateMany(
  652. { "queue._id": song._id },
  653. {
  654. $set: {
  655. "queue.$.youtubeId": youtubeId,
  656. "queue.$.title": title,
  657. "queue.$.artists": artists,
  658. "queue.$.thumbnail": thumbnail,
  659. "queue.$.duration": duration,
  660. "queue.$.verified": verified
  661. }
  662. },
  663. next
  664. );
  665. },
  666. (res, next) => {
  667. stationModel.find({ "queue._id": song._id }, next);
  668. },
  669. (stations, next) => {
  670. stations.forEach(station => {
  671. stationsToUpdate.add(station._id.toString());
  672. });
  673. next();
  674. }
  675. ],
  676. next
  677. );
  678. },
  679. err => {
  680. next(err, songs, stationsToUpdate);
  681. }
  682. );
  683. },
  684. // Updates all playlists that the songs are in
  685. (songs, stationsToUpdate, next) => {
  686. async.eachLimit(
  687. stationsToUpdate,
  688. 1,
  689. (stationId, next) => {
  690. StationsModule.runJob(
  691. "UPDATE_STATION",
  692. {
  693. stationId
  694. },
  695. this
  696. )
  697. .then(() => {
  698. next();
  699. })
  700. .catch(err => {
  701. next(err);
  702. });
  703. },
  704. err => {
  705. next(err, songs);
  706. }
  707. );
  708. },
  709. // Autofill the genre playlists of all genres of all songs
  710. (songs, next) => {
  711. const genresToAutofill = new Set();
  712. songs.forEach(song => {
  713. if (song.verified)
  714. song.genres.forEach(genre => {
  715. genresToAutofill.add(genre);
  716. });
  717. });
  718. async.eachLimit(
  719. genresToAutofill,
  720. 1,
  721. (genre, next) => {
  722. PlaylistsModule.runJob("AUTOFILL_GENRE_PLAYLIST", { genre, createPlaylist: true }, this)
  723. .then(() => {
  724. next();
  725. })
  726. .catch(err => next(err));
  727. },
  728. err => {
  729. next(err, songs);
  730. }
  731. );
  732. },
  733. // Send event that the song was updated
  734. (songs, next) => {
  735. async.eachLimit(
  736. songs,
  737. 1,
  738. (song, next) => {
  739. CacheModule.runJob("PUB", {
  740. channel: "song.updated",
  741. value: { songId: song._id, oldStatus: null }
  742. });
  743. next();
  744. },
  745. () => {
  746. next();
  747. }
  748. );
  749. }
  750. ],
  751. err => {
  752. if (err && err !== true) return reject(new Error(err));
  753. return resolve();
  754. }
  755. );
  756. });
  757. }
  758. /**
  759. * Updates all songs
  760. *
  761. * @returns {Promise} - returns a promise (resolve, reject)
  762. */
  763. UPDATE_ALL_SONGS() {
  764. return new Promise((resolve, reject) => {
  765. async.waterfall(
  766. [
  767. next => {
  768. SongsModule.SongModel.find({}, next);
  769. },
  770. (songs, next) => {
  771. let index = 0;
  772. const { length } = songs;
  773. async.eachLimit(
  774. songs,
  775. 2,
  776. (song, next) => {
  777. index += 1;
  778. console.log(`Updating song #${index} out of ${length}: ${song._id}`);
  779. SongsModule.runJob("UPDATE_SONG", { songId: song._id }, this)
  780. .then(() => {
  781. next();
  782. })
  783. .catch(err => {
  784. next(err);
  785. });
  786. },
  787. err => {
  788. next(err);
  789. }
  790. );
  791. }
  792. ],
  793. err => {
  794. if (err && err !== true) return reject(new Error(err));
  795. return resolve();
  796. }
  797. );
  798. });
  799. }
  800. // /**
  801. // * Deletes song from id from Mongo and cache
  802. // *
  803. // * @param {object} payload - returns an object containing the payload
  804. // * @param {string} payload.songId - the song id of the song we are trying to delete
  805. // * @returns {Promise} - returns a promise (resolve, reject)
  806. // */
  807. // DELETE_SONG(payload) {
  808. // return new Promise((resolve, reject) =>
  809. // async.waterfall(
  810. // [
  811. // next => {
  812. // SongsModule.SongModel.deleteOne({ _id: payload.songId }, next);
  813. // },
  814. // next => {
  815. // CacheModule.runJob(
  816. // "HDEL",
  817. // {
  818. // table: "songs",
  819. // key: payload.songId
  820. // },
  821. // this
  822. // )
  823. // .then(() => next())
  824. // .catch(next);
  825. // },
  826. // next => {
  827. // this.log("INFO", `Going to update playlists and stations now for deleted song ${payload.songId}`);
  828. // DBModule.runJob("GET_MODEL", { modelName: "playlist" }).then(playlistModel => {
  829. // playlistModel.find({ "songs._id": song._id }, (err, playlists) => {
  830. // if (err) this.log("ERROR", err);
  831. // else {
  832. // playlistModel.updateMany(
  833. // { "songs._id": payload.songId },
  834. // { $pull: { "songs.$._id": payload.songId} },
  835. // err => {
  836. // if (err) this.log("ERROR", err);
  837. // else {
  838. // playlists.forEach(playlist => {
  839. // PlaylistsModule.runJob("UPDATE_PLAYLIST", {
  840. // playlistId: playlist._id
  841. // });
  842. // });
  843. // }
  844. // }
  845. // );
  846. // }
  847. // });
  848. // });
  849. // DBModule.runJob("GET_MODEL", { modelName: "station" }).then(stationModel => {
  850. // stationModel.find({ "queue._id": payload.songId }, (err, stations) => {
  851. // stationModel.updateMany(
  852. // { "queue._id": payload.songId },
  853. // {
  854. // $pull: { "queue._id": }
  855. // },
  856. // err => {
  857. // if (err) this.log("ERROR", err);
  858. // else {
  859. // stations.forEach(station => {
  860. // StationsModule.runJob("UPDATE_STATION", { stationId: station._id });
  861. // });
  862. // }
  863. // }
  864. // );
  865. // });
  866. // });
  867. // }
  868. // ],
  869. // err => {
  870. // if (err && err !== true) return reject(new Error(err));
  871. // return resolve();
  872. // }
  873. // )
  874. // );
  875. // }
  876. /**
  877. * Searches through songs
  878. *
  879. * @param {object} payload - object that contains the payload
  880. * @param {string} payload.query - the query
  881. * @param {string} payload.includeUnverified - include unverified songs
  882. * @param {string} payload.includeVerified - include verified songs
  883. * @param {string} payload.trimmed - include trimmed songs
  884. * @param {string} payload.page - page (default 1)
  885. * @returns {Promise} - returns promise (reject, resolve)
  886. */
  887. SEARCH(payload) {
  888. return new Promise((resolve, reject) => {
  889. async.waterfall(
  890. [
  891. next => {
  892. const isVerified = [];
  893. if (payload.includeUnverified) isVerified.push(false);
  894. if (payload.includeVerified) isVerified.push(true);
  895. if (isVerified.length === 0) return next("No verified status has been included.");
  896. let { query } = payload;
  897. const isRegex =
  898. query.length > 2 && query.indexOf("/") === 0 && query.lastIndexOf("/") === query.length - 1;
  899. if (isRegex) query = query.slice(1, query.length - 1);
  900. else query = query.replaceAll(/[.*+?^${}()|[\]\\]/g, "\\$&");
  901. const filterArray = [
  902. {
  903. title: new RegExp(`${query}`, "i"),
  904. verified: { $in: isVerified }
  905. },
  906. {
  907. artists: new RegExp(`${query}`, "i"),
  908. verified: { $in: isVerified }
  909. }
  910. ];
  911. return next(null, filterArray);
  912. },
  913. (filterArray, next) => {
  914. const page = payload.page ? payload.page : 1;
  915. const pageSize = 15;
  916. const skipAmount = pageSize * (page - 1);
  917. SongsModule.SongModel.find({ $or: filterArray }).count((err, count) => {
  918. if (err) next(err);
  919. else {
  920. SongsModule.SongModel.find({ $or: filterArray })
  921. .skip(skipAmount)
  922. .limit(pageSize)
  923. .exec((err, songs) => {
  924. if (err) next(err);
  925. else {
  926. next(null, {
  927. songs,
  928. page,
  929. pageSize,
  930. skipAmount,
  931. count
  932. });
  933. }
  934. });
  935. }
  936. });
  937. },
  938. (data, next) => {
  939. if (data.songs.length === 0) next("No songs found");
  940. else if (payload.trimmed) {
  941. next(null, {
  942. songs: data.songs.map(song => {
  943. const { _id, youtubeId, title, artists, thumbnail, duration, verified } = song;
  944. return {
  945. _id,
  946. youtubeId,
  947. title,
  948. artists,
  949. thumbnail,
  950. duration,
  951. verified
  952. };
  953. }),
  954. ...data
  955. });
  956. } else next(null, data);
  957. }
  958. ],
  959. (err, data) => {
  960. if (err && err !== true) return reject(new Error(err));
  961. return resolve(data);
  962. }
  963. );
  964. });
  965. }
  966. /**
  967. * Recalculates dislikes and likes for a song
  968. *
  969. * @param {object} payload - returns an object containing the payload
  970. * @param {string} payload.youtubeId - the youtube id of the song
  971. * @param {string} payload.songId - the song id of the song
  972. * @returns {Promise} - returns a promise (resolve, reject)
  973. */
  974. async RECALCULATE_SONG_RATINGS(payload) {
  975. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  976. return new Promise((resolve, reject) => {
  977. async.waterfall(
  978. [
  979. next => {
  980. playlistModel.countDocuments(
  981. { songs: { $elemMatch: { _id: payload.songId } }, type: "user-liked" },
  982. (err, likes) => {
  983. if (err) return next(err);
  984. return next(null, likes);
  985. }
  986. );
  987. },
  988. (likes, next) => {
  989. playlistModel.countDocuments(
  990. { songs: { $elemMatch: { _id: payload.songId } }, type: "user-disliked" },
  991. (err, dislikes) => {
  992. if (err) return next(err);
  993. return next(err, { likes, dislikes });
  994. }
  995. );
  996. },
  997. ({ likes, dislikes }, next) => {
  998. SongsModule.SongModel.updateOne(
  999. { _id: payload.songId },
  1000. {
  1001. $set: {
  1002. likes,
  1003. dislikes
  1004. }
  1005. },
  1006. err => next(err, { likes, dislikes })
  1007. );
  1008. }
  1009. ],
  1010. (err, { likes, dislikes }) => {
  1011. if (err) return reject(new Error(err));
  1012. return resolve({ likes, dislikes });
  1013. }
  1014. );
  1015. });
  1016. }
  1017. /**
  1018. * Recalculates dislikes and likes for all songs
  1019. *
  1020. * @returns {Promise} - returns a promise (resolve, reject)
  1021. */
  1022. RECALCULATE_ALL_SONG_RATINGS() {
  1023. return new Promise((resolve, reject) => {
  1024. async.waterfall(
  1025. [
  1026. next => {
  1027. SongsModule.SongModel.find({}, { _id: true }, next);
  1028. },
  1029. (songs, next) => {
  1030. async.eachLimit(
  1031. songs,
  1032. 2,
  1033. (song, next) => {
  1034. SongsModule.runJob("RECALCULATE_SONG_RATINGS", { songId: song._id }, this)
  1035. .then(() => {
  1036. next();
  1037. })
  1038. .catch(err => {
  1039. next(err);
  1040. });
  1041. },
  1042. err => {
  1043. next(err);
  1044. }
  1045. );
  1046. }
  1047. ],
  1048. err => {
  1049. if (err) return reject(new Error(err));
  1050. return resolve();
  1051. }
  1052. );
  1053. });
  1054. }
  1055. /**
  1056. * Gets an array of all genres
  1057. *
  1058. * @returns {Promise} - returns a promise (resolve, reject)
  1059. */
  1060. GET_ALL_GENRES() {
  1061. return new Promise((resolve, reject) => {
  1062. async.waterfall(
  1063. [
  1064. next => {
  1065. SongsModule.SongModel.find({ verified: true }, { genres: 1, _id: false }, next);
  1066. },
  1067. (songs, next) => {
  1068. let allGenres = [];
  1069. songs.forEach(song => {
  1070. allGenres = allGenres.concat(song.genres);
  1071. });
  1072. const lowerCaseGenres = allGenres.map(genre => genre.toLowerCase());
  1073. const uniqueGenres = lowerCaseGenres.filter(
  1074. (value, index, self) => self.indexOf(value) === index
  1075. );
  1076. next(null, uniqueGenres);
  1077. }
  1078. ],
  1079. (err, genres) => {
  1080. if (err && err !== true) return reject(new Error(err));
  1081. return resolve({ genres });
  1082. }
  1083. );
  1084. });
  1085. }
  1086. /**
  1087. * Gets an array of all artists
  1088. *
  1089. * @returns {Promise} - returns a promise (resolve, reject)
  1090. */
  1091. GET_ALL_ARTISTS() {
  1092. return new Promise((resolve, reject) => {
  1093. async.waterfall(
  1094. [
  1095. next => {
  1096. SongsModule.SongModel.find({ status: "verified" }, { artists: 1, _id: false }, next);
  1097. },
  1098. (songs, next) => {
  1099. let allArtists = [];
  1100. songs.forEach(song => {
  1101. allArtists = allArtists.concat(song.artists);
  1102. });
  1103. const lowerCaseArtists = allArtists.map(artist => artist.toLowerCase());
  1104. const uniqueArtists = lowerCaseArtists.filter(
  1105. (value, index, self) => self.indexOf(value) === index
  1106. );
  1107. next(null, uniqueArtists);
  1108. }
  1109. ],
  1110. (err, artists) => {
  1111. if (err && err !== true) return reject(new Error(err));
  1112. return resolve({ artists });
  1113. }
  1114. );
  1115. });
  1116. }
  1117. /**
  1118. * Gets an array of all songs with a specific genre
  1119. *
  1120. * @param {object} payload - returns an object containing the payload
  1121. * @param {string} payload.genre - the genre
  1122. * @returns {Promise} - returns a promise (resolve, reject)
  1123. */
  1124. GET_ALL_SONGS_WITH_GENRE(payload) {
  1125. return new Promise((resolve, reject) => {
  1126. async.waterfall(
  1127. [
  1128. next => {
  1129. SongsModule.SongModel.find(
  1130. {
  1131. verified: true,
  1132. genres: { $regex: new RegExp(`^${payload.genre.toLowerCase()}$`, "i") }
  1133. },
  1134. next
  1135. );
  1136. }
  1137. ],
  1138. (err, songs) => {
  1139. if (err && err !== true) return reject(new Error(err));
  1140. return resolve({ songs });
  1141. }
  1142. );
  1143. });
  1144. }
  1145. /**
  1146. * Gets an array of all songs with a specific artist
  1147. *
  1148. * @param {object} payload - returns an object containing the payload
  1149. * @param {string} payload.artist - the artist
  1150. * @returns {Promise} - returns a promise (resolve, reject)
  1151. */
  1152. GET_ALL_SONGS_WITH_ARTIST(payload) {
  1153. return new Promise((resolve, reject) => {
  1154. async.waterfall(
  1155. [
  1156. next => {
  1157. SongsModule.SongModel.find(
  1158. {
  1159. status: "verified",
  1160. artists: { $regex: new RegExp(`^${payload.artist.toLowerCase()}$`, "i") }
  1161. },
  1162. next
  1163. );
  1164. }
  1165. ],
  1166. (err, songs) => {
  1167. if (err && err !== true) return reject(new Error(err));
  1168. return resolve({ songs });
  1169. }
  1170. );
  1171. });
  1172. }
  1173. // runjob songs GET_ORPHANED_PLAYLIST_SONGS {}
  1174. /**
  1175. * Gets a orphaned playlist songs
  1176. *
  1177. * @returns {Promise} - returns promise (reject, resolve)
  1178. */
  1179. GET_ORPHANED_PLAYLIST_SONGS() {
  1180. return new Promise((resolve, reject) => {
  1181. DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this).then(playlistModel => {
  1182. playlistModel.find({}, (err, playlists) => {
  1183. if (err) reject(new Error(err));
  1184. else {
  1185. SongsModule.SongModel.find({}, { _id: true, youtubeId: true }, (err, songs) => {
  1186. if (err) reject(new Error(err));
  1187. else {
  1188. const songIds = songs.map(song => song._id.toString());
  1189. const orphanedYoutubeIds = new Set();
  1190. async.eachLimit(
  1191. playlists,
  1192. 1,
  1193. (playlist, next) => {
  1194. playlist.songs.forEach(song => {
  1195. if (
  1196. (!song._id || songIds.indexOf(song._id.toString() === -1)) &&
  1197. !orphanedYoutubeIds.has(song.youtubeId)
  1198. ) {
  1199. orphanedYoutubeIds.add(song.youtubeId);
  1200. }
  1201. });
  1202. next();
  1203. },
  1204. () => {
  1205. resolve({ youtubeIds: Array.from(orphanedYoutubeIds) });
  1206. }
  1207. );
  1208. }
  1209. });
  1210. }
  1211. });
  1212. });
  1213. });
  1214. }
  1215. /**
  1216. * Requests a song, adding it to the DB
  1217. *
  1218. * @param {object} payload - The payload
  1219. * @param {string} payload.youtubeId - The YouTube song id of the song
  1220. * @param {string} payload.userId - The user id of the person requesting the song
  1221. * @returns {Promise} - returns promise (reject, resolve)
  1222. */
  1223. REQUEST_SONG(payload) {
  1224. return new Promise((resolve, reject) => {
  1225. const { youtubeId, userId } = payload;
  1226. const requestedAt = Date.now();
  1227. async.waterfall(
  1228. [
  1229. next => {
  1230. DBModule.runJob("GET_MODEL", { modelName: "user" }, this)
  1231. .then(UserModel => {
  1232. UserModel.findOne({ _id: userId }, { "preferences.anonymousSongRequests": 1 }, next);
  1233. })
  1234. .catch(next);
  1235. },
  1236. (user, next) => {
  1237. SongsModule.SongModel.findOne({ youtubeId }, (err, song) => next(err, user, song));
  1238. },
  1239. // Get YouTube data from id
  1240. (user, song, next) => {
  1241. if (song) return next("This song is already in the database.", song);
  1242. // TODO Add err object as first param of callback
  1243. return YouTubeModule.runJob("GET_SONG", { youtubeId }, this)
  1244. .then(response => {
  1245. const { song } = response;
  1246. song.artists = [];
  1247. song.genres = [];
  1248. song.skipDuration = 0;
  1249. song.explicit = false;
  1250. song.requestedBy = user.preferences.anonymousSongRequests ? null : userId;
  1251. song.requestedAt = requestedAt;
  1252. song.verified = false;
  1253. next(null, song);
  1254. })
  1255. .catch(next);
  1256. },
  1257. (newSong, next) => {
  1258. const song = new SongsModule.SongModel(newSong);
  1259. song.save({ validateBeforeSave: false }, err => {
  1260. if (err) return next(err, song);
  1261. return next(null, song);
  1262. });
  1263. },
  1264. (song, next) => {
  1265. DBModule.runJob("GET_MODEL", { modelName: "user" }, this)
  1266. .then(UserModel => {
  1267. UserModel.findOne({ _id: userId }, (err, user) => {
  1268. if (err) return next(err);
  1269. if (!user) return next(null, song);
  1270. user.statistics.songsRequested += 1;
  1271. return user.save(err => {
  1272. if (err) return next(err);
  1273. return next(null, song);
  1274. });
  1275. });
  1276. })
  1277. .catch(next);
  1278. }
  1279. ],
  1280. async (err, song) => {
  1281. if (err && err !== "This song is already in the database.") return reject(err);
  1282. const { _id, youtubeId, title, artists, thumbnail, duration, verified } = song;
  1283. const trimmedSong = {
  1284. _id,
  1285. youtubeId,
  1286. title,
  1287. artists,
  1288. thumbnail,
  1289. duration,
  1290. verified
  1291. };
  1292. if (err && err === "This song is already in the database.")
  1293. return reject(new ErrorWithData(err, { song: trimmedSong }));
  1294. SongsModule.runJob("UPDATE_SONG", { songId: song._id });
  1295. return resolve({ song: trimmedSong });
  1296. }
  1297. );
  1298. });
  1299. }
  1300. // runjob songs REQUEST_ORPHANED_PLAYLIST_SONGS {}
  1301. /**
  1302. * Requests all orphaned playlist songs, adding them to the database
  1303. *
  1304. * @returns {Promise} - returns promise (reject, resolve)
  1305. */
  1306. REQUEST_ORPHANED_PLAYLIST_SONGS() {
  1307. return new Promise((resolve, reject) => {
  1308. DBModule.runJob("GET_MODEL", { modelName: "playlist" })
  1309. .then(playlistModel => {
  1310. SongsModule.runJob("GET_ORPHANED_PLAYLIST_SONGS", {}, this).then(response => {
  1311. const { youtubeIds } = response;
  1312. const playlistsToUpdate = new Set();
  1313. async.eachLimit(
  1314. youtubeIds,
  1315. 1,
  1316. (youtubeId, next) => {
  1317. async.waterfall(
  1318. [
  1319. next => {
  1320. console.log(
  1321. youtubeId,
  1322. `this is song ${youtubeIds.indexOf(youtubeId) + 1}/${youtubeIds.length}`
  1323. );
  1324. setTimeout(next, 150);
  1325. },
  1326. next => {
  1327. SongsModule.runJob(
  1328. "ENSURE_SONG_EXISTS_BY_SONG_ID",
  1329. { youtubeId, automaticallyRequested: true },
  1330. this
  1331. )
  1332. .then(() => next())
  1333. .catch(next);
  1334. },
  1335. next => {
  1336. console.log(444, youtubeId);
  1337. SongsModule.SongModel.findOne({ youtubeId }, next);
  1338. },
  1339. (song, next) => {
  1340. const { _id, title, artists, thumbnail, duration, verified } = song;
  1341. const trimmedSong = {
  1342. _id,
  1343. youtubeId,
  1344. title,
  1345. artists,
  1346. thumbnail,
  1347. duration,
  1348. verified
  1349. };
  1350. playlistModel.updateMany(
  1351. { "songs.youtubeId": song.youtubeId },
  1352. { $set: { "songs.$": trimmedSong } },
  1353. err => {
  1354. next(err, song);
  1355. }
  1356. );
  1357. },
  1358. (song, next) => {
  1359. playlistModel.find({ "songs._id": song._id }, next);
  1360. },
  1361. (playlists, next) => {
  1362. playlists.forEach(playlist => {
  1363. playlistsToUpdate.add(playlist._id.toString());
  1364. });
  1365. next();
  1366. }
  1367. ],
  1368. next
  1369. );
  1370. },
  1371. err => {
  1372. if (err) reject(err);
  1373. else {
  1374. async.eachLimit(
  1375. Array.from(playlistsToUpdate),
  1376. 1,
  1377. (playlistId, next) => {
  1378. PlaylistsModule.runJob(
  1379. "UPDATE_PLAYLIST",
  1380. {
  1381. playlistId
  1382. },
  1383. this
  1384. )
  1385. .then(() => {
  1386. next();
  1387. })
  1388. .catch(next);
  1389. },
  1390. err => {
  1391. if (err) reject(err);
  1392. else resolve();
  1393. }
  1394. );
  1395. }
  1396. }
  1397. );
  1398. });
  1399. })
  1400. .catch(reject);
  1401. });
  1402. }
  1403. /**
  1404. * Gets a list of all genres
  1405. *
  1406. * @returns {Promise} - returns promise (reject, resolve)
  1407. */
  1408. GET_GENRES() {
  1409. return new Promise((resolve, reject) => {
  1410. async.waterfall(
  1411. [
  1412. next => {
  1413. SongsModule.SongModel.distinct("genres", next);
  1414. }
  1415. ],
  1416. (err, genres) => {
  1417. if (err) reject(err);
  1418. resolve({ genres });
  1419. }
  1420. );
  1421. });
  1422. }
  1423. /**
  1424. * Gets a list of all artists
  1425. *
  1426. * @returns {Promise} - returns promise (reject, resolve)
  1427. */
  1428. GET_ARTISTS() {
  1429. return new Promise((resolve, reject) => {
  1430. async.waterfall(
  1431. [
  1432. next => {
  1433. SongsModule.SongModel.distinct("artists", next);
  1434. }
  1435. ],
  1436. (err, artists) => {
  1437. if (err) reject(err);
  1438. resolve({ artists });
  1439. }
  1440. );
  1441. });
  1442. }
  1443. /**
  1444. * Gets a list of all tags
  1445. *
  1446. * @returns {Promise} - returns promise (reject, resolve)
  1447. */
  1448. GET_TAGS() {
  1449. return new Promise((resolve, reject) => {
  1450. async.waterfall(
  1451. [
  1452. next => {
  1453. SongsModule.SongModel.distinct("tags", next);
  1454. }
  1455. ],
  1456. (err, tags) => {
  1457. if (err) reject(err);
  1458. resolve({ tags });
  1459. }
  1460. );
  1461. });
  1462. }
  1463. }
  1464. export default new _SongsModule();