playlists.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  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. ).then(() => {
  1216. next();
  1217. })
  1218. .catch(err => next(err));
  1219. }
  1220. ],
  1221. err => {
  1222. if (err && err !== true) return reject(new Error(err));
  1223. return resolve();
  1224. }
  1225. )
  1226. );
  1227. }
  1228. /**
  1229. * Searches through playlists
  1230. *
  1231. * @param {object} payload - object that contains the payload
  1232. * @param {string} payload.query - the query
  1233. * @param {string} payload.includePrivate - include private playlists
  1234. * @param {string} payload.includeStation - include station playlists
  1235. * @param {string} payload.includeUser - include user playlists
  1236. * @param {string} payload.includeGenre - include genre playlists
  1237. * @param {string} payload.includeArtist - include artist playlists
  1238. * @param {string} payload.includeOwn - include own user playlists
  1239. * @param {string} payload.userId - the user id of the person requesting
  1240. * @param {string} payload.includeSongs - include songs
  1241. * @param {string} payload.page - page (default 1)
  1242. * @returns {Promise} - returns promise (reject, resolve)
  1243. */
  1244. SEARCH(payload) {
  1245. return new Promise((resolve, reject) =>
  1246. async.waterfall(
  1247. [
  1248. next => {
  1249. const types = [];
  1250. if (payload.includeStation) types.push("station");
  1251. if (payload.includeUser) types.push("user");
  1252. if (payload.includeGenre) types.push("genre");
  1253. if (payload.includeArtist) types.push("artist");
  1254. if (types.length === 0 && !payload.includeOwn) return next("No types have been included.");
  1255. const privacies = ["public"];
  1256. if (payload.includePrivate) privacies.push("private");
  1257. const includeObject = payload.includeSongs ? null : { songs: false };
  1258. const filterArray = [
  1259. {
  1260. displayName: new RegExp(`${payload.query}`, "i"),
  1261. privacy: { $in: privacies },
  1262. type: { $in: types }
  1263. }
  1264. ];
  1265. if (payload.includeOwn && payload.userId)
  1266. filterArray.push({
  1267. displayName: new RegExp(`${payload.query}`, "i"),
  1268. type: "user",
  1269. createdBy: payload.userId
  1270. });
  1271. return next(null, filterArray, includeObject);
  1272. },
  1273. (filterArray, includeObject, next) => {
  1274. const page = payload.page ? payload.page : 1;
  1275. const pageSize = 15;
  1276. const skipAmount = pageSize * (page - 1);
  1277. PlaylistsModule.playlistModel.find({ $or: filterArray }).count((err, count) => {
  1278. if (err) next(err);
  1279. else {
  1280. PlaylistsModule.playlistModel
  1281. .find({ $or: filterArray }, includeObject)
  1282. .skip(skipAmount)
  1283. .limit(pageSize)
  1284. .exec((err, playlists) => {
  1285. if (err) next(err);
  1286. else {
  1287. next(null, {
  1288. playlists,
  1289. page,
  1290. pageSize,
  1291. skipAmount,
  1292. count
  1293. });
  1294. }
  1295. });
  1296. }
  1297. });
  1298. },
  1299. (data, next) => {
  1300. if (data.playlists.length > 0) next(null, data);
  1301. else next("No playlists found");
  1302. }
  1303. ],
  1304. (err, data) => {
  1305. if (err && err !== true) return reject(new Error(err));
  1306. return resolve(data);
  1307. }
  1308. )
  1309. );
  1310. }
  1311. /**
  1312. * Clears and refills a station playlist
  1313. *
  1314. * @param {object} payload - object that contains the payload
  1315. * @param {string} payload.playlistId - the id of the playlist we are trying to clear and refill
  1316. * @returns {Promise} - returns promise (reject, resolve)
  1317. */
  1318. CLEAR_AND_REFILL_STATION_PLAYLIST(payload) {
  1319. return new Promise((resolve, reject) => {
  1320. const { playlistId } = payload;
  1321. async.waterfall(
  1322. [
  1323. next => {
  1324. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1325. .then(playlist => {
  1326. next(null, playlist);
  1327. })
  1328. .catch(err => {
  1329. next(err);
  1330. });
  1331. },
  1332. (playlist, next) => {
  1333. if (playlist.type !== "station") next("This playlist is not a station playlist.");
  1334. else next(null, playlist.createdFor);
  1335. },
  1336. (stationId, next) => {
  1337. PlaylistsModule.runJob("AUTOFILL_STATION_PLAYLIST", { stationId }, this)
  1338. .then(() => {
  1339. next();
  1340. })
  1341. .catch(err => {
  1342. next(err);
  1343. });
  1344. }
  1345. ],
  1346. err => {
  1347. if (err && err !== true) return reject(new Error(err));
  1348. return resolve();
  1349. }
  1350. );
  1351. });
  1352. }
  1353. /**
  1354. * Clears and refills a genre playlist
  1355. *
  1356. * @param {object} payload - object that contains the payload
  1357. * @param {string} payload.playlistId - the id of the playlist we are trying to clear and refill
  1358. * @returns {Promise} - returns promise (reject, resolve)
  1359. */
  1360. CLEAR_AND_REFILL_GENRE_PLAYLIST(payload) {
  1361. return new Promise((resolve, reject) => {
  1362. const { playlistId } = payload;
  1363. async.waterfall(
  1364. [
  1365. next => {
  1366. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1367. .then(playlist => {
  1368. next(null, playlist);
  1369. })
  1370. .catch(err => {
  1371. next(err);
  1372. });
  1373. },
  1374. (playlist, next) => {
  1375. if (playlist.type !== "genre") next("This playlist is not a genre playlist.");
  1376. else next(null, playlist.createdFor);
  1377. },
  1378. (genre, next) => {
  1379. PlaylistsModule.runJob("AUTOFILL_GENRE_PLAYLIST", { genre }, this)
  1380. .then(() => {
  1381. next();
  1382. })
  1383. .catch(err => {
  1384. next(err);
  1385. });
  1386. }
  1387. ],
  1388. err => {
  1389. if (err && err !== true) return reject(new Error(err));
  1390. return resolve();
  1391. }
  1392. );
  1393. });
  1394. }
  1395. /**
  1396. * Clears and refills a artist playlist
  1397. *
  1398. * @param {object} payload - object that contains the payload
  1399. * @param {string} payload.playlistId - the id of the playlist we are trying to clear and refill
  1400. * @returns {Promise} - returns promise (reject, resolve)
  1401. */
  1402. CLEAR_AND_REFILL_ARTIST_PLAYLIST(payload) {
  1403. return new Promise((resolve, reject) => {
  1404. const { playlistId } = payload;
  1405. async.waterfall(
  1406. [
  1407. next => {
  1408. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1409. .then(playlist => {
  1410. next(null, playlist);
  1411. })
  1412. .catch(err => {
  1413. next(err);
  1414. });
  1415. },
  1416. (playlist, next) => {
  1417. if (playlist.type !== "artist") next("This playlist is not a artist playlist.");
  1418. else next(null, playlist.createdFor);
  1419. },
  1420. (artist, next) => {
  1421. PlaylistsModule.runJob("AUTOFILL_ARTIST_PLAYLIST", { artist }, this)
  1422. .then(() => {
  1423. next();
  1424. })
  1425. .catch(err => {
  1426. next(err);
  1427. });
  1428. }
  1429. ],
  1430. err => {
  1431. if (err && err !== true) return reject(new Error(err));
  1432. return resolve();
  1433. }
  1434. );
  1435. });
  1436. }
  1437. }
  1438. export default new _PlaylistsModule();