playlists.js 39 KB

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