songs.js 31 KB

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