playlists.js 29 KB

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