1
0

playlists.js 58 KB

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