songs.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. import async from "async";
  2. import config from "config";
  3. import mongoose from "mongoose";
  4. import CoreClass from "../core";
  5. let SongsModule;
  6. let CacheModule;
  7. let DBModule;
  8. let UtilsModule;
  9. let YouTubeModule;
  10. let StationsModule;
  11. let PlaylistsModule;
  12. class _SongsModule extends CoreClass {
  13. // eslint-disable-next-line require-jsdoc
  14. constructor() {
  15. super("songs");
  16. SongsModule = this;
  17. }
  18. /**
  19. * Initialises the songs module
  20. *
  21. * @returns {Promise} - returns promise (reject, resolve)
  22. */
  23. async initialize() {
  24. this.setStage(1);
  25. CacheModule = this.moduleManager.modules.cache;
  26. DBModule = this.moduleManager.modules.db;
  27. UtilsModule = this.moduleManager.modules.utils;
  28. YouTubeModule = this.moduleManager.modules.youtube;
  29. StationsModule = this.moduleManager.modules.stations;
  30. PlaylistsModule = this.moduleManager.modules.playlists;
  31. this.SongModel = await DBModule.runJob("GET_MODEL", { modelName: "song" });
  32. this.SongSchemaCache = await CacheModule.runJob("GET_SCHEMA", { schemaName: "song" });
  33. this.setStage(2);
  34. return new Promise((resolve, reject) =>
  35. async.waterfall(
  36. [
  37. next => {
  38. this.setStage(2);
  39. CacheModule.runJob("HGETALL", { table: "songs" })
  40. .then(songs => {
  41. next(null, songs);
  42. })
  43. .catch(next);
  44. },
  45. (songs, next) => {
  46. this.setStage(3);
  47. if (!songs) return next();
  48. const songIds = Object.keys(songs);
  49. return async.each(
  50. songIds,
  51. (songId, next) => {
  52. SongsModule.SongModel.findOne({ songId }, (err, song) => {
  53. if (err) next(err);
  54. else if (!song)
  55. CacheModule.runJob("HDEL", {
  56. table: "songs",
  57. key: songId
  58. })
  59. .then(() => next())
  60. .catch(next);
  61. else next();
  62. });
  63. },
  64. next
  65. );
  66. },
  67. next => {
  68. this.setStage(4);
  69. SongsModule.SongModel.find({}, next);
  70. },
  71. (songs, next) => {
  72. this.setStage(5);
  73. async.each(
  74. songs,
  75. (song, next) => {
  76. CacheModule.runJob("HSET", {
  77. table: "songs",
  78. key: song.songId,
  79. value: SongsModule.SongSchemaCache(song)
  80. })
  81. .then(() => next())
  82. .catch(next);
  83. },
  84. next
  85. );
  86. }
  87. ],
  88. async err => {
  89. if (err) {
  90. err = await UtilsModule.runJob("GET_ERROR", { error: err });
  91. reject(new Error(err));
  92. } else resolve();
  93. }
  94. )
  95. );
  96. }
  97. /**
  98. * Gets a song by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  99. *
  100. * @param {object} payload - object containing the payload
  101. * @param {string} payload.id - the id of the song we are trying to get
  102. * @returns {Promise} - returns a promise (resolve, reject)
  103. */
  104. GET_SONG(payload) {
  105. return new Promise((resolve, reject) =>
  106. async.waterfall(
  107. [
  108. next => {
  109. if (!mongoose.Types.ObjectId.isValid(payload.id)) return next("Id is not a valid ObjectId.");
  110. return CacheModule.runJob("HGET", { table: "songs", key: payload.id }, 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.id }, next);
  117. },
  118. (song, next) => {
  119. if (song) {
  120. CacheModule.runJob(
  121. "HSET",
  122. {
  123. table: "songs",
  124. key: payload.id,
  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. * Makes sure that if a song is not currently in the songs db, to add it
  141. *
  142. * @param {object} payload - an object containing the payload
  143. * @param {string} payload.songId - the youtube song id of the song we are trying to ensure is in the songs db
  144. * @returns {Promise} - returns a promise (resolve, reject)
  145. */
  146. ENSURE_SONG_EXISTS_BY_SONG_ID(payload) {
  147. return new Promise((resolve, reject) =>
  148. async.waterfall(
  149. [
  150. next => {
  151. SongsModule.SongModel.findOne({ songId: payload.songId }, next);
  152. },
  153. (song, next) => {
  154. if (song && song.duration > 0) next(true, song);
  155. else {
  156. YouTubeModule.runJob("GET_SONG", { songId: payload.songId }, this)
  157. .then(response => {
  158. next(null, song, response.song);
  159. })
  160. .catch(next);
  161. }
  162. // else if (song && song.duration <= 0) {
  163. // YouTubeModule.runJob("GET_SONG", { songId: payload.songId }, this)
  164. // .then(response => next(null, { ...response.song }, false))
  165. // .catch(next);
  166. // } else {
  167. // YouTubeModule.runJob("GET_SONG", { songId: payload.songId }, this)
  168. // .then(response => next(null, { ...response.song }, false))
  169. // .catch(next);
  170. // }
  171. },
  172. (song, youtubeSong, next) => {
  173. if (song && song.duration <= 0) {
  174. song.duration = youtubeSong.duration;
  175. song.save({ validateBeforeSave: true }, err => {
  176. if (err) return next(err, song);
  177. return next(null, song);
  178. });
  179. } else {
  180. const song = new SongsModule.SongModel({ ...youtubeSong });
  181. song.save({ validateBeforeSave: true }, err => {
  182. if (err) return next(err, song);
  183. return next(null, song);
  184. });
  185. }
  186. }
  187. ],
  188. (err, song) => {
  189. if (err && err !== true) return reject(new Error(err));
  190. return resolve({ song });
  191. }
  192. )
  193. );
  194. }
  195. /**
  196. * Gets a song by song id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  197. *
  198. * @param {object} payload - an object containing the payload
  199. * @param {string} payload.songId - the mongo id of the song we are trying to get
  200. * @returns {Promise} - returns a promise (resolve, reject)
  201. */
  202. GET_SONG_FROM_ID(payload) {
  203. return new Promise((resolve, reject) =>
  204. async.waterfall(
  205. [
  206. next => {
  207. SongsModule.SongModel.findOne({ songId: payload.songId }, next);
  208. }
  209. ],
  210. (err, song) => {
  211. if (err && err !== true) return reject(new Error(err));
  212. return resolve({ song });
  213. }
  214. )
  215. );
  216. }
  217. /**
  218. * Gets a song from id from Mongo and updates the cache with it
  219. *
  220. * @param {object} payload - an object containing the payload
  221. * @param {string} payload.songId - the id of the song we are trying to update
  222. * @returns {Promise} - returns a promise (resolve, reject)
  223. */
  224. UPDATE_SONG(payload) {
  225. return new Promise((resolve, reject) =>
  226. async.waterfall(
  227. [
  228. next => {
  229. SongsModule.SongModel.findOne({ _id: payload.songId }, next);
  230. },
  231. (song, next) => {
  232. if (!song) {
  233. CacheModule.runJob("HDEL", {
  234. table: "songs",
  235. key: payload.songId
  236. });
  237. return next("Song not found.");
  238. }
  239. return CacheModule.runJob(
  240. "HSET",
  241. {
  242. table: "songs",
  243. key: payload.songId,
  244. value: song
  245. },
  246. this
  247. )
  248. .then(song => {
  249. next(null, song);
  250. })
  251. .catch(next);
  252. },
  253. (song, next) => {
  254. next(null, song);
  255. const { _id, songId, title, artists, thumbnail, duration, status } = song;
  256. const trimmedSong = {
  257. _id,
  258. songId,
  259. title,
  260. artists,
  261. thumbnail,
  262. duration,
  263. status
  264. };
  265. this.log("INFO", `Going to update playlists and stations now for song ${_id}`);
  266. DBModule.runJob("GET_MODEL", { modelName: "playlist" }).then(playlistModel => {
  267. playlistModel.updateMany(
  268. { "songs._id": song._id },
  269. { $set: { "songs.$": trimmedSong } },
  270. err => {
  271. if (err) this.log("ERROR", err);
  272. else
  273. playlistModel.find({ "songs._id": song._id }, (err, playlists) => {
  274. playlists.forEach(playlist => {
  275. PlaylistsModule.runJob("UPDATE_PLAYLIST", {
  276. playlistId: playlist._id
  277. });
  278. });
  279. });
  280. }
  281. );
  282. });
  283. DBModule.runJob("GET_MODEL", { modelName: "station" }).then(stationModel => {
  284. stationModel.updateMany(
  285. { "queue._id": song._id },
  286. {
  287. $set: {
  288. "queue.$.songId": songId,
  289. "queue.$.title": title,
  290. "queue.$.artists": artists,
  291. "queue.$.thumbnail": thumbnail,
  292. "queue.$.duration": duration,
  293. "queue.$.status": status
  294. }
  295. },
  296. err => {
  297. if (err) this.log("ERROR", err);
  298. else
  299. stationModel.find({ "queue._id": song._id }, (err, stations) => {
  300. stations.forEach(station => {
  301. StationsModule.runJob("UPDATE_STATION", { stationId: station._id });
  302. });
  303. });
  304. }
  305. );
  306. });
  307. },
  308. (song, next) => {
  309. async.eachLimit(
  310. song.genres,
  311. 1,
  312. (genre, next) => {
  313. PlaylistsModule.runJob("AUTOFILL_GENRE_PLAYLIST", { genre }, this)
  314. .then(() => {
  315. next();
  316. })
  317. .catch(err => next(err));
  318. },
  319. err => {
  320. next(err, song);
  321. }
  322. );
  323. }
  324. ],
  325. (err, song) => {
  326. if (err && err !== true) return reject(new Error(err));
  327. return resolve(song);
  328. }
  329. )
  330. );
  331. }
  332. /**
  333. * Deletes song from id from Mongo and cache
  334. *
  335. * @param {object} payload - returns an object containing the payload
  336. * @param {string} payload.songId - the id of the song we are trying to delete
  337. * @returns {Promise} - returns a promise (resolve, reject)
  338. */
  339. DELETE_SONG(payload) {
  340. return new Promise((resolve, reject) =>
  341. async.waterfall(
  342. [
  343. next => {
  344. SongsModule.SongModel.deleteOne({ songId: payload.songId }, next);
  345. },
  346. next => {
  347. CacheModule.runJob(
  348. "HDEL",
  349. {
  350. table: "songs",
  351. key: payload.songId
  352. },
  353. this
  354. )
  355. .then(() => next())
  356. .catch(next);
  357. }
  358. ],
  359. err => {
  360. if (err && err !== true) return reject(new Error(err));
  361. return resolve();
  362. }
  363. )
  364. );
  365. }
  366. /**
  367. * Recalculates dislikes and likes for a song
  368. *
  369. * @param {object} payload - returns an object containing the payload
  370. * @param {string} payload.musareSongId - the (musare) id of the song
  371. * @param {string} payload.songId - the (mongodb) id of the song
  372. * @returns {Promise} - returns a promise (resolve, reject)
  373. */
  374. async RECALCULATE_SONG_RATINGS(payload) {
  375. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  376. return new Promise((resolve, reject) => {
  377. async.waterfall(
  378. [
  379. next => {
  380. playlistModel.countDocuments(
  381. { songs: { $elemMatch: { songId: payload.musareSongId } }, displayName: "Liked Songs" },
  382. (err, likes) => {
  383. if (err) return next(err);
  384. return next(null, likes);
  385. }
  386. );
  387. },
  388. (likes, next) => {
  389. playlistModel.countDocuments(
  390. { songs: { $elemMatch: { songId: payload.musareSongId } }, displayName: "Disliked Songs" },
  391. (err, dislikes) => {
  392. if (err) return next(err);
  393. return next(err, { likes, dislikes });
  394. }
  395. );
  396. },
  397. ({ likes, dislikes }, next) => {
  398. SongsModule.SongModel.updateOne(
  399. { _id: payload.songId },
  400. {
  401. $set: {
  402. likes,
  403. dislikes
  404. }
  405. },
  406. err => next(err, { likes, dislikes })
  407. );
  408. }
  409. ],
  410. (err, { likes, dislikes }) => {
  411. if (err) return reject(new Error(err));
  412. return resolve({ likes, dislikes });
  413. }
  414. );
  415. });
  416. }
  417. /**
  418. * Gets an array of all genres
  419. *
  420. * @returns {Promise} - returns a promise (resolve, reject)
  421. */
  422. GET_ALL_GENRES() {
  423. return new Promise((resolve, reject) =>
  424. async.waterfall(
  425. [
  426. next => {
  427. SongsModule.SongModel.find({ status: "verified" }, { genres: 1, _id: false }, next);
  428. },
  429. (songs, next) => {
  430. let allGenres = [];
  431. songs.forEach(song => {
  432. allGenres = allGenres.concat(song.genres);
  433. });
  434. const lowerCaseGenres = allGenres.map(genre => genre.toLowerCase());
  435. const uniqueGenres = lowerCaseGenres.filter(
  436. (value, index, self) => self.indexOf(value) === index
  437. );
  438. next(null, uniqueGenres);
  439. }
  440. ],
  441. (err, genres) => {
  442. if (err && err !== true) return reject(new Error(err));
  443. return resolve({ genres });
  444. }
  445. )
  446. );
  447. }
  448. /**
  449. * Gets an array of all songs with a specific genre
  450. *
  451. * @param {object} payload - returns an object containing the payload
  452. * @param {string} payload.genre - the genre
  453. * @returns {Promise} - returns a promise (resolve, reject)
  454. */
  455. GET_ALL_SONGS_WITH_GENRE(payload) {
  456. return new Promise((resolve, reject) =>
  457. async.waterfall(
  458. [
  459. next => {
  460. SongsModule.SongModel.find(
  461. {
  462. status: "verified",
  463. genres: { $regex: new RegExp(`^${payload.genre.toLowerCase()}$`, "i") }
  464. },
  465. next
  466. );
  467. }
  468. ],
  469. (err, songs) => {
  470. if (err && err !== true) return reject(new Error(err));
  471. return resolve({ songs });
  472. }
  473. )
  474. );
  475. }
  476. // runjob songs GET_ORPHANED_PLAYLIST_SONGS {}
  477. /**
  478. * Gets a orphaned playlist songs
  479. *
  480. * @returns {Promise} - returns promise (reject, resolve)
  481. */
  482. GET_ORPHANED_PLAYLIST_SONGS() {
  483. return new Promise((resolve, reject) => {
  484. DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this).then(playlistModel => {
  485. playlistModel.find({}, (err, playlists) => {
  486. if (err) reject(new Error(err));
  487. else {
  488. SongsModule.SongModel.find({}, { _id: true, songId: true }, (err, songs) => {
  489. if (err) reject(new Error(err));
  490. else {
  491. const musareSongIds = songs.map(song => song._id.toString());
  492. const orphanedSongIds = new Set();
  493. async.eachLimit(
  494. playlists,
  495. 1,
  496. (playlist, next) => {
  497. playlist.songs.forEach(song => {
  498. if (
  499. (!song._id || musareSongIds.indexOf(song._id.toString() === -1)) &&
  500. !orphanedSongIds.has(song.songId)
  501. ) {
  502. orphanedSongIds.add(song.songId);
  503. }
  504. });
  505. next();
  506. },
  507. () => {
  508. resolve({ songIds: Array.from(orphanedSongIds) });
  509. }
  510. );
  511. }
  512. });
  513. }
  514. });
  515. });
  516. });
  517. }
  518. /**
  519. * Requests a song, adding it to the DB
  520. *
  521. * @param {object} payload - The payload
  522. * @param {string} payload.songId - The YouTube song id of the song
  523. * @param {string} payload.userId - The user id of the person requesting the song
  524. * @returns {Promise} - returns promise (reject, resolve)
  525. */
  526. REQUEST_SONG(payload) {
  527. return new Promise((resolve, reject) => {
  528. const { songId, userId } = payload;
  529. const requestedAt = Date.now();
  530. async.waterfall(
  531. [
  532. next => {
  533. SongsModule.SongModel.findOne({ songId }, next);
  534. },
  535. // Get YouTube data from id
  536. (song, next) => {
  537. if (song) return next("This song is already in the database.");
  538. // TODO Add err object as first param of callback
  539. return YouTubeModule.runJob("GET_SONG", { songId }, this)
  540. .then(response => {
  541. const { song } = response;
  542. song.artists = [];
  543. song.genres = [];
  544. song.skipDuration = 0;
  545. song.explicit = false;
  546. song.requestedBy = userId;
  547. song.requestedAt = requestedAt;
  548. song.status = "unverified";
  549. next(null, song);
  550. })
  551. .catch(next);
  552. },
  553. (newSong, next) => {
  554. const song = new SongsModule.SongModel(newSong);
  555. song.save({ validateBeforeSave: false }, err => {
  556. if (err) return next(err, song);
  557. return next(null, song);
  558. });
  559. },
  560. (song, next) => {
  561. DBModule.runJob("GET_MODEL", { modelName: "user" }, this)
  562. .then(UserModel => {
  563. UserModel.findOne({ _id: userId }, (err, user) => {
  564. if (err) return next(err);
  565. if (!user) return next(null, song);
  566. user.statistics.songsRequested += 1;
  567. return user.save(err => {
  568. if (err) return next(err);
  569. return next(null, song);
  570. });
  571. });
  572. })
  573. .catch(next);
  574. }
  575. ],
  576. async (err, song) => {
  577. if (err) reject(err);
  578. SongsModule.runJob("UPDATE_SONG", { songId });
  579. CacheModule.runJob("PUB", {
  580. channel: "song.newUnverifiedSong",
  581. value: song._id
  582. });
  583. resolve();
  584. }
  585. );
  586. });
  587. }
  588. /**
  589. * Hides a song
  590. *
  591. * @param {object} payload - The payload
  592. * @param {string} payload.songId - The Musare song id of the song
  593. * @returns {Promise} - returns promise (reject, resolve)
  594. */
  595. HIDE_SONG(payload) {
  596. return new Promise((resolve, reject) => {
  597. const { songId } = payload;
  598. async.waterfall(
  599. [
  600. next => {
  601. SongsModule.SongModel.findOne({ _id: songId }, next);
  602. },
  603. // Get YouTube data from id
  604. (song, next) => {
  605. if (!song) return next("This song does not exist.");
  606. if (song.status === "hidden") return next("This song is already hidden.");
  607. if (song.status === "verified") return next("Verified songs cannot be hidden.");
  608. // TODO Add err object as first param of callback
  609. return next();
  610. },
  611. next => {
  612. SongsModule.SongModel.updateOne({ _id: songId }, { status: "hidden" }, next);
  613. },
  614. (res, next) => {
  615. SongsModule.runJob("UPDATE_SONG", { songId });
  616. next();
  617. }
  618. ],
  619. async err => {
  620. if (err) reject(err);
  621. CacheModule.runJob("PUB", {
  622. channel: "song.newHiddenSong",
  623. value: songId
  624. });
  625. CacheModule.runJob("PUB", {
  626. channel: "song.removedUnverifiedSong",
  627. value: songId
  628. });
  629. resolve();
  630. }
  631. );
  632. });
  633. }
  634. /**
  635. * Unhides a song
  636. *
  637. * @param {object} payload - The payload
  638. * @param {string} payload.songId - The Musare song id of the song
  639. * @returns {Promise} - returns promise (reject, resolve)
  640. */
  641. UNHIDE_SONG(payload) {
  642. return new Promise((resolve, reject) => {
  643. const { songId } = payload;
  644. async.waterfall(
  645. [
  646. next => {
  647. SongsModule.SongModel.findOne({ _id: songId }, next);
  648. },
  649. // Get YouTube data from id
  650. (song, next) => {
  651. if (!song) return next("This song does not exist.");
  652. if (song.status !== "hidden") return next("This song is not hidden.");
  653. // TODO Add err object as first param of callback
  654. return next();
  655. },
  656. next => {
  657. SongsModule.SongModel.updateOne({ _id: songId }, { status: "unverified" }, next);
  658. },
  659. (res, next) => {
  660. SongsModule.runJob("UPDATE_SONG", { songId });
  661. next();
  662. }
  663. ],
  664. async err => {
  665. if (err) reject(err);
  666. CacheModule.runJob("PUB", {
  667. channel: "song.newUnverifiedSong",
  668. value: songId
  669. });
  670. CacheModule.runJob("PUB", {
  671. channel: "song.removedHiddenSong",
  672. value: songId
  673. });
  674. resolve();
  675. }
  676. );
  677. });
  678. }
  679. // runjob songs REQUEST_ORPHANED_PLAYLIST_SONGS {}
  680. /**
  681. * Requests all orphaned playlist songs, adding them to the database
  682. *
  683. * @returns {Promise} - returns promise (reject, resolve)
  684. */
  685. REQUEST_ORPHANED_PLAYLIST_SONGS() {
  686. return new Promise((resolve, reject) => {
  687. DBModule.runJob("GET_MODEL", { modelName: "playlist" })
  688. .then(playlistModel => {
  689. SongsModule.runJob("GET_ORPHANED_PLAYLIST_SONGS", {}, this).then(response => {
  690. const { songIds } = response;
  691. const playlistsToUpdate = new Set();
  692. async.eachLimit(
  693. songIds,
  694. 1,
  695. (songId, next) => {
  696. async.waterfall(
  697. [
  698. next => {
  699. console.log(
  700. songId,
  701. `this is song ${songIds.indexOf(songId) + 1}/${songIds.length}`
  702. );
  703. setTimeout(next, 150);
  704. },
  705. next => {
  706. SongsModule.runJob("ENSURE_SONG_EXISTS_BY_SONG_ID", { songId }, this)
  707. .then(() => next())
  708. .catch(next);
  709. // SongsModule.runJob("REQUEST_SONG", { songId, userId: null }, this)
  710. // .then(() => {
  711. // next();
  712. // })
  713. // .catch(next);
  714. },
  715. next => {
  716. console.log(444, songId);
  717. SongsModule.SongModel.findOne({ songId }, next);
  718. },
  719. (song, next) => {
  720. const { _id, title, artists, thumbnail, duration, status } = song;
  721. const trimmedSong = {
  722. _id,
  723. songId,
  724. title,
  725. artists,
  726. thumbnail,
  727. duration,
  728. status
  729. };
  730. playlistModel.updateMany(
  731. { "songs.songId": song.songId },
  732. { $set: { "songs.$": trimmedSong } },
  733. (err, res) => {
  734. next(err, song);
  735. }
  736. );
  737. },
  738. (song, next) => {
  739. playlistModel.find({ "songs._id": song._id }, next);
  740. },
  741. (playlists, next) => {
  742. playlists.forEach(playlist => {
  743. playlistsToUpdate.add(playlist._id.toString());
  744. });
  745. next();
  746. }
  747. ],
  748. next
  749. );
  750. },
  751. err => {
  752. if (err) reject(err);
  753. else {
  754. async.eachLimit(
  755. Array.from(playlistsToUpdate),
  756. 1,
  757. (playlistId, next) => {
  758. PlaylistsModule.runJob(
  759. "UPDATE_PLAYLIST",
  760. {
  761. playlistId
  762. },
  763. this
  764. )
  765. .then(() => {
  766. next();
  767. })
  768. .catch(next);
  769. },
  770. err => {
  771. if (err) reject(err);
  772. else resolve();
  773. }
  774. );
  775. }
  776. }
  777. );
  778. });
  779. })
  780. .catch(reject);
  781. });
  782. }
  783. }
  784. export default new _SongsModule();