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