playlists.js 57 KB

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