playlists.js 39 KB

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