playlists.js 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. import async from "async";
  2. import { isLoginRequired } from "./hooks";
  3. import moduleManager from "../../index";
  4. const DBModule = moduleManager.modules.db;
  5. const UtilsModule = moduleManager.modules.utils;
  6. const IOModule = moduleManager.modules.io;
  7. const SongsModule = moduleManager.modules.songs;
  8. const CacheModule = moduleManager.modules.cache;
  9. const PlaylistsModule = moduleManager.modules.playlists;
  10. const YouTubeModule = moduleManager.modules.youtube;
  11. const ActivitiesModule = moduleManager.modules.activities;
  12. CacheModule.runJob("SUB", {
  13. channel: "playlist.create",
  14. cb: playlist => {
  15. IOModule.runJob("SOCKETS_FROM_USER", { userId: playlist.createdBy }, this).then(response => {
  16. response.sockets.forEach(socket => {
  17. socket.emit("event:playlist.create", playlist);
  18. });
  19. });
  20. }
  21. });
  22. CacheModule.runJob("SUB", {
  23. channel: "playlist.delete",
  24. cb: res => {
  25. IOModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(response => {
  26. response.sockets.forEach(socket => {
  27. socket.emit("event:playlist.delete", res.playlistId);
  28. });
  29. });
  30. }
  31. });
  32. CacheModule.runJob("SUB", {
  33. channel: "playlist.moveSongToTop",
  34. cb: res => {
  35. IOModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(response => {
  36. response.sockets.forEach(socket => {
  37. socket.emit("event:playlist.moveSongToTop", {
  38. playlistId: res.playlistId,
  39. songId: res.songId
  40. });
  41. });
  42. });
  43. }
  44. });
  45. CacheModule.runJob("SUB", {
  46. channel: "playlist.moveSongToBottom",
  47. cb: res => {
  48. IOModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(response => {
  49. response.sockets.forEach(socket => {
  50. socket.emit("event:playlist.moveSongToBottom", {
  51. playlistId: res.playlistId,
  52. songId: res.songId
  53. });
  54. });
  55. });
  56. }
  57. });
  58. CacheModule.runJob("SUB", {
  59. channel: "playlist.addSong",
  60. cb: res => {
  61. IOModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(response => {
  62. response.sockets.forEach(socket => {
  63. socket.emit("event:playlist.addSong", {
  64. playlistId: res.playlistId,
  65. song: res.song
  66. });
  67. });
  68. });
  69. }
  70. });
  71. CacheModule.runJob("SUB", {
  72. channel: "playlist.removeSong",
  73. cb: res => {
  74. IOModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(response => {
  75. response.sockets.forEach(socket => {
  76. socket.emit("event:playlist.removeSong", {
  77. playlistId: res.playlistId,
  78. songId: res.songId
  79. });
  80. });
  81. });
  82. }
  83. });
  84. CacheModule.runJob("SUB", {
  85. channel: "playlist.updateDisplayName",
  86. cb: res => {
  87. IOModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(response => {
  88. response.sockets.forEach(socket => {
  89. socket.emit("event:playlist.updateDisplayName", {
  90. playlistId: res.playlistId,
  91. displayName: res.displayName
  92. });
  93. });
  94. });
  95. }
  96. });
  97. CacheModule.runJob("SUB", {
  98. channel: "playlist.updatePrivacy",
  99. cb: res => {
  100. IOModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(response => {
  101. response.sockets.forEach(socket => {
  102. socket.emit("event:playlist.updatePrivacy", {
  103. playlistId: res.playlistId,
  104. privacy: res.privacy
  105. });
  106. });
  107. });
  108. }
  109. });
  110. export default {
  111. /**
  112. * Gets the first song from a private playlist
  113. *
  114. * @param {object} session - the session object automatically added by socket.io
  115. * @param {string} playlistId - the id of the playlist we are getting the first song from
  116. * @param {Function} cb - gets called with the result
  117. */
  118. getFirstSong: isLoginRequired(function getFirstSong(session, playlistId, cb) {
  119. async.waterfall(
  120. [
  121. next => {
  122. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  123. .then(playlist => {
  124. next(null, playlist);
  125. })
  126. .catch(next);
  127. },
  128. (playlist, next) => {
  129. if (!playlist || playlist.createdBy !== session.userId) return next("Playlist not found.");
  130. return next(null, playlist.songs[0]);
  131. }
  132. ],
  133. async (err, song) => {
  134. if (err) {
  135. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  136. this.log(
  137. "ERROR",
  138. "PLAYLIST_GET_FIRST_SONG",
  139. `Getting the first song of playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  140. );
  141. return cb({ status: "failure", message: err });
  142. }
  143. this.log(
  144. "SUCCESS",
  145. "PLAYLIST_GET_FIRST_SONG",
  146. `Successfully got the first song of playlist "${playlistId}" for user "${session.userId}".`
  147. );
  148. return cb({
  149. status: "success",
  150. song
  151. });
  152. }
  153. );
  154. }),
  155. /**
  156. * Gets all playlists for the user requesting it
  157. *
  158. * @param {object} session - the session object automatically added by socket.io
  159. * @param {boolean} showNonModifiablePlaylists - whether or not to show non modifiable playlists e.g. liked songs
  160. * @param {Function} cb - gets called with the result
  161. */
  162. indexForUser: isLoginRequired(async function indexForUser(session, showNonModifiablePlaylists, cb) {
  163. const playlistModel = await DBModule.runJob(
  164. "GET_MODEL",
  165. {
  166. modelName: "playlist"
  167. },
  168. this
  169. );
  170. async.waterfall(
  171. [
  172. next => {
  173. if (showNonModifiablePlaylists) playlistModel.find({ createdBy: session.userId }, next);
  174. else playlistModel.find({ createdBy: session.userId, isUserModifiable: true }, next);
  175. }
  176. ],
  177. async (err, playlists) => {
  178. if (err) {
  179. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  180. this.log(
  181. "ERROR",
  182. "PLAYLIST_INDEX_FOR_USER",
  183. `Indexing playlists for user "${session.userId}" failed. "${err}"`
  184. );
  185. return cb({ status: "failure", message: err });
  186. }
  187. this.log(
  188. "SUCCESS",
  189. "PLAYLIST_INDEX_FOR_USER",
  190. `Successfully indexed playlists for user "${session.userId}".`
  191. );
  192. return cb({
  193. status: "success",
  194. data: playlists
  195. });
  196. }
  197. );
  198. }),
  199. /**
  200. * Creates a new private playlist
  201. *
  202. * @param {object} session - the session object automatically added by socket.io
  203. * @param {object} data - the data for the new private playlist
  204. * @param {Function} cb - gets called with the result
  205. */
  206. create: isLoginRequired(async function create(session, data, cb) {
  207. const playlistModel = await DBModule.runJob(
  208. "GET_MODEL",
  209. {
  210. modelName: "playlist"
  211. },
  212. this
  213. );
  214. const blacklist = ["liked songs", "likedsongs", "disliked songs", "dislikedsongs"];
  215. async.waterfall(
  216. [
  217. next => (data ? next() : cb({ status: "failure", message: "Invalid data" })),
  218. next => {
  219. const { displayName, songs } = data;
  220. if (blacklist.indexOf(displayName.toLowerCase()) !== -1)
  221. return next("That playlist name is blacklisted. Please use a different name.");
  222. return playlistModel.create(
  223. {
  224. displayName,
  225. songs,
  226. createdBy: session.userId,
  227. createdAt: Date.now()
  228. },
  229. next
  230. );
  231. }
  232. ],
  233. async (err, playlist) => {
  234. if (err) {
  235. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  236. this.log(
  237. "ERROR",
  238. "PLAYLIST_CREATE",
  239. `Creating private playlist failed for user "${session.userId}". "${err}"`
  240. );
  241. return cb({ status: "failure", message: err });
  242. }
  243. CacheModule.runJob("PUB", {
  244. channel: "playlist.create",
  245. value: playlist
  246. });
  247. ActivitiesModule.runJob("ADD_ACTIVITY", {
  248. userId: session.userId,
  249. activityType: "created_playlist",
  250. payload: [playlist._id]
  251. });
  252. this.log(
  253. "SUCCESS",
  254. "PLAYLIST_CREATE",
  255. `Successfully created private playlist for user "${session.userId}".`
  256. );
  257. return cb({
  258. status: "success",
  259. message: "Successfully created playlist",
  260. data: {
  261. _id: playlist._id
  262. }
  263. });
  264. }
  265. );
  266. }),
  267. /**
  268. * Gets a playlist from id
  269. *
  270. * @param {object} session - the session object automatically added by socket.io
  271. * @param {string} playlistId - the id of the playlist we are getting
  272. * @param {Function} cb - gets called with the result
  273. */
  274. getPlaylist: isLoginRequired(function getPlaylist(session, playlistId, cb) {
  275. async.waterfall(
  276. [
  277. next => {
  278. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  279. .then(playlist => {
  280. next(null, playlist);
  281. })
  282. .catch(next);
  283. },
  284. (playlist, next) => {
  285. if (!playlist || playlist.createdBy !== session.userId) return next("Playlist not found");
  286. return next(null, playlist);
  287. }
  288. ],
  289. async (err, playlist) => {
  290. if (err) {
  291. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  292. this.log(
  293. "ERROR",
  294. "PLAYLIST_GET",
  295. `Getting private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  296. );
  297. return cb({ status: "failure", message: err });
  298. }
  299. this.log(
  300. "SUCCESS",
  301. "PLAYLIST_GET",
  302. `Successfully got private playlist "${playlistId}" for user "${session.userId}".`
  303. );
  304. return cb({
  305. status: "success",
  306. data: playlist
  307. });
  308. }
  309. );
  310. }),
  311. /**
  312. * Obtains basic metadata of a playlist in order to format an activity
  313. *
  314. * @param {object} session - the session object automatically added by socket.io
  315. * @param {string} playlistId - the playlist id
  316. * @param {Function} cb - callback
  317. */
  318. getPlaylistForActivity(session, playlistId, cb) {
  319. async.waterfall(
  320. [
  321. next => {
  322. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  323. .then(playlist => {
  324. next(null, playlist);
  325. })
  326. .catch(next);
  327. }
  328. ],
  329. async (err, playlist) => {
  330. if (err) {
  331. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  332. this.log(
  333. "ERROR",
  334. "PLAYLISTS_GET_PLAYLIST_FOR_ACTIVITY",
  335. `Failed to obtain metadata of playlist ${playlistId} for activity formatting. "${err}"`
  336. );
  337. return cb({ status: "failure", message: err });
  338. }
  339. this.log(
  340. "SUCCESS",
  341. "PLAYLISTS_GET_PLAYLIST_FOR_ACTIVITY",
  342. `Obtained metadata of playlist ${playlistId} for activity formatting successfully.`
  343. );
  344. return cb({
  345. status: "success",
  346. data: {
  347. title: playlist.displayName
  348. }
  349. });
  350. }
  351. );
  352. },
  353. // TODO Remove this
  354. /**
  355. * Updates a private playlist
  356. *
  357. * @param {object} session - the session object automatically added by socket.io
  358. * @param {string} playlistId - the id of the playlist we are updating
  359. * @param {object} playlist - the new private playlist object
  360. * @param {Function} cb - gets called with the result
  361. */
  362. update: isLoginRequired(async function update(session, playlistId, playlist, cb) {
  363. const playlistModel = await DBModule.runJob(
  364. "GET_MODEL",
  365. {
  366. modelName: "playlist"
  367. },
  368. this
  369. );
  370. async.waterfall(
  371. [
  372. next => {
  373. playlistModel.updateOne(
  374. { _id: playlistId, createdBy: session.userId },
  375. playlist,
  376. { runValidators: true },
  377. next
  378. );
  379. },
  380. (res, next) => {
  381. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  382. .then(playlist => {
  383. next(null, playlist);
  384. })
  385. .catch(next);
  386. }
  387. ],
  388. async (err, playlist) => {
  389. if (err) {
  390. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  391. this.log(
  392. "ERROR",
  393. "PLAYLIST_UPDATE",
  394. `Updating private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  395. );
  396. return cb({ status: "failure", message: err });
  397. }
  398. this.log(
  399. "SUCCESS",
  400. "PLAYLIST_UPDATE",
  401. `Successfully updated private playlist "${playlistId}" for user "${session.userId}".`
  402. );
  403. return cb({
  404. status: "success",
  405. data: playlist
  406. });
  407. }
  408. );
  409. }),
  410. /**
  411. * Updates a private playlist
  412. *
  413. * @param {object} session - the session object automatically added by socket.io
  414. * @param {string} playlistId - the id of the playlist we are updating
  415. * @param {Function} cb - gets called with the result
  416. */
  417. shuffle: isLoginRequired(async function shuffle(session, playlistId, cb) {
  418. const playlistModel = await DBModule.runJob(
  419. "GET_MODEL",
  420. {
  421. modelName: "playlist"
  422. },
  423. this
  424. );
  425. async.waterfall(
  426. [
  427. next => {
  428. if (!playlistId) return next("No playlist id.");
  429. return playlistModel.findById(playlistId, next);
  430. },
  431. (playlist, next) => {
  432. if (!playlist.isUserModifiable) return next("Playlist cannot be shuffled.");
  433. return UtilsModule.runJob("SHUFFLE", { array: playlist.songs }, this)
  434. .then(result => next(null, result.array))
  435. .catch(next);
  436. },
  437. (songs, next) => {
  438. playlistModel.updateOne({ _id: playlistId }, { $set: { songs } }, { runValidators: true }, next);
  439. },
  440. (res, next) => {
  441. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  442. .then(playlist => {
  443. next(null, playlist);
  444. })
  445. .catch(next);
  446. }
  447. ],
  448. async (err, playlist) => {
  449. if (err) {
  450. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  451. this.log(
  452. "ERROR",
  453. "PLAYLIST_SHUFFLE",
  454. `Updating private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  455. );
  456. return cb({ status: "failure", message: err });
  457. }
  458. this.log(
  459. "SUCCESS",
  460. "PLAYLIST_SHUFFLE",
  461. `Successfully updated private playlist "${playlistId}" for user "${session.userId}".`
  462. );
  463. return cb({
  464. status: "success",
  465. message: "Successfully shuffled playlist.",
  466. data: playlist
  467. });
  468. }
  469. );
  470. }),
  471. /**
  472. * Adds a song to a private playlist
  473. *
  474. * @param {object} session - the session object automatically added by socket.io
  475. * @param {boolean} isSet - is the song part of a set of songs to be added
  476. * @param {string} songId - the id of the song we are trying to add
  477. * @param {string} playlistId - the id of the playlist we are adding the song to
  478. * @param {Function} cb - gets called with the result
  479. */
  480. addSongToPlaylist: isLoginRequired(async function addSongToPlaylist(session, isSet, songId, playlistId, cb) {
  481. const playlistModel = await DBModule.runJob(
  482. "GET_MODEL",
  483. {
  484. modelName: "playlist"
  485. },
  486. this
  487. );
  488. async.waterfall(
  489. [
  490. next => {
  491. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  492. .then(playlist => {
  493. if (!playlist || playlist.createdBy !== session.userId)
  494. return next("Something went wrong when trying to get the playlist");
  495. return async.each(
  496. playlist.songs,
  497. (song, next) => {
  498. if (song.songId === songId) return next("That song is already in the playlist");
  499. return next();
  500. },
  501. next
  502. );
  503. })
  504. .catch(next);
  505. },
  506. next => {
  507. SongsModule.runJob("GET_SONG", { id: songId }, this)
  508. .then(response => {
  509. const { song } = response;
  510. next(null, {
  511. _id: song._id,
  512. songId,
  513. title: song.title,
  514. duration: song.duration
  515. });
  516. })
  517. .catch(() => {
  518. YouTubeModule.runJob("GET_SONG", { songId }, this)
  519. .then(response => next(null, response.song))
  520. .catch(next);
  521. });
  522. },
  523. (newSong, next) => {
  524. playlistModel.updateOne(
  525. { _id: playlistId },
  526. { $push: { songs: newSong } },
  527. { runValidators: true },
  528. err => {
  529. if (err) return next(err);
  530. return PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  531. .then(playlist => next(null, playlist, newSong))
  532. .catch(next);
  533. }
  534. );
  535. }
  536. ],
  537. async (err, playlist, newSong) => {
  538. if (err) {
  539. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  540. this.log(
  541. "ERROR",
  542. "PLAYLIST_ADD_SONG",
  543. `Adding song "${songId}" to private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  544. );
  545. return cb({ status: "failure", message: err });
  546. }
  547. this.log(
  548. "SUCCESS",
  549. "PLAYLIST_ADD_SONG",
  550. `Successfully added song "${songId}" to private playlist "${playlistId}" for user "${session.userId}".`
  551. );
  552. if (!isSet)
  553. ActivitiesModule.runJob("ADD_ACTIVITY", {
  554. userId: session.userId,
  555. activityType: "added_song_to_playlist",
  556. payload: [{ songId, playlistId }]
  557. });
  558. CacheModule.runJob("PUB", {
  559. channel: "playlist.addSong",
  560. value: {
  561. playlistId: playlist._id,
  562. song: newSong,
  563. userId: session.userId
  564. }
  565. });
  566. return cb({
  567. status: "success",
  568. message: "Song has been successfully added to the playlist",
  569. data: playlist.songs
  570. });
  571. }
  572. );
  573. }),
  574. /**
  575. * Adds a set of songs to a private playlist
  576. *
  577. * @param {object} session - the session object automatically added by socket.io
  578. * @param {string} url - the url of the the YouTube playlist
  579. * @param {string} playlistId - the id of the playlist we are adding the set of songs to
  580. * @param {boolean} musicOnly - whether to only add music to the playlist
  581. * @param {Function} cb - gets called with the result
  582. */
  583. addSetToPlaylist: isLoginRequired(function addSetToPlaylist(session, url, playlistId, musicOnly, cb) {
  584. let videosInPlaylistTotal = 0;
  585. let songsInPlaylistTotal = 0;
  586. let addSongsStats = null;
  587. const addedSongs = [];
  588. async.waterfall(
  589. [
  590. next => {
  591. YouTubeModule.runJob(
  592. "GET_PLAYLIST",
  593. {
  594. url,
  595. musicOnly
  596. },
  597. this
  598. ).then(response => {
  599. if (response.filteredSongs) {
  600. videosInPlaylistTotal = response.songs.length;
  601. songsInPlaylistTotal = response.filteredSongs.length;
  602. } else {
  603. songsInPlaylistTotal = videosInPlaylistTotal = response.songs.length;
  604. }
  605. next(null, response.songs);
  606. });
  607. },
  608. (songIds, next) => {
  609. let successful = 0;
  610. let failed = 0;
  611. let alreadyInPlaylist = 0;
  612. if (songIds.length === 0) next();
  613. async.eachLimit(
  614. songIds,
  615. 1,
  616. (songId, next) => {
  617. IOModule.runJob(
  618. "RUN_ACTION2",
  619. {
  620. session,
  621. namespace: "playlists",
  622. action: "addSongToPlaylist",
  623. args: [true, songId, playlistId]
  624. },
  625. this
  626. )
  627. .then(res => {
  628. if (res.status === "success") {
  629. successful += 1;
  630. addedSongs.push(songId);
  631. } else failed += 1;
  632. if (res.message === "That song is already in the playlist") alreadyInPlaylist += 1;
  633. })
  634. .catch(() => {
  635. failed += 1;
  636. })
  637. .finally(() => {
  638. next();
  639. });
  640. },
  641. () => {
  642. addSongsStats = { successful, failed, alreadyInPlaylist };
  643. next(null);
  644. }
  645. );
  646. },
  647. next => {
  648. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  649. .then(playlist => {
  650. next(null, playlist);
  651. })
  652. .catch(next);
  653. },
  654. (playlist, next) => {
  655. if (!playlist || playlist.createdBy !== session.userId) return next("Playlist not found.");
  656. if (!playlist.isUserModifiable) return next("Playlist cannot be modified.");
  657. return next(null, playlist);
  658. }
  659. ],
  660. async (err, playlist) => {
  661. if (err) {
  662. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  663. this.log(
  664. "ERROR",
  665. "PLAYLIST_IMPORT",
  666. `Importing a YouTube playlist to private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  667. );
  668. return cb({ status: "failure", message: err });
  669. }
  670. ActivitiesModule.runJob("ADD_ACTIVITY", {
  671. userId: session.userId,
  672. activityType: "added_songs_to_playlist",
  673. payload: addedSongs
  674. });
  675. this.log(
  676. "SUCCESS",
  677. "PLAYLIST_IMPORT",
  678. `Successfully imported a YouTube playlist to private playlist "${playlistId}" for user "${session.userId}". Videos in playlist: ${videosInPlaylistTotal}, songs in playlist: ${songsInPlaylistTotal}, songs successfully added: ${addSongsStats.successful}, songs failed: ${addSongsStats.failed}, already in playlist: ${addSongsStats.alreadyInPlaylist}.`
  679. );
  680. return cb({
  681. status: "success",
  682. message: `Playlist has been imported. ${addSongsStats.successful} were added successfully, ${addSongsStats.failed} failed (${addSongsStats.alreadyInPlaylist} were already in the playlist)`,
  683. data: playlist.songs,
  684. stats: {
  685. videosInPlaylistTotal,
  686. songsInPlaylistTotal
  687. }
  688. });
  689. }
  690. );
  691. }),
  692. /**
  693. * Removes a song from a private playlist
  694. *
  695. * @param {object} session - the session object automatically added by socket.io
  696. * @param {string} songId - the id of the song we are removing from the private playlist
  697. * @param {string} playlistId - the id of the playlist we are removing the song from
  698. * @param {Function} cb - gets called with the result
  699. */
  700. removeSongFromPlaylist: isLoginRequired(async function removeSongFromPlaylist(session, songId, playlistId, cb) {
  701. const playlistModel = await DBModule.runJob(
  702. "GET_MODEL",
  703. {
  704. modelName: "playlist"
  705. },
  706. this
  707. );
  708. async.waterfall(
  709. [
  710. next => {
  711. if (!songId || typeof songId !== "string") return next("Invalid song id.");
  712. if (!playlistId) return next("Invalid playlist id.");
  713. return next();
  714. },
  715. next => {
  716. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  717. .then(playlist => {
  718. next(null, playlist);
  719. })
  720. .catch(next);
  721. },
  722. (playlist, next) => {
  723. if (!playlist || playlist.createdBy !== session.userId) return next("Playlist not found");
  724. return playlistModel.updateOne({ _id: playlistId }, { $pull: { songs: { songId } } }, next);
  725. },
  726. (res, next) => {
  727. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  728. .then(playlist => {
  729. next(null, playlist);
  730. })
  731. .catch(next);
  732. }
  733. ],
  734. async (err, playlist) => {
  735. if (err) {
  736. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  737. this.log(
  738. "ERROR",
  739. "PLAYLIST_REMOVE_SONG",
  740. `Removing song "${songId}" from private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  741. );
  742. return cb({ status: "failure", message: err });
  743. }
  744. this.log(
  745. "SUCCESS",
  746. "PLAYLIST_REMOVE_SONG",
  747. `Successfully removed song "${songId}" from private playlist "${playlistId}" for user "${session.userId}".`
  748. );
  749. CacheModule.runJob("PUB", {
  750. channel: "playlist.removeSong",
  751. value: {
  752. playlistId: playlist._id,
  753. songId,
  754. userId: session.userId
  755. }
  756. });
  757. return cb({
  758. status: "success",
  759. message: "Song has been successfully removed from playlist",
  760. data: playlist.songs
  761. });
  762. }
  763. );
  764. }),
  765. /**
  766. * Updates the displayName of a private playlist
  767. *
  768. * @param {object} session - the session object automatically added by socket.io
  769. * @param {string} playlistId - the id of the playlist we are updating the displayName for
  770. * @param {Function} cb - gets called with the result
  771. */
  772. updateDisplayName: isLoginRequired(async function updateDisplayName(session, playlistId, displayName, cb) {
  773. const playlistModel = await DBModule.runJob(
  774. "GET_MODEL",
  775. {
  776. modelName: "playlist"
  777. },
  778. this
  779. );
  780. async.waterfall(
  781. [
  782. next => {
  783. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  784. .then(playlist => next(null, playlist))
  785. .catch(next);
  786. },
  787. (playlist, next) => {
  788. if (!playlist.isUserModifiable) return next("Playlist cannot be modified.");
  789. return next(null);
  790. },
  791. next => {
  792. playlistModel.updateOne(
  793. { _id: playlistId, createdBy: session.userId },
  794. { $set: { displayName } },
  795. { runValidators: true },
  796. next
  797. );
  798. },
  799. (res, next) => {
  800. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  801. .then(playlist => {
  802. next(null, playlist);
  803. })
  804. .catch(next);
  805. }
  806. ],
  807. async err => {
  808. if (err) {
  809. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  810. this.log(
  811. "ERROR",
  812. "PLAYLIST_UPDATE_DISPLAY_NAME",
  813. `Updating display name to "${displayName}" for private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  814. );
  815. return cb({ status: "failure", message: err });
  816. }
  817. this.log(
  818. "SUCCESS",
  819. "PLAYLIST_UPDATE_DISPLAY_NAME",
  820. `Successfully updated display name to "${displayName}" for private playlist "${playlistId}" for user "${session.userId}".`
  821. );
  822. CacheModule.runJob("PUB", {
  823. channel: "playlist.updateDisplayName",
  824. value: {
  825. playlistId,
  826. displayName,
  827. userId: session.userId
  828. }
  829. });
  830. return cb({
  831. status: "success",
  832. message: "Playlist has been successfully updated"
  833. });
  834. }
  835. );
  836. }),
  837. /**
  838. * Moves a song to the top of the list in a private playlist
  839. *
  840. * @param {object} session - the session object automatically added by socket.io
  841. * @param {string} playlistId - the id of the playlist we are moving the song to the top from
  842. * @param {string} songId - the id of the song we are moving to the top of the list
  843. * @param {Function} cb - gets called with the result
  844. */
  845. moveSongToTop: isLoginRequired(async function moveSongToTop(session, playlistId, songId, cb) {
  846. const playlistModel = await DBModule.runJob(
  847. "GET_MODEL",
  848. {
  849. modelName: "playlist"
  850. },
  851. this
  852. );
  853. async.waterfall(
  854. [
  855. next => {
  856. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  857. .then(playlist => {
  858. next(null, playlist);
  859. })
  860. .catch(next);
  861. },
  862. (playlist, next) => {
  863. if (!playlist || playlist.createdBy !== session.userId) return next("Playlist not found");
  864. if (!playlist.isUserModifiable) return next("Playlist cannot be modified.");
  865. return async.each(
  866. playlist.songs,
  867. (song, next) => {
  868. if (song.songId === songId) return next(song);
  869. return next();
  870. },
  871. err => {
  872. if (err && err.songId) return next(null, err);
  873. return next("Song not found");
  874. }
  875. );
  876. },
  877. (song, next) => {
  878. playlistModel.updateOne({ _id: playlistId }, { $pull: { songs: { songId } } }, err => {
  879. if (err) return next(err);
  880. return next(null, song);
  881. });
  882. },
  883. (song, next) => {
  884. playlistModel.updateOne(
  885. { _id: playlistId },
  886. {
  887. $push: {
  888. songs: {
  889. $each: [song],
  890. $position: 0
  891. }
  892. }
  893. },
  894. next
  895. );
  896. },
  897. (res, next) => {
  898. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  899. .then(playlist => {
  900. next(null, playlist);
  901. })
  902. .catch(next);
  903. }
  904. ],
  905. async err => {
  906. if (err) {
  907. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  908. this.log(
  909. "ERROR",
  910. "PLAYLIST_MOVE_SONG_TO_TOP",
  911. `Moving song "${songId}" to the top for private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  912. );
  913. return cb({ status: "failure", message: err });
  914. }
  915. this.log(
  916. "SUCCESS",
  917. "PLAYLIST_MOVE_SONG_TO_TOP",
  918. `Successfully moved song "${songId}" to the top for private playlist "${playlistId}" for user "${session.userId}".`
  919. );
  920. CacheModule.runJob("PUB", {
  921. channel: "playlist.moveSongToTop",
  922. value: {
  923. playlistId,
  924. songId,
  925. userId: session.userId
  926. }
  927. });
  928. return cb({
  929. status: "success",
  930. message: "Playlist has been successfully updated"
  931. });
  932. }
  933. );
  934. }),
  935. /**
  936. * Moves a song to the bottom of the list in a private playlist
  937. *
  938. * @param {object} session - the session object automatically added by socket.io
  939. * @param {string} playlistId - the id of the playlist we are moving the song to the bottom from
  940. * @param {string} songId - the id of the song we are moving to the bottom of the list
  941. * @param {Function} cb - gets called with the result
  942. */
  943. moveSongToBottom: isLoginRequired(async function moveSongToBottom(session, playlistId, songId, cb) {
  944. const playlistModel = await DBModule.runJob(
  945. "GET_MODEL",
  946. {
  947. modelName: "playlist"
  948. },
  949. this
  950. );
  951. async.waterfall(
  952. [
  953. next => {
  954. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  955. .then(playlist => {
  956. next(null, playlist);
  957. })
  958. .catch(next);
  959. },
  960. (playlist, next) => {
  961. if (!playlist || playlist.createdBy !== session.userId) return next("Playlist not found");
  962. if (!playlist.isUserModifiable) return next("Playlist cannot be modified.");
  963. return async.each(
  964. playlist.songs,
  965. (song, next) => {
  966. if (song.songId === songId) return next(song);
  967. return next();
  968. },
  969. err => {
  970. if (err && err.songId) return next(null, err);
  971. return next("Song not found");
  972. }
  973. );
  974. },
  975. (song, next) => {
  976. playlistModel.updateOne({ _id: playlistId }, { $pull: { songs: { songId } } }, err => {
  977. if (err) return next(err);
  978. return next(null, song);
  979. });
  980. },
  981. (song, next) => {
  982. playlistModel.updateOne(
  983. { _id: playlistId },
  984. {
  985. $push: {
  986. songs: song
  987. }
  988. },
  989. next
  990. );
  991. },
  992. (res, next) => {
  993. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  994. .then(playlist => {
  995. next(null, playlist);
  996. })
  997. .catch(next);
  998. }
  999. ],
  1000. async err => {
  1001. if (err) {
  1002. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1003. this.log(
  1004. "ERROR",
  1005. "PLAYLIST_MOVE_SONG_TO_BOTTOM",
  1006. `Moving song "${songId}" to the bottom for private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1007. );
  1008. return cb({ status: "failure", message: err });
  1009. }
  1010. this.log(
  1011. "SUCCESS",
  1012. "PLAYLIST_MOVE_SONG_TO_BOTTOM",
  1013. `Successfully moved song "${songId}" to the bottom for private playlist "${playlistId}" for user "${session.userId}".`
  1014. );
  1015. CacheModule.runJob("PUB", {
  1016. channel: "playlist.moveSongToBottom",
  1017. value: {
  1018. playlistId,
  1019. songId,
  1020. userId: session.userId
  1021. }
  1022. });
  1023. return cb({
  1024. status: "success",
  1025. message: "Playlist has been successfully updated"
  1026. });
  1027. }
  1028. );
  1029. }),
  1030. /**
  1031. * Removes a private playlist
  1032. *
  1033. * @param {object} session - the session object automatically added by socket.io
  1034. * @param {string} playlistId - the id of the playlist we are moving the song to the top from
  1035. * @param {Function} cb - gets called with the result
  1036. */
  1037. remove: isLoginRequired(async function remove(session, playlistId, cb) {
  1038. const stationModel = await DBModule.runJob(
  1039. "GET_MODEL",
  1040. {
  1041. modelName: "station"
  1042. },
  1043. this
  1044. );
  1045. async.waterfall(
  1046. [
  1047. next => {
  1048. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1049. .then(playlist => next(null, playlist))
  1050. .catch(next);
  1051. },
  1052. (playlist, next) => {
  1053. if (!playlist.isUserModifiable) return next("Playlist cannot be removed.");
  1054. return next(null);
  1055. },
  1056. next => {
  1057. PlaylistsModule.runJob("DELETE_PLAYLIST", { playlistId }, this).then(next).catch(next);
  1058. },
  1059. next => {
  1060. stationModel.find({ privatePlaylist: playlistId }, (err, res) => {
  1061. next(err, res);
  1062. });
  1063. },
  1064. (stations, next) => {
  1065. async.each(
  1066. stations,
  1067. (station, next) => {
  1068. async.waterfall(
  1069. [
  1070. next => {
  1071. stationModel.updateOne(
  1072. { _id: station._id },
  1073. { $set: { privatePlaylist: null } },
  1074. { runValidators: true },
  1075. next
  1076. );
  1077. },
  1078. (res, next) => {
  1079. if (!station.partyMode) {
  1080. moduleManager.modules.stations
  1081. .runJob(
  1082. "UPDATE_STATION",
  1083. {
  1084. stationId: station._id
  1085. },
  1086. this
  1087. )
  1088. .then(station => next(null, station))
  1089. .catch(next);
  1090. CacheModule.runJob("PUB", {
  1091. channel: "privatePlaylist.selected",
  1092. value: {
  1093. playlistId: null,
  1094. stationId: station._id
  1095. }
  1096. });
  1097. } else next();
  1098. }
  1099. ],
  1100. () => {
  1101. next();
  1102. }
  1103. );
  1104. },
  1105. () => {
  1106. next();
  1107. }
  1108. );
  1109. }
  1110. ],
  1111. async err => {
  1112. if (err) {
  1113. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1114. this.log(
  1115. "ERROR",
  1116. "PLAYLIST_REMOVE",
  1117. `Removing private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1118. );
  1119. return cb({ status: "failure", message: err });
  1120. }
  1121. this.log(
  1122. "SUCCESS",
  1123. "PLAYLIST_REMOVE",
  1124. `Successfully removed private playlist "${playlistId}" for user "${session.userId}".`
  1125. );
  1126. CacheModule.runJob("PUB", {
  1127. channel: "playlist.delete",
  1128. value: {
  1129. userId: session.userId,
  1130. playlistId
  1131. }
  1132. });
  1133. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1134. userId: session.userId,
  1135. activityType: "deleted_playlist",
  1136. payload: [playlistId]
  1137. });
  1138. return cb({
  1139. status: "success",
  1140. message: "Playlist successfully removed"
  1141. });
  1142. }
  1143. );
  1144. }),
  1145. /**
  1146. * Updates the privacy of a private playlist
  1147. *
  1148. * @param {object} session - the session object automatically added by socket.io
  1149. * @param {string} playlistId - the id of the playlist we are updating the displayName for
  1150. * @param {Function} cb - gets called with the result
  1151. */
  1152. updatePrivacy: isLoginRequired(async function updatePrivacy(session, playlistId, privacy, cb) {
  1153. const playlistModel = await DBModule.runJob(
  1154. "GET_MODEL",
  1155. {
  1156. modelName: "playlist"
  1157. },
  1158. this
  1159. );
  1160. async.waterfall(
  1161. [
  1162. next => {
  1163. playlistModel.updateOne(
  1164. { _id: playlistId, createdBy: session.userId },
  1165. { $set: { privacy } },
  1166. { runValidators: true },
  1167. next
  1168. );
  1169. },
  1170. (res, next) => {
  1171. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  1172. .then(playlist => {
  1173. next(null, playlist);
  1174. })
  1175. .catch(next);
  1176. }
  1177. ],
  1178. async err => {
  1179. if (err) {
  1180. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1181. this.log(
  1182. "ERROR",
  1183. "PLAYLIST_UPDATE_PRIVACY",
  1184. `Updating privacy to "${privacy}" for private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1185. );
  1186. return cb({ status: "failure", message: err });
  1187. }
  1188. this.log(
  1189. "SUCCESS",
  1190. "PLAYLIST_UPDATE_PRIVACY",
  1191. `Successfully updated privacy to "${privacy}" for private playlist "${playlistId}" for user "${session.userId}".`
  1192. );
  1193. CacheModule.runJob("PUB", {
  1194. channel: "playlist.updatePrivacy",
  1195. value: {
  1196. playlistId,
  1197. privacy,
  1198. userId: session.userId
  1199. }
  1200. });
  1201. return cb({
  1202. status: "success",
  1203. message: "Playlist has been successfully updated"
  1204. });
  1205. }
  1206. );
  1207. })
  1208. };