playlists.js 58 KB

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