playlists.js 39 KB

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