playlists.js 39 KB

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