playlists.js 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070
  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 WSModule = moduleManager.modules.ws;
  7. const SongsModule = moduleManager.modules.songs;
  8. const StationsModule = moduleManager.modules.stations;
  9. const CacheModule = moduleManager.modules.cache;
  10. const PlaylistsModule = moduleManager.modules.playlists;
  11. const YouTubeModule = moduleManager.modules.youtube;
  12. const ActivitiesModule = moduleManager.modules.activities;
  13. CacheModule.runJob("SUB", {
  14. channel: "playlist.create",
  15. cb: playlist => {
  16. WSModule.runJob("SOCKETS_FROM_USER", { userId: playlist.createdBy }, this).then(sockets => {
  17. sockets.forEach(socket => socket.dispatch("event:playlist.create", { data: { playlist } }));
  18. });
  19. if (playlist.privacy === "public")
  20. WSModule.runJob("EMIT_TO_ROOM", {
  21. room: `profile-${playlist.createdBy}-playlists`,
  22. args: ["event:playlist.create", { data: { playlist } }]
  23. });
  24. }
  25. });
  26. CacheModule.runJob("SUB", {
  27. channel: "playlist.delete",
  28. cb: res => {
  29. WSModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(sockets => {
  30. sockets.forEach(socket => {
  31. socket.dispatch("event:playlist.delete", { data: { playlistId: res.playlistId } });
  32. });
  33. });
  34. WSModule.runJob("EMIT_TO_ROOM", {
  35. room: `profile-${res.userId}-playlists`,
  36. args: ["event:playlist.delete", { data: { playlistId: res.playlistId } }]
  37. });
  38. }
  39. });
  40. CacheModule.runJob("SUB", {
  41. channel: "playlist.repositionSongs",
  42. cb: res => {
  43. WSModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(sockets =>
  44. sockets.forEach(socket =>
  45. socket.dispatch("event:playlist.repositionSongs", {
  46. data: {
  47. playlistId: res.playlistId,
  48. songsBeingChanged: res.songsBeingChanged
  49. }
  50. })
  51. )
  52. );
  53. }
  54. });
  55. CacheModule.runJob("SUB", {
  56. channel: "playlist.addSong",
  57. cb: res => {
  58. WSModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(sockets => {
  59. sockets.forEach(socket => {
  60. socket.dispatch("event:playlist.addSong", {
  61. data: {
  62. playlistId: res.playlistId,
  63. song: res.song
  64. }
  65. });
  66. });
  67. });
  68. if (res.privacy === "public")
  69. WSModule.runJob("EMIT_TO_ROOM", {
  70. room: `profile-${res.userId}-playlists`,
  71. args: [
  72. "event:playlist.addSong",
  73. {
  74. data: {
  75. playlistId: res.playlistId,
  76. song: res.song
  77. }
  78. }
  79. ]
  80. });
  81. }
  82. });
  83. CacheModule.runJob("SUB", {
  84. channel: "playlist.removeSong",
  85. cb: res => {
  86. WSModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(sockets => {
  87. sockets.forEach(socket => {
  88. socket.dispatch("event:playlist.removeSong", {
  89. data: {
  90. playlistId: res.playlistId,
  91. youtubeId: res.youtubeId
  92. }
  93. });
  94. });
  95. });
  96. if (res.privacy === "public")
  97. WSModule.runJob("EMIT_TO_ROOM", {
  98. room: `profile-${res.userId}-playlists`,
  99. args: [
  100. "event:playlist.removeSong",
  101. {
  102. data: {
  103. playlistId: res.playlistId,
  104. youtubeId: res.youtubeId
  105. }
  106. }
  107. ]
  108. });
  109. }
  110. });
  111. CacheModule.runJob("SUB", {
  112. channel: "playlist.updateDisplayName",
  113. cb: res => {
  114. WSModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(sockets => {
  115. sockets.forEach(socket => {
  116. socket.dispatch("event:playlist.updateDisplayName", {
  117. data: {
  118. playlistId: res.playlistId,
  119. displayName: res.displayName
  120. }
  121. });
  122. });
  123. });
  124. if (res.privacy === "public")
  125. WSModule.runJob("EMIT_TO_ROOM", {
  126. room: `profile-${res.userId}-playlists`,
  127. args: [
  128. "event:playlist.updateDisplayName",
  129. {
  130. data: {
  131. playlistId: res.playlistId,
  132. displayName: res.displayName
  133. }
  134. }
  135. ]
  136. });
  137. }
  138. });
  139. CacheModule.runJob("SUB", {
  140. channel: "playlist.updatePrivacy",
  141. cb: res => {
  142. WSModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(sockets => {
  143. sockets.forEach(socket => {
  144. socket.dispatch("event:playlist.updatePrivacy", {
  145. data: {
  146. playlist: res.playlist
  147. }
  148. });
  149. });
  150. });
  151. if (res.playlist.privacy === "public")
  152. return WSModule.runJob("EMIT_TO_ROOM", {
  153. room: `profile-${res.userId}-playlists`,
  154. args: [
  155. "event:playlist.create",
  156. {
  157. data: {
  158. playlist: res.playlist
  159. }
  160. }
  161. ]
  162. });
  163. return WSModule.runJob("EMIT_TO_ROOM", {
  164. room: `profile-${res.userId}-playlists`,
  165. args: [
  166. "event:playlist.delete",
  167. {
  168. data: {
  169. playlistId: res.playlist._id
  170. }
  171. }
  172. ]
  173. });
  174. }
  175. });
  176. export default {
  177. /**
  178. * Gets all playlists
  179. *
  180. * @param {object} session - the session object automatically added by the websocket
  181. * @param {Function} cb - gets called with the result
  182. */
  183. index: isAdminRequired(async function index(session, cb) {
  184. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  185. async.waterfall(
  186. [
  187. next => {
  188. playlistModel.find({}).sort({ createdAt: "desc" }).exec(next);
  189. }
  190. ],
  191. async (err, playlists) => {
  192. if (err) {
  193. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  194. this.log("ERROR", "PLAYLISTS_INDEX", `Indexing playlists failed. "${err}"`);
  195. return cb({ status: "error", message: err });
  196. }
  197. this.log("SUCCESS", "PLAYLISTS_INDEX", "Indexing playlists successful.");
  198. return cb({ status: "success", data: { playlists } });
  199. }
  200. );
  201. }),
  202. /**
  203. * Searches through all playlists that can be included in a community station
  204. *
  205. * @param {object} session - the session object automatically added by the websocket
  206. * @param {string} query - the query
  207. * @param {string} query - the page
  208. * @param {Function} cb - gets called with the result
  209. */
  210. searchCommunity: isLoginRequired(async function searchCommunity(session, query, page, cb) {
  211. async.waterfall(
  212. [
  213. next => {
  214. if ((!query && query !== "") || typeof query !== "string") next("Invalid query.");
  215. else next();
  216. },
  217. next => {
  218. PlaylistsModule.runJob("SEARCH", {
  219. query,
  220. includeUser: true,
  221. includeGenre: true,
  222. includeOwn: true,
  223. includeSongs: true,
  224. userId: session.userId,
  225. page
  226. })
  227. .then(response => {
  228. next(null, response.playlists);
  229. })
  230. .catch(err => {
  231. next(err);
  232. });
  233. }
  234. ],
  235. async (err, playlists) => {
  236. if (err) {
  237. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  238. this.log("ERROR", "PLAYLISTS_SEARCH_COMMUNITY", `Searching playlists failed. "${err}"`);
  239. return cb({ status: "error", message: err });
  240. }
  241. this.log("SUCCESS", "PLAYLISTS_SEARCH_COMMUNITY", "Searching playlists successful.");
  242. return cb({ status: "success", data: { playlists } });
  243. }
  244. );
  245. }),
  246. /**
  247. * Searches through all playlists that can be included in an official station
  248. *
  249. * @param {object} session - the session object automatically added by the websocket
  250. * @param {string} query - the query
  251. * @param {string} query - the page
  252. * @param {Function} cb - gets called with the result
  253. */
  254. searchOfficial: isAdminRequired(async function searchOfficial(session, query, page, cb) {
  255. async.waterfall(
  256. [
  257. next => {
  258. if ((!query && query !== "") || typeof query !== "string") next("Invalid query.");
  259. else next();
  260. },
  261. next => {
  262. PlaylistsModule.runJob("SEARCH", {
  263. query,
  264. includeGenre: true,
  265. includePrivate: true,
  266. includeSongs: true,
  267. page
  268. })
  269. .then(response => {
  270. next(null, response.playlists);
  271. })
  272. .catch(err => {
  273. next(err);
  274. });
  275. }
  276. ],
  277. async (err, playlists) => {
  278. if (err) {
  279. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  280. this.log("ERROR", "PLAYLISTS_SEARCH_OFFICIAL", `Searching playlists failed. "${err}"`);
  281. return cb({ status: "error", message: err });
  282. }
  283. this.log("SUCCESS", "PLAYLISTS_SEARCH_OFFICIAL", "Searching playlists successful.");
  284. return cb({ status: "success", data: { playlists } });
  285. }
  286. );
  287. }),
  288. /**
  289. * Gets the first song from a private playlist
  290. *
  291. * @param {object} session - the session object automatically added by the websocket
  292. * @param {string} playlistId - the id of the playlist we are getting the first song from
  293. * @param {Function} cb - gets called with the result
  294. */
  295. getFirstSong: isLoginRequired(function getFirstSong(session, playlistId, cb) {
  296. async.waterfall(
  297. [
  298. next => {
  299. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  300. .then(playlist => next(null, playlist))
  301. .catch(next);
  302. },
  303. (playlist, next) => {
  304. if (!playlist || playlist.createdBy !== session.userId) return next("Playlist not found.");
  305. playlist.songs.sort((a, b) => a.position - b.position);
  306. return next(null, playlist.songs[0]);
  307. }
  308. ],
  309. async (err, song) => {
  310. if (err) {
  311. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  312. this.log(
  313. "ERROR",
  314. "PLAYLIST_GET_FIRST_SONG",
  315. `Getting the first song of playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  316. );
  317. return cb({ status: "error", message: err });
  318. }
  319. this.log(
  320. "SUCCESS",
  321. "PLAYLIST_GET_FIRST_SONG",
  322. `Successfully got the first song of playlist "${playlistId}" for user "${session.userId}".`
  323. );
  324. return cb({
  325. status: "success",
  326. data: { song }
  327. });
  328. }
  329. );
  330. }),
  331. /**
  332. * Gets a list of all the playlists for a specific user
  333. *
  334. * @param {object} session - the session object automatically added by the websocket
  335. * @param {string} userId - the user id in question
  336. * @param {Function} cb - gets called with the result
  337. */
  338. indexForUser: async function indexForUser(session, userId, cb) {
  339. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  340. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  341. async.waterfall(
  342. [
  343. next => {
  344. userModel.findById(userId).select({ "preferences.orderOfPlaylists": -1 }).exec(next);
  345. },
  346. ({ preferences }, next) => {
  347. const { orderOfPlaylists } = preferences;
  348. const match = {
  349. createdBy: userId
  350. };
  351. // if a playlist order exists
  352. if (orderOfPlaylists > 0) match._id = { $in: orderOfPlaylists };
  353. playlistModel
  354. .aggregate()
  355. .match(match)
  356. .addFields({
  357. weight: { $indexOfArray: [orderOfPlaylists, "$_id"] }
  358. })
  359. .sort({ weight: 1 })
  360. .exec(next);
  361. },
  362. (playlists, next) => {
  363. if (session.userId === userId) return next(null, playlists); // user requesting playlists is the owner of the playlists
  364. const filteredPlaylists = [];
  365. return async.each(
  366. playlists,
  367. (playlist, nextPlaylist) => {
  368. if (playlist.privacy === "public") filteredPlaylists.push(playlist);
  369. return nextPlaylist();
  370. },
  371. () => next(null, filteredPlaylists)
  372. );
  373. }
  374. ],
  375. async (err, playlists) => {
  376. if (err) {
  377. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  378. this.log(
  379. "ERROR",
  380. "PLAYLIST_INDEX_FOR_USER",
  381. `Indexing playlists for user "${userId}" failed. "${err}"`
  382. );
  383. return cb({ status: "error", message: err });
  384. }
  385. this.log("SUCCESS", "PLAYLIST_INDEX_FOR_USER", `Successfully indexed playlists for user "${userId}".`);
  386. return cb({
  387. status: "success",
  388. data: { playlists }
  389. });
  390. }
  391. );
  392. },
  393. /**
  394. * Gets all playlists for the user requesting it
  395. *
  396. * @param {object} session - the session object automatically added by the websocket
  397. * @param {boolean} showNonModifiablePlaylists - whether or not to show non modifiable playlists e.g. liked songs
  398. * @param {Function} cb - gets called with the result
  399. */
  400. indexMyPlaylists: isLoginRequired(async function indexMyPlaylists(session, showNonModifiablePlaylists, cb) {
  401. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  402. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  403. async.waterfall(
  404. [
  405. next => {
  406. userModel.findById(session.userId).select({ "preferences.orderOfPlaylists": -1 }).exec(next);
  407. },
  408. ({ preferences }, next) => {
  409. const { orderOfPlaylists } = preferences;
  410. const match = {
  411. createdBy: session.userId
  412. };
  413. // if non modifiable playlists should be shown as well
  414. if (!showNonModifiablePlaylists) match.isUserModifiable = true;
  415. // if a playlist order exists
  416. if (orderOfPlaylists > 0) match._id = { $in: orderOfPlaylists };
  417. playlistModel
  418. .aggregate()
  419. .match(match)
  420. .addFields({
  421. weight: { $indexOfArray: [orderOfPlaylists, "$_id"] }
  422. })
  423. .sort({ weight: 1 })
  424. .exec(next);
  425. }
  426. ],
  427. async (err, playlists) => {
  428. if (err) {
  429. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  430. this.log(
  431. "ERROR",
  432. "PLAYLIST_INDEX_FOR_ME",
  433. `Indexing playlists for user "${session.userId}" failed. "${err}"`
  434. );
  435. return cb({ status: "error", message: err });
  436. }
  437. this.log(
  438. "SUCCESS",
  439. "PLAYLIST_INDEX_FOR_ME",
  440. `Successfully indexed playlists for user "${session.userId}".`
  441. );
  442. return cb({
  443. status: "success",
  444. data: { playlists }
  445. });
  446. }
  447. );
  448. }),
  449. /**
  450. * Creates a new private playlist
  451. *
  452. * @param {object} session - the session object automatically added by the websocket
  453. * @param {object} data - the data for the new private playlist
  454. * @param {Function} cb - gets called with the result
  455. */
  456. create: isLoginRequired(async function create(session, data, cb) {
  457. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  458. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  459. const blacklist = ["liked songs", "likedsongs", "disliked songs", "dislikedsongs"];
  460. async.waterfall(
  461. [
  462. next => (data ? next() : cb({ status: "error", message: "Invalid data" })),
  463. next => {
  464. const { displayName, songs, privacy } = data;
  465. if (blacklist.indexOf(displayName.toLowerCase()) !== -1)
  466. return next("That playlist name is blacklisted. Please use a different name.");
  467. return playlistModel.create(
  468. {
  469. displayName,
  470. songs,
  471. privacy,
  472. createdBy: session.userId,
  473. createdAt: Date.now(),
  474. createdFor: null,
  475. type: "user"
  476. },
  477. next
  478. );
  479. },
  480. (playlist, next) => {
  481. userModel.updateOne(
  482. { _id: session.userId },
  483. { $push: { "preferences.orderOfPlaylists": playlist._id } },
  484. err => {
  485. if (err) return next(err);
  486. return next(null, playlist);
  487. }
  488. );
  489. }
  490. ],
  491. async (err, playlist) => {
  492. if (err) {
  493. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  494. this.log(
  495. "ERROR",
  496. "PLAYLIST_CREATE",
  497. `Creating private playlist failed for user "${session.userId}". "${err}"`
  498. );
  499. return cb({ status: "error", message: err });
  500. }
  501. CacheModule.runJob("PUB", {
  502. channel: "playlist.create",
  503. value: playlist
  504. });
  505. ActivitiesModule.runJob("ADD_ACTIVITY", {
  506. userId: playlist.createdBy,
  507. type: "playlist__create",
  508. payload: {
  509. message: `Created playlist <playlistId>${playlist.displayName}</playlistId>`,
  510. playlistId: playlist._id
  511. }
  512. });
  513. this.log(
  514. "SUCCESS",
  515. "PLAYLIST_CREATE",
  516. `Successfully created private playlist for user "${session.userId}".`
  517. );
  518. return cb({
  519. status: "success",
  520. message: "Successfully created playlist",
  521. data: {
  522. playlistId: playlist._id
  523. }
  524. });
  525. }
  526. );
  527. }),
  528. /**
  529. * Gets a playlist from id
  530. *
  531. * @param {object} session - the session object automatically added by the websocket
  532. * @param {string} playlistId - the id of the playlist we are getting
  533. * @param {Function} cb - gets called with the result
  534. */
  535. getPlaylist: function getPlaylist(session, playlistId, cb) {
  536. async.waterfall(
  537. [
  538. next => {
  539. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  540. .then(playlist => next(null, playlist))
  541. .catch(next);
  542. },
  543. (playlist, next) => {
  544. if (!playlist) return next("Playlist not found");
  545. if (playlist.privacy !== "public" && playlist.createdBy !== session.userId) {
  546. if (session)
  547. // check if user requested to get a playlist is an admin
  548. return DBModule.runJob("GET_MODEL", { modelName: "user" }, this).then(userModel => {
  549. userModel.findOne({ _id: session.userId }, (err, user) => {
  550. if (user && user.role === "admin") return next(null, playlist);
  551. return next("User unauthorised to view playlist.");
  552. });
  553. });
  554. return next("User unauthorised to view playlist.");
  555. }
  556. return next(null, playlist);
  557. }
  558. ],
  559. async (err, playlist) => {
  560. if (err) {
  561. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  562. this.log(
  563. "ERROR",
  564. "PLAYLIST_GET",
  565. `Getting private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  566. );
  567. return cb({ status: "error", message: err });
  568. }
  569. this.log(
  570. "SUCCESS",
  571. "PLAYLIST_GET",
  572. `Successfully got private playlist "${playlistId}" for user "${session.userId}".`
  573. );
  574. return cb({
  575. status: "success",
  576. data: { playlist }
  577. });
  578. }
  579. );
  580. },
  581. /**
  582. * Obtains basic metadata of a playlist in order to format an activity
  583. *
  584. * @param {object} session - the session object automatically added by the websocket
  585. * @param {string} playlistId - the playlist id
  586. * @param {Function} cb - callback
  587. */
  588. getPlaylistForActivity(session, playlistId, cb) {
  589. async.waterfall(
  590. [
  591. next => {
  592. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  593. .then(playlist => {
  594. next(null, playlist);
  595. })
  596. .catch(next);
  597. }
  598. ],
  599. async (err, playlist) => {
  600. if (err) {
  601. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  602. this.log(
  603. "ERROR",
  604. "PLAYLISTS_GET_PLAYLIST_FOR_ACTIVITY",
  605. `Failed to obtain metadata of playlist ${playlistId} for activity formatting. "${err}"`
  606. );
  607. return cb({ status: "error", message: err });
  608. }
  609. this.log(
  610. "SUCCESS",
  611. "PLAYLISTS_GET_PLAYLIST_FOR_ACTIVITY",
  612. `Obtained metadata of playlist ${playlistId} for activity formatting successfully.`
  613. );
  614. return cb({
  615. status: "success",
  616. data: {
  617. title: playlist.displayName
  618. }
  619. });
  620. }
  621. );
  622. },
  623. /**
  624. * Gets a playlist from station id
  625. *
  626. * @param {object} session - the session object automatically added by the websocket
  627. * @param {string} stationId - the id of the station we are getting
  628. * @param {string} includeSongs - include songs
  629. * @param {Function} cb - gets called with the result
  630. */
  631. getPlaylistForStation: function getPlaylist(session, stationId, includeSongs, cb) {
  632. async.waterfall(
  633. [
  634. next => {
  635. PlaylistsModule.runJob("GET_STATION_PLAYLIST", { stationId, includeSongs }, this)
  636. .then(response => next(null, response.playlist))
  637. .catch(next);
  638. },
  639. (playlist, next) => {
  640. if (!playlist) return next("Playlist not found");
  641. if (playlist.privacy !== "public" && playlist.createdBy !== session.userId) {
  642. if (session)
  643. // check if user requested to get a playlist is an admin
  644. return DBModule.runJob("GET_MODEL", { modelName: "user" }, this).then(userModel => {
  645. userModel.findOne({ _id: session.userId }, (err, user) => {
  646. if (user && user.role === "admin") return next(null, playlist);
  647. return next("User unauthorised to view playlist.");
  648. });
  649. });
  650. return next("User unauthorised to view playlist.");
  651. }
  652. return next(null, playlist);
  653. }
  654. ],
  655. async (err, playlist) => {
  656. if (err) {
  657. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  658. this.log(
  659. "ERROR",
  660. "PLAYLIST_GET",
  661. `Getting playlist for station "${stationId}" failed for user "${session.userId}". "${err}"`
  662. );
  663. return cb({ status: "error", message: err });
  664. }
  665. this.log(
  666. "SUCCESS",
  667. "PLAYLIST_GET",
  668. `Successfully got playlist for station "${stationId}" for user "${session.userId}".`
  669. );
  670. return cb({
  671. status: "success",
  672. data: { playlist }
  673. });
  674. }
  675. );
  676. },
  677. // TODO Remove this
  678. /**
  679. * Updates a private playlist
  680. *
  681. * @param {object} session - the session object automatically added by the websocket
  682. * @param {string} playlistId - the id of the playlist we are updating
  683. * @param {object} playlist - the new private playlist object
  684. * @param {Function} cb - gets called with the result
  685. */
  686. update: isLoginRequired(async function update(session, playlistId, playlist, cb) {
  687. const playlistModel = await DBModule.runJob(
  688. "GET_MODEL",
  689. {
  690. modelName: "playlist"
  691. },
  692. this
  693. );
  694. async.waterfall(
  695. [
  696. next => {
  697. playlistModel.updateOne(
  698. { _id: playlistId, createdBy: session.userId },
  699. playlist,
  700. { runValidators: true },
  701. next
  702. );
  703. },
  704. (res, next) => {
  705. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  706. .then(playlist => {
  707. next(null, playlist);
  708. })
  709. .catch(next);
  710. }
  711. ],
  712. async (err, playlist) => {
  713. if (err) {
  714. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  715. this.log(
  716. "ERROR",
  717. "PLAYLIST_UPDATE",
  718. `Updating private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  719. );
  720. return cb({ status: "error", message: err });
  721. }
  722. this.log(
  723. "SUCCESS",
  724. "PLAYLIST_UPDATE",
  725. `Successfully updated private playlist "${playlistId}" for user "${session.userId}".`
  726. );
  727. return cb({
  728. status: "success",
  729. data: { playlist }
  730. });
  731. }
  732. );
  733. }),
  734. /**
  735. * Shuffles songs in a private playlist
  736. *
  737. * @param {object} session - the session object automatically added by the websocket
  738. * @param {string} playlistId - the id of the playlist we are updating
  739. * @param {Function} cb - gets called with the result
  740. */
  741. shuffle: isLoginRequired(async function shuffle(session, playlistId, cb) {
  742. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  743. async.waterfall(
  744. [
  745. next => {
  746. if (!playlistId) return next("No playlist id.");
  747. return playlistModel.findById(playlistId, next);
  748. },
  749. (playlist, next) => {
  750. if (!playlist.isUserModifiable) return next("Playlist cannot be shuffled.");
  751. return UtilsModule.runJob("SHUFFLE_SONG_POSITIONS", { array: playlist.songs }, this)
  752. .then(result => next(null, result.array))
  753. .catch(next);
  754. },
  755. (songs, next) => {
  756. playlistModel.updateOne({ _id: playlistId }, { $set: { songs } }, { runValidators: true }, next);
  757. },
  758. (res, next) => {
  759. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  760. .then(playlist => next(null, playlist))
  761. .catch(next);
  762. }
  763. ],
  764. async (err, playlist) => {
  765. if (err) {
  766. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  767. this.log(
  768. "ERROR",
  769. "PLAYLIST_SHUFFLE",
  770. `Updating private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  771. );
  772. return cb({ status: "error", message: err });
  773. }
  774. this.log(
  775. "SUCCESS",
  776. "PLAYLIST_SHUFFLE",
  777. `Successfully updated private playlist "${playlistId}" for user "${session.userId}".`
  778. );
  779. return cb({
  780. status: "success",
  781. message: "Successfully shuffled playlist.",
  782. data: { playlist }
  783. });
  784. }
  785. );
  786. }),
  787. /**
  788. * Changes the order of song(s) in a private playlist
  789. *
  790. * @param {object} session - the session object automatically added by the websocket
  791. * @param {string} playlistId - the id of the playlist we are targeting
  792. * @param {Array} songsBeingChanged - the songs to be repositioned, each element contains "youtubeId" and "position" properties
  793. * @param {Function} cb - gets called with the result
  794. */
  795. repositionSongs: isLoginRequired(async function repositionSongs(session, playlistId, songsBeingChanged, cb) {
  796. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  797. async.waterfall(
  798. [
  799. // update playlist object with each song's new position
  800. next =>
  801. async.each(
  802. songsBeingChanged,
  803. (song, nextSong) =>
  804. playlistModel.updateOne(
  805. { _id: playlistId, "songs.youtubeId": song.youtubeId },
  806. {
  807. $set: {
  808. "songs.$.position": song.position
  809. }
  810. },
  811. err => {
  812. if (err) return next(err);
  813. return nextSong();
  814. }
  815. ),
  816. next
  817. ),
  818. // update the cache with the new songs positioning
  819. next => {
  820. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  821. .then(playlist => next(null, playlist))
  822. .catch(next);
  823. }
  824. ],
  825. async err => {
  826. if (err) {
  827. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  828. this.log(
  829. "ERROR",
  830. "PLAYLIST_REPOSITION_SONGS",
  831. `Repositioning songs for private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  832. );
  833. return cb({ status: "error", message: err });
  834. }
  835. this.log(
  836. "SUCCESS",
  837. "PLAYLIST_REPOSITION_SONGS",
  838. `Successfully repositioned songs for private playlist "${playlistId}" for user "${session.userId}".`
  839. );
  840. CacheModule.runJob("PUB", {
  841. channel: "playlist.repositionSongs",
  842. value: {
  843. userId: session.userId,
  844. playlistId,
  845. songsBeingChanged
  846. }
  847. });
  848. return cb({
  849. status: "success",
  850. message: "Order of songs successfully updated"
  851. });
  852. }
  853. );
  854. }),
  855. /**
  856. * Moves a song to the bottom of the list in a private playlist
  857. *
  858. * @param {object} session - the session object automatically added by the websocket
  859. * @param {string} playlistId - the id of the playlist we are moving the song to the bottom from
  860. * @param {string} youtubeId - the youtube id of the song we are moving to the bottom of the list
  861. * @param {Function} cb - gets called with the result
  862. */
  863. moveSongToBottom: isLoginRequired(async function moveSongToBottom(session, playlistId, youtubeId, cb) {
  864. async.waterfall(
  865. [
  866. next => {
  867. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  868. .then(playlist => next(null, playlist))
  869. .catch(next);
  870. },
  871. (playlist, next) => {
  872. if (!playlist || playlist.createdBy !== session.userId) return next("Playlist not found");
  873. if (!playlist.isUserModifiable) return next("Playlist cannot be modified.");
  874. // sort array by position
  875. playlist.songs.sort((a, b) => a.position - b.position);
  876. // find index of youtubeId
  877. playlist.songs.forEach((song, index) => {
  878. // reorder array (simulates what would be done with a drag and drop interface)
  879. if (song.youtubeId === youtubeId)
  880. playlist.songs.splice(playlist.songs.length, 0, playlist.songs.splice(index, 1)[0]);
  881. });
  882. const songsBeingChanged = [];
  883. playlist.songs.forEach((song, index) => {
  884. // check if position needs updated based on index
  885. if (song.position !== index + 1)
  886. songsBeingChanged.push({
  887. youtubeId: song.youtubeId,
  888. position: index + 1
  889. });
  890. });
  891. // update position property on songs that need to be changed
  892. return WSModule.runJob(
  893. "RUN_ACTION2",
  894. {
  895. session,
  896. namespace: "playlists",
  897. action: "repositionSongs",
  898. args: [playlistId, songsBeingChanged]
  899. },
  900. this
  901. )
  902. .then(res => {
  903. if (res.status === "success") return next();
  904. return next("Unable to reposition song in playlist.");
  905. })
  906. .catch(next);
  907. }
  908. ],
  909. async err => {
  910. if (err) {
  911. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  912. this.log(
  913. "ERROR",
  914. "PLAYLIST_MOVE_SONG_TO_BOTTOM",
  915. `Moving song "${youtubeId}" to the bottom for private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  916. );
  917. return cb({ status: "error", message: err });
  918. }
  919. this.log(
  920. "SUCCESS",
  921. "PLAYLIST_MOVE_SONG_TO_BOTTOM",
  922. `Successfully moved song "${youtubeId}" to the bottom for private playlist "${playlistId}" for user "${session.userId}".`
  923. );
  924. return cb({
  925. status: "success",
  926. message: "Order of songs successfully updated"
  927. });
  928. }
  929. );
  930. }),
  931. /**
  932. * Adds a song to a private playlist
  933. *
  934. * @param {object} session - the session object automatically added by the websocket
  935. * @param {boolean} isSet - is the song part of a set of songs to be added
  936. * @param {string} youtubeId - the youtube id of the song we are trying to add
  937. * @param {string} playlistId - the id of the playlist we are adding the song to
  938. * @param {Function} cb - gets called with the result
  939. */
  940. addSongToPlaylist: isLoginRequired(async function addSongToPlaylist(session, isSet, youtubeId, playlistId, cb) {
  941. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  942. async.waterfall(
  943. [
  944. next => {
  945. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  946. .then(playlist => {
  947. if (!playlist || playlist.createdBy !== session.userId)
  948. return next("Something went wrong when trying to get the playlist");
  949. return async.each(
  950. playlist.songs,
  951. (song, nextSong) => {
  952. if (song.youtubeId === youtubeId)
  953. return next("That song is already in the playlist");
  954. return nextSong();
  955. },
  956. err => next(err)
  957. );
  958. })
  959. .catch(next);
  960. },
  961. next => {
  962. DBModule.runJob("GET_MODEL", { modelName: "user" }, this)
  963. .then(UserModel => {
  964. UserModel.findOne(
  965. { _id: session.userId },
  966. { "preferences.anonymousSongRequests": 1 },
  967. next
  968. );
  969. })
  970. .catch(next);
  971. },
  972. (user, next) => {
  973. SongsModule.runJob(
  974. "ENSURE_SONG_EXISTS_BY_YOUTUBE_ID",
  975. {
  976. youtubeId,
  977. userId: user.preferences.anonymousSongRequests ? null : session.userId,
  978. automaticallyRequested: true
  979. },
  980. this
  981. )
  982. .then(response => {
  983. const { song } = response;
  984. const { _id, title, thumbnail, duration, status } = song;
  985. next(null, {
  986. _id,
  987. youtubeId,
  988. title,
  989. thumbnail,
  990. duration,
  991. status
  992. });
  993. })
  994. .catch(next);
  995. },
  996. (newSong, next) => {
  997. playlistModel.updateOne(
  998. { _id: playlistId },
  999. { $push: { songs: newSong } },
  1000. { runValidators: true },
  1001. err => {
  1002. if (err) return next(err);
  1003. return PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  1004. .then(playlist => next(null, playlist, newSong))
  1005. .catch(next);
  1006. }
  1007. );
  1008. }
  1009. ],
  1010. async (err, playlist, newSong) => {
  1011. if (err) {
  1012. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1013. this.log(
  1014. "ERROR",
  1015. "PLAYLIST_ADD_SONG",
  1016. `Adding song "${youtubeId}" to private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1017. );
  1018. return cb({ status: "error", message: err });
  1019. }
  1020. this.log(
  1021. "SUCCESS",
  1022. "PLAYLIST_ADD_SONG",
  1023. `Successfully added song "${youtubeId}" to private playlist "${playlistId}" for user "${session.userId}".`
  1024. );
  1025. if (!isSet && playlist.displayName !== "Liked Songs" && playlist.displayName !== "Disliked Songs") {
  1026. const songName = newSong.artists
  1027. ? `${newSong.title} by ${newSong.artists.join(", ")}`
  1028. : newSong.title;
  1029. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1030. userId: session.userId,
  1031. type: "playlist__add_song",
  1032. payload: {
  1033. message: `Added <youtubeId>${songName}</youtubeId> to playlist <playlistId>${playlist.displayName}</playlistId>`,
  1034. thumbnail: newSong.thumbnail,
  1035. playlistId,
  1036. youtubeId
  1037. }
  1038. });
  1039. }
  1040. StationsModule.runJob("GET_STATIONS_THAT_INCLUDE_OR_EXCLUDE_PLAYLIST", { playlistId })
  1041. .then(response => {
  1042. response.stationIds.forEach(stationId => {
  1043. PlaylistsModule.runJob("AUTOFILL_STATION_PLAYLIST", { stationId }).then().catch();
  1044. });
  1045. })
  1046. .catch();
  1047. CacheModule.runJob("PUB", {
  1048. channel: "playlist.addSong",
  1049. value: {
  1050. playlistId: playlist._id,
  1051. song: newSong,
  1052. userId: session.userId,
  1053. privacy: playlist.privacy
  1054. }
  1055. });
  1056. return cb({
  1057. status: "success",
  1058. message: "Song has been successfully added to the playlist",
  1059. data: { songs: playlist.songs }
  1060. });
  1061. }
  1062. );
  1063. }),
  1064. /**
  1065. * Adds a set of songs to a private playlist
  1066. *
  1067. * @param {object} session - the session object automatically added by the websocket
  1068. * @param {string} url - the url of the the YouTube playlist
  1069. * @param {string} playlistId - the id of the playlist we are adding the set of songs to
  1070. * @param {boolean} musicOnly - whether to only add music to the playlist
  1071. * @param {Function} cb - gets called with the result
  1072. */
  1073. addSetToPlaylist: isLoginRequired(function addSetToPlaylist(session, url, playlistId, musicOnly, cb) {
  1074. let videosInPlaylistTotal = 0;
  1075. let songsInPlaylistTotal = 0;
  1076. let addSongsStats = null;
  1077. const addedSongs = [];
  1078. async.waterfall(
  1079. [
  1080. next => {
  1081. YouTubeModule.runJob("GET_PLAYLIST", { url, musicOnly }, this)
  1082. .then(res => {
  1083. if (res.filteredSongs) {
  1084. videosInPlaylistTotal = res.songs.length;
  1085. songsInPlaylistTotal = res.filteredSongs.length;
  1086. } else {
  1087. songsInPlaylistTotal = videosInPlaylistTotal = res.songs.length;
  1088. }
  1089. next(null, res.songs);
  1090. })
  1091. .catch(err => {
  1092. next(err);
  1093. });
  1094. },
  1095. (youtubeIds, next) => {
  1096. let successful = 0;
  1097. let failed = 0;
  1098. let alreadyInPlaylist = 0;
  1099. if (youtubeIds.length === 0) next();
  1100. console.log(youtubeIds);
  1101. async.eachLimit(
  1102. youtubeIds,
  1103. 1,
  1104. (youtubeId, next) => {
  1105. WSModule.runJob(
  1106. "RUN_ACTION2",
  1107. {
  1108. session,
  1109. namespace: "playlists",
  1110. action: "addSongToPlaylist",
  1111. args: [true, youtubeId, playlistId]
  1112. },
  1113. this
  1114. )
  1115. .then(res => {
  1116. if (res.status === "success") {
  1117. successful += 1;
  1118. addedSongs.push(youtubeId);
  1119. } else failed += 1;
  1120. if (res.message === "That song is already in the playlist") alreadyInPlaylist += 1;
  1121. })
  1122. .catch(() => {
  1123. failed += 1;
  1124. })
  1125. .finally(() => next());
  1126. },
  1127. () => {
  1128. addSongsStats = { successful, failed, alreadyInPlaylist };
  1129. next(null);
  1130. }
  1131. );
  1132. },
  1133. next => {
  1134. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1135. .then(playlist => next(null, playlist))
  1136. .catch(next);
  1137. },
  1138. (playlist, next) => {
  1139. if (!playlist || playlist.createdBy !== session.userId) return next("Playlist not found.");
  1140. if (!playlist.isUserModifiable) return next("Playlist cannot be modified.");
  1141. return next(null, playlist);
  1142. }
  1143. ],
  1144. async (err, playlist) => {
  1145. if (err) {
  1146. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1147. this.log(
  1148. "ERROR",
  1149. "PLAYLIST_IMPORT",
  1150. `Importing a YouTube playlist to private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1151. );
  1152. return cb({ status: "error", message: err });
  1153. }
  1154. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1155. userId: session.userId,
  1156. type: "playlist__import_playlist",
  1157. payload: {
  1158. message: `Imported ${addSongsStats.successful} songs to playlist <playlistId>${playlist.displayName}</playlistId>`,
  1159. playlistId
  1160. }
  1161. });
  1162. this.log(
  1163. "SUCCESS",
  1164. "PLAYLIST_IMPORT",
  1165. `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}.`
  1166. );
  1167. return cb({
  1168. status: "success",
  1169. message: `Playlist has been imported. ${addSongsStats.successful} were added successfully, ${addSongsStats.failed} failed (${addSongsStats.alreadyInPlaylist} were already in the playlist)`,
  1170. data: {
  1171. songs: playlist.songs,
  1172. stats: {
  1173. videosInPlaylistTotal,
  1174. songsInPlaylistTotal
  1175. }
  1176. }
  1177. });
  1178. }
  1179. );
  1180. }),
  1181. /**
  1182. * Removes a song from a private playlist
  1183. *
  1184. * @param {object} session - the session object automatically added by the websocket
  1185. * @param {string} youtubeId - the youtube id of the song we are removing from the private playlist
  1186. * @param {string} playlistId - the id of the playlist we are removing the song from
  1187. * @param {Function} cb - gets called with the result
  1188. */
  1189. removeSongFromPlaylist: isLoginRequired(async function removeSongFromPlaylist(session, youtubeId, playlistId, cb) {
  1190. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  1191. async.waterfall(
  1192. [
  1193. next => {
  1194. if (!youtubeId || typeof youtubeId !== "string") return next("Invalid song id.");
  1195. if (!playlistId) return next("Invalid playlist id.");
  1196. return next();
  1197. },
  1198. next => {
  1199. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1200. .then(playlist => next(null, playlist))
  1201. .catch(next);
  1202. },
  1203. (playlist, next) => {
  1204. if (!playlist || playlist.createdBy !== session.userId) return next("Playlist not found");
  1205. // sort array by position
  1206. playlist.songs.sort((a, b) => a.position - b.position);
  1207. // find index of youtubeId
  1208. playlist.songs.forEach((song, ind) => {
  1209. // remove song from array
  1210. if (song.youtubeId === youtubeId) playlist.songs.splice(ind, 1);
  1211. });
  1212. const songsBeingChanged = [];
  1213. playlist.songs.forEach((song, index) => {
  1214. // check if position needs updated based on index
  1215. if (song.position !== index + 1)
  1216. songsBeingChanged.push({
  1217. youtubeId: song.youtubeId,
  1218. position: index + 1
  1219. });
  1220. });
  1221. // update position property on songs that need to be changed
  1222. return WSModule.runJob(
  1223. "RUN_ACTION2",
  1224. {
  1225. session,
  1226. namespace: "playlists",
  1227. action: "repositionSongs",
  1228. args: [playlistId, songsBeingChanged]
  1229. },
  1230. this
  1231. )
  1232. .then(res => {
  1233. if (res.status === "success") return next();
  1234. return next("Unable to reposition song in playlist.");
  1235. })
  1236. .catch(next);
  1237. },
  1238. next => playlistModel.updateOne({ _id: playlistId }, { $pull: { songs: { youtubeId } } }, next),
  1239. (res, next) => {
  1240. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  1241. .then(playlist => next(null, playlist))
  1242. .catch(next);
  1243. },
  1244. (playlist, next) => {
  1245. StationsModule.runJob("GET_STATIONS_THAT_INCLUDE_OR_EXCLUDE_PLAYLIST", { playlistId })
  1246. .then(response => {
  1247. response.stationIds.forEach(stationId => {
  1248. PlaylistsModule.runJob("AUTOFILL_STATION_PLAYLIST", { stationId }).then().catch();
  1249. });
  1250. })
  1251. .catch();
  1252. SongsModule.runJob("GET_SONG_FROM_YOUTUBE_ID", { youtubeId }, this)
  1253. .then(res =>
  1254. next(null, playlist, {
  1255. title: res.song.title,
  1256. thumbnail: res.song.thumbnail,
  1257. artists: res.song.artists
  1258. })
  1259. )
  1260. .catch(() => {
  1261. YouTubeModule.runJob("GET_SONG", { youtubeId }, this)
  1262. .then(response => next(null, playlist, response.song))
  1263. .catch(next);
  1264. });
  1265. },
  1266. (playlist, youtubeSong, next) => {
  1267. const songName = youtubeSong.artists
  1268. ? `${youtubeSong.title} by ${youtubeSong.artists.join(", ")}`
  1269. : youtubeSong.title;
  1270. if (playlist.displayName !== "Liked Songs" && playlist.displayName !== "Disliked Songs") {
  1271. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1272. userId: session.userId,
  1273. type: "playlist__remove_song",
  1274. payload: {
  1275. message: `Removed <youtubeId>${songName}</youtubeId> from playlist <playlistId>${playlist.displayName}</playlistId>`,
  1276. thumbnail: youtubeSong.thumbnail,
  1277. playlistId,
  1278. youtubeId
  1279. }
  1280. });
  1281. }
  1282. return next(null, playlist);
  1283. }
  1284. ],
  1285. async (err, playlist) => {
  1286. if (err) {
  1287. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1288. this.log(
  1289. "ERROR",
  1290. "PLAYLIST_REMOVE_SONG",
  1291. `Removing song "${youtubeId}" from private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1292. );
  1293. return cb({ status: "error", message: err });
  1294. }
  1295. this.log(
  1296. "SUCCESS",
  1297. "PLAYLIST_REMOVE_SONG",
  1298. `Successfully removed song "${youtubeId}" from private playlist "${playlistId}" for user "${session.userId}".`
  1299. );
  1300. CacheModule.runJob("PUB", {
  1301. channel: "playlist.removeSong",
  1302. value: {
  1303. playlistId: playlist._id,
  1304. youtubeId,
  1305. userId: session.userId,
  1306. privacy: playlist.privacy
  1307. }
  1308. });
  1309. return cb({
  1310. status: "success",
  1311. message: "Song has been successfully removed from playlist",
  1312. data: { songs: playlist.songs }
  1313. });
  1314. }
  1315. );
  1316. }),
  1317. /**
  1318. * Updates the displayName of a private playlist
  1319. *
  1320. * @param {object} session - the session object automatically added by the websocket
  1321. * @param {string} playlistId - the id of the playlist we are updating the displayName for
  1322. * @param {Function} cb - gets called with the result
  1323. */
  1324. updateDisplayName: isLoginRequired(async function updateDisplayName(session, playlistId, displayName, cb) {
  1325. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  1326. async.waterfall(
  1327. [
  1328. next => {
  1329. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1330. .then(playlist => next(null, playlist))
  1331. .catch(next);
  1332. },
  1333. (playlist, next) => {
  1334. if (!playlist.isUserModifiable) return next("Playlist cannot be modified.");
  1335. return next(null);
  1336. },
  1337. next => {
  1338. playlistModel.updateOne(
  1339. { _id: playlistId, createdBy: session.userId },
  1340. { $set: { displayName } },
  1341. { runValidators: true },
  1342. next
  1343. );
  1344. },
  1345. (res, next) => {
  1346. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  1347. .then(playlist => next(null, playlist))
  1348. .catch(next);
  1349. }
  1350. ],
  1351. async (err, playlist) => {
  1352. if (err) {
  1353. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1354. this.log(
  1355. "ERROR",
  1356. "PLAYLIST_UPDATE_DISPLAY_NAME",
  1357. `Updating display name to "${displayName}" for private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1358. );
  1359. return cb({ status: "error", message: err });
  1360. }
  1361. this.log(
  1362. "SUCCESS",
  1363. "PLAYLIST_UPDATE_DISPLAY_NAME",
  1364. `Successfully updated display name to "${displayName}" for private playlist "${playlistId}" for user "${session.userId}".`
  1365. );
  1366. CacheModule.runJob("PUB", {
  1367. channel: "playlist.updateDisplayName",
  1368. value: {
  1369. playlistId,
  1370. displayName,
  1371. userId: session.userId,
  1372. privacy: playlist.privacy
  1373. }
  1374. });
  1375. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1376. userId: session.userId,
  1377. type: "playlist__edit_display_name",
  1378. payload: {
  1379. message: `Changed display name of playlist <playlistId>${displayName}</playlistId>`,
  1380. playlistId
  1381. }
  1382. });
  1383. return cb({
  1384. status: "success",
  1385. message: "Playlist has been successfully updated"
  1386. });
  1387. }
  1388. );
  1389. }),
  1390. /**
  1391. * Removes a private playlist
  1392. *
  1393. * @param {object} session - the session object automatically added by the websocket
  1394. * @param {string} playlistId - the id of the playlist we are moving the song to the top from
  1395. * @param {Function} cb - gets called with the result
  1396. */
  1397. remove: isLoginRequired(async function remove(session, playlistId, cb) {
  1398. // const stationModel = await DBModule.runJob("GET_MODEL", { modelName: "station" }, this);
  1399. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  1400. async.waterfall(
  1401. [
  1402. next => {
  1403. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1404. .then(playlist => next(null, playlist))
  1405. .catch(next);
  1406. },
  1407. (playlist, next) => {
  1408. if (!playlist.isUserModifiable) return next("Playlist cannot be removed.");
  1409. return next(null, playlist);
  1410. },
  1411. (playlist, next) => {
  1412. userModel.updateOne(
  1413. { _id: playlist.createdBy },
  1414. { $pull: { "preferences.orderOfPlaylists": playlist._id } },
  1415. err => next(err, playlist)
  1416. );
  1417. },
  1418. (playlist, next) => {
  1419. PlaylistsModule.runJob("DELETE_PLAYLIST", { playlistId }, this)
  1420. .then(() => next(null, playlist))
  1421. .catch(next);
  1422. }
  1423. // (playlist, next) => {
  1424. // stationModel.find({ privatePlaylist: playlistId }, (err, res) => {
  1425. // next(err, playlist, res);
  1426. // });
  1427. // },
  1428. // (playlist, stations, next) => {
  1429. // async.each(
  1430. // stations,
  1431. // (station, next) => {
  1432. // async.waterfall(
  1433. // [
  1434. // next => {
  1435. // stationModel.updateOne(
  1436. // { _id: station._id },
  1437. // { $set: { privatePlaylist: null } },
  1438. // { runValidators: true },
  1439. // next
  1440. // );
  1441. // },
  1442. // (res, next) => {
  1443. // if (!station.partyMode) {
  1444. // moduleManager.modules.stations
  1445. // .runJob("UPDATE_STATION", { stationId: station._id }, this)
  1446. // .then(station => next(null, station))
  1447. // .catch(next);
  1448. // CacheModule.runJob("PUB", {
  1449. // channel: "privatePlaylist.selected",
  1450. // value: {
  1451. // playlistId: null,
  1452. // stationId: station._id
  1453. // }
  1454. // });
  1455. // } else next();
  1456. // }
  1457. // ],
  1458. // () => next()
  1459. // );
  1460. // },
  1461. // () => next(null, playlist)
  1462. // );
  1463. // }
  1464. ],
  1465. async (err, playlist) => {
  1466. if (err) {
  1467. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1468. this.log(
  1469. "ERROR",
  1470. "PLAYLIST_REMOVE",
  1471. `Removing private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1472. );
  1473. return cb({ status: "error", message: err });
  1474. }
  1475. this.log(
  1476. "SUCCESS",
  1477. "PLAYLIST_REMOVE",
  1478. `Successfully removed private playlist "${playlistId}" for user "${session.userId}".`
  1479. );
  1480. CacheModule.runJob("PUB", {
  1481. channel: "playlist.delete",
  1482. value: {
  1483. userId: session.userId,
  1484. playlistId
  1485. }
  1486. });
  1487. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1488. userId: playlist.createdBy,
  1489. type: "playlist__remove",
  1490. payload: {
  1491. message: `Removed playlist ${playlist.displayName}`
  1492. }
  1493. });
  1494. return cb({
  1495. status: "success",
  1496. message: "Playlist successfully removed"
  1497. });
  1498. }
  1499. );
  1500. }),
  1501. /**
  1502. * Updates the privacy of a private playlist
  1503. *
  1504. * @param {object} session - the session object automatically added by the websocket
  1505. * @param {string} playlistId - the id of the playlist we are updating the privacy for
  1506. * @param {string} privacy - what the new privacy of the playlist should be e.g. public
  1507. * @param {Function} cb - gets called with the result
  1508. */
  1509. updatePrivacy: isLoginRequired(async function updatePrivacy(session, playlistId, privacy, cb) {
  1510. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  1511. async.waterfall(
  1512. [
  1513. next => {
  1514. playlistModel.updateOne(
  1515. { _id: playlistId, createdBy: session.userId },
  1516. { $set: { privacy } },
  1517. { runValidators: true },
  1518. next
  1519. );
  1520. },
  1521. (res, next) => {
  1522. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  1523. .then(playlist => next(null, playlist))
  1524. .catch(next);
  1525. }
  1526. ],
  1527. async (err, playlist) => {
  1528. if (err) {
  1529. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1530. this.log(
  1531. "ERROR",
  1532. "PLAYLIST_UPDATE_PRIVACY",
  1533. `Updating privacy to "${privacy}" for private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1534. );
  1535. return cb({ status: "error", message: err });
  1536. }
  1537. this.log(
  1538. "SUCCESS",
  1539. "PLAYLIST_UPDATE_PRIVACY",
  1540. `Successfully updated privacy to "${privacy}" for private playlist "${playlistId}" for user "${session.userId}".`
  1541. );
  1542. CacheModule.runJob("PUB", {
  1543. channel: "playlist.updatePrivacy",
  1544. value: {
  1545. userId: session.userId,
  1546. playlist
  1547. }
  1548. });
  1549. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1550. userId: session.userId,
  1551. type: "playlist__edit_privacy",
  1552. payload: {
  1553. message: `Changed privacy of playlist <playlistId>${playlist.displayName}</playlistId> to ${privacy}`,
  1554. playlistId
  1555. }
  1556. });
  1557. return cb({
  1558. status: "success",
  1559. message: "Playlist has been successfully updated"
  1560. });
  1561. }
  1562. );
  1563. }),
  1564. /**
  1565. * Deletes all orphaned station playlists
  1566. *
  1567. * @param {object} session - the session object automatically added by socket.io
  1568. * @param {Function} cb - gets called with the result
  1569. */
  1570. deleteOrphanedStationPlaylists: isAdminRequired(async function index(session, cb) {
  1571. async.waterfall(
  1572. [
  1573. next => {
  1574. PlaylistsModule.runJob("DELETE_ORPHANED_STATION_PLAYLISTS", {}, this)
  1575. .then(() => next())
  1576. .catch(next);
  1577. }
  1578. ],
  1579. async err => {
  1580. if (err) {
  1581. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1582. this.log(
  1583. "ERROR",
  1584. "PLAYLISTS_DELETE_ORPHANED_STATION_PLAYLISTS",
  1585. `Deleting orphaned station playlists failed. "${err}"`
  1586. );
  1587. return cb({ status: "error", message: err });
  1588. }
  1589. this.log(
  1590. "SUCCESS",
  1591. "PLAYLISTS_DELETE_ORPHANED_STATION_PLAYLISTS",
  1592. "Deleting orphaned station playlists successful."
  1593. );
  1594. return cb({ status: "success", message: "Successfully deleted orphaned station playlists." });
  1595. }
  1596. );
  1597. }),
  1598. /**
  1599. * Deletes all orphaned genre playlists
  1600. *
  1601. * @param {object} session - the session object automatically added by socket.io
  1602. * @param {Function} cb - gets called with the result
  1603. */
  1604. deleteOrphanedGenrePlaylists: isAdminRequired(async function index(session, cb) {
  1605. async.waterfall(
  1606. [
  1607. next => {
  1608. PlaylistsModule.runJob("DELETE_ORPHANED_GENRE_PLAYLISTS", {}, this)
  1609. .then(() => next())
  1610. .catch(next);
  1611. }
  1612. ],
  1613. async err => {
  1614. if (err) {
  1615. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1616. this.log(
  1617. "ERROR",
  1618. "PLAYLISTS_DELETE_ORPHANED_GENRE_PLAYLISTS",
  1619. `Deleting orphaned genre playlists failed. "${err}"`
  1620. );
  1621. return cb({ status: "error", message: err });
  1622. }
  1623. this.log(
  1624. "SUCCESS",
  1625. "PLAYLISTS_DELETE_ORPHANED_GENRE_PLAYLISTS",
  1626. "Deleting orphaned genre playlists successful."
  1627. );
  1628. return cb({ status: "success", message: "Successfully deleted orphaned genre playlists." });
  1629. }
  1630. );
  1631. }),
  1632. /**
  1633. * Requests orpahned playlist songs
  1634. *
  1635. * @param {object} session - the session object automatically added by socket.io
  1636. * @param {Function} cb - gets called with the result
  1637. */
  1638. requestOrphanedPlaylistSongs: isAdminRequired(async function index(session, cb) {
  1639. async.waterfall(
  1640. [
  1641. next => {
  1642. SongsModule.runJob("REQUEST_ORPHANED_PLAYLIST_SONGS", {}, this)
  1643. .then(() => next())
  1644. .catch(next);
  1645. }
  1646. ],
  1647. async err => {
  1648. if (err) {
  1649. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1650. this.log(
  1651. "ERROR",
  1652. "REQUEST_ORPHANED_PLAYLIST_SONGS",
  1653. `Requesting orphaned playlist songs failed. "${err}"`
  1654. );
  1655. return cb({ status: "error", message: err });
  1656. }
  1657. this.log(
  1658. "SUCCESS",
  1659. "REQUEST_ORPHANED_PLAYLIST_SONGS",
  1660. "Requesting orphaned playlist songs was successful."
  1661. );
  1662. return cb({ status: "success", message: "Successfully requested orphaned playlist songs." });
  1663. }
  1664. );
  1665. }),
  1666. /**
  1667. * Clears and refills a station playlist
  1668. *
  1669. * @param {object} session - the session object automatically added by socket.io
  1670. * @param {string} playlistId - the id of the playlist we are clearing and refilling
  1671. * @param {Function} cb - gets called with the result
  1672. */
  1673. clearAndRefillStationPlaylist: isAdminRequired(async function index(session, playlistId, cb) {
  1674. async.waterfall(
  1675. [
  1676. next => {
  1677. if (!playlistId) next("Please specify a playlist id");
  1678. else {
  1679. PlaylistsModule.runJob("CLEAR_AND_REFILL_STATION_PLAYLIST", { playlistId }, this)
  1680. .then(() => {
  1681. next();
  1682. })
  1683. .catch(err => {
  1684. next(err);
  1685. });
  1686. }
  1687. }
  1688. ],
  1689. async err => {
  1690. if (err) {
  1691. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1692. this.log(
  1693. "ERROR",
  1694. "PLAYLIST_CLEAR_AND_REFILL_STATION_PLAYLIST",
  1695. `Clearing and refilling station playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1696. );
  1697. return cb({ status: "error", message: err });
  1698. }
  1699. this.log(
  1700. "SUCCESS",
  1701. "PLAYLIST_CLEAR_AND_REFILL_STATION_PLAYLIST",
  1702. `Successfully cleared and refilled station playlist "${playlistId}" for user "${session.userId}".`
  1703. );
  1704. return cb({
  1705. status: "success",
  1706. message: "Playlist has been successfully cleared and refilled"
  1707. });
  1708. }
  1709. );
  1710. }),
  1711. /**
  1712. * Clears and refills a genre playlist
  1713. *
  1714. * @param {object} session - the session object automatically added by socket.io
  1715. * @param {string} playlistId - the id of the playlist we are clearing and refilling
  1716. * @param {Function} cb - gets called with the result
  1717. */
  1718. clearAndRefillGenrePlaylist: isAdminRequired(async function index(session, playlistId, cb) {
  1719. async.waterfall(
  1720. [
  1721. next => {
  1722. if (!playlistId) next("Please specify a playlist id");
  1723. else {
  1724. PlaylistsModule.runJob("CLEAR_AND_REFILL_GENRE_PLAYLIST", { playlistId }, this)
  1725. .then(() => {
  1726. next();
  1727. })
  1728. .catch(err => {
  1729. next(err);
  1730. });
  1731. }
  1732. }
  1733. ],
  1734. async err => {
  1735. if (err) {
  1736. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1737. this.log(
  1738. "ERROR",
  1739. "PLAYLIST_CLEAR_AND_REFILL_GENRE_PLAYLIST",
  1740. `Clearing and refilling genre playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1741. );
  1742. return cb({ status: "error", message: err });
  1743. }
  1744. this.log(
  1745. "SUCCESS",
  1746. "PLAYLIST_CLEAR_AND_REFILL_GENRE_PLAYLIST",
  1747. `Successfully cleared and refilled genre playlist "${playlistId}" for user "${session.userId}".`
  1748. );
  1749. return cb({
  1750. status: "success",
  1751. message: "Playlist has been successfully cleared and refilled"
  1752. });
  1753. }
  1754. );
  1755. }),
  1756. /**
  1757. * Clears and refills all station playlists
  1758. *
  1759. * @param {object} session - the session object automatically added by socket.io
  1760. * @param {Function} cb - gets called with the result
  1761. */
  1762. clearAndRefillAllStationPlaylists: isAdminRequired(async function index(session, cb) {
  1763. async.waterfall(
  1764. [
  1765. next => {
  1766. PlaylistsModule.runJob("GET_ALL_STATION_PLAYLISTS", {}, this)
  1767. .then(response => {
  1768. next(null, response.playlists);
  1769. })
  1770. .catch(err => {
  1771. next(err);
  1772. });
  1773. },
  1774. (playlists, next) => {
  1775. async.eachLimit(
  1776. playlists,
  1777. 1,
  1778. (playlist, next) => {
  1779. PlaylistsModule.runJob(
  1780. "CLEAR_AND_REFILL_STATION_PLAYLIST",
  1781. { playlistId: playlist._id },
  1782. this
  1783. )
  1784. .then(() => {
  1785. next();
  1786. })
  1787. .catch(err => {
  1788. next(err);
  1789. });
  1790. },
  1791. next
  1792. );
  1793. }
  1794. ],
  1795. async err => {
  1796. if (err) {
  1797. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1798. this.log(
  1799. "ERROR",
  1800. "PLAYLIST_CLEAR_AND_REFILL_ALL_STATION_PLAYLISTS",
  1801. `Clearing and refilling all station playlists failed for user "${session.userId}". "${err}"`
  1802. );
  1803. return cb({ status: "error", message: err });
  1804. }
  1805. this.log(
  1806. "SUCCESS",
  1807. "PLAYLIST_CLEAR_AND_REFILL_ALL_STATION_PLAYLISTS",
  1808. `Successfully cleared and refilled all station playlists for user "${session.userId}".`
  1809. );
  1810. return cb({
  1811. status: "success",
  1812. message: "Playlists have been successfully cleared and refilled"
  1813. });
  1814. }
  1815. );
  1816. }),
  1817. /**
  1818. * Clears and refills all genre playlists
  1819. *
  1820. * @param {object} session - the session object automatically added by socket.io
  1821. * @param {Function} cb - gets called with the result
  1822. */
  1823. clearAndRefillAllGenrePlaylists: isAdminRequired(async function index(session, cb) {
  1824. async.waterfall(
  1825. [
  1826. next => {
  1827. PlaylistsModule.runJob("GET_ALL_GENRE_PLAYLISTS", {}, this)
  1828. .then(response => {
  1829. next(null, response.playlists);
  1830. })
  1831. .catch(err => {
  1832. next(err);
  1833. });
  1834. },
  1835. (playlists, next) => {
  1836. async.eachLimit(
  1837. playlists,
  1838. 1,
  1839. (playlist, next) => {
  1840. PlaylistsModule.runJob(
  1841. "CLEAR_AND_REFILL_GENRE_PLAYLIST",
  1842. { playlistId: playlist._id },
  1843. this
  1844. )
  1845. .then(() => {
  1846. next();
  1847. })
  1848. .catch(err => {
  1849. next(err);
  1850. });
  1851. },
  1852. next
  1853. );
  1854. }
  1855. ],
  1856. async err => {
  1857. if (err) {
  1858. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1859. this.log(
  1860. "ERROR",
  1861. "PLAYLIST_CLEAR_AND_REFILL_ALL_GENRE_PLAYLISTS",
  1862. `Clearing and refilling all genre playlists failed for user "${session.userId}". "${err}"`
  1863. );
  1864. return cb({ status: "error", message: err });
  1865. }
  1866. this.log(
  1867. "SUCCESS",
  1868. "PLAYLIST_CLEAR_AND_REFILL_ALL_GENRE_PLAYLISTS",
  1869. `Successfully cleared and refilled all genre playlists for user "${session.userId}".`
  1870. );
  1871. return cb({
  1872. status: "success",
  1873. message: "Playlists have been successfully cleared and refilled"
  1874. });
  1875. }
  1876. );
  1877. })
  1878. };