playlists.js 39 KB

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