playlists.js 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740
  1. import async from "async";
  2. import config from "config";
  3. import { isAdminRequired, isLoginRequired } from "./hooks";
  4. // eslint-disable-next-line
  5. import moduleManager from "../../index";
  6. const DBModule = moduleManager.modules.db;
  7. const UtilsModule = moduleManager.modules.utils;
  8. const WSModule = moduleManager.modules.ws;
  9. const SongsModule = moduleManager.modules.songs;
  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. const MediaModule = moduleManager.modules.media;
  15. CacheModule.runJob("SUB", {
  16. channel: "playlist.create",
  17. cb: playlist => {
  18. WSModule.runJob("SOCKETS_FROM_USER", { userId: playlist.createdBy }, this).then(sockets =>
  19. sockets.forEach(socket => socket.dispatch("event:playlist.created", { data: { playlist } }))
  20. );
  21. if (playlist.privacy === "public")
  22. WSModule.runJob("EMIT_TO_ROOM", {
  23. room: `profile.${playlist.createdBy}.playlists`,
  24. args: ["event:playlist.created", { data: { playlist } }]
  25. });
  26. WSModule.runJob("EMIT_TO_ROOM", {
  27. room: "admin.playlists",
  28. args: ["event:admin.playlist.created", { data: { playlist } }]
  29. });
  30. }
  31. });
  32. CacheModule.runJob("SUB", {
  33. channel: "playlist.delete",
  34. cb: res => {
  35. WSModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(sockets => {
  36. sockets.forEach(socket => {
  37. socket.dispatch("event:playlist.deleted", { data: { playlistId: res.playlistId } });
  38. });
  39. });
  40. WSModule.runJob("EMIT_TO_ROOM", {
  41. room: `profile.${res.userId}.playlists`,
  42. args: ["event:playlist.deleted", { data: { playlistId: res.playlistId } }]
  43. });
  44. WSModule.runJob("EMIT_TO_ROOM", {
  45. room: "admin.playlists",
  46. args: ["event:admin.playlist.deleted", { data: { playlistId: res.playlistId } }]
  47. });
  48. }
  49. });
  50. CacheModule.runJob("SUB", {
  51. channel: "playlist.repositionSong",
  52. cb: res => {
  53. const { userId, playlistId, song } = res;
  54. WSModule.runJob("SOCKETS_FROM_USER", { userId }, this).then(sockets =>
  55. sockets.forEach(socket =>
  56. socket.dispatch("event:playlist.song.repositioned", {
  57. data: { playlistId, song }
  58. })
  59. )
  60. );
  61. }
  62. });
  63. CacheModule.runJob("SUB", {
  64. channel: "playlist.addSong",
  65. cb: res => {
  66. WSModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(sockets => {
  67. sockets.forEach(socket => {
  68. socket.dispatch("event:playlist.song.added", {
  69. data: {
  70. playlistId: res.playlistId,
  71. song: res.song
  72. }
  73. });
  74. });
  75. });
  76. if (res.privacy === "public")
  77. WSModule.runJob("EMIT_TO_ROOM", {
  78. room: `profile.${res.userId}.playlists`,
  79. args: [
  80. "event:playlist.song.added",
  81. {
  82. data: {
  83. playlistId: res.playlistId,
  84. song: res.song
  85. }
  86. }
  87. ]
  88. });
  89. WSModule.runJob("EMIT_TO_ROOM", {
  90. room: "admin.playlists",
  91. args: ["event:admin.playlist.song.added", { data: { playlistId: res.playlistId, song: res.song } }]
  92. });
  93. }
  94. });
  95. CacheModule.runJob("SUB", {
  96. channel: "playlist.removeSong",
  97. cb: res => {
  98. WSModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(sockets => {
  99. sockets.forEach(socket => {
  100. socket.dispatch("event:playlist.song.removed", {
  101. data: {
  102. playlistId: res.playlistId,
  103. youtubeId: res.youtubeId
  104. }
  105. });
  106. });
  107. });
  108. if (res.privacy === "public")
  109. WSModule.runJob("EMIT_TO_ROOM", {
  110. room: `profile.${res.userId}.playlists`,
  111. args: [
  112. "event:playlist.song.removed",
  113. {
  114. data: {
  115. playlistId: res.playlistId,
  116. youtubeId: res.youtubeId
  117. }
  118. }
  119. ]
  120. });
  121. WSModule.runJob("EMIT_TO_ROOM", {
  122. room: "admin.playlists",
  123. args: [
  124. "event:admin.playlist.song.removed",
  125. { data: { playlistId: res.playlistId, youtubeId: res.youtubeId } }
  126. ]
  127. });
  128. }
  129. });
  130. CacheModule.runJob("SUB", {
  131. channel: "playlist.updateDisplayName",
  132. cb: res => {
  133. WSModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(sockets => {
  134. sockets.forEach(socket => {
  135. socket.dispatch("event:playlist.displayName.updated", {
  136. data: {
  137. playlistId: res.playlistId,
  138. displayName: res.displayName
  139. }
  140. });
  141. });
  142. });
  143. if (res.privacy === "public")
  144. WSModule.runJob("EMIT_TO_ROOM", {
  145. room: `profile.${res.userId}.playlists`,
  146. args: [
  147. "event:playlist.displayName.updated",
  148. {
  149. data: {
  150. playlistId: res.playlistId,
  151. displayName: res.displayName
  152. }
  153. }
  154. ]
  155. });
  156. WSModule.runJob("EMIT_TO_ROOM", {
  157. room: "admin.playlists",
  158. args: [
  159. "event:admin.playlist.displayName.updated",
  160. { data: { playlistId: res.playlistId, displayName: res.displayName } }
  161. ]
  162. });
  163. }
  164. });
  165. CacheModule.runJob("SUB", {
  166. channel: "playlist.updatePrivacy",
  167. cb: res => {
  168. WSModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(sockets => {
  169. sockets.forEach(socket => {
  170. socket.dispatch("event:playlist.privacy.updated", {
  171. data: {
  172. playlist: res.playlist
  173. }
  174. });
  175. });
  176. });
  177. WSModule.runJob("EMIT_TO_ROOM", {
  178. room: "admin.playlists",
  179. args: [
  180. "event:admin.playlist.privacy.updated",
  181. { data: { playlistId: res.playlist._id, privacy: res.playlist.privacy } }
  182. ]
  183. });
  184. if (res.playlist.privacy === "public")
  185. return WSModule.runJob("EMIT_TO_ROOM", {
  186. room: `profile.${res.userId}.playlists`,
  187. args: [
  188. "event:playlist.created",
  189. {
  190. data: {
  191. playlist: res.playlist
  192. }
  193. }
  194. ]
  195. });
  196. return WSModule.runJob("EMIT_TO_ROOM", {
  197. room: `profile.${res.userId}.playlists`,
  198. args: [
  199. "event:playlist.deleted",
  200. {
  201. data: {
  202. playlistId: res.playlist._id
  203. }
  204. }
  205. ]
  206. });
  207. }
  208. });
  209. CacheModule.runJob("SUB", {
  210. channel: "playlist.updated",
  211. cb: async data => {
  212. const playlistModel = await DBModule.runJob("GET_MODEL", {
  213. modelName: "playlist"
  214. });
  215. playlistModel.findOne(
  216. { _id: data.playlistId },
  217. ["_id", "displayName", "type", "privacy", "songs", "createdBy", "createdAt", "createdFor"],
  218. (err, playlist) => {
  219. const newPlaylist = {
  220. ...playlist._doc,
  221. songsCount: playlist.songs.length,
  222. songsLength: playlist.songs.reduce(
  223. (previous, current) => ({
  224. duration: previous.duration + current.duration
  225. }),
  226. { duration: 0 }
  227. ).duration
  228. };
  229. delete newPlaylist.songs;
  230. WSModule.runJob("EMIT_TO_ROOMS", {
  231. rooms: ["admin.playlists"],
  232. args: ["event:admin.playlist.updated", { data: { playlist: newPlaylist } }]
  233. });
  234. }
  235. );
  236. }
  237. });
  238. export default {
  239. /**
  240. * Gets playlists, used in the admin playlists page by the AdvancedTable component
  241. *
  242. * @param {object} session - the session object automatically added by the websocket
  243. * @param page - the page
  244. * @param pageSize - the size per page
  245. * @param properties - the properties to return for each playlist
  246. * @param sort - the sort object
  247. * @param queries - the queries array
  248. * @param operator - the operator for queries
  249. * @param cb
  250. */
  251. getData: isAdminRequired(async function getSet(session, page, pageSize, properties, sort, queries, operator, cb) {
  252. async.waterfall(
  253. [
  254. next => {
  255. DBModule.runJob(
  256. "GET_DATA",
  257. {
  258. page,
  259. pageSize,
  260. properties,
  261. sort,
  262. queries,
  263. operator,
  264. modelName: "playlist",
  265. blacklistedProperties: [],
  266. specialProperties: {
  267. totalLength: [
  268. {
  269. $addFields: {
  270. totalLength: { $sum: "$songs.duration" }
  271. }
  272. }
  273. ],
  274. songsCount: [
  275. {
  276. $addFields: {
  277. songsCount: { $size: "$songs" }
  278. }
  279. }
  280. ],
  281. createdBy: [
  282. {
  283. $addFields: {
  284. createdByOID: {
  285. $convert: {
  286. input: "$createdBy",
  287. to: "objectId",
  288. onError: "unknown",
  289. onNull: "unknown"
  290. }
  291. }
  292. }
  293. },
  294. {
  295. $lookup: {
  296. from: "users",
  297. localField: "createdByOID",
  298. foreignField: "_id",
  299. as: "createdByUser"
  300. }
  301. },
  302. {
  303. $unwind: {
  304. path: "$createdByUser",
  305. preserveNullAndEmptyArrays: true
  306. }
  307. },
  308. {
  309. $addFields: {
  310. createdByUsername: {
  311. $cond: [
  312. { $eq: ["$createdBy", "Musare"] },
  313. "Musare",
  314. { $ifNull: ["$createdByUser.username", "unknown"] }
  315. ]
  316. }
  317. }
  318. },
  319. {
  320. $project: {
  321. createdByOID: 0,
  322. createdByUser: 0
  323. }
  324. }
  325. ]
  326. },
  327. specialQueries: {
  328. createdBy: newQuery => ({ $or: [newQuery, { createdByUsername: newQuery.createdBy }] })
  329. }
  330. },
  331. this
  332. )
  333. .then(response => {
  334. next(null, response);
  335. })
  336. .catch(err => {
  337. next(err);
  338. });
  339. }
  340. ],
  341. async (err, response) => {
  342. if (err) {
  343. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  344. this.log("ERROR", "PLAYLISTS_GET_DATA", `Failed to get data from playlists. "${err}"`);
  345. return cb({ status: "error", message: err });
  346. }
  347. this.log("SUCCESS", "PLAYLISTS_GET_DATA", `Got data from playlists successfully.`);
  348. return cb({ status: "success", message: "Successfully got data from playlists.", data: response });
  349. }
  350. );
  351. }),
  352. /**
  353. * Searches through all playlists that can be included in a community station
  354. *
  355. * @param {object} session - the session object automatically added by the websocket
  356. * @param {string} query - the query
  357. * @param {string} query - the page
  358. * @param {Function} cb - gets called with the result
  359. */
  360. searchCommunity: isLoginRequired(async function searchCommunity(session, query, page, cb) {
  361. async.waterfall(
  362. [
  363. next => {
  364. if ((!query && query !== "") || typeof query !== "string") next("Invalid query.");
  365. else next();
  366. },
  367. next => {
  368. PlaylistsModule.runJob("SEARCH", {
  369. query,
  370. includeUser: true,
  371. includeGenre: true,
  372. includeArtist: true,
  373. includeOwn: true,
  374. includeSongs: true,
  375. userId: session.userId,
  376. page
  377. })
  378. .then(response => {
  379. next(null, response);
  380. })
  381. .catch(err => {
  382. next(err);
  383. });
  384. }
  385. ],
  386. async (err, data) => {
  387. if (err) {
  388. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  389. this.log("ERROR", "PLAYLISTS_SEARCH_COMMUNITY", `Searching playlists failed. "${err}"`);
  390. return cb({ status: "error", message: err });
  391. }
  392. this.log("SUCCESS", "PLAYLISTS_SEARCH_COMMUNITY", "Searching playlists successful.");
  393. return cb({ status: "success", data });
  394. }
  395. );
  396. }),
  397. /**
  398. * Searches through all playlists that can be included in an official station
  399. *
  400. * @param {object} session - the session object automatically added by the websocket
  401. * @param {string} query - the query
  402. * @param {string} query - the page
  403. * @param {Function} cb - gets called with the result
  404. */
  405. searchOfficial: isAdminRequired(async function searchOfficial(session, query, page, cb) {
  406. async.waterfall(
  407. [
  408. next => {
  409. if ((!query && query !== "") || typeof query !== "string") next("Invalid query.");
  410. else next();
  411. },
  412. next => {
  413. PlaylistsModule.runJob("SEARCH", {
  414. query,
  415. includeGenre: true,
  416. includePrivate: true,
  417. includeSongs: true,
  418. page
  419. })
  420. .then(response => {
  421. next(null, response);
  422. })
  423. .catch(err => {
  424. next(err);
  425. });
  426. }
  427. ],
  428. async (err, data) => {
  429. if (err) {
  430. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  431. this.log("ERROR", "PLAYLISTS_SEARCH_OFFICIAL", `Searching playlists failed. "${err}"`);
  432. return cb({ status: "error", message: err });
  433. }
  434. this.log("SUCCESS", "PLAYLISTS_SEARCH_OFFICIAL", "Searching playlists successful.");
  435. return cb({ status: "success", data });
  436. }
  437. );
  438. }),
  439. /**
  440. * Gets the first song from a private playlist
  441. *
  442. * @param {object} session - the session object automatically added by the websocket
  443. * @param {string} playlistId - the id of the playlist we are getting the first song from
  444. * @param {Function} cb - gets called with the result
  445. */
  446. getFirstSong: isLoginRequired(function getFirstSong(session, playlistId, cb) {
  447. async.waterfall(
  448. [
  449. next => {
  450. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  451. .then(playlist => next(null, playlist))
  452. .catch(next);
  453. },
  454. (playlist, next) => {
  455. if (!playlist || playlist.createdBy !== session.userId) return next("Playlist not found.");
  456. playlist.songs.sort((a, b) => a.position - b.position);
  457. return next(null, playlist.songs[0]);
  458. }
  459. ],
  460. async (err, song) => {
  461. if (err) {
  462. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  463. this.log(
  464. "ERROR",
  465. "PLAYLIST_GET_FIRST_SONG",
  466. `Getting the first song of playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  467. );
  468. return cb({ status: "error", message: err });
  469. }
  470. this.log(
  471. "SUCCESS",
  472. "PLAYLIST_GET_FIRST_SONG",
  473. `Successfully got the first song of playlist "${playlistId}" for user "${session.userId}".`
  474. );
  475. return cb({
  476. status: "success",
  477. data: { song }
  478. });
  479. }
  480. );
  481. }),
  482. /**
  483. * Gets a list of all the playlists for a specific user
  484. *
  485. * @param {object} session - the session object automatically added by the websocket
  486. * @param {string} userId - the user id in question
  487. * @param {Function} cb - gets called with the result
  488. */
  489. indexForUser: async function indexForUser(session, userId, cb) {
  490. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  491. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  492. async.waterfall(
  493. [
  494. next => {
  495. userModel.findById(userId).select({ "preferences.orderOfPlaylists": -1 }).exec(next);
  496. },
  497. (user, next) => {
  498. if (!user) next("User not found");
  499. else {
  500. const { preferences } = user;
  501. const { orderOfPlaylists } = preferences;
  502. const match = {
  503. createdBy: userId,
  504. type: { $in: ["user", "user-liked", "user-disliked"] }
  505. };
  506. // if a playlist order exists
  507. if (orderOfPlaylists > 0) match._id = { $in: orderOfPlaylists };
  508. playlistModel
  509. .aggregate()
  510. .match(match)
  511. .addFields({
  512. weight: { $indexOfArray: [orderOfPlaylists, "$_id"] }
  513. })
  514. .sort({ weight: 1 })
  515. .exec(next);
  516. }
  517. },
  518. (playlists, next) => {
  519. if (session.userId === userId) return next(null, playlists); // user requesting playlists is the owner of the playlists
  520. const filteredPlaylists = [];
  521. return async.each(
  522. playlists,
  523. (playlist, nextPlaylist) => {
  524. if (playlist.privacy === "public") filteredPlaylists.push(playlist);
  525. return nextPlaylist();
  526. },
  527. () => next(null, filteredPlaylists)
  528. );
  529. }
  530. ],
  531. async (err, playlists) => {
  532. if (err) {
  533. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  534. this.log(
  535. "ERROR",
  536. "PLAYLIST_INDEX_FOR_USER",
  537. `Indexing playlists for user "${userId}" failed. "${err}"`
  538. );
  539. return cb({ status: "error", message: err });
  540. }
  541. this.log("SUCCESS", "PLAYLIST_INDEX_FOR_USER", `Successfully indexed playlists for user "${userId}".`);
  542. return cb({
  543. status: "success",
  544. data: { playlists }
  545. });
  546. }
  547. );
  548. },
  549. /**
  550. * Gets all playlists for the user requesting it
  551. *
  552. * @param {object} session - the session object automatically added by the websocket
  553. * @param {Function} cb - gets called with the result
  554. */
  555. indexMyPlaylists: isLoginRequired(async function indexMyPlaylists(session, cb) {
  556. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  557. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  558. async.waterfall(
  559. [
  560. next => {
  561. userModel.findById(session.userId).select({ "preferences.orderOfPlaylists": -1 }).exec(next);
  562. },
  563. (user, next) => {
  564. if (!user) next("User not found");
  565. else {
  566. const { preferences } = user;
  567. const { orderOfPlaylists } = preferences;
  568. const match = {
  569. createdBy: session.userId,
  570. type: { $in: ["user", "user-liked", "user-disliked"] }
  571. };
  572. // if a playlist order exists
  573. if (orderOfPlaylists > 0) match._id = { $in: orderOfPlaylists };
  574. playlistModel
  575. .aggregate()
  576. .match(match)
  577. .addFields({
  578. weight: { $indexOfArray: [orderOfPlaylists, "$_id"] }
  579. })
  580. .sort({ weight: 1 })
  581. .exec(next);
  582. }
  583. }
  584. ],
  585. async (err, playlists) => {
  586. if (err) {
  587. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  588. this.log(
  589. "ERROR",
  590. "PLAYLIST_INDEX_FOR_ME",
  591. `Indexing playlists for user "${session.userId}" failed. "${err}"`
  592. );
  593. return cb({ status: "error", message: err });
  594. }
  595. this.log(
  596. "SUCCESS",
  597. "PLAYLIST_INDEX_FOR_ME",
  598. `Successfully indexed playlists for user "${session.userId}".`
  599. );
  600. return cb({
  601. status: "success",
  602. data: { playlists }
  603. });
  604. }
  605. );
  606. }),
  607. /**
  608. * Gets all playlists playlists
  609. *
  610. * @param {object} session - the session object automatically added by the websocket
  611. * @param {Function} cb - gets called with the result
  612. */
  613. indexFeaturedPlaylists: isLoginRequired(async function indexMyPlaylists(session, cb) {
  614. async.waterfall(
  615. [
  616. next => {
  617. const featuredPlaylistIds = config.get("featuredPlaylists");
  618. if (featuredPlaylistIds.length === 0) next(true, []);
  619. else next(null, featuredPlaylistIds);
  620. },
  621. (featuredPlaylistIds, next) => {
  622. const featuredPlaylists = [];
  623. async.eachLimit(
  624. featuredPlaylistIds,
  625. 1,
  626. (playlistId, next) => {
  627. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  628. .then(playlist => {
  629. if (playlist.privacy === "public") featuredPlaylists.push(playlist);
  630. next();
  631. })
  632. .catch(next);
  633. },
  634. err => {
  635. next(err, featuredPlaylists);
  636. }
  637. );
  638. }
  639. ],
  640. async (err, playlists) => {
  641. if (err && err !== true) {
  642. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  643. this.log("ERROR", "PLAYLIST_INDEX_FEATURED", `Indexing featured playlists failed. "${err}"`);
  644. return cb({ status: "error", message: err });
  645. }
  646. this.log("SUCCESS", "PLAYLIST_INDEX_FEATURED", `Successfully indexed featured playlists.`);
  647. return cb({
  648. status: "success",
  649. data: { playlists }
  650. });
  651. }
  652. );
  653. }),
  654. /**
  655. * Creates a new private playlist
  656. *
  657. * @param {object} session - the session object automatically added by the websocket
  658. * @param {object} data - the data for the new private playlist
  659. * @param {Function} cb - gets called with the result
  660. */
  661. create: isLoginRequired(async function create(session, data, cb) {
  662. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  663. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  664. const blacklist = ["liked songs", "likedsongs", "disliked songs", "dislikedsongs"];
  665. async.waterfall(
  666. [
  667. next => (data ? next() : cb({ status: "error", message: "Invalid data" })),
  668. next => {
  669. const { displayName, songs, privacy } = data;
  670. if (blacklist.indexOf(displayName.toLowerCase()) !== -1)
  671. return next("That playlist name is blacklisted. Please use a different name.");
  672. return playlistModel.create(
  673. {
  674. displayName,
  675. songs,
  676. privacy,
  677. createdBy: session.userId,
  678. createdAt: Date.now(),
  679. createdFor: null,
  680. type: "user"
  681. },
  682. next
  683. );
  684. },
  685. (playlist, next) => {
  686. userModel.updateOne(
  687. { _id: session.userId },
  688. { $push: { "preferences.orderOfPlaylists": playlist._id } },
  689. err => {
  690. if (err) return next(err);
  691. return next(null, playlist);
  692. }
  693. );
  694. }
  695. ],
  696. async (err, playlist) => {
  697. if (err) {
  698. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  699. this.log(
  700. "ERROR",
  701. "PLAYLIST_CREATE",
  702. `Creating private playlist failed for user "${session.userId}". "${err}"`
  703. );
  704. return cb({ status: "error", message: err });
  705. }
  706. CacheModule.runJob("PUB", {
  707. channel: "playlist.create",
  708. value: playlist
  709. });
  710. ActivitiesModule.runJob("ADD_ACTIVITY", {
  711. userId: playlist.createdBy,
  712. type: "playlist__create",
  713. payload: {
  714. message: `Created playlist <playlistId>${playlist.displayName}</playlistId>`,
  715. playlistId: playlist._id
  716. }
  717. });
  718. this.log(
  719. "SUCCESS",
  720. "PLAYLIST_CREATE",
  721. `Successfully created private playlist for user "${session.userId}".`
  722. );
  723. return cb({
  724. status: "success",
  725. message: "Successfully created playlist",
  726. data: {
  727. playlistId: playlist._id
  728. }
  729. });
  730. }
  731. );
  732. }),
  733. /**
  734. * Gets a playlist from id
  735. *
  736. * @param {object} session - the session object automatically added by the websocket
  737. * @param {string} playlistId - the id of the playlist we are getting
  738. * @param {Function} cb - gets called with the result
  739. */
  740. getPlaylist: function getPlaylist(session, playlistId, cb) {
  741. async.waterfall(
  742. [
  743. next => {
  744. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  745. .then(playlist => next(null, playlist))
  746. .catch(next);
  747. },
  748. (playlist, next) => {
  749. if (!playlist) return next("Playlist not found");
  750. if (playlist.privacy !== "public" && playlist.createdBy !== session.userId) {
  751. if (session)
  752. // check if user requested to get a playlist is an admin
  753. return DBModule.runJob("GET_MODEL", { modelName: "user" }, this).then(userModel => {
  754. userModel.findOne({ _id: session.userId }, (err, user) => {
  755. if (user && user.role === "admin") return next(null, playlist);
  756. return next("User unauthorised to view playlist.");
  757. });
  758. });
  759. return next("User unauthorised to view playlist.");
  760. }
  761. return next(null, playlist);
  762. }
  763. ],
  764. async (err, playlist) => {
  765. if (err) {
  766. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  767. this.log(
  768. "ERROR",
  769. "PLAYLIST_GET",
  770. `Getting private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  771. );
  772. return cb({ status: "error", message: err });
  773. }
  774. this.log(
  775. "SUCCESS",
  776. "PLAYLIST_GET",
  777. `Successfully got private playlist "${playlistId}" for user "${session.userId}".`
  778. );
  779. return cb({
  780. status: "success",
  781. data: { playlist }
  782. });
  783. }
  784. );
  785. },
  786. /**
  787. * Gets a playlist from station id
  788. *
  789. * @param {object} session - the session object automatically added by the websocket
  790. * @param {string} stationId - the id of the station we are getting
  791. * @param {string} includeSongs - include songs
  792. * @param {Function} cb - gets called with the result
  793. */
  794. getPlaylistForStation: function getPlaylist(session, stationId, includeSongs, cb) {
  795. async.waterfall(
  796. [
  797. next => {
  798. PlaylistsModule.runJob("GET_STATION_PLAYLIST", { stationId, includeSongs }, this)
  799. .then(response => next(null, response.playlist))
  800. .catch(next);
  801. },
  802. (playlist, next) => {
  803. if (!playlist) return next("Playlist not found");
  804. if (playlist.privacy !== "public" && playlist.createdBy !== session.userId) {
  805. if (session)
  806. // check if user requested to get a playlist is an admin
  807. return DBModule.runJob("GET_MODEL", { modelName: "user" }, this).then(userModel => {
  808. userModel.findOne({ _id: session.userId }, (err, user) => {
  809. if (user && user.role === "admin") return next(null, playlist);
  810. return next("User unauthorised to view playlist.");
  811. });
  812. });
  813. return next("User unauthorised to view playlist.");
  814. }
  815. return next(null, playlist);
  816. }
  817. ],
  818. async (err, playlist) => {
  819. if (err) {
  820. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  821. this.log(
  822. "ERROR",
  823. "PLAYLIST_GET",
  824. `Getting playlist for station "${stationId}" failed for user "${session.userId}". "${err}"`
  825. );
  826. return cb({ status: "error", message: err });
  827. }
  828. this.log(
  829. "SUCCESS",
  830. "PLAYLIST_GET",
  831. `Successfully got playlist for station "${stationId}" for user "${session.userId}".`
  832. );
  833. return cb({
  834. status: "success",
  835. data: { playlist }
  836. });
  837. }
  838. );
  839. },
  840. /**
  841. * Shuffles songs in a private playlist
  842. *
  843. * @param {object} session - the session object automatically added by the websocket
  844. * @param {string} playlistId - the id of the playlist we are updating
  845. * @param {Function} cb - gets called with the result
  846. */
  847. shuffle: isLoginRequired(async function shuffle(session, playlistId, cb) {
  848. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  849. async.waterfall(
  850. [
  851. next => {
  852. if (!playlistId) return next("No playlist id.");
  853. return next();
  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 next(null, playlist);
  861. })
  862. .catch(next);
  863. },
  864. (playlist, next) => {
  865. if (!playlist.isUserModifiable) return next("Playlist cannot be shuffled.");
  866. return UtilsModule.runJob("SHUFFLE_SONG_POSITIONS", { array: playlist.songs }, this)
  867. .then(result => next(null, result.array))
  868. .catch(next);
  869. },
  870. (songs, next) => {
  871. playlistModel.updateOne({ _id: playlistId }, { $set: { songs } }, { runValidators: true }, next);
  872. },
  873. (res, next) => {
  874. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  875. .then(playlist => next(null, playlist))
  876. .catch(next);
  877. }
  878. ],
  879. async (err, playlist) => {
  880. if (err) {
  881. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  882. this.log(
  883. "ERROR",
  884. "PLAYLIST_SHUFFLE",
  885. `Updating private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  886. );
  887. return cb({ status: "error", message: err });
  888. }
  889. this.log(
  890. "SUCCESS",
  891. "PLAYLIST_SHUFFLE",
  892. `Successfully updated private playlist "${playlistId}" for user "${session.userId}".`
  893. );
  894. return cb({
  895. status: "success",
  896. message: "Successfully shuffled playlist.",
  897. data: { playlist }
  898. });
  899. }
  900. );
  901. }),
  902. /**
  903. * Changes the order (position) of a song in a playlist
  904. *
  905. * @param {object} session - the session object automatically added by the websocket
  906. * @param {string} playlistId - the id of the playlist we are targeting
  907. * @param {object} song - the song to be repositioned
  908. * @param {string} song.youtubeId - the youtube id of the song being repositioned
  909. * @param {string} song.newIndex - the new position of the song in the playlist
  910. * @param {...any} song.args - any other elements that would be included with a song item in a playlist
  911. * @param {Function} cb - gets called with the result
  912. */
  913. repositionSong: isLoginRequired(async function repositionSong(session, playlistId, song, cb) {
  914. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  915. async.waterfall(
  916. [
  917. next => {
  918. if (!playlistId) return next("Please provide a playlist.");
  919. if (!song || !song.youtubeId) return next("You must provide a song to reposition.");
  920. return next();
  921. },
  922. next => {
  923. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  924. .then(playlist => {
  925. if (!playlist || playlist.createdBy !== session.userId) {
  926. return DBModule.runJob("GET_MODEL", { modelName: "user" }, this).then(userModel => {
  927. userModel.findOne({ _id: session.userId }, (err, user) => {
  928. if (user && user.role === "admin") return next();
  929. return next("Something went wrong when trying to get the playlist");
  930. });
  931. });
  932. }
  933. return next();
  934. })
  935. .catch(next);
  936. },
  937. // remove song from playlist
  938. next => {
  939. playlistModel.updateOne(
  940. { _id: playlistId },
  941. { $pull: { songs: { youtubeId: song.youtubeId } } },
  942. next
  943. );
  944. },
  945. // add song back to playlist (in new position)
  946. (res, next) => {
  947. playlistModel.updateOne(
  948. { _id: playlistId },
  949. { $push: { songs: { $each: [song], $position: song.newIndex } } },
  950. err => next(err)
  951. );
  952. },
  953. // update the cache with the new songs positioning
  954. next => {
  955. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  956. .then(playlist => next(null, playlist))
  957. .catch(next);
  958. }
  959. ],
  960. async err => {
  961. if (err) {
  962. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  963. this.log(
  964. "ERROR",
  965. "PLAYLIST_REPOSITION_SONG",
  966. `Repositioning song ${song.youtubeId} for private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  967. );
  968. return cb({ status: "error", message: err });
  969. }
  970. this.log(
  971. "SUCCESS",
  972. "PLAYLIST_REPOSITION_SONG",
  973. `Successfully repositioned song ${song.youtubeId} for private playlist "${playlistId}" for user "${session.userId}".`
  974. );
  975. CacheModule.runJob("PUB", {
  976. channel: "playlist.repositionSong",
  977. value: {
  978. userId: session.userId,
  979. playlistId,
  980. song
  981. }
  982. });
  983. return cb({
  984. status: "success",
  985. message: "Successfully repositioned song"
  986. });
  987. }
  988. );
  989. }),
  990. /**
  991. * Adds a song to a private playlist
  992. *
  993. * @param {object} session - the session object automatically added by the websocket
  994. * @param {boolean} isSet - is the song part of a set of songs to be added
  995. * @param {string} youtubeId - the youtube id of the song we are trying to add
  996. * @param {string} playlistId - the id of the playlist we are adding the song to
  997. * @param {Function} cb - gets called with the result
  998. */
  999. addSongToPlaylist: isLoginRequired(async function addSongToPlaylist(session, isSet, youtubeId, playlistId, cb) {
  1000. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  1001. async.waterfall(
  1002. [
  1003. next => {
  1004. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1005. .then(playlist => {
  1006. if (!playlist || playlist.createdBy !== session.userId) {
  1007. DBModule.runJob("GET_MODEL", { modelName: "user" }, this).then(userModel => {
  1008. userModel.findOne({ _id: session.userId }, (err, user) => {
  1009. if (user && user.role === "admin") return next(null, playlist);
  1010. return next("Something went wrong when trying to get the playlist");
  1011. });
  1012. });
  1013. } else next(null, playlist);
  1014. })
  1015. .catch(next);
  1016. },
  1017. (playlist, next) => {
  1018. if (playlist.type === "user-liked" || playlist.type === "user-disliked") {
  1019. const oppositeType = playlist.type === "user-liked" ? "user-disliked" : "user-liked";
  1020. const oppositePlaylistName = oppositeType === "user-liked" ? "Liked Songs" : "Disliked Songs";
  1021. playlistModel.count(
  1022. { type: oppositeType, createdBy: session.userId, "songs.youtubeId": youtubeId },
  1023. (err, results) => {
  1024. if (err) next(err);
  1025. else if (results > 0)
  1026. next(
  1027. `That song is already in your ${oppositePlaylistName} playlist. A song cannot be in both the Liked Songs playlist and the Disliked Songs playlist at the same time.`
  1028. );
  1029. else next();
  1030. }
  1031. );
  1032. } else next();
  1033. },
  1034. next => {
  1035. PlaylistsModule.runJob("ADD_SONG_TO_PLAYLIST", { playlistId, youtubeId }, this)
  1036. .then(res => {
  1037. const { playlist, song, ratings } = res;
  1038. next(null, playlist, song, ratings);
  1039. })
  1040. .catch(next);
  1041. }
  1042. ],
  1043. async (err, playlist, newSong, ratings) => {
  1044. if (err) {
  1045. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1046. this.log(
  1047. "ERROR",
  1048. "PLAYLIST_ADD_SONG",
  1049. `Adding song "${youtubeId}" to private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1050. );
  1051. return cb({ status: "error", message: err });
  1052. }
  1053. this.log(
  1054. "SUCCESS",
  1055. "PLAYLIST_ADD_SONG",
  1056. `Successfully added song "${youtubeId}" to private playlist "${playlistId}" for user "${session.userId}".`
  1057. );
  1058. if (!isSet && playlist.type === "user" && playlist.privacy === "public") {
  1059. const songName = newSong.artists
  1060. ? `${newSong.title} by ${newSong.artists.join(", ")}`
  1061. : newSong.title;
  1062. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1063. userId: session.userId,
  1064. type: "playlist__add_song",
  1065. payload: {
  1066. message: `Added <youtubeId>${songName}</youtubeId> to playlist <playlistId>${playlist.displayName}</playlistId>`,
  1067. thumbnail: newSong.thumbnail,
  1068. playlistId,
  1069. youtubeId
  1070. }
  1071. });
  1072. }
  1073. CacheModule.runJob("PUB", {
  1074. channel: "playlist.addSong",
  1075. value: {
  1076. playlistId: playlist._id,
  1077. song: newSong,
  1078. userId: session.userId,
  1079. privacy: playlist.privacy
  1080. }
  1081. });
  1082. CacheModule.runJob("PUB", {
  1083. channel: "playlist.updated",
  1084. value: { playlistId }
  1085. });
  1086. if (ratings && (playlist.type === "user-liked" || playlist.type === "user-disliked")) {
  1087. const { _id, youtubeId, title, artists, thumbnail } = newSong;
  1088. const { likes, dislikes } = ratings;
  1089. SongsModule.runJob("UPDATE_SONG", { songId: _id });
  1090. if (playlist.type === "user-liked") {
  1091. CacheModule.runJob("PUB", {
  1092. channel: "song.like",
  1093. value: JSON.stringify({
  1094. youtubeId,
  1095. userId: session.userId,
  1096. likes,
  1097. dislikes
  1098. })
  1099. });
  1100. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1101. userId: session.userId,
  1102. type: "song__like",
  1103. payload: {
  1104. message: `Liked song <youtubeId>${title} by ${artists.join(", ")}</youtubeId>`,
  1105. youtubeId,
  1106. thumbnail
  1107. }
  1108. });
  1109. } else {
  1110. CacheModule.runJob("PUB", {
  1111. channel: "song.dislike",
  1112. value: JSON.stringify({
  1113. youtubeId,
  1114. userId: session.userId,
  1115. likes,
  1116. dislikes
  1117. })
  1118. });
  1119. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1120. userId: session.userId,
  1121. type: "song__dislike",
  1122. payload: {
  1123. message: `Disliked song <youtubeId>${title} by ${artists.join(", ")}</youtubeId>`,
  1124. youtubeId,
  1125. thumbnail
  1126. }
  1127. });
  1128. }
  1129. }
  1130. return cb({
  1131. status: "success",
  1132. message: "Song has been successfully added to the playlist",
  1133. data: { songs: playlist.songs }
  1134. });
  1135. }
  1136. );
  1137. }),
  1138. /**
  1139. * Adds a set of songs to a private playlist
  1140. *
  1141. * @param {object} session - the session object automatically added by the websocket
  1142. * @param {string} url - the url of the the YouTube playlist
  1143. * @param {string} playlistId - the id of the playlist we are adding the set of songs to
  1144. * @param {boolean} musicOnly - whether to only add music to the playlist
  1145. * @param {Function} cb - gets called with the result
  1146. */
  1147. addSetToPlaylist: isLoginRequired(async function addSetToPlaylist(session, url, playlistId, musicOnly, cb) {
  1148. let videosInPlaylistTotal = 0;
  1149. let songsInPlaylistTotal = 0;
  1150. let addSongsStats = null;
  1151. const addedSongs = [];
  1152. this.keepLongJob();
  1153. this.publishProgress({
  1154. status: "started",
  1155. title: "Import YouTube playlist",
  1156. message: "Importing YouTube playlist.",
  1157. id: this.toString()
  1158. });
  1159. await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
  1160. await CacheModule.runJob(
  1161. "PUB",
  1162. {
  1163. channel: "longJob.added",
  1164. value: { jobId: this.toString(), userId: session.userId }
  1165. },
  1166. this
  1167. );
  1168. async.waterfall(
  1169. [
  1170. next => {
  1171. this.publishProgress({ status: "update", message: `Importing YouTube playlist (stage 1)` });
  1172. YouTubeModule.runJob("GET_PLAYLIST", { url, musicOnly }, this)
  1173. .then(res => {
  1174. if (res.filteredSongs) {
  1175. videosInPlaylistTotal = res.songs.length;
  1176. songsInPlaylistTotal = res.filteredSongs.length;
  1177. } else {
  1178. songsInPlaylistTotal = videosInPlaylistTotal = res.songs.length;
  1179. }
  1180. next(null, res.songs);
  1181. })
  1182. .catch(err => {
  1183. next(err);
  1184. });
  1185. },
  1186. (youtubeIds, next) => {
  1187. this.publishProgress({ status: "update", message: `Importing YouTube playlist (stage 2)` });
  1188. let successful = 0;
  1189. let failed = 0;
  1190. let alreadyInPlaylist = 0;
  1191. let alreadyInLikedPlaylist = 0;
  1192. let alreadyInDislikedPlaylist = 0;
  1193. if (youtubeIds.length === 0) next();
  1194. async.eachLimit(
  1195. youtubeIds,
  1196. 1,
  1197. (youtubeId, next) => {
  1198. WSModule.runJob(
  1199. "RUN_ACTION2",
  1200. {
  1201. session,
  1202. namespace: "playlists",
  1203. action: "addSongToPlaylist",
  1204. args: [true, youtubeId, playlistId]
  1205. },
  1206. this
  1207. )
  1208. .then(res => {
  1209. if (res.status === "success") {
  1210. successful += 1;
  1211. addedSongs.push(youtubeId);
  1212. } else failed += 1;
  1213. if (res.message === "That song is already in the playlist") alreadyInPlaylist += 1;
  1214. else if (
  1215. res.message ===
  1216. "That song is already in your Liked Songs playlist. " +
  1217. "A song cannot be in both the Liked Songs playlist" +
  1218. " and the Disliked Songs playlist at the same time."
  1219. )
  1220. alreadyInLikedPlaylist += 1;
  1221. else if (
  1222. res.message ===
  1223. "That song is already in your Disliked Songs playlist. " +
  1224. "A song cannot be in both the Liked Songs playlist " +
  1225. "and the Disliked Songs playlist at the same time."
  1226. )
  1227. alreadyInDislikedPlaylist += 1;
  1228. })
  1229. .catch(() => {
  1230. failed += 1;
  1231. })
  1232. .finally(() => next());
  1233. },
  1234. () => {
  1235. addSongsStats = {
  1236. successful,
  1237. failed,
  1238. alreadyInPlaylist,
  1239. alreadyInLikedPlaylist,
  1240. alreadyInDislikedPlaylist
  1241. };
  1242. next(null);
  1243. }
  1244. );
  1245. },
  1246. next => {
  1247. this.publishProgress({ status: "update", message: `Importing YouTube playlist (stage 3)` });
  1248. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1249. .then(playlist => next(null, playlist))
  1250. .catch(next);
  1251. },
  1252. (playlist, next) => {
  1253. this.publishProgress({ status: "update", message: `Importing YouTube playlist (stage 4)` });
  1254. if (!playlist || playlist.createdBy !== session.userId) {
  1255. return DBModule.runJob("GET_MODEL", { modelName: "user" }, this).then(userModel => {
  1256. userModel.findOne({ _id: session.userId }, (err, user) => {
  1257. if (user && user.role === "admin") return next(null, playlist);
  1258. return next("Something went wrong when trying to get the playlist");
  1259. });
  1260. });
  1261. }
  1262. return next(null, playlist);
  1263. }
  1264. ],
  1265. async (err, playlist) => {
  1266. if (err) {
  1267. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1268. this.log(
  1269. "ERROR",
  1270. "PLAYLIST_IMPORT",
  1271. `Importing a YouTube playlist to private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1272. );
  1273. this.publishProgress({
  1274. status: "error",
  1275. message: err
  1276. });
  1277. return cb({ status: "error", message: err });
  1278. }
  1279. if (playlist.privacy === "public")
  1280. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1281. userId: session.userId,
  1282. type: "playlist__import_playlist",
  1283. payload: {
  1284. message: `Imported ${addSongsStats.successful} songs to playlist <playlistId>${playlist.displayName}</playlistId>`,
  1285. playlistId
  1286. }
  1287. });
  1288. this.log(
  1289. "SUCCESS",
  1290. "PLAYLIST_IMPORT",
  1291. `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}, already in liked ${addSongsStats.alreadyInLikedPlaylist}, already in disliked ${addSongsStats.alreadyInDislikedPlaylist}.`
  1292. );
  1293. this.publishProgress({
  1294. status: "success",
  1295. message: `Playlist has been imported. ${addSongsStats.successful} were added successfully, ${addSongsStats.failed} failed (${addSongsStats.alreadyInPlaylist} were already in the playlist)`
  1296. });
  1297. return cb({
  1298. status: "success",
  1299. message: `Playlist has been imported. ${addSongsStats.successful} were added successfully, ${addSongsStats.failed} failed (${addSongsStats.alreadyInPlaylist} were already in the playlist)`,
  1300. data: {
  1301. songs: playlist.songs,
  1302. stats: {
  1303. videosInPlaylistTotal,
  1304. songsInPlaylistTotal,
  1305. alreadyInLikedPlaylist: addSongsStats.alreadyInLikedPlaylist,
  1306. alreadyInDislikedPlaylist: addSongsStats.alreadyInDislikedPlaylist
  1307. }
  1308. }
  1309. });
  1310. }
  1311. );
  1312. }),
  1313. /**
  1314. * Removes a song from a private playlist
  1315. *
  1316. * @param {object} session - the session object automatically added by the websocket
  1317. * @param {string} youtubeId - the youtube id of the song we are removing from the private playlist
  1318. * @param {string} playlistId - the id of the playlist we are removing the song from
  1319. * @param {Function} cb - gets called with the result
  1320. */
  1321. removeSongFromPlaylist: isLoginRequired(async function removeSongFromPlaylist(session, youtubeId, playlistId, cb) {
  1322. async.waterfall(
  1323. [
  1324. next => {
  1325. if (!youtubeId || typeof youtubeId !== "string") return next("Invalid song id.");
  1326. if (!playlistId || typeof youtubeId !== "string") return next("Invalid playlist id.");
  1327. return next();
  1328. },
  1329. next => {
  1330. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1331. .then(playlist => {
  1332. if (!playlist || playlist.createdBy !== session.userId) {
  1333. return DBModule.runJob("GET_MODEL", { modelName: "user" }, this).then(userModel => {
  1334. userModel.findOne({ _id: session.userId }, (err, user) => {
  1335. if (user && user.role === "admin") return next(null, playlist);
  1336. return next("Something went wrong when trying to get the playlist");
  1337. });
  1338. });
  1339. }
  1340. return next(null, playlist);
  1341. })
  1342. .catch(next);
  1343. },
  1344. (playlist, next) => {
  1345. MediaModule.runJob("GET_MEDIA", { youtubeId }, this)
  1346. .then(res =>
  1347. next(null, playlist, {
  1348. _id: res.song._id,
  1349. title: res.song.title,
  1350. thumbnail: res.song.thumbnail,
  1351. artists: res.song.artists,
  1352. youtubeId: res.song.youtubeId
  1353. })
  1354. )
  1355. .catch(next);
  1356. },
  1357. (playlist, newSong, next) => {
  1358. PlaylistsModule.runJob("REMOVE_FROM_PLAYLIST", { playlistId, youtubeId }, this)
  1359. .then(res => {
  1360. const { ratings } = res;
  1361. next(null, playlist, newSong, ratings);
  1362. })
  1363. .catch(next);
  1364. },
  1365. (playlist, newSong, ratings, next) => {
  1366. const { _id, title, artists, thumbnail } = newSong;
  1367. const songName = artists ? `${title} by ${artists.join(", ")}` : title;
  1368. if (playlist.type === "user" && playlist.privacy === "public") {
  1369. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1370. userId: session.userId,
  1371. type: "playlist__remove_song",
  1372. payload: {
  1373. message: `Removed <youtubeId>${songName}</youtubeId> from playlist <playlistId>${playlist.displayName}</playlistId>`,
  1374. thumbnail,
  1375. playlistId,
  1376. youtubeId: newSong.youtubeId
  1377. }
  1378. });
  1379. }
  1380. if (ratings && (playlist.type === "user-liked" || playlist.type === "user-disliked")) {
  1381. const { likes, dislikes } = ratings;
  1382. SongsModule.runJob("UPDATE_SONG", { songId: _id });
  1383. if (playlist.type === "user-liked") {
  1384. CacheModule.runJob("PUB", {
  1385. channel: "ratings.unlike",
  1386. value: JSON.stringify({
  1387. youtubeId: newSong.youtubeId,
  1388. userId: session.userId,
  1389. likes,
  1390. dislikes
  1391. })
  1392. });
  1393. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1394. userId: session.userId,
  1395. type: "song__unlike",
  1396. payload: {
  1397. message: `Removed <youtubeId>${title} by ${artists.join(
  1398. ", "
  1399. )}</youtubeId> from your Liked Songs`,
  1400. youtubeId: newSong.youtubeId,
  1401. thumbnail
  1402. }
  1403. });
  1404. } else {
  1405. CacheModule.runJob("PUB", {
  1406. channel: "song.undislike",
  1407. value: JSON.stringify({
  1408. youtubeId: newSong.youtubeId,
  1409. userId: session.userId,
  1410. likes,
  1411. dislikes
  1412. })
  1413. });
  1414. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1415. userId: session.userId,
  1416. type: "song__undislike",
  1417. payload: {
  1418. message: `Removed <youtubeId>${title} by ${artists.join(
  1419. ", "
  1420. )}</youtubeId> from your Disliked Songs`,
  1421. youtubeId: newSong.youtubeId,
  1422. thumbnail
  1423. }
  1424. });
  1425. }
  1426. }
  1427. return next(null, playlist);
  1428. }
  1429. ],
  1430. async (err, playlist) => {
  1431. if (err) {
  1432. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1433. this.log(
  1434. "ERROR",
  1435. "PLAYLIST_REMOVE_SONG",
  1436. `Removing song "${youtubeId}" from private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1437. );
  1438. return cb({ status: "error", message: err });
  1439. }
  1440. this.log(
  1441. "SUCCESS",
  1442. "PLAYLIST_REMOVE_SONG",
  1443. `Successfully removed song "${youtubeId}" from private playlist "${playlistId}" for user "${session.userId}".`
  1444. );
  1445. CacheModule.runJob("PUB", {
  1446. channel: "playlist.removeSong",
  1447. value: {
  1448. playlistId: playlist._id,
  1449. youtubeId,
  1450. userId: session.userId,
  1451. privacy: playlist.privacy
  1452. }
  1453. });
  1454. return cb({
  1455. status: "success",
  1456. message: "Song has been successfully removed from playlist",
  1457. data: { songs: playlist.songs }
  1458. });
  1459. }
  1460. );
  1461. }),
  1462. /**
  1463. * Updates the displayName of a private playlist
  1464. *
  1465. * @param {object} session - the session object automatically added by the websocket
  1466. * @param {string} playlistId - the id of the playlist we are updating the displayName for
  1467. * @param {Function} cb - gets called with the result
  1468. */
  1469. updateDisplayName: isLoginRequired(async function updateDisplayName(session, playlistId, displayName, cb) {
  1470. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  1471. async.waterfall(
  1472. [
  1473. next => {
  1474. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1475. .then(playlist => next(null, playlist))
  1476. .catch(next);
  1477. },
  1478. (playlist, next) => {
  1479. if (playlist.type !== "user") return next("Playlist cannot be modified.");
  1480. return next(null);
  1481. },
  1482. next => {
  1483. playlistModel.updateOne(
  1484. { _id: playlistId, createdBy: session.userId },
  1485. { $set: { displayName } },
  1486. { runValidators: true },
  1487. next
  1488. );
  1489. },
  1490. (res, next) => {
  1491. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  1492. .then(playlist => next(null, playlist))
  1493. .catch(next);
  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_DISPLAY_NAME",
  1502. `Updating display name to "${displayName}" for private 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_DISPLAY_NAME",
  1509. `Successfully updated display name to "${displayName}" for private playlist "${playlistId}" for user "${session.userId}".`
  1510. );
  1511. CacheModule.runJob("PUB", {
  1512. channel: "playlist.updateDisplayName",
  1513. value: {
  1514. playlistId,
  1515. displayName,
  1516. userId: session.userId,
  1517. privacy: playlist.privacy
  1518. }
  1519. });
  1520. CacheModule.runJob("PUB", {
  1521. channel: "playlist.updated",
  1522. value: { playlistId }
  1523. });
  1524. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1525. userId: session.userId,
  1526. type: "playlist__edit_display_name",
  1527. payload: {
  1528. message: `Changed display name of playlist <playlistId>${displayName}</playlistId>`,
  1529. playlistId
  1530. }
  1531. });
  1532. return cb({
  1533. status: "success",
  1534. message: "Playlist has been successfully updated"
  1535. });
  1536. }
  1537. );
  1538. }),
  1539. /**
  1540. * Removes a user's own modifiable user playlist
  1541. *
  1542. * @param {object} session - the session object automatically added by the websocket
  1543. * @param {string} playlistId - the id of the playlist we are removing
  1544. * @param {Function} cb - gets called with the result
  1545. */
  1546. remove: isLoginRequired(async function remove(session, playlistId, cb) {
  1547. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  1548. async.waterfall(
  1549. [
  1550. next => {
  1551. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1552. .then(playlist => next(null, playlist))
  1553. .catch(next);
  1554. },
  1555. (playlist, next) => {
  1556. if (playlist.createdBy !== session.userId) return next("You do not own this playlist.");
  1557. if (playlist.type !== "user") return next("Playlist cannot be removed.");
  1558. return next(null, playlist);
  1559. },
  1560. (playlist, next) => {
  1561. userModel.updateOne(
  1562. { _id: playlist.createdBy },
  1563. { $pull: { "preferences.orderOfPlaylists": playlist._id } },
  1564. err => next(err, playlist)
  1565. );
  1566. },
  1567. (playlist, next) => {
  1568. PlaylistsModule.runJob("DELETE_PLAYLIST", { playlistId }, this)
  1569. .then(() => next(null, playlist))
  1570. .catch(next);
  1571. }
  1572. ],
  1573. async (err, playlist) => {
  1574. if (err) {
  1575. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1576. this.log(
  1577. "ERROR",
  1578. "PLAYLIST_REMOVE",
  1579. `Removing private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1580. );
  1581. return cb({ status: "error", message: err });
  1582. }
  1583. this.log(
  1584. "SUCCESS",
  1585. "PLAYLIST_REMOVE",
  1586. `Successfully removed private playlist "${playlistId}" for user "${session.userId}".`
  1587. );
  1588. CacheModule.runJob("PUB", {
  1589. channel: "playlist.delete",
  1590. value: {
  1591. userId: session.userId,
  1592. playlistId
  1593. }
  1594. });
  1595. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1596. userId: playlist.createdBy,
  1597. type: "playlist__remove",
  1598. payload: {
  1599. message: `Removed playlist ${playlist.displayName}`
  1600. }
  1601. });
  1602. ActivitiesModule.runJob("REMOVE_ACTIVITY_REFERENCES", { type: "playlistId", playlistId });
  1603. return cb({
  1604. status: "success",
  1605. message: "Playlist successfully removed"
  1606. });
  1607. }
  1608. );
  1609. }),
  1610. /**
  1611. * Removes a user's modifiable user playlist as an admin
  1612. *
  1613. * @param {object} session - the session object automatically added by the websocket
  1614. * @param {string} playlistId - the id of the playlist we are removing
  1615. * @param {Function} cb - gets called with the result
  1616. */
  1617. removeAdmin: isAdminRequired(async function removeAdmin(session, playlistId, cb) {
  1618. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  1619. async.waterfall(
  1620. [
  1621. next => {
  1622. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1623. .then(playlist => next(null, playlist))
  1624. .catch(next);
  1625. },
  1626. (playlist, next) => {
  1627. if (playlist.type !== "user") return next("Playlist cannot be removed.");
  1628. return next(null, playlist);
  1629. },
  1630. (playlist, next) => {
  1631. userModel.updateOne(
  1632. { _id: playlist.createdBy },
  1633. { $pull: { "preferences.orderOfPlaylists": playlist._id } },
  1634. err => next(err, playlist, playlist.createdBy)
  1635. );
  1636. },
  1637. (playlist, playlistCreator, next) => {
  1638. PlaylistsModule.runJob("DELETE_PLAYLIST", { playlistId }, this)
  1639. .then(() => next(null, playlistCreator))
  1640. .catch(next);
  1641. }
  1642. ],
  1643. async (err, playlistCreator) => {
  1644. if (err) {
  1645. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1646. this.log(
  1647. "ERROR",
  1648. "PLAYLIST_REMOVE_ADMIN",
  1649. `Removing private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1650. );
  1651. return cb({ status: "error", message: err });
  1652. }
  1653. this.log(
  1654. "SUCCESS",
  1655. "PLAYLIST_REMOVE_ADMIN",
  1656. `Successfully removed private playlist "${playlistId}" for user "${session.userId}".`
  1657. );
  1658. CacheModule.runJob("PUB", {
  1659. channel: "playlist.delete",
  1660. value: {
  1661. userId: playlistCreator,
  1662. playlistId
  1663. }
  1664. });
  1665. ActivitiesModule.runJob("REMOVE_ACTIVITY_REFERENCES", { type: "playlistId", playlistId });
  1666. return cb({
  1667. status: "success",
  1668. message: "Playlist successfully removed"
  1669. });
  1670. }
  1671. );
  1672. }),
  1673. /**
  1674. * Updates the privacy of a private playlist
  1675. *
  1676. * @param {object} session - the session object automatically added by the websocket
  1677. * @param {string} playlistId - the id of the playlist we are updating the privacy for
  1678. * @param {string} privacy - what the new privacy of the playlist should be e.g. public
  1679. * @param {Function} cb - gets called with the result
  1680. */
  1681. updatePrivacy: isLoginRequired(async function updatePrivacy(session, playlistId, privacy, cb) {
  1682. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  1683. async.waterfall(
  1684. [
  1685. next => {
  1686. playlistModel.updateOne(
  1687. { _id: playlistId, createdBy: session.userId },
  1688. { $set: { privacy } },
  1689. { runValidators: true },
  1690. next
  1691. );
  1692. },
  1693. (res, next) => {
  1694. if (res.n === 0) next("No user playlist found with that id and owned by you.");
  1695. else if (res.nModified === 0) next(`Nothing changed, the playlist was already ${privacy}.`);
  1696. else {
  1697. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  1698. .then(playlist => next(null, playlist))
  1699. .catch(next);
  1700. }
  1701. }
  1702. ],
  1703. async (err, playlist) => {
  1704. if (err) {
  1705. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1706. this.log(
  1707. "ERROR",
  1708. "PLAYLIST_UPDATE_PRIVACY",
  1709. `Updating privacy to "${privacy}" for private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1710. );
  1711. return cb({ status: "error", message: err });
  1712. }
  1713. this.log(
  1714. "SUCCESS",
  1715. "PLAYLIST_UPDATE_PRIVACY",
  1716. `Successfully updated privacy to "${privacy}" for private playlist "${playlistId}" for user "${session.userId}".`
  1717. );
  1718. CacheModule.runJob("PUB", {
  1719. channel: "playlist.updatePrivacy",
  1720. value: {
  1721. userId: session.userId,
  1722. playlist
  1723. }
  1724. });
  1725. CacheModule.runJob("PUB", {
  1726. channel: "playlist.updated",
  1727. value: { playlistId }
  1728. });
  1729. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1730. userId: session.userId,
  1731. type: "playlist__edit_privacy",
  1732. payload: {
  1733. message: `Changed privacy of playlist <playlistId>${playlist.displayName}</playlistId> to ${privacy}`,
  1734. playlistId
  1735. }
  1736. });
  1737. return cb({
  1738. status: "success",
  1739. message: "Playlist has been successfully updated"
  1740. });
  1741. }
  1742. );
  1743. }),
  1744. /**
  1745. * Updates the privacy of a playlist
  1746. *
  1747. * @param {object} session - the session object automatically added by the websocket
  1748. * @param {string} playlistId - the id of the playlist we are updating the privacy for
  1749. * @param {string} privacy - what the new privacy of the playlist should be e.g. public
  1750. * @param {Function} cb - gets called with the result
  1751. */
  1752. updatePrivacyAdmin: isAdminRequired(async function updatePrivacyAdmin(session, playlistId, privacy, cb) {
  1753. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  1754. async.waterfall(
  1755. [
  1756. next => {
  1757. playlistModel.updateOne({ _id: playlistId }, { $set: { privacy } }, { runValidators: true }, next);
  1758. },
  1759. (res, next) => {
  1760. if (res.n === 0) next("No playlist found with that id.");
  1761. else if (res.nModified === 0) next(`Nothing changed, the playlist was already ${privacy}.`);
  1762. else {
  1763. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  1764. .then(playlist => next(null, playlist))
  1765. .catch(next);
  1766. }
  1767. }
  1768. ],
  1769. async (err, playlist) => {
  1770. if (err) {
  1771. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1772. this.log(
  1773. "ERROR",
  1774. "PLAYLIST_UPDATE_PRIVACY_ADMIN",
  1775. `Updating privacy to "${privacy}" for playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1776. );
  1777. return cb({ status: "error", message: err });
  1778. }
  1779. this.log(
  1780. "SUCCESS",
  1781. "PLAYLIST_UPDATE_PRIVACY_ADMIn",
  1782. `Successfully updated privacy to "${privacy}" for playlist "${playlistId}" for user "${session.userId}".`
  1783. );
  1784. if (playlist.type === "user") {
  1785. CacheModule.runJob("PUB", {
  1786. channel: "playlist.updatePrivacy",
  1787. value: {
  1788. userId: playlist.createdBy,
  1789. playlist
  1790. }
  1791. });
  1792. }
  1793. CacheModule.runJob("PUB", {
  1794. channel: "playlist.updated",
  1795. value: { playlistId }
  1796. });
  1797. return cb({
  1798. status: "success",
  1799. message: "Playlist has been successfully updated"
  1800. });
  1801. }
  1802. );
  1803. }),
  1804. /**
  1805. * Deletes all orphaned station playlists
  1806. *
  1807. * @param {object} session - the session object automatically added by socket.io
  1808. * @param {Function} cb - gets called with the result
  1809. */
  1810. deleteOrphanedStationPlaylists: isAdminRequired(async function index(session, cb) {
  1811. this.keepLongJob();
  1812. this.publishProgress({
  1813. status: "started",
  1814. title: "Delete orphaned station playlists",
  1815. message: "Deleting orphaned station playlists.",
  1816. id: this.toString()
  1817. });
  1818. await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
  1819. await CacheModule.runJob(
  1820. "PUB",
  1821. {
  1822. channel: "longJob.added",
  1823. value: { jobId: this.toString(), userId: session.userId }
  1824. },
  1825. this
  1826. );
  1827. async.waterfall(
  1828. [
  1829. next => {
  1830. PlaylistsModule.runJob("DELETE_ORPHANED_STATION_PLAYLISTS", {}, this)
  1831. .then(() => next())
  1832. .catch(next);
  1833. }
  1834. ],
  1835. async err => {
  1836. if (err) {
  1837. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1838. this.log(
  1839. "ERROR",
  1840. "PLAYLISTS_DELETE_ORPHANED_STATION_PLAYLISTS",
  1841. `Deleting orphaned station playlists failed. "${err}"`
  1842. );
  1843. this.publishProgress({
  1844. status: "error",
  1845. message: err
  1846. });
  1847. return cb({ status: "error", message: err });
  1848. }
  1849. this.log(
  1850. "SUCCESS",
  1851. "PLAYLISTS_DELETE_ORPHANED_STATION_PLAYLISTS",
  1852. "Deleting orphaned station playlists successful."
  1853. );
  1854. this.publishProgress({
  1855. status: "success",
  1856. message: "Successfully deleted orphaned station playlists."
  1857. });
  1858. return cb({ status: "success", message: "Successfully deleted orphaned station playlists." });
  1859. }
  1860. );
  1861. }),
  1862. /**
  1863. * Deletes all orphaned genre playlists
  1864. *
  1865. * @param {object} session - the session object automatically added by socket.io
  1866. * @param {Function} cb - gets called with the result
  1867. */
  1868. deleteOrphanedGenrePlaylists: isAdminRequired(async function index(session, cb) {
  1869. this.keepLongJob();
  1870. this.publishProgress({
  1871. status: "started",
  1872. title: "Delete orphaned genre playlists",
  1873. message: "Deleting orphaned genre playlists.",
  1874. id: this.toString()
  1875. });
  1876. await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
  1877. await CacheModule.runJob(
  1878. "PUB",
  1879. {
  1880. channel: "longJob.added",
  1881. value: { jobId: this.toString(), userId: session.userId }
  1882. },
  1883. this
  1884. );
  1885. async.waterfall(
  1886. [
  1887. next => {
  1888. PlaylistsModule.runJob("DELETE_ORPHANED_GENRE_PLAYLISTS", {}, this)
  1889. .then(() => next())
  1890. .catch(next);
  1891. }
  1892. ],
  1893. async err => {
  1894. if (err) {
  1895. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1896. this.log(
  1897. "ERROR",
  1898. "PLAYLISTS_DELETE_ORPHANED_GENRE_PLAYLISTS",
  1899. `Deleting orphaned genre playlists failed. "${err}"`
  1900. );
  1901. this.publishProgress({
  1902. status: "error",
  1903. message: err
  1904. });
  1905. return cb({ status: "error", message: err });
  1906. }
  1907. this.log(
  1908. "SUCCESS",
  1909. "PLAYLISTS_DELETE_ORPHANED_GENRE_PLAYLISTS",
  1910. "Deleting orphaned genre playlists successful."
  1911. );
  1912. this.publishProgress({
  1913. status: "success",
  1914. message: "Successfully deleted orphaned genre playlists."
  1915. });
  1916. return cb({ status: "success", message: "Successfully deleted orphaned genre playlists." });
  1917. }
  1918. );
  1919. }),
  1920. /**
  1921. * Deletes all orphaned 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. deleteOrphanedArtistPlaylists: isAdminRequired(async function index(session, cb) {
  1927. async.waterfall(
  1928. [
  1929. next => {
  1930. PlaylistsModule.runJob("DELETE_ORPHANED_ARTIST_PLAYLISTS", {}, this)
  1931. .then(() => next())
  1932. .catch(next);
  1933. }
  1934. ],
  1935. async err => {
  1936. if (err) {
  1937. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1938. this.log(
  1939. "ERROR",
  1940. "PLAYLISTS_DELETE_ORPHANED_ARTIST_PLAYLISTS",
  1941. `Deleting orphaned artist playlists failed. "${err}"`
  1942. );
  1943. return cb({ status: "error", message: err });
  1944. }
  1945. this.log(
  1946. "SUCCESS",
  1947. "PLAYLISTS_DELETE_ORPHANED_ARTIST_PLAYLISTS",
  1948. "Deleting orphaned artist playlists successful."
  1949. );
  1950. return cb({ status: "success", message: "Successfully deleted orphaned artist playlists." });
  1951. }
  1952. );
  1953. }),
  1954. /**
  1955. * Requests orpahned playlist songs
  1956. *
  1957. * @param {object} session - the session object automatically added by socket.io
  1958. * @param {Function} cb - gets called with the result
  1959. */
  1960. requestOrphanedPlaylistSongs: isAdminRequired(async function index(session, cb) {
  1961. this.keepLongJob();
  1962. this.publishProgress({
  1963. status: "started",
  1964. title: "Request orphaned playlist songs",
  1965. message: "Requesting orphaned playlist songs.",
  1966. id: this.toString()
  1967. });
  1968. await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
  1969. await CacheModule.runJob(
  1970. "PUB",
  1971. {
  1972. channel: "longJob.added",
  1973. value: { jobId: this.toString(), userId: session.userId }
  1974. },
  1975. this
  1976. );
  1977. async.waterfall(
  1978. [
  1979. next => {
  1980. SongsModule.runJob("REQUEST_ORPHANED_PLAYLIST_SONGS", {}, this)
  1981. .then(() => next())
  1982. .catch(next);
  1983. }
  1984. ],
  1985. async err => {
  1986. if (err) {
  1987. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1988. this.log(
  1989. "ERROR",
  1990. "REQUEST_ORPHANED_PLAYLIST_SONGS",
  1991. `Requesting orphaned playlist songs failed. "${err}"`
  1992. );
  1993. this.publishProgress({
  1994. status: "error",
  1995. message: err
  1996. });
  1997. return cb({ status: "error", message: err });
  1998. }
  1999. this.log(
  2000. "SUCCESS",
  2001. "REQUEST_ORPHANED_PLAYLIST_SONGS",
  2002. "Requesting orphaned playlist songs was successful."
  2003. );
  2004. this.publishProgress({
  2005. status: "success",
  2006. message: "Successfully requested orphaned playlist songs."
  2007. });
  2008. return cb({ status: "success", message: "Successfully requested orphaned playlist songs." });
  2009. }
  2010. );
  2011. }),
  2012. /**
  2013. * Clears and refills a station playlist
  2014. *
  2015. * @param {object} session - the session object automatically added by socket.io
  2016. * @param {string} playlistId - the id of the playlist we are clearing and refilling
  2017. * @param {Function} cb - gets called with the result
  2018. */
  2019. clearAndRefillStationPlaylist: isAdminRequired(async function index(session, playlistId, cb) {
  2020. async.waterfall(
  2021. [
  2022. next => {
  2023. if (!playlistId) next("Please specify a playlist id");
  2024. else {
  2025. PlaylistsModule.runJob("CLEAR_AND_REFILL_STATION_PLAYLIST", { playlistId }, this)
  2026. .then(() => {
  2027. next();
  2028. })
  2029. .catch(err => {
  2030. next(err);
  2031. });
  2032. }
  2033. }
  2034. ],
  2035. async err => {
  2036. if (err) {
  2037. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2038. this.log(
  2039. "ERROR",
  2040. "PLAYLIST_CLEAR_AND_REFILL_STATION_PLAYLIST",
  2041. `Clearing and refilling station playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  2042. );
  2043. return cb({ status: "error", message: err });
  2044. }
  2045. this.log(
  2046. "SUCCESS",
  2047. "PLAYLIST_CLEAR_AND_REFILL_STATION_PLAYLIST",
  2048. `Successfully cleared and refilled station playlist "${playlistId}" for user "${session.userId}".`
  2049. );
  2050. return cb({
  2051. status: "success",
  2052. message: "Playlist has been successfully cleared and refilled"
  2053. });
  2054. }
  2055. );
  2056. }),
  2057. /**
  2058. * Clears and refills a genre playlist
  2059. *
  2060. * @param {object} session - the session object automatically added by socket.io
  2061. * @param {string} playlistId - the id of the playlist we are clearing and refilling
  2062. * @param {Function} cb - gets called with the result
  2063. */
  2064. clearAndRefillGenrePlaylist: isAdminRequired(async function index(session, playlistId, cb) {
  2065. async.waterfall(
  2066. [
  2067. next => {
  2068. if (!playlistId) next("Please specify a playlist id");
  2069. else {
  2070. PlaylistsModule.runJob("CLEAR_AND_REFILL_GENRE_PLAYLIST", { playlistId }, this)
  2071. .then(() => {
  2072. next();
  2073. })
  2074. .catch(err => {
  2075. next(err);
  2076. });
  2077. }
  2078. }
  2079. ],
  2080. async err => {
  2081. if (err) {
  2082. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2083. this.log(
  2084. "ERROR",
  2085. "PLAYLIST_CLEAR_AND_REFILL_GENRE_PLAYLIST",
  2086. `Clearing and refilling genre playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  2087. );
  2088. return cb({ status: "error", message: err });
  2089. }
  2090. this.log(
  2091. "SUCCESS",
  2092. "PLAYLIST_CLEAR_AND_REFILL_GENRE_PLAYLIST",
  2093. `Successfully cleared and refilled genre playlist "${playlistId}" for user "${session.userId}".`
  2094. );
  2095. return cb({
  2096. status: "success",
  2097. message: "Playlist has been successfully cleared and refilled"
  2098. });
  2099. }
  2100. );
  2101. }),
  2102. /**
  2103. * Clears and refills a artist playlist
  2104. *
  2105. * @param {object} session - the session object automatically added by socket.io
  2106. * @param {string} playlistId - the id of the playlist we are clearing and refilling
  2107. * @param {Function} cb - gets called with the result
  2108. */
  2109. clearAndRefillArtistPlaylist: isAdminRequired(async function index(session, playlistId, cb) {
  2110. async.waterfall(
  2111. [
  2112. next => {
  2113. if (!playlistId) next("Please specify a playlist id");
  2114. else {
  2115. PlaylistsModule.runJob("CLEAR_AND_REFILL_ARTIST_PLAYLIST", { playlistId }, this)
  2116. .then(() => {
  2117. next();
  2118. })
  2119. .catch(err => {
  2120. next(err);
  2121. });
  2122. }
  2123. }
  2124. ],
  2125. async err => {
  2126. if (err) {
  2127. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2128. this.log(
  2129. "ERROR",
  2130. "PLAYLIST_CLEAR_AND_REFILL_ARTIST_PLAYLIST",
  2131. `Clearing and refilling artist playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  2132. );
  2133. return cb({ status: "error", message: err });
  2134. }
  2135. this.log(
  2136. "SUCCESS",
  2137. "PLAYLIST_CLEAR_AND_REFILL_ARTIST_PLAYLIST",
  2138. `Successfully cleared and refilled artist playlist "${playlistId}" for user "${session.userId}".`
  2139. );
  2140. return cb({
  2141. status: "success",
  2142. message: "Playlist has been successfully cleared and refilled"
  2143. });
  2144. }
  2145. );
  2146. }),
  2147. /**
  2148. * Clears and refills all station playlists
  2149. *
  2150. * @param {object} session - the session object automatically added by socket.io
  2151. * @param {Function} cb - gets called with the result
  2152. */
  2153. clearAndRefillAllStationPlaylists: isAdminRequired(async function index(session, cb) {
  2154. this.keepLongJob();
  2155. this.publishProgress({
  2156. status: "started",
  2157. title: "Clear and refill all station playlists",
  2158. message: "Clearing and refilling all station playlists.",
  2159. id: this.toString()
  2160. });
  2161. await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
  2162. await CacheModule.runJob(
  2163. "PUB",
  2164. {
  2165. channel: "longJob.added",
  2166. value: { jobId: this.toString(), userId: session.userId }
  2167. },
  2168. this
  2169. );
  2170. async.waterfall(
  2171. [
  2172. next => {
  2173. PlaylistsModule.runJob("GET_ALL_STATION_PLAYLISTS", {}, this)
  2174. .then(response => {
  2175. next(null, response.playlists);
  2176. })
  2177. .catch(err => {
  2178. next(err);
  2179. });
  2180. },
  2181. (playlists, next) => {
  2182. async.eachLimit(
  2183. playlists,
  2184. 1,
  2185. (playlist, next) => {
  2186. this.publishProgress({
  2187. status: "update",
  2188. message: `Clearing and refilling "${playlist._id}"`
  2189. });
  2190. PlaylistsModule.runJob(
  2191. "CLEAR_AND_REFILL_STATION_PLAYLIST",
  2192. { playlistId: playlist._id },
  2193. this
  2194. )
  2195. .then(() => {
  2196. next();
  2197. })
  2198. .catch(err => {
  2199. next(err);
  2200. });
  2201. },
  2202. next
  2203. );
  2204. }
  2205. ],
  2206. async err => {
  2207. if (err) {
  2208. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2209. this.log(
  2210. "ERROR",
  2211. "PLAYLIST_CLEAR_AND_REFILL_ALL_STATION_PLAYLISTS",
  2212. `Clearing and refilling all station playlists failed for user "${session.userId}". "${err}"`
  2213. );
  2214. this.publishProgress({
  2215. status: "error",
  2216. message: err
  2217. });
  2218. return cb({ status: "error", message: err });
  2219. }
  2220. this.log(
  2221. "SUCCESS",
  2222. "PLAYLIST_CLEAR_AND_REFILL_ALL_STATION_PLAYLISTS",
  2223. `Successfully cleared and refilled all station playlists for user "${session.userId}".`
  2224. );
  2225. this.publishProgress({
  2226. status: "success",
  2227. message: "Playlists have been successfully cleared and refilled."
  2228. });
  2229. return cb({
  2230. status: "success",
  2231. message: "Playlists have been successfully cleared and refilled"
  2232. });
  2233. }
  2234. );
  2235. }),
  2236. /**
  2237. * Clears and refills all genre playlists
  2238. *
  2239. * @param {object} session - the session object automatically added by socket.io
  2240. * @param {Function} cb - gets called with the result
  2241. */
  2242. clearAndRefillAllGenrePlaylists: isAdminRequired(async function index(session, cb) {
  2243. this.keepLongJob();
  2244. this.publishProgress({
  2245. status: "started",
  2246. title: "Clear and refill all genre playlists",
  2247. message: "Clearing and refilling all genre playlists.",
  2248. id: this.toString()
  2249. });
  2250. await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
  2251. await CacheModule.runJob(
  2252. "PUB",
  2253. {
  2254. channel: "longJob.added",
  2255. value: { jobId: this.toString(), userId: session.userId }
  2256. },
  2257. this
  2258. );
  2259. async.waterfall(
  2260. [
  2261. next => {
  2262. PlaylistsModule.runJob("GET_ALL_GENRE_PLAYLISTS", {}, this)
  2263. .then(response => {
  2264. next(null, response.playlists);
  2265. })
  2266. .catch(err => {
  2267. next(err);
  2268. });
  2269. },
  2270. (playlists, next) => {
  2271. async.eachLimit(
  2272. playlists,
  2273. 1,
  2274. (playlist, next) => {
  2275. this.publishProgress({
  2276. status: "update",
  2277. message: `Clearing and refilling "${playlist._id}"`
  2278. });
  2279. PlaylistsModule.runJob(
  2280. "CLEAR_AND_REFILL_GENRE_PLAYLIST",
  2281. { playlistId: playlist._id },
  2282. this
  2283. )
  2284. .then(() => {
  2285. next();
  2286. })
  2287. .catch(err => {
  2288. next(err);
  2289. });
  2290. },
  2291. next
  2292. );
  2293. }
  2294. ],
  2295. async err => {
  2296. if (err) {
  2297. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2298. this.log(
  2299. "ERROR",
  2300. "PLAYLIST_CLEAR_AND_REFILL_ALL_GENRE_PLAYLISTS",
  2301. `Clearing and refilling all genre playlists failed for user "${session.userId}". "${err}"`
  2302. );
  2303. this.publishProgress({
  2304. status: "error",
  2305. message: err
  2306. });
  2307. return cb({ status: "error", message: err });
  2308. }
  2309. this.log(
  2310. "SUCCESS",
  2311. "PLAYLIST_CLEAR_AND_REFILL_ALL_GENRE_PLAYLISTS",
  2312. `Successfully cleared and refilled all genre playlists for user "${session.userId}".`
  2313. );
  2314. this.publishProgress({
  2315. status: "success",
  2316. message: "Playlists have been successfully cleared and refilled."
  2317. });
  2318. return cb({
  2319. status: "success",
  2320. message: "Playlists have been successfully cleared and refilled"
  2321. });
  2322. }
  2323. );
  2324. }),
  2325. /**
  2326. * Clears and refills all artist playlists
  2327. *
  2328. * @param {object} session - the session object automatically added by socket.io
  2329. * @param {Function} cb - gets called with the result
  2330. */
  2331. clearAndRefillAllArtistPlaylists: isAdminRequired(async function index(session, cb) {
  2332. async.waterfall(
  2333. [
  2334. next => {
  2335. PlaylistsModule.runJob("GET_ALL_ARTIST_PLAYLISTS", {}, this)
  2336. .then(response => {
  2337. next(null, response.playlists);
  2338. })
  2339. .catch(err => {
  2340. next(err);
  2341. });
  2342. },
  2343. (playlists, next) => {
  2344. async.eachLimit(
  2345. playlists,
  2346. 1,
  2347. (playlist, next) => {
  2348. PlaylistsModule.runJob(
  2349. "CLEAR_AND_REFILL_ARTIST_PLAYLIST",
  2350. { playlistId: playlist._id },
  2351. this
  2352. )
  2353. .then(() => {
  2354. next();
  2355. })
  2356. .catch(err => {
  2357. next(err);
  2358. });
  2359. },
  2360. next
  2361. );
  2362. }
  2363. // next => {
  2364. // // PlaylistsModule.runJob("CREATE_MISSING_ARTIST_PLAYLISTS", {}, null)
  2365. // // .then()
  2366. // // .catch()
  2367. // // .finally(() => {
  2368. // // SongsModule.runJob("GET_ALL_ARTISTS", {}, null)
  2369. // // .then(response => {
  2370. // // const { artists } = response;
  2371. // // artists.forEach(artist => {
  2372. // // PlaylistsModule.runJob("AUTOFILL_ARTIST_PLAYLIST", { artist }, null).then().catch();
  2373. // // });
  2374. // // })
  2375. // // .catch();
  2376. // // });
  2377. // PlaylistsModule.runJob("GET_MISSING_ARTIST_PLAYLISTS", {}, this).then(response => {
  2378. // console.log(response);
  2379. // });
  2380. // }
  2381. ],
  2382. async err => {
  2383. if (err) {
  2384. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2385. this.log(
  2386. "ERROR",
  2387. "PLAYLIST_CLEAR_AND_REFILL_ALL_ARTIST_PLAYLISTS",
  2388. `Clearing and refilling all artist playlists failed for user "${session.userId}". "${err}"`
  2389. );
  2390. return cb({ status: "error", message: err });
  2391. }
  2392. this.log(
  2393. "SUCCESS",
  2394. "PLAYLIST_CLEAR_AND_REFILL_ALL_ARTIST_PLAYLISTS",
  2395. `Successfully cleared and refilled all artist playlists for user "${session.userId}".`
  2396. );
  2397. return cb({
  2398. status: "success",
  2399. message: "Playlists have been successfully cleared and refilled"
  2400. });
  2401. }
  2402. );
  2403. }),
  2404. /**
  2405. * Create missing genre playlists
  2406. *
  2407. * @param {object} session - the session object automatically added by socket.io
  2408. * @param {Function} cb - gets called with the result
  2409. */
  2410. createMissingGenrePlaylists: isAdminRequired(async function index(session, cb) {
  2411. this.keepLongJob();
  2412. this.publishProgress({
  2413. status: "started",
  2414. title: "Create missing genre playlists",
  2415. message: "Creating missing genre playlists.",
  2416. id: this.toString()
  2417. });
  2418. await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
  2419. await CacheModule.runJob(
  2420. "PUB",
  2421. {
  2422. channel: "longJob.added",
  2423. value: { jobId: this.toString(), userId: session.userId }
  2424. },
  2425. this
  2426. );
  2427. async.waterfall(
  2428. [
  2429. next => {
  2430. PlaylistsModule.runJob("CREATE_MISSING_GENRE_PLAYLISTS", this)
  2431. .then(() => {
  2432. next();
  2433. })
  2434. .catch(err => {
  2435. next(err);
  2436. });
  2437. }
  2438. ],
  2439. async err => {
  2440. if (err) {
  2441. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2442. this.log(
  2443. "ERROR",
  2444. "PLAYLIST_CREATE_MISSING_GENRE_PLAYLISTS",
  2445. `Creating missing genre playlists failed for user "${session.userId}". "${err}"`
  2446. );
  2447. this.publishProgress({
  2448. status: "error",
  2449. message: err
  2450. });
  2451. return cb({ status: "error", message: err });
  2452. }
  2453. this.log(
  2454. "SUCCESS",
  2455. "PLAYLIST_CREATE_MISSING_GENRE_PLAYLISTS",
  2456. `Successfully created missing genre playlists for user "${session.userId}".`
  2457. );
  2458. this.publishProgress({
  2459. status: "success",
  2460. message: "Missing genre playlists have been successfully created."
  2461. });
  2462. return cb({
  2463. status: "success",
  2464. message: "Missing genre playlists have been successfully created"
  2465. });
  2466. }
  2467. );
  2468. }),
  2469. /**
  2470. * Create missing artist playlists
  2471. *
  2472. * @param {object} session - the session object automatically added by socket.io
  2473. * @param {Function} cb - gets called with the result
  2474. */
  2475. createMissingArtistPlaylists: isAdminRequired(async function index(session, cb) {
  2476. async.waterfall(
  2477. [
  2478. next => {
  2479. PlaylistsModule.runJob("CREATE_MISSING_ARTIST_PLAYLISTS", this)
  2480. .then(() => {
  2481. next();
  2482. })
  2483. .catch(err => {
  2484. next(err);
  2485. });
  2486. }
  2487. ],
  2488. async err => {
  2489. if (err) {
  2490. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2491. this.log(
  2492. "ERROR",
  2493. "PLAYLIST_CREATE_MISSING_ARTIST_PLAYLISTS",
  2494. `Creating missing artist playlists failed for user "${session.userId}". "${err}"`
  2495. );
  2496. return cb({ status: "error", message: err });
  2497. }
  2498. this.log(
  2499. "SUCCESS",
  2500. "PLAYLIST_CREATE_MISSING_ARTIST_PLAYLISTS",
  2501. `Successfully created missing artist playlists for user "${session.userId}".`
  2502. );
  2503. return cb({
  2504. status: "success",
  2505. message: "Missing artist playlists have been successfully created"
  2506. });
  2507. }
  2508. );
  2509. })
  2510. };