playlists.js 61 KB

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