playlists.js 39 KB

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