playlists.js 57 KB

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