playlists.js 34 KB

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