songs.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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 songIds = Object.keys(songs);
  48. return async.each(
  49. songIds,
  50. (songId, next) => {
  51. SongsModule.SongModel.findOne({ songId }, (err, song) => {
  52. if (err) next(err);
  53. else if (!song)
  54. CacheModule.runJob("HDEL", {
  55. table: "songs",
  56. key: songId
  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.songId,
  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.id - 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.id)) return next("Id is not a valid ObjectId.");
  109. return CacheModule.runJob("HGET", { table: "songs", key: payload.id }, this)
  110. .then(song => next(null, song))
  111. .catch(next);
  112. },
  113. (song, next) => {
  114. if (song) return next(true, song);
  115. return SongsModule.SongModel.findOne({ _id: payload.id }, next);
  116. },
  117. (song, next) => {
  118. if (song) {
  119. CacheModule.runJob(
  120. "HSET",
  121. {
  122. table: "songs",
  123. key: payload.id,
  124. value: song
  125. },
  126. this
  127. ).then(song => next(null, song));
  128. } else next("Song not found.");
  129. }
  130. ],
  131. (err, song) => {
  132. if (err && err !== true) return reject(new Error(err));
  133. return resolve({ song });
  134. }
  135. )
  136. );
  137. }
  138. /**
  139. * Makes sure that if a song is not currently in the songs db, to add it
  140. *
  141. * @param {object} payload - an object containing the payload
  142. * @param {string} payload.songId - the youtube song id of the song we are trying to ensure is in the songs db
  143. * @param {string} payload.userId - 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, userId: payload.userId }, 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. DBModule.runJob("GET_MODEL", { modelName: "user" }, this)
  534. .then(UserModel => {
  535. UserModel.findOne({ _id: userId }, { "preferences.anonymousSongRequests": 1 }, next);
  536. })
  537. .catch(next);
  538. },
  539. (user, next) => {
  540. SongsModule.SongModel.findOne({ songId }, (err, song) => next(err, user, song));
  541. },
  542. // Get YouTube data from id
  543. (user, song, next) => {
  544. if (song) return next("This song is already in the database.");
  545. // TODO Add err object as first param of callback
  546. return YouTubeModule.runJob("GET_SONG", { songId }, this)
  547. .then(response => {
  548. const { song } = response;
  549. song.artists = [];
  550. song.genres = [];
  551. song.skipDuration = 0;
  552. song.explicit = false;
  553. song.requestedBy = user.preferences.anonymousSongRequests ? null : userId;
  554. song.requestedAt = requestedAt;
  555. song.status = "unverified";
  556. next(null, song);
  557. })
  558. .catch(next);
  559. },
  560. (newSong, next) => {
  561. const song = new SongsModule.SongModel(newSong);
  562. song.save({ validateBeforeSave: false }, err => {
  563. if (err) return next(err, song);
  564. return next(null, song);
  565. });
  566. },
  567. (song, next) => {
  568. DBModule.runJob("GET_MODEL", { modelName: "user" }, this)
  569. .then(UserModel => {
  570. UserModel.findOne({ _id: userId }, (err, user) => {
  571. if (err) return next(err);
  572. if (!user) return next(null, song);
  573. user.statistics.songsRequested += 1;
  574. return user.save(err => {
  575. if (err) return next(err);
  576. return next(null, song);
  577. });
  578. });
  579. })
  580. .catch(next);
  581. }
  582. ],
  583. async (err, song) => {
  584. if (err) reject(err);
  585. SongsModule.runJob("UPDATE_SONG", { songId: song._id });
  586. CacheModule.runJob("PUB", {
  587. channel: "song.newUnverifiedSong",
  588. value: song._id
  589. });
  590. resolve();
  591. }
  592. );
  593. });
  594. }
  595. /**
  596. * Hides a song
  597. *
  598. * @param {object} payload - The payload
  599. * @param {string} payload.songId - The Musare song id of the song
  600. * @returns {Promise} - returns promise (reject, resolve)
  601. */
  602. HIDE_SONG(payload) {
  603. return new Promise((resolve, reject) => {
  604. const { songId } = payload;
  605. async.waterfall(
  606. [
  607. next => {
  608. SongsModule.SongModel.findOne({ _id: songId }, next);
  609. },
  610. // Get YouTube data from id
  611. (song, next) => {
  612. if (!song) return next("This song does not exist.");
  613. if (song.status === "hidden") return next("This song is already hidden.");
  614. if (song.status === "verified") return next("Verified songs cannot be hidden.");
  615. // TODO Add err object as first param of callback
  616. return next();
  617. },
  618. next => {
  619. SongsModule.SongModel.updateOne({ _id: songId }, { status: "hidden" }, next);
  620. },
  621. (res, next) => {
  622. SongsModule.runJob("UPDATE_SONG", { songId });
  623. next();
  624. }
  625. ],
  626. async err => {
  627. if (err) reject(err);
  628. CacheModule.runJob("PUB", {
  629. channel: "song.newHiddenSong",
  630. value: songId
  631. });
  632. CacheModule.runJob("PUB", {
  633. channel: "song.removedUnverifiedSong",
  634. value: songId
  635. });
  636. resolve();
  637. }
  638. );
  639. });
  640. }
  641. /**
  642. * Unhides a song
  643. *
  644. * @param {object} payload - The payload
  645. * @param {string} payload.songId - The Musare song id of the song
  646. * @returns {Promise} - returns promise (reject, resolve)
  647. */
  648. UNHIDE_SONG(payload) {
  649. return new Promise((resolve, reject) => {
  650. const { songId } = payload;
  651. async.waterfall(
  652. [
  653. next => {
  654. SongsModule.SongModel.findOne({ _id: songId }, next);
  655. },
  656. // Get YouTube data from id
  657. (song, next) => {
  658. if (!song) return next("This song does not exist.");
  659. if (song.status !== "hidden") return next("This song is not hidden.");
  660. // TODO Add err object as first param of callback
  661. return next();
  662. },
  663. next => {
  664. SongsModule.SongModel.updateOne({ _id: songId }, { status: "unverified" }, next);
  665. },
  666. (res, next) => {
  667. SongsModule.runJob("UPDATE_SONG", { songId });
  668. next();
  669. }
  670. ],
  671. async err => {
  672. if (err) reject(err);
  673. CacheModule.runJob("PUB", {
  674. channel: "song.newUnverifiedSong",
  675. value: songId
  676. });
  677. CacheModule.runJob("PUB", {
  678. channel: "song.removedHiddenSong",
  679. value: songId
  680. });
  681. resolve();
  682. }
  683. );
  684. });
  685. }
  686. // runjob songs REQUEST_ORPHANED_PLAYLIST_SONGS {}
  687. /**
  688. * Requests all orphaned playlist songs, adding them to the database
  689. *
  690. * @returns {Promise} - returns promise (reject, resolve)
  691. */
  692. REQUEST_ORPHANED_PLAYLIST_SONGS() {
  693. return new Promise((resolve, reject) => {
  694. DBModule.runJob("GET_MODEL", { modelName: "playlist" })
  695. .then(playlistModel => {
  696. SongsModule.runJob("GET_ORPHANED_PLAYLIST_SONGS", {}, this).then(response => {
  697. const { songIds } = response;
  698. const playlistsToUpdate = new Set();
  699. async.eachLimit(
  700. songIds,
  701. 1,
  702. (songId, next) => {
  703. async.waterfall(
  704. [
  705. next => {
  706. console.log(
  707. songId,
  708. `this is song ${songIds.indexOf(songId) + 1}/${songIds.length}`
  709. );
  710. setTimeout(next, 150);
  711. },
  712. next => {
  713. SongsModule.runJob("ENSURE_SONG_EXISTS_BY_SONG_ID", { songId }, this)
  714. .then(() => next())
  715. .catch(next);
  716. // SongsModule.runJob("REQUEST_SONG", { songId, userId: null }, this)
  717. // .then(() => {
  718. // next();
  719. // })
  720. // .catch(next);
  721. },
  722. next => {
  723. console.log(444, songId);
  724. SongsModule.SongModel.findOne({ songId }, next);
  725. },
  726. (song, next) => {
  727. const { _id, title, artists, thumbnail, duration, status } = song;
  728. const trimmedSong = {
  729. _id,
  730. songId,
  731. title,
  732. artists,
  733. thumbnail,
  734. duration,
  735. status
  736. };
  737. playlistModel.updateMany(
  738. { "songs.songId": song.songId },
  739. { $set: { "songs.$": trimmedSong } },
  740. err => {
  741. next(err, song);
  742. }
  743. );
  744. },
  745. (song, next) => {
  746. playlistModel.find({ "songs._id": song._id }, next);
  747. },
  748. (playlists, next) => {
  749. playlists.forEach(playlist => {
  750. playlistsToUpdate.add(playlist._id.toString());
  751. });
  752. next();
  753. }
  754. ],
  755. next
  756. );
  757. },
  758. err => {
  759. if (err) reject(err);
  760. else {
  761. async.eachLimit(
  762. Array.from(playlistsToUpdate),
  763. 1,
  764. (playlistId, next) => {
  765. PlaylistsModule.runJob(
  766. "UPDATE_PLAYLIST",
  767. {
  768. playlistId
  769. },
  770. this
  771. )
  772. .then(() => {
  773. next();
  774. })
  775. .catch(next);
  776. },
  777. err => {
  778. if (err) reject(err);
  779. else resolve();
  780. }
  781. );
  782. }
  783. }
  784. );
  785. });
  786. })
  787. .catch(reject);
  788. });
  789. }
  790. }
  791. export default new _SongsModule();