playlists.js 39 KB

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