1
0

playlists.js 41 KB

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