playlists.js 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  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("d", { data: { playlist } }))
  18. );
  19. if (playlist.privacy === "public")
  20. WSModule.runJob("EMIT_TO_ROOM", {
  21. room: `profile-${playlist.createdBy}-playlists`,
  22. args: ["event:playlist.created", { 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.deleted", { data: { playlistId: res.playlistId } });
  32. });
  33. });
  34. WSModule.runJob("EMIT_TO_ROOM", {
  35. room: `profile-${res.userId}-playlists`,
  36. args: ["event:playlist.deleted", { 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.songs.repositioned", {
  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.song.added", {
  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.song.added",
  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.song.removed", {
  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.song.removed",
  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.displayName.updated", {
  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.displayName.updated",
  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.privacy.updated", {
  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.created",
  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.deleted",
  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. async.eachLimit(
  1103. youtubeIds,
  1104. 1,
  1105. (youtubeId, next) => {
  1106. WSModule.runJob(
  1107. "RUN_ACTION2",
  1108. {
  1109. session,
  1110. namespace: "playlists",
  1111. action: "addSongToPlaylist",
  1112. args: [true, youtubeId, playlistId]
  1113. },
  1114. this
  1115. )
  1116. .then(res => {
  1117. if (res.status === "success") {
  1118. successful += 1;
  1119. addedSongs.push(youtubeId);
  1120. } else failed += 1;
  1121. if (res.message === "That song is already in the playlist") alreadyInPlaylist += 1;
  1122. })
  1123. .catch(() => {
  1124. failed += 1;
  1125. })
  1126. .finally(() => next());
  1127. },
  1128. () => {
  1129. addSongsStats = { successful, failed, alreadyInPlaylist };
  1130. next(null);
  1131. }
  1132. );
  1133. },
  1134. next => {
  1135. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1136. .then(playlist => next(null, playlist))
  1137. .catch(next);
  1138. },
  1139. (playlist, next) => {
  1140. if (!playlist || playlist.createdBy !== session.userId) return next("Playlist not found.");
  1141. if (!playlist.isUserModifiable) return next("Playlist cannot be modified.");
  1142. return next(null, playlist);
  1143. }
  1144. ],
  1145. async (err, playlist) => {
  1146. if (err) {
  1147. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1148. this.log(
  1149. "ERROR",
  1150. "PLAYLIST_IMPORT",
  1151. `Importing a YouTube playlist to private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1152. );
  1153. return cb({ status: "error", message: err });
  1154. }
  1155. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1156. userId: session.userId,
  1157. type: "playlist__import_playlist",
  1158. payload: {
  1159. message: `Imported ${addSongsStats.successful} songs to playlist <playlistId>${playlist.displayName}</playlistId>`,
  1160. playlistId
  1161. }
  1162. });
  1163. this.log(
  1164. "SUCCESS",
  1165. "PLAYLIST_IMPORT",
  1166. `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}.`
  1167. );
  1168. return cb({
  1169. status: "success",
  1170. message: `Playlist has been imported. ${addSongsStats.successful} were added successfully, ${addSongsStats.failed} failed (${addSongsStats.alreadyInPlaylist} were already in the playlist)`,
  1171. data: {
  1172. songs: playlist.songs,
  1173. stats: {
  1174. videosInPlaylistTotal,
  1175. songsInPlaylistTotal
  1176. }
  1177. }
  1178. });
  1179. }
  1180. );
  1181. }),
  1182. /**
  1183. * Removes a song from a private playlist
  1184. *
  1185. * @param {object} session - the session object automatically added by the websocket
  1186. * @param {string} youtubeId - the youtube id of the song we are removing from the private playlist
  1187. * @param {string} playlistId - the id of the playlist we are removing the song from
  1188. * @param {Function} cb - gets called with the result
  1189. */
  1190. removeSongFromPlaylist: isLoginRequired(async function removeSongFromPlaylist(session, youtubeId, playlistId, cb) {
  1191. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  1192. async.waterfall(
  1193. [
  1194. next => {
  1195. if (!youtubeId || typeof youtubeId !== "string") return next("Invalid song id.");
  1196. if (!playlistId) return next("Invalid playlist id.");
  1197. return next();
  1198. },
  1199. next => {
  1200. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1201. .then(playlist => next(null, playlist))
  1202. .catch(next);
  1203. },
  1204. (playlist, next) => {
  1205. if (!playlist || playlist.createdBy !== session.userId) return next("Playlist not found");
  1206. // sort array by position
  1207. playlist.songs.sort((a, b) => a.position - b.position);
  1208. // find index of youtubeId
  1209. playlist.songs.forEach((song, ind) => {
  1210. // remove song from array
  1211. if (song.youtubeId === youtubeId) playlist.songs.splice(ind, 1);
  1212. });
  1213. const songsBeingChanged = [];
  1214. playlist.songs.forEach((song, index) => {
  1215. // check if position needs updated based on index
  1216. if (song.position !== index + 1)
  1217. songsBeingChanged.push({
  1218. youtubeId: song.youtubeId,
  1219. position: index + 1
  1220. });
  1221. });
  1222. // update position property on songs that need to be changed
  1223. return WSModule.runJob(
  1224. "RUN_ACTION2",
  1225. {
  1226. session,
  1227. namespace: "playlists",
  1228. action: "repositionSongs",
  1229. args: [playlistId, songsBeingChanged]
  1230. },
  1231. this
  1232. )
  1233. .then(res => {
  1234. if (res.status === "success") return next();
  1235. return next("Unable to reposition song in playlist.");
  1236. })
  1237. .catch(next);
  1238. },
  1239. next => playlistModel.updateOne({ _id: playlistId }, { $pull: { songs: { youtubeId } } }, next),
  1240. (res, next) => {
  1241. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  1242. .then(playlist => next(null, playlist))
  1243. .catch(next);
  1244. },
  1245. (playlist, next) => {
  1246. StationsModule.runJob("GET_STATIONS_THAT_INCLUDE_OR_EXCLUDE_PLAYLIST", { playlistId })
  1247. .then(response => {
  1248. response.stationIds.forEach(stationId => {
  1249. PlaylistsModule.runJob("AUTOFILL_STATION_PLAYLIST", { stationId }).then().catch();
  1250. });
  1251. })
  1252. .catch();
  1253. SongsModule.runJob("GET_SONG_FROM_YOUTUBE_ID", { youtubeId }, this)
  1254. .then(res =>
  1255. next(null, playlist, {
  1256. title: res.song.title,
  1257. thumbnail: res.song.thumbnail,
  1258. artists: res.song.artists
  1259. })
  1260. )
  1261. .catch(() => {
  1262. YouTubeModule.runJob("GET_SONG", { youtubeId }, this)
  1263. .then(response => next(null, playlist, response.song))
  1264. .catch(next);
  1265. });
  1266. },
  1267. (playlist, youtubeSong, next) => {
  1268. const songName = youtubeSong.artists
  1269. ? `${youtubeSong.title} by ${youtubeSong.artists.join(", ")}`
  1270. : youtubeSong.title;
  1271. if (playlist.displayName !== "Liked Songs" && playlist.displayName !== "Disliked Songs") {
  1272. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1273. userId: session.userId,
  1274. type: "playlist__remove_song",
  1275. payload: {
  1276. message: `Removed <youtubeId>${songName}</youtubeId> from playlist <playlistId>${playlist.displayName}</playlistId>`,
  1277. thumbnail: youtubeSong.thumbnail,
  1278. playlistId,
  1279. youtubeId
  1280. }
  1281. });
  1282. }
  1283. return next(null, playlist);
  1284. }
  1285. ],
  1286. async (err, playlist) => {
  1287. if (err) {
  1288. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1289. this.log(
  1290. "ERROR",
  1291. "PLAYLIST_REMOVE_SONG",
  1292. `Removing song "${youtubeId}" from private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1293. );
  1294. return cb({ status: "error", message: err });
  1295. }
  1296. this.log(
  1297. "SUCCESS",
  1298. "PLAYLIST_REMOVE_SONG",
  1299. `Successfully removed song "${youtubeId}" from private playlist "${playlistId}" for user "${session.userId}".`
  1300. );
  1301. CacheModule.runJob("PUB", {
  1302. channel: "playlist.removeSong",
  1303. value: {
  1304. playlistId: playlist._id,
  1305. youtubeId,
  1306. userId: session.userId,
  1307. privacy: playlist.privacy
  1308. }
  1309. });
  1310. return cb({
  1311. status: "success",
  1312. message: "Song has been successfully removed from playlist",
  1313. data: { songs: playlist.songs }
  1314. });
  1315. }
  1316. );
  1317. }),
  1318. /**
  1319. * Updates the displayName of a private playlist
  1320. *
  1321. * @param {object} session - the session object automatically added by the websocket
  1322. * @param {string} playlistId - the id of the playlist we are updating the displayName for
  1323. * @param {Function} cb - gets called with the result
  1324. */
  1325. updateDisplayName: isLoginRequired(async function updateDisplayName(session, playlistId, displayName, cb) {
  1326. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  1327. async.waterfall(
  1328. [
  1329. next => {
  1330. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1331. .then(playlist => next(null, playlist))
  1332. .catch(next);
  1333. },
  1334. (playlist, next) => {
  1335. if (!playlist.isUserModifiable) return next("Playlist cannot be modified.");
  1336. return next(null);
  1337. },
  1338. next => {
  1339. playlistModel.updateOne(
  1340. { _id: playlistId, createdBy: session.userId },
  1341. { $set: { displayName } },
  1342. { runValidators: true },
  1343. next
  1344. );
  1345. },
  1346. (res, next) => {
  1347. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  1348. .then(playlist => next(null, playlist))
  1349. .catch(next);
  1350. }
  1351. ],
  1352. async (err, playlist) => {
  1353. if (err) {
  1354. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1355. this.log(
  1356. "ERROR",
  1357. "PLAYLIST_UPDATE_DISPLAY_NAME",
  1358. `Updating display name to "${displayName}" for private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1359. );
  1360. return cb({ status: "error", message: err });
  1361. }
  1362. this.log(
  1363. "SUCCESS",
  1364. "PLAYLIST_UPDATE_DISPLAY_NAME",
  1365. `Successfully updated display name to "${displayName}" for private playlist "${playlistId}" for user "${session.userId}".`
  1366. );
  1367. CacheModule.runJob("PUB", {
  1368. channel: "playlist.updateDisplayName",
  1369. value: {
  1370. playlistId,
  1371. displayName,
  1372. userId: session.userId,
  1373. privacy: playlist.privacy
  1374. }
  1375. });
  1376. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1377. userId: session.userId,
  1378. type: "playlist__edit_display_name",
  1379. payload: {
  1380. message: `Changed display name of playlist <playlistId>${displayName}</playlistId>`,
  1381. playlistId
  1382. }
  1383. });
  1384. return cb({
  1385. status: "success",
  1386. message: "Playlist has been successfully updated"
  1387. });
  1388. }
  1389. );
  1390. }),
  1391. /**
  1392. * Removes a private playlist
  1393. *
  1394. * @param {object} session - the session object automatically added by the websocket
  1395. * @param {string} playlistId - the id of the playlist we are moving the song to the top from
  1396. * @param {Function} cb - gets called with the result
  1397. */
  1398. remove: isLoginRequired(async function remove(session, playlistId, cb) {
  1399. // const stationModel = await DBModule.runJob("GET_MODEL", { modelName: "station" }, this);
  1400. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  1401. async.waterfall(
  1402. [
  1403. next => {
  1404. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1405. .then(playlist => next(null, playlist))
  1406. .catch(next);
  1407. },
  1408. (playlist, next) => {
  1409. if (!playlist.isUserModifiable) return next("Playlist cannot be removed.");
  1410. return next(null, playlist);
  1411. },
  1412. (playlist, next) => {
  1413. userModel.updateOne(
  1414. { _id: playlist.createdBy },
  1415. { $pull: { "preferences.orderOfPlaylists": playlist._id } },
  1416. err => next(err, playlist)
  1417. );
  1418. },
  1419. (playlist, next) => {
  1420. PlaylistsModule.runJob("DELETE_PLAYLIST", { playlistId }, this)
  1421. .then(() => next(null, playlist))
  1422. .catch(next);
  1423. }
  1424. // (playlist, next) => {
  1425. // stationModel.find({ privatePlaylist: playlistId }, (err, res) => {
  1426. // next(err, playlist, res);
  1427. // });
  1428. // },
  1429. // (playlist, stations, next) => {
  1430. // async.each(
  1431. // stations,
  1432. // (station, next) => {
  1433. // async.waterfall(
  1434. // [
  1435. // next => {
  1436. // stationModel.updateOne(
  1437. // { _id: station._id },
  1438. // { $set: { privatePlaylist: null } },
  1439. // { runValidators: true },
  1440. // next
  1441. // );
  1442. // },
  1443. // (res, next) => {
  1444. // if (!station.partyMode) {
  1445. // moduleManager.modules.stations
  1446. // .runJob("UPDATE_STATION", { stationId: station._id }, this)
  1447. // .then(station => next(null, station))
  1448. // .catch(next);
  1449. // CacheModule.runJob("PUB", {
  1450. // channel: "privatePlaylist.selected",
  1451. // value: {
  1452. // playlistId: null,
  1453. // stationId: station._id
  1454. // }
  1455. // });
  1456. // } else next();
  1457. // }
  1458. // ],
  1459. // () => next()
  1460. // );
  1461. // },
  1462. // () => next(null, playlist)
  1463. // );
  1464. // }
  1465. ],
  1466. async (err, playlist) => {
  1467. if (err) {
  1468. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1469. this.log(
  1470. "ERROR",
  1471. "PLAYLIST_REMOVE",
  1472. `Removing private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1473. );
  1474. return cb({ status: "error", message: err });
  1475. }
  1476. this.log(
  1477. "SUCCESS",
  1478. "PLAYLIST_REMOVE",
  1479. `Successfully removed private playlist "${playlistId}" for user "${session.userId}".`
  1480. );
  1481. CacheModule.runJob("PUB", {
  1482. channel: "playlist.delete",
  1483. value: {
  1484. userId: session.userId,
  1485. playlistId
  1486. }
  1487. });
  1488. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1489. userId: playlist.createdBy,
  1490. type: "playlist__remove",
  1491. payload: {
  1492. message: `Removed playlist ${playlist.displayName}`
  1493. }
  1494. });
  1495. ActivitiesModule.runJob("REMOVE_ACTIVITY_REFERENCES", { type: "playlistId", playlistId });
  1496. return cb({
  1497. status: "success",
  1498. message: "Playlist successfully removed"
  1499. });
  1500. }
  1501. );
  1502. }),
  1503. /**
  1504. * Updates the privacy of a private playlist
  1505. *
  1506. * @param {object} session - the session object automatically added by the websocket
  1507. * @param {string} playlistId - the id of the playlist we are updating the privacy for
  1508. * @param {string} privacy - what the new privacy of the playlist should be e.g. public
  1509. * @param {Function} cb - gets called with the result
  1510. */
  1511. updatePrivacy: isLoginRequired(async function updatePrivacy(session, playlistId, privacy, cb) {
  1512. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  1513. async.waterfall(
  1514. [
  1515. next => {
  1516. playlistModel.updateOne(
  1517. { _id: playlistId, createdBy: session.userId },
  1518. { $set: { privacy } },
  1519. { runValidators: true },
  1520. next
  1521. );
  1522. },
  1523. (res, next) => {
  1524. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  1525. .then(playlist => next(null, playlist))
  1526. .catch(next);
  1527. }
  1528. ],
  1529. async (err, playlist) => {
  1530. if (err) {
  1531. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1532. this.log(
  1533. "ERROR",
  1534. "PLAYLIST_UPDATE_PRIVACY",
  1535. `Updating privacy to "${privacy}" for private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1536. );
  1537. return cb({ status: "error", message: err });
  1538. }
  1539. this.log(
  1540. "SUCCESS",
  1541. "PLAYLIST_UPDATE_PRIVACY",
  1542. `Successfully updated privacy to "${privacy}" for private playlist "${playlistId}" for user "${session.userId}".`
  1543. );
  1544. CacheModule.runJob("PUB", {
  1545. channel: "playlist.updatePrivacy",
  1546. value: {
  1547. userId: session.userId,
  1548. playlist
  1549. }
  1550. });
  1551. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1552. userId: session.userId,
  1553. type: "playlist__edit_privacy",
  1554. payload: {
  1555. message: `Changed privacy of playlist <playlistId>${playlist.displayName}</playlistId> to ${privacy}`,
  1556. playlistId
  1557. }
  1558. });
  1559. return cb({
  1560. status: "success",
  1561. message: "Playlist has been successfully updated"
  1562. });
  1563. }
  1564. );
  1565. }),
  1566. /**
  1567. * Deletes all orphaned station playlists
  1568. *
  1569. * @param {object} session - the session object automatically added by socket.io
  1570. * @param {Function} cb - gets called with the result
  1571. */
  1572. deleteOrphanedStationPlaylists: isAdminRequired(async function index(session, cb) {
  1573. async.waterfall(
  1574. [
  1575. next => {
  1576. PlaylistsModule.runJob("DELETE_ORPHANED_STATION_PLAYLISTS", {}, this)
  1577. .then(() => next())
  1578. .catch(next);
  1579. }
  1580. ],
  1581. async err => {
  1582. if (err) {
  1583. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1584. this.log(
  1585. "ERROR",
  1586. "PLAYLISTS_DELETE_ORPHANED_STATION_PLAYLISTS",
  1587. `Deleting orphaned station playlists failed. "${err}"`
  1588. );
  1589. return cb({ status: "error", message: err });
  1590. }
  1591. this.log(
  1592. "SUCCESS",
  1593. "PLAYLISTS_DELETE_ORPHANED_STATION_PLAYLISTS",
  1594. "Deleting orphaned station playlists successful."
  1595. );
  1596. return cb({ status: "success", message: "Successfully deleted orphaned station playlists." });
  1597. }
  1598. );
  1599. }),
  1600. /**
  1601. * Deletes all orphaned genre playlists
  1602. *
  1603. * @param {object} session - the session object automatically added by socket.io
  1604. * @param {Function} cb - gets called with the result
  1605. */
  1606. deleteOrphanedGenrePlaylists: isAdminRequired(async function index(session, cb) {
  1607. async.waterfall(
  1608. [
  1609. next => {
  1610. PlaylistsModule.runJob("DELETE_ORPHANED_GENRE_PLAYLISTS", {}, this)
  1611. .then(() => next())
  1612. .catch(next);
  1613. }
  1614. ],
  1615. async err => {
  1616. if (err) {
  1617. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1618. this.log(
  1619. "ERROR",
  1620. "PLAYLISTS_DELETE_ORPHANED_GENRE_PLAYLISTS",
  1621. `Deleting orphaned genre playlists failed. "${err}"`
  1622. );
  1623. return cb({ status: "error", message: err });
  1624. }
  1625. this.log(
  1626. "SUCCESS",
  1627. "PLAYLISTS_DELETE_ORPHANED_GENRE_PLAYLISTS",
  1628. "Deleting orphaned genre playlists successful."
  1629. );
  1630. return cb({ status: "success", message: "Successfully deleted orphaned genre playlists." });
  1631. }
  1632. );
  1633. }),
  1634. /**
  1635. * Requests orpahned playlist songs
  1636. *
  1637. * @param {object} session - the session object automatically added by socket.io
  1638. * @param {Function} cb - gets called with the result
  1639. */
  1640. requestOrphanedPlaylistSongs: isAdminRequired(async function index(session, cb) {
  1641. async.waterfall(
  1642. [
  1643. next => {
  1644. SongsModule.runJob("REQUEST_ORPHANED_PLAYLIST_SONGS", {}, this)
  1645. .then(() => next())
  1646. .catch(next);
  1647. }
  1648. ],
  1649. async err => {
  1650. if (err) {
  1651. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1652. this.log(
  1653. "ERROR",
  1654. "REQUEST_ORPHANED_PLAYLIST_SONGS",
  1655. `Requesting orphaned playlist songs failed. "${err}"`
  1656. );
  1657. return cb({ status: "error", message: err });
  1658. }
  1659. this.log(
  1660. "SUCCESS",
  1661. "REQUEST_ORPHANED_PLAYLIST_SONGS",
  1662. "Requesting orphaned playlist songs was successful."
  1663. );
  1664. return cb({ status: "success", message: "Successfully requested orphaned playlist songs." });
  1665. }
  1666. );
  1667. }),
  1668. /**
  1669. * Clears and refills a station playlist
  1670. *
  1671. * @param {object} session - the session object automatically added by socket.io
  1672. * @param {string} playlistId - the id of the playlist we are clearing and refilling
  1673. * @param {Function} cb - gets called with the result
  1674. */
  1675. clearAndRefillStationPlaylist: isAdminRequired(async function index(session, playlistId, cb) {
  1676. async.waterfall(
  1677. [
  1678. next => {
  1679. if (!playlistId) next("Please specify a playlist id");
  1680. else {
  1681. PlaylistsModule.runJob("CLEAR_AND_REFILL_STATION_PLAYLIST", { playlistId }, this)
  1682. .then(() => {
  1683. next();
  1684. })
  1685. .catch(err => {
  1686. next(err);
  1687. });
  1688. }
  1689. }
  1690. ],
  1691. async err => {
  1692. if (err) {
  1693. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1694. this.log(
  1695. "ERROR",
  1696. "PLAYLIST_CLEAR_AND_REFILL_STATION_PLAYLIST",
  1697. `Clearing and refilling station playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1698. );
  1699. return cb({ status: "error", message: err });
  1700. }
  1701. this.log(
  1702. "SUCCESS",
  1703. "PLAYLIST_CLEAR_AND_REFILL_STATION_PLAYLIST",
  1704. `Successfully cleared and refilled station playlist "${playlistId}" for user "${session.userId}".`
  1705. );
  1706. return cb({
  1707. status: "success",
  1708. message: "Playlist has been successfully cleared and refilled"
  1709. });
  1710. }
  1711. );
  1712. }),
  1713. /**
  1714. * Clears and refills a genre playlist
  1715. *
  1716. * @param {object} session - the session object automatically added by socket.io
  1717. * @param {string} playlistId - the id of the playlist we are clearing and refilling
  1718. * @param {Function} cb - gets called with the result
  1719. */
  1720. clearAndRefillGenrePlaylist: isAdminRequired(async function index(session, playlistId, cb) {
  1721. async.waterfall(
  1722. [
  1723. next => {
  1724. if (!playlistId) next("Please specify a playlist id");
  1725. else {
  1726. PlaylistsModule.runJob("CLEAR_AND_REFILL_GENRE_PLAYLIST", { playlistId }, this)
  1727. .then(() => {
  1728. next();
  1729. })
  1730. .catch(err => {
  1731. next(err);
  1732. });
  1733. }
  1734. }
  1735. ],
  1736. async err => {
  1737. if (err) {
  1738. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1739. this.log(
  1740. "ERROR",
  1741. "PLAYLIST_CLEAR_AND_REFILL_GENRE_PLAYLIST",
  1742. `Clearing and refilling genre playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1743. );
  1744. return cb({ status: "error", message: err });
  1745. }
  1746. this.log(
  1747. "SUCCESS",
  1748. "PLAYLIST_CLEAR_AND_REFILL_GENRE_PLAYLIST",
  1749. `Successfully cleared and refilled genre playlist "${playlistId}" for user "${session.userId}".`
  1750. );
  1751. return cb({
  1752. status: "success",
  1753. message: "Playlist has been successfully cleared and refilled"
  1754. });
  1755. }
  1756. );
  1757. }),
  1758. /**
  1759. * Clears and refills all station playlists
  1760. *
  1761. * @param {object} session - the session object automatically added by socket.io
  1762. * @param {Function} cb - gets called with the result
  1763. */
  1764. clearAndRefillAllStationPlaylists: isAdminRequired(async function index(session, cb) {
  1765. async.waterfall(
  1766. [
  1767. next => {
  1768. PlaylistsModule.runJob("GET_ALL_STATION_PLAYLISTS", {}, this)
  1769. .then(response => {
  1770. next(null, response.playlists);
  1771. })
  1772. .catch(err => {
  1773. next(err);
  1774. });
  1775. },
  1776. (playlists, next) => {
  1777. async.eachLimit(
  1778. playlists,
  1779. 1,
  1780. (playlist, next) => {
  1781. PlaylistsModule.runJob(
  1782. "CLEAR_AND_REFILL_STATION_PLAYLIST",
  1783. { playlistId: playlist._id },
  1784. this
  1785. )
  1786. .then(() => {
  1787. next();
  1788. })
  1789. .catch(err => {
  1790. next(err);
  1791. });
  1792. },
  1793. next
  1794. );
  1795. }
  1796. ],
  1797. async err => {
  1798. if (err) {
  1799. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1800. this.log(
  1801. "ERROR",
  1802. "PLAYLIST_CLEAR_AND_REFILL_ALL_STATION_PLAYLISTS",
  1803. `Clearing and refilling all station playlists failed for user "${session.userId}". "${err}"`
  1804. );
  1805. return cb({ status: "error", message: err });
  1806. }
  1807. this.log(
  1808. "SUCCESS",
  1809. "PLAYLIST_CLEAR_AND_REFILL_ALL_STATION_PLAYLISTS",
  1810. `Successfully cleared and refilled all station playlists for user "${session.userId}".`
  1811. );
  1812. return cb({
  1813. status: "success",
  1814. message: "Playlists have been successfully cleared and refilled"
  1815. });
  1816. }
  1817. );
  1818. }),
  1819. /**
  1820. * Clears and refills all genre playlists
  1821. *
  1822. * @param {object} session - the session object automatically added by socket.io
  1823. * @param {Function} cb - gets called with the result
  1824. */
  1825. clearAndRefillAllGenrePlaylists: isAdminRequired(async function index(session, cb) {
  1826. async.waterfall(
  1827. [
  1828. next => {
  1829. PlaylistsModule.runJob("GET_ALL_GENRE_PLAYLISTS", {}, this)
  1830. .then(response => {
  1831. next(null, response.playlists);
  1832. })
  1833. .catch(err => {
  1834. next(err);
  1835. });
  1836. },
  1837. (playlists, next) => {
  1838. async.eachLimit(
  1839. playlists,
  1840. 1,
  1841. (playlist, next) => {
  1842. PlaylistsModule.runJob(
  1843. "CLEAR_AND_REFILL_GENRE_PLAYLIST",
  1844. { playlistId: playlist._id },
  1845. this
  1846. )
  1847. .then(() => {
  1848. next();
  1849. })
  1850. .catch(err => {
  1851. next(err);
  1852. });
  1853. },
  1854. next
  1855. );
  1856. }
  1857. ],
  1858. async err => {
  1859. if (err) {
  1860. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1861. this.log(
  1862. "ERROR",
  1863. "PLAYLIST_CLEAR_AND_REFILL_ALL_GENRE_PLAYLISTS",
  1864. `Clearing and refilling all genre playlists failed for user "${session.userId}". "${err}"`
  1865. );
  1866. return cb({ status: "error", message: err });
  1867. }
  1868. this.log(
  1869. "SUCCESS",
  1870. "PLAYLIST_CLEAR_AND_REFILL_ALL_GENRE_PLAYLISTS",
  1871. `Successfully cleared and refilled all genre playlists for user "${session.userId}".`
  1872. );
  1873. return cb({
  1874. status: "success",
  1875. message: "Playlists have been successfully cleared and refilled"
  1876. });
  1877. }
  1878. );
  1879. })
  1880. };