playlists.js 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376
  1. import async from "async";
  2. import config from "config";
  3. import isLoginRequired from "../hooks/loginRequired";
  4. import { hasPermission, useHasPermission } from "../hooks/hasPermission";
  5. // eslint-disable-next-line
  6. import moduleManager from "../../index";
  7. const DBModule = moduleManager.modules.db;
  8. const UtilsModule = moduleManager.modules.utils;
  9. const WSModule = moduleManager.modules.ws;
  10. const SongsModule = moduleManager.modules.songs;
  11. const CacheModule = moduleManager.modules.cache;
  12. const PlaylistsModule = moduleManager.modules.playlists;
  13. const YouTubeModule = moduleManager.modules.youtube;
  14. const SoundcloudModule = moduleManager.modules.soundcloud;
  15. const SpotifyModule = moduleManager.modules.spotify;
  16. const ActivitiesModule = moduleManager.modules.activities;
  17. const MediaModule = moduleManager.modules.media;
  18. CacheModule.runJob("SUB", {
  19. channel: "playlist.create",
  20. cb: playlist => {
  21. if (playlist.createdBy !== "Musare") {
  22. WSModule.runJob("SOCKETS_FROM_USER", { userId: playlist.createdBy }, this).then(sockets =>
  23. sockets.forEach(socket => socket.dispatch("event:playlist.created", { data: { playlist } }))
  24. );
  25. if (playlist.privacy === "public")
  26. WSModule.runJob("EMIT_TO_ROOM", {
  27. room: `profile.${playlist.createdBy}.playlists`,
  28. args: ["event:playlist.created", { data: { playlist } }]
  29. });
  30. }
  31. WSModule.runJob("EMIT_TO_ROOM", {
  32. room: "admin.playlists",
  33. args: ["event:admin.playlist.created", { data: { playlist } }]
  34. });
  35. }
  36. });
  37. CacheModule.runJob("SUB", {
  38. channel: "playlist.delete",
  39. cb: res => {
  40. if (res.createdBy !== "Musare") {
  41. WSModule.runJob("SOCKETS_FROM_USER", { userId: res.createdBy }, this).then(sockets => {
  42. sockets.forEach(socket => {
  43. socket.dispatch("event:playlist.deleted", { data: { playlistId: res.playlistId } });
  44. });
  45. });
  46. WSModule.runJob("EMIT_TO_ROOM", {
  47. room: `profile.${res.createdBy}.playlists`,
  48. args: ["event:playlist.deleted", { data: { playlistId: res.playlistId } }]
  49. });
  50. }
  51. WSModule.runJob("EMIT_TO_ROOM", {
  52. room: "admin.playlists",
  53. args: ["event:admin.playlist.deleted", { data: { playlistId: res.playlistId } }]
  54. });
  55. }
  56. });
  57. CacheModule.runJob("SUB", {
  58. channel: "playlist.repositionSong",
  59. cb: res => {
  60. const { createdBy, playlistId, song } = res;
  61. if (createdBy !== "Musare") {
  62. WSModule.runJob("SOCKETS_FROM_USER", { userId: createdBy }, this).then(sockets =>
  63. sockets.forEach(socket =>
  64. socket.dispatch("event:playlist.song.repositioned", {
  65. data: { playlistId, song }
  66. })
  67. )
  68. );
  69. }
  70. }
  71. });
  72. CacheModule.runJob("SUB", {
  73. channel: "playlist.addSong",
  74. cb: res => {
  75. if (res.createdBy !== "Musare") {
  76. WSModule.runJob("SOCKETS_FROM_USER", { userId: res.createdBy }, this).then(sockets => {
  77. sockets.forEach(socket => {
  78. socket.dispatch("event:playlist.song.added", {
  79. data: {
  80. playlistId: res.playlistId,
  81. song: res.song
  82. }
  83. });
  84. });
  85. });
  86. if (res.privacy === "public")
  87. WSModule.runJob("EMIT_TO_ROOM", {
  88. room: `profile.${res.createdBy}.playlists`,
  89. args: [
  90. "event:playlist.song.added",
  91. {
  92. data: {
  93. playlistId: res.playlistId,
  94. song: res.song
  95. }
  96. }
  97. ]
  98. });
  99. }
  100. WSModule.runJob("EMIT_TO_ROOM", {
  101. room: "admin.playlists",
  102. args: ["event:admin.playlist.song.added", { data: { playlistId: res.playlistId, song: res.song } }]
  103. });
  104. }
  105. });
  106. CacheModule.runJob("SUB", {
  107. channel: "playlist.removeSong",
  108. cb: res => {
  109. if (res.createdBy !== "Musare") {
  110. WSModule.runJob("SOCKETS_FROM_USER", { userId: res.createdBy }, this).then(sockets => {
  111. sockets.forEach(socket => {
  112. socket.dispatch("event:playlist.song.removed", {
  113. data: {
  114. playlistId: res.playlistId,
  115. mediaSource: res.mediaSource
  116. }
  117. });
  118. });
  119. });
  120. if (res.privacy === "public")
  121. WSModule.runJob("EMIT_TO_ROOM", {
  122. room: `profile.${res.createdBy}.playlists`,
  123. args: [
  124. "event:playlist.song.removed",
  125. {
  126. data: {
  127. playlistId: res.playlistId,
  128. mediaSource: res.mediaSource
  129. }
  130. }
  131. ]
  132. });
  133. }
  134. WSModule.runJob("EMIT_TO_ROOM", {
  135. room: "admin.playlists",
  136. args: [
  137. "event:admin.playlist.song.removed",
  138. { data: { playlistId: res.playlistId, mediaSource: res.mediaSource } }
  139. ]
  140. });
  141. }
  142. });
  143. CacheModule.runJob("SUB", {
  144. channel: "playlist.updateDisplayName",
  145. cb: res => {
  146. if (res.createdBy !== "Musare") {
  147. WSModule.runJob("SOCKETS_FROM_USER", { userId: res.createdBy }, this).then(sockets => {
  148. sockets.forEach(socket => {
  149. socket.dispatch("event:playlist.displayName.updated", {
  150. data: {
  151. playlistId: res.playlistId,
  152. displayName: res.displayName
  153. }
  154. });
  155. });
  156. });
  157. if (res.privacy === "public")
  158. WSModule.runJob("EMIT_TO_ROOM", {
  159. room: `profile.${res.createdBy}.playlists`,
  160. args: [
  161. "event:playlist.displayName.updated",
  162. {
  163. data: {
  164. playlistId: res.playlistId,
  165. displayName: res.displayName
  166. }
  167. }
  168. ]
  169. });
  170. }
  171. WSModule.runJob("EMIT_TO_ROOM", {
  172. room: "admin.playlists",
  173. args: [
  174. "event:admin.playlist.displayName.updated",
  175. { data: { playlistId: res.playlistId, displayName: res.displayName } }
  176. ]
  177. });
  178. }
  179. });
  180. CacheModule.runJob("SUB", {
  181. channel: "playlist.updatePrivacy",
  182. cb: res => {
  183. WSModule.runJob("EMIT_TO_ROOM", {
  184. room: "admin.playlists",
  185. args: [
  186. "event:admin.playlist.privacy.updated",
  187. { data: { playlistId: res.playlist._id, privacy: res.playlist.privacy } }
  188. ]
  189. });
  190. if (res.createdBy !== "Musare") {
  191. WSModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(sockets => {
  192. sockets.forEach(socket => {
  193. socket.dispatch("event:playlist.privacy.updated", {
  194. data: {
  195. playlist: res.playlist
  196. }
  197. });
  198. });
  199. });
  200. if (res.playlist.privacy === "public")
  201. return WSModule.runJob("EMIT_TO_ROOM", {
  202. room: `profile.${res.userId}.playlists`,
  203. args: [
  204. "event:playlist.created",
  205. {
  206. data: {
  207. playlist: res.playlist
  208. }
  209. }
  210. ]
  211. });
  212. return WSModule.runJob("EMIT_TO_ROOM", {
  213. room: `profile.${res.userId}.playlists`,
  214. args: [
  215. "event:playlist.deleted",
  216. {
  217. data: {
  218. playlistId: res.playlist._id
  219. }
  220. }
  221. ]
  222. });
  223. }
  224. return null;
  225. }
  226. });
  227. CacheModule.runJob("SUB", {
  228. channel: "playlist.updated",
  229. cb: async data => {
  230. const playlistModel = await DBModule.runJob("GET_MODEL", {
  231. modelName: "playlist"
  232. });
  233. playlistModel.findOne(
  234. { _id: data.playlistId },
  235. ["_id", "displayName", "type", "privacy", "songs", "createdBy", "createdAt", "createdFor"],
  236. (err, playlist) => {
  237. const newPlaylist = {
  238. ...playlist._doc,
  239. songsCount: playlist.songs.length,
  240. songsLength: playlist.songs.reduce(
  241. (previous, current) => ({
  242. duration: previous.duration + current.duration
  243. }),
  244. { duration: 0 }
  245. ).duration
  246. };
  247. delete newPlaylist.songs;
  248. WSModule.runJob("EMIT_TO_ROOMS", {
  249. rooms: ["admin.playlists"],
  250. args: ["event:admin.playlist.updated", { data: { playlist: newPlaylist } }]
  251. });
  252. }
  253. );
  254. }
  255. });
  256. export default {
  257. /**
  258. * Gets playlists, used in the admin playlists page by the AdvancedTable component
  259. *
  260. * @param {object} session - the session object automatically added by the websocket
  261. * @param page - the page
  262. * @param pageSize - the size per page
  263. * @param properties - the properties to return for each playlist
  264. * @param sort - the sort object
  265. * @param queries - the queries array
  266. * @param operator - the operator for queries
  267. * @param cb
  268. */
  269. getData: useHasPermission(
  270. "admin.view.playlists",
  271. async function getSet(session, page, pageSize, properties, sort, queries, operator, cb) {
  272. async.waterfall(
  273. [
  274. next => {
  275. DBModule.runJob(
  276. "GET_DATA",
  277. {
  278. page,
  279. pageSize,
  280. properties,
  281. sort,
  282. queries,
  283. operator,
  284. modelName: "playlist",
  285. blacklistedProperties: [],
  286. specialProperties: {
  287. totalLength: [
  288. {
  289. $addFields: {
  290. totalLength: { $sum: "$songs.duration" }
  291. }
  292. }
  293. ],
  294. songsCount: [
  295. {
  296. $addFields: {
  297. songsCount: { $size: "$songs" }
  298. }
  299. }
  300. ],
  301. createdBy: [
  302. {
  303. $addFields: {
  304. createdByOID: {
  305. $convert: {
  306. input: "$createdBy",
  307. to: "objectId",
  308. onError: "unknown",
  309. onNull: "unknown"
  310. }
  311. }
  312. }
  313. },
  314. {
  315. $lookup: {
  316. from: "users",
  317. localField: "createdByOID",
  318. foreignField: "_id",
  319. as: "createdByUser"
  320. }
  321. },
  322. {
  323. $unwind: {
  324. path: "$createdByUser",
  325. preserveNullAndEmptyArrays: true
  326. }
  327. },
  328. {
  329. $addFields: {
  330. createdByUsername: {
  331. $cond: [
  332. { $eq: ["$createdBy", "Musare"] },
  333. "Musare",
  334. { $ifNull: ["$createdByUser.username", "unknown"] }
  335. ]
  336. }
  337. }
  338. },
  339. {
  340. $project: {
  341. createdByOID: 0,
  342. createdByUser: 0
  343. }
  344. }
  345. ]
  346. },
  347. specialQueries: {
  348. createdBy: newQuery => ({
  349. $or: [newQuery, { createdByUsername: newQuery.createdBy }]
  350. })
  351. }
  352. },
  353. this
  354. )
  355. .then(response => {
  356. next(null, response);
  357. })
  358. .catch(err => {
  359. next(err);
  360. });
  361. }
  362. ],
  363. async (err, response) => {
  364. if (err) {
  365. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  366. this.log("ERROR", "PLAYLISTS_GET_DATA", `Failed to get data from playlists. "${err}"`);
  367. return cb({ status: "error", message: err });
  368. }
  369. this.log("SUCCESS", "PLAYLISTS_GET_DATA", `Got data from playlists successfully.`);
  370. return cb({ status: "success", message: "Successfully got data from playlists.", data: response });
  371. }
  372. );
  373. }
  374. ),
  375. /**
  376. * Searches through all playlists that can be included in a community station
  377. *
  378. * @param {object} session - the session object automatically added by the websocket
  379. * @param {string} query - the query
  380. * @param {string} query - the page
  381. * @param {Function} cb - gets called with the result
  382. */
  383. searchCommunity: isLoginRequired(async function searchCommunity(session, query, page, cb) {
  384. async.waterfall(
  385. [
  386. next => {
  387. if ((!query && query !== "") || typeof query !== "string") next("Invalid query.");
  388. else next();
  389. },
  390. next => {
  391. PlaylistsModule.runJob("SEARCH", {
  392. query,
  393. includeUser: true,
  394. includeGenre: true,
  395. includeAdmin: true,
  396. includeOwn: true,
  397. includeSongs: true,
  398. userId: session.userId,
  399. page
  400. })
  401. .then(response => {
  402. next(null, response);
  403. })
  404. .catch(err => {
  405. next(err);
  406. });
  407. }
  408. ],
  409. async (err, data) => {
  410. if (err) {
  411. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  412. this.log("ERROR", "PLAYLISTS_SEARCH_COMMUNITY", `Searching playlists failed. "${err}"`);
  413. return cb({ status: "error", message: err });
  414. }
  415. this.log("SUCCESS", "PLAYLISTS_SEARCH_COMMUNITY", "Searching playlists successful.");
  416. return cb({ status: "success", data });
  417. }
  418. );
  419. }),
  420. /**
  421. * Searches through all playlists that can be included in an official station
  422. *
  423. * @param {object} session - the session object automatically added by the websocket
  424. * @param {string} query - the query
  425. * @param {string} query - the page
  426. * @param {Function} cb - gets called with the result
  427. */
  428. searchOfficial: useHasPermission("playlists.get", async function searchOfficial(session, query, page, cb) {
  429. async.waterfall(
  430. [
  431. next => {
  432. if ((!query && query !== "") || typeof query !== "string") next("Invalid query.");
  433. else next();
  434. },
  435. next => {
  436. PlaylistsModule.runJob("SEARCH", {
  437. query,
  438. includeGenre: true,
  439. includePrivate: true,
  440. includeSongs: true,
  441. includeAdmin: true,
  442. page
  443. })
  444. .then(response => {
  445. next(null, response);
  446. })
  447. .catch(err => {
  448. next(err);
  449. });
  450. }
  451. ],
  452. async (err, data) => {
  453. if (err) {
  454. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  455. this.log("ERROR", "PLAYLISTS_SEARCH_OFFICIAL", `Searching playlists failed. "${err}"`);
  456. return cb({ status: "error", message: err });
  457. }
  458. this.log("SUCCESS", "PLAYLISTS_SEARCH_OFFICIAL", "Searching playlists successful.");
  459. return cb({ status: "success", data });
  460. }
  461. );
  462. }),
  463. /**
  464. * Searches through all admin playlists
  465. *
  466. * @param {object} session - the session object automatically added by the websocket
  467. * @param {string} query - the query
  468. * @param {string} query - the page
  469. * @param {Function} cb - gets called with the result
  470. */
  471. searchAdmin: useHasPermission("playlists.get", async function searchAdmin(session, query, page, cb) {
  472. async.waterfall(
  473. [
  474. next => {
  475. if ((!query && query !== "") || typeof query !== "string") next("Invalid query.");
  476. else next();
  477. },
  478. next => {
  479. PlaylistsModule.runJob("SEARCH", {
  480. query,
  481. includePrivate: true,
  482. includeSongs: true,
  483. includeAdmin: true,
  484. page
  485. })
  486. .then(response => {
  487. next(null, response);
  488. })
  489. .catch(err => {
  490. next(err);
  491. });
  492. }
  493. ],
  494. async (err, data) => {
  495. if (err) {
  496. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  497. this.log("ERROR", "PLAYLISTS_SEARCH_ADMIN", `Searching playlists failed. "${err}"`);
  498. return cb({ status: "error", message: err });
  499. }
  500. this.log("SUCCESS", "PLAYLISTS_SEARCH_ADMIN", "Searching playlists successful.");
  501. return cb({ status: "success", data });
  502. }
  503. );
  504. }),
  505. /**
  506. * Gets the first song from a private playlist
  507. *
  508. * @param {object} session - the session object automatically added by the websocket
  509. * @param {string} playlistId - the id of the playlist we are getting the first song from
  510. * @param {Function} cb - gets called with the result
  511. */
  512. getFirstSong: isLoginRequired(function getFirstSong(session, playlistId, cb) {
  513. async.waterfall(
  514. [
  515. next => {
  516. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  517. .then(playlist => next(null, playlist))
  518. .catch(next);
  519. },
  520. (playlist, next) => {
  521. if (!playlist || playlist.createdBy !== session.userId) return next("Playlist not found.");
  522. playlist.songs.sort((a, b) => a.position - b.position);
  523. return next(null, playlist.songs[0]);
  524. }
  525. ],
  526. async (err, song) => {
  527. if (err) {
  528. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  529. this.log(
  530. "ERROR",
  531. "PLAYLIST_GET_FIRST_SONG",
  532. `Getting the first song of playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  533. );
  534. return cb({ status: "error", message: err });
  535. }
  536. this.log(
  537. "SUCCESS",
  538. "PLAYLIST_GET_FIRST_SONG",
  539. `Successfully got the first song of playlist "${playlistId}" for user "${session.userId}".`
  540. );
  541. return cb({
  542. status: "success",
  543. data: { song }
  544. });
  545. }
  546. );
  547. }),
  548. /**
  549. * Gets a list of all the playlists for a specific user
  550. *
  551. * @param {object} session - the session object automatically added by the websocket
  552. * @param {string} userId - the user id in question
  553. * @param {Function} cb - gets called with the result
  554. */
  555. indexForUser: async function indexForUser(session, userId, 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(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: 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. (playlists, next) => {
  585. if (session.userId === userId) return next(null, playlists); // user requesting playlists is the owner of the playlists
  586. const filteredPlaylists = [];
  587. return async.each(
  588. playlists,
  589. (playlist, nextPlaylist) => {
  590. if (playlist.privacy === "public") filteredPlaylists.push(playlist);
  591. return nextPlaylist();
  592. },
  593. () => next(null, filteredPlaylists)
  594. );
  595. }
  596. ],
  597. async (err, playlists) => {
  598. if (err) {
  599. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  600. this.log(
  601. "ERROR",
  602. "PLAYLIST_INDEX_FOR_USER",
  603. `Indexing playlists for user "${userId}" failed. "${err}"`
  604. );
  605. return cb({ status: "error", message: err });
  606. }
  607. this.log("SUCCESS", "PLAYLIST_INDEX_FOR_USER", `Successfully indexed playlists for user "${userId}".`);
  608. return cb({
  609. status: "success",
  610. data: { playlists }
  611. });
  612. }
  613. );
  614. },
  615. /**
  616. * Gets all playlists for the user requesting it
  617. *
  618. * @param {object} session - the session object automatically added by the websocket
  619. * @param {Function} cb - gets called with the result
  620. */
  621. indexMyPlaylists: isLoginRequired(async function indexMyPlaylists(session, cb) {
  622. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  623. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  624. async.waterfall(
  625. [
  626. next => {
  627. userModel.findById(session.userId).select({ "preferences.orderOfPlaylists": -1 }).exec(next);
  628. },
  629. (user, next) => {
  630. if (!user) next("User not found");
  631. else {
  632. const { preferences } = user;
  633. const { orderOfPlaylists } = preferences;
  634. const match = {
  635. createdBy: session.userId,
  636. type: { $in: ["user", "user-liked", "user-disliked"] }
  637. };
  638. // if a playlist order exists
  639. if (orderOfPlaylists > 0) match._id = { $in: orderOfPlaylists };
  640. playlistModel
  641. .aggregate()
  642. .match(match)
  643. .addFields({
  644. weight: { $indexOfArray: [orderOfPlaylists, "$_id"] }
  645. })
  646. .sort({ weight: 1 })
  647. .exec(next);
  648. }
  649. }
  650. ],
  651. async (err, playlists) => {
  652. if (err) {
  653. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  654. this.log(
  655. "ERROR",
  656. "PLAYLIST_INDEX_FOR_ME",
  657. `Indexing playlists for user "${session.userId}" failed. "${err}"`
  658. );
  659. return cb({ status: "error", message: err });
  660. }
  661. this.log(
  662. "SUCCESS",
  663. "PLAYLIST_INDEX_FOR_ME",
  664. `Successfully indexed playlists for user "${session.userId}".`
  665. );
  666. return cb({
  667. status: "success",
  668. data: { playlists }
  669. });
  670. }
  671. );
  672. }),
  673. /**
  674. * Gets all playlists playlists
  675. *
  676. * @param {object} session - the session object automatically added by the websocket
  677. * @param {Function} cb - gets called with the result
  678. */
  679. indexFeaturedPlaylists: isLoginRequired(async function indexMyPlaylists(session, cb) {
  680. async.waterfall(
  681. [
  682. next => {
  683. const featuredPlaylistIds = config.get("featuredPlaylists");
  684. if (featuredPlaylistIds.length === 0) next(true, []);
  685. else next(null, featuredPlaylistIds);
  686. },
  687. (featuredPlaylistIds, next) => {
  688. const featuredPlaylists = [];
  689. async.eachLimit(
  690. featuredPlaylistIds,
  691. 1,
  692. (playlistId, next) => {
  693. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  694. .then(playlist => {
  695. if (playlist.privacy === "public") featuredPlaylists.push(playlist);
  696. next();
  697. })
  698. .catch(next);
  699. },
  700. err => {
  701. next(err, featuredPlaylists);
  702. }
  703. );
  704. }
  705. ],
  706. async (err, playlists) => {
  707. if (err && err !== true) {
  708. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  709. this.log("ERROR", "PLAYLIST_INDEX_FEATURED", `Indexing featured playlists failed. "${err}"`);
  710. return cb({ status: "error", message: err });
  711. }
  712. this.log("SUCCESS", "PLAYLIST_INDEX_FEATURED", `Successfully indexed featured playlists.`);
  713. return cb({
  714. status: "success",
  715. data: { playlists }
  716. });
  717. }
  718. );
  719. }),
  720. /**
  721. * Creates a new private playlist
  722. *
  723. * @param {object} session - the session object automatically added by the websocket
  724. * @param {object} data - the data for the new private playlist
  725. * @param {Function} cb - gets called with the result
  726. */
  727. create: isLoginRequired(async function create(session, data, cb) {
  728. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  729. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  730. const blacklist = ["liked songs", "likedsongs", "disliked songs", "dislikedsongs"];
  731. async.waterfall(
  732. [
  733. next => (data ? next() : cb({ status: "error", message: "Invalid data" })),
  734. next => {
  735. const { displayName, songs, privacy, admin } = data;
  736. if (blacklist.indexOf(displayName.toLowerCase()) !== -1)
  737. return next("That playlist name is blacklisted. Please use a different name.");
  738. return playlistModel.create(
  739. {
  740. displayName,
  741. songs,
  742. privacy,
  743. createdBy: admin ? "Musare" : session.userId,
  744. createdAt: Date.now(),
  745. createdFor: null,
  746. type: admin ? "admin" : "user"
  747. },
  748. next
  749. );
  750. },
  751. (playlist, next) => {
  752. if (data.admin) next(null, playlist);
  753. else
  754. userModel.updateOne(
  755. { _id: session.userId },
  756. { $push: { "preferences.orderOfPlaylists": playlist._id } },
  757. err => {
  758. if (err) return next(err);
  759. return next(null, playlist);
  760. }
  761. );
  762. }
  763. ],
  764. async (err, playlist) => {
  765. let type = "unknown";
  766. if (data && data.admin) type = "admin";
  767. else if (data && !data.admin) type = "user";
  768. if (err) {
  769. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  770. this.log(
  771. "ERROR",
  772. "PLAYLIST_CREATE",
  773. `Creating ${type} playlist failed for user "${session.userId}". "${err}"`
  774. );
  775. return cb({ status: "error", message: err });
  776. }
  777. CacheModule.runJob("PUB", {
  778. channel: "playlist.create",
  779. value: playlist
  780. });
  781. if (!data.admin)
  782. ActivitiesModule.runJob("ADD_ACTIVITY", {
  783. userId: playlist.createdBy,
  784. type: "playlist__create",
  785. payload: {
  786. message: `Created playlist <playlistId>${playlist.displayName}</playlistId>`,
  787. playlistId: playlist._id
  788. }
  789. });
  790. this.log(
  791. "SUCCESS",
  792. "PLAYLIST_CREATE",
  793. `Successfully created ${type} playlist for user "${session.userId}".`
  794. );
  795. return cb({
  796. status: "success",
  797. message: "Successfully created playlist",
  798. data: {
  799. playlistId: playlist._id
  800. }
  801. });
  802. }
  803. );
  804. }),
  805. /**
  806. * Gets a playlist from id
  807. *
  808. * @param {object} session - the session object automatically added by the websocket
  809. * @param {string} playlistId - the id of the playlist we are getting
  810. * @param {Function} cb - gets called with the result
  811. */
  812. getPlaylist: function getPlaylist(session, playlistId, cb) {
  813. async.waterfall(
  814. [
  815. next => {
  816. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  817. .then(playlist => next(null, playlist))
  818. .catch(next);
  819. },
  820. (playlist, next) => {
  821. if (!playlist) return next("Playlist not found");
  822. if (playlist.privacy !== "public" && playlist.createdBy !== session.userId)
  823. return hasPermission("playlists.get", session)
  824. .then(() => next(null, playlist))
  825. .catch(() => next("User unauthorised to view playlist."));
  826. return next(null, playlist);
  827. }
  828. ],
  829. async (err, playlist) => {
  830. if (err) {
  831. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  832. this.log(
  833. "ERROR",
  834. "PLAYLIST_GET",
  835. `Getting private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  836. );
  837. return cb({ status: "error", message: err });
  838. }
  839. this.log(
  840. "SUCCESS",
  841. "PLAYLIST_GET",
  842. `Successfully got private playlist "${playlistId}" for user "${session.userId}".`
  843. );
  844. return cb({
  845. status: "success",
  846. data: { playlist }
  847. });
  848. }
  849. );
  850. },
  851. /**
  852. * Gets a playlist from station id
  853. *
  854. * @param {object} session - the session object automatically added by the websocket
  855. * @param {string} stationId - the id of the station we are getting
  856. * @param {string} includeSongs - include songs
  857. * @param {Function} cb - gets called with the result
  858. */
  859. getPlaylistForStation: function getPlaylist(session, stationId, includeSongs, cb) {
  860. async.waterfall(
  861. [
  862. next => {
  863. PlaylistsModule.runJob("GET_STATION_PLAYLIST", { stationId, includeSongs }, this)
  864. .then(response => next(null, response.playlist))
  865. .catch(next);
  866. },
  867. (playlist, next) => {
  868. if (!playlist) return next("Playlist not found");
  869. if (playlist.privacy !== "public")
  870. return hasPermission("stations.view", session, stationId)
  871. .then(() => next(null, playlist))
  872. .catch(() => next("User unauthorised to view playlist."));
  873. return next(null, playlist);
  874. }
  875. ],
  876. async (err, playlist) => {
  877. if (err) {
  878. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  879. this.log(
  880. "ERROR",
  881. "PLAYLIST_GET",
  882. `Getting playlist for station "${stationId}" failed for user "${session.userId}". "${err}"`
  883. );
  884. return cb({ status: "error", message: err });
  885. }
  886. this.log(
  887. "SUCCESS",
  888. "PLAYLIST_GET",
  889. `Successfully got playlist for station "${stationId}" for user "${session.userId}".`
  890. );
  891. return cb({
  892. status: "success",
  893. data: { playlist }
  894. });
  895. }
  896. );
  897. },
  898. /**
  899. * Shuffles songs in a private playlist
  900. *
  901. * @param {object} session - the session object automatically added by the websocket
  902. * @param {string} playlistId - the id of the playlist we are updating
  903. * @param {Function} cb - gets called with the result
  904. */
  905. shuffle: isLoginRequired(async function shuffle(session, playlistId, cb) {
  906. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  907. async.waterfall(
  908. [
  909. next => {
  910. if (!playlistId) return next("No playlist id.");
  911. return next();
  912. },
  913. next => {
  914. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  915. .then(playlist => {
  916. if (!playlist || playlist.createdBy !== session.userId)
  917. return next("Something went wrong when trying to get the playlist");
  918. return next(null, playlist);
  919. })
  920. .catch(next);
  921. },
  922. (playlist, next) => {
  923. if (!playlist.isUserModifiable) return next("Playlist cannot be shuffled.");
  924. return UtilsModule.runJob("SHUFFLE_SONG_POSITIONS", { array: playlist.songs }, this)
  925. .then(result => next(null, result.array))
  926. .catch(next);
  927. },
  928. (songs, next) => {
  929. playlistModel.updateOne({ _id: playlistId }, { $set: { songs } }, { runValidators: true }, next);
  930. },
  931. (res, next) => {
  932. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  933. .then(playlist => next(null, playlist))
  934. .catch(next);
  935. }
  936. ],
  937. async (err, playlist) => {
  938. if (err) {
  939. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  940. this.log(
  941. "ERROR",
  942. "PLAYLIST_SHUFFLE",
  943. `Updating private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  944. );
  945. return cb({ status: "error", message: err });
  946. }
  947. this.log(
  948. "SUCCESS",
  949. "PLAYLIST_SHUFFLE",
  950. `Successfully updated private playlist "${playlistId}" for user "${session.userId}".`
  951. );
  952. return cb({
  953. status: "success",
  954. message: "Successfully shuffled playlist.",
  955. data: { playlist }
  956. });
  957. }
  958. );
  959. }),
  960. /**
  961. * Changes the order (position) of a song in a playlist
  962. *
  963. * @param {object} session - the session object automatically added by the websocket
  964. * @param {string} playlistId - the id of the playlist we are targeting
  965. * @param {object} song - the song to be repositioned
  966. * @param {string} song.mediaSource - the media source of the song being repositioned
  967. * @param {string} song.newIndex - the new position of the song in the playlist
  968. * @param {...any} song.args - any other elements that would be included with a song item in a playlist
  969. * @param {Function} cb - gets called with the result
  970. */
  971. repositionSong: isLoginRequired(async function repositionSong(session, playlistId, song, cb) {
  972. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  973. async.waterfall(
  974. [
  975. next => {
  976. if (!playlistId) return next("Please provide a playlist.");
  977. if (!song || !song.mediaSource) return next("You must provide a song to reposition.");
  978. return next();
  979. },
  980. next => {
  981. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  982. .then(playlist => {
  983. if (!playlist) return next("Playlist not found.");
  984. if (playlist.createdBy !== session.userId)
  985. return hasPermission("playlists.songs.reposition", session)
  986. .then(() => next())
  987. .catch(() => next("Invalid permissions."));
  988. return next();
  989. })
  990. .catch(next);
  991. },
  992. // remove song from playlist
  993. next => {
  994. playlistModel.updateOne(
  995. { _id: playlistId },
  996. { $pull: { songs: { mediaSource: song.mediaSource } } },
  997. next
  998. );
  999. },
  1000. // add song back to playlist (in new position)
  1001. (res, next) => {
  1002. playlistModel.updateOne(
  1003. { _id: playlistId },
  1004. { $push: { songs: { $each: [song], $position: song.newIndex } } },
  1005. err => next(err)
  1006. );
  1007. },
  1008. // update the cache with the new songs positioning
  1009. next => {
  1010. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  1011. .then(playlist => next(null, playlist))
  1012. .catch(next);
  1013. }
  1014. ],
  1015. async (err, playlist) => {
  1016. if (err) {
  1017. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1018. this.log(
  1019. "ERROR",
  1020. "PLAYLIST_REPOSITION_SONG",
  1021. `Repositioning song ${song.mediaSource} for private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1022. );
  1023. return cb({ status: "error", message: err });
  1024. }
  1025. this.log(
  1026. "SUCCESS",
  1027. "PLAYLIST_REPOSITION_SONG",
  1028. `Successfully repositioned song ${song.mediaSource} for private playlist "${playlistId}" for user "${session.userId}".`
  1029. );
  1030. CacheModule.runJob("PUB", {
  1031. channel: "playlist.repositionSong",
  1032. value: {
  1033. createdBy: playlist.createdBy,
  1034. playlistId,
  1035. song
  1036. }
  1037. });
  1038. return cb({
  1039. status: "success",
  1040. message: "Successfully repositioned song"
  1041. });
  1042. }
  1043. );
  1044. }),
  1045. /**
  1046. * Adds a song to a private playlist
  1047. *
  1048. * @param {object} session - the session object automatically added by the websocket
  1049. * @param {boolean} isSet - is the song part of a set of songs to be added
  1050. * @param {string} mediaSource - the media source of the song we are trying to add
  1051. * @param {string} playlistId - the id of the playlist we are adding the song to
  1052. * @param {Function} cb - gets called with the result
  1053. */
  1054. addSongToPlaylist: isLoginRequired(async function addSongToPlaylist(session, isSet, mediaSource, playlistId, cb) {
  1055. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  1056. async.waterfall(
  1057. [
  1058. next => {
  1059. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1060. .then(playlist => {
  1061. if (!playlist) return next("Playlist not found.");
  1062. if (playlist.createdBy !== session.userId)
  1063. return hasPermission("playlists.songs.add", session)
  1064. .then(() => next(null, playlist))
  1065. .catch(() => next("Invalid permissions."));
  1066. return next(null, playlist);
  1067. })
  1068. .catch(next);
  1069. },
  1070. (playlist, next) => {
  1071. MediaModule.runJob("GET_MEDIA", { mediaSource }, this)
  1072. .then(res =>
  1073. next(null, playlist, {
  1074. _id: res.song._id,
  1075. title: res.song.title,
  1076. thumbnail: res.song.thumbnail,
  1077. artists: res.song.artists,
  1078. mediaSource: res.song.mediaSource
  1079. })
  1080. )
  1081. .catch(next);
  1082. },
  1083. (playlist, song, next) => {
  1084. if (playlist.type === "user-liked" || playlist.type === "user-disliked") {
  1085. const oppositeType = playlist.type === "user-liked" ? "user-disliked" : "user-liked";
  1086. const oppositePlaylistName = oppositeType === "user-liked" ? "Liked Songs" : "Disliked Songs";
  1087. playlistModel.count(
  1088. { type: oppositeType, createdBy: session.userId, "songs.mediaSource": song.mediaSource },
  1089. (err, results) => {
  1090. if (err) next(err);
  1091. else if (results > 0)
  1092. next(
  1093. `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.`
  1094. );
  1095. else next(null, song);
  1096. }
  1097. );
  1098. } else next(null, song);
  1099. },
  1100. (_song, next) => {
  1101. PlaylistsModule.runJob("ADD_SONG_TO_PLAYLIST", { playlistId, mediaSource: _song.mediaSource }, this)
  1102. .then(res => {
  1103. const { playlist, song, ratings } = res;
  1104. next(null, playlist, song, ratings);
  1105. })
  1106. .catch(next);
  1107. }
  1108. ],
  1109. async (err, playlist, newSong, ratings) => {
  1110. if (err) {
  1111. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1112. this.log(
  1113. "ERROR",
  1114. "PLAYLIST_ADD_SONG",
  1115. `Adding song "${mediaSource}" to private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1116. );
  1117. return cb({ status: "error", message: err });
  1118. }
  1119. this.log(
  1120. "SUCCESS",
  1121. "PLAYLIST_ADD_SONG",
  1122. `Successfully added song "${mediaSource}" to private playlist "${playlistId}" for user "${session.userId}".`
  1123. );
  1124. if (!isSet && playlist.type === "user" && playlist.privacy === "public") {
  1125. const songName = newSong.artists
  1126. ? `${newSong.title} by ${newSong.artists.join(", ")}`
  1127. : newSong.title;
  1128. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1129. userId: session.userId,
  1130. type: "playlist__add_song",
  1131. payload: {
  1132. message: `Added <mediaSource>${songName}</mediaSource> to playlist <playlistId>${playlist.displayName}</playlistId>`,
  1133. thumbnail: newSong.thumbnail,
  1134. playlistId,
  1135. mediaSource
  1136. }
  1137. });
  1138. }
  1139. CacheModule.runJob("PUB", {
  1140. channel: "playlist.addSong",
  1141. value: {
  1142. playlistId: playlist._id,
  1143. song: newSong,
  1144. createdBy: playlist.createdBy,
  1145. privacy: playlist.privacy
  1146. }
  1147. });
  1148. CacheModule.runJob("PUB", {
  1149. channel: "playlist.updated",
  1150. value: { playlistId }
  1151. });
  1152. if (ratings && (playlist.type === "user-liked" || playlist.type === "user-disliked")) {
  1153. const { _id, mediaSource, title, artists, thumbnail } = newSong;
  1154. const { likes, dislikes } = ratings;
  1155. if (_id) SongsModule.runJob("UPDATE_SONG", { songId: _id });
  1156. if (playlist.type === "user-liked") {
  1157. CacheModule.runJob("PUB", {
  1158. channel: "ratings.like",
  1159. value: JSON.stringify({
  1160. mediaSource,
  1161. userId: session.userId,
  1162. likes,
  1163. dislikes
  1164. })
  1165. });
  1166. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1167. userId: session.userId,
  1168. type: "song__like",
  1169. payload: {
  1170. message: `Liked song <mediaSource>${title} by ${artists.join(", ")}</mediaSource>`,
  1171. mediaSource,
  1172. thumbnail
  1173. }
  1174. });
  1175. } else {
  1176. CacheModule.runJob("PUB", {
  1177. channel: "ratings.dislike",
  1178. value: JSON.stringify({
  1179. mediaSource,
  1180. userId: session.userId,
  1181. likes,
  1182. dislikes
  1183. })
  1184. });
  1185. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1186. userId: session.userId,
  1187. type: "song__dislike",
  1188. payload: {
  1189. message: `Disliked song <mediaSource>${title} by ${artists.join(
  1190. mediaSource
  1191. )}</mediaSource>`,
  1192. mediaSource,
  1193. thumbnail
  1194. }
  1195. });
  1196. }
  1197. }
  1198. return cb({
  1199. status: "success",
  1200. message: "Song has been successfully added to the playlist",
  1201. data: { songs: playlist.songs }
  1202. });
  1203. }
  1204. );
  1205. }),
  1206. /**
  1207. * Adds songs to a playlist
  1208. *
  1209. * @param {object} session - the session object automatically added by the websocket
  1210. * @param {string} playlistId - the id of the playlist we are adding the songs to
  1211. * @param {Array} mediaSources - the media sources of the songs we are trying to add
  1212. * @param {Function} cb - gets called with the result
  1213. */
  1214. addSongsToPlaylist: isLoginRequired(async function addSongsToPlaylist(session, playlistId, mediaSources, cb) {
  1215. const successful = [];
  1216. const existing = [];
  1217. const failed = {};
  1218. const errors = {};
  1219. const lastYoutubeId = "none";
  1220. const songsAdded = [];
  1221. const addError = message => {
  1222. if (!errors[message]) errors[message] = 1;
  1223. else errors[message] += 1;
  1224. };
  1225. async.waterfall(
  1226. [
  1227. next => {
  1228. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1229. .then(playlist => {
  1230. if (!playlist) return next("Playlist not found.");
  1231. if (playlist.createdBy !== session.userId)
  1232. return hasPermission("playlists.songs.add", session)
  1233. .then(() => next(null, playlist))
  1234. .catch(() => next("Invalid permissions."));
  1235. return next(null, playlist);
  1236. })
  1237. .catch(next);
  1238. },
  1239. async () => {
  1240. this.keepLongJob();
  1241. this.publishProgress({
  1242. status: "started",
  1243. title: "Bulk add songs to playlist",
  1244. message: "Adding songs to playlist.",
  1245. id: this.toString()
  1246. });
  1247. await CacheModule.runJob(
  1248. "RPUSH",
  1249. { key: `longJobs.${session.userId}`, value: this.toString() },
  1250. this
  1251. );
  1252. await CacheModule.runJob(
  1253. "PUB",
  1254. {
  1255. channel: "longJob.added",
  1256. value: { jobId: this.toString(), userId: session.userId }
  1257. },
  1258. this
  1259. );
  1260. },
  1261. (nothing, next) => {
  1262. async.eachLimit(
  1263. mediaSources,
  1264. 1,
  1265. (mediaSource, next) => {
  1266. this.publishProgress({ status: "update", message: `Adding song "${mediaSource}"` });
  1267. PlaylistsModule.runJob("ADD_SONG_TO_PLAYLIST", { playlistId, mediaSource }, this)
  1268. .then(({ song }) => {
  1269. successful.push(mediaSource);
  1270. songsAdded.push(song);
  1271. next();
  1272. })
  1273. .catch(async err => {
  1274. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1275. if (err === "That song is already in the playlist.") {
  1276. existing.push(mediaSource);
  1277. next();
  1278. } else {
  1279. addError(err);
  1280. failed[mediaSource] = err;
  1281. next();
  1282. }
  1283. });
  1284. },
  1285. err => {
  1286. if (err) next(err);
  1287. else next();
  1288. }
  1289. );
  1290. },
  1291. next => {
  1292. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1293. .then(playlist => {
  1294. if (!playlist) return next("Playlist not found.");
  1295. return next(null, playlist);
  1296. })
  1297. .catch(next);
  1298. }
  1299. ],
  1300. async (err, playlist) => {
  1301. if (err) {
  1302. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1303. this.log(
  1304. "ERROR",
  1305. "PLAYLIST_ADD_SONGS",
  1306. `Adding songs to playlist "${playlistId}" failed for user "${
  1307. session.userId
  1308. }". "${err}". Stats: successful:${successful.length}, existing:${existing.length}, failed:${
  1309. Object.keys(failed).length
  1310. }, last mediaSource:${lastYoutubeId}, mediaSources length:${
  1311. mediaSources ? mediaSources.length : null
  1312. }`
  1313. );
  1314. return cb({
  1315. status: "error",
  1316. message: err,
  1317. data: {
  1318. stats: {
  1319. successful,
  1320. existing,
  1321. failed,
  1322. errors
  1323. }
  1324. }
  1325. });
  1326. }
  1327. this.log(
  1328. "SUCCESS",
  1329. "PLAYLIST_ADD_SONGS",
  1330. `Successfully added songs to playlist "${playlistId}" for user "${
  1331. session.userId
  1332. }". Stats: successful:${successful.length}, existing:${existing.length}, failed:${
  1333. Object.keys(failed).length
  1334. }, mediaSources length:${mediaSources ? mediaSources.length : null}`
  1335. );
  1336. await async.eachLimit(songsAdded, 1, (song, next) => {
  1337. CacheModule.runJob("PUB", {
  1338. channel: "playlist.addSong",
  1339. value: {
  1340. playlistId: playlist._id,
  1341. song,
  1342. createdBy: playlist.createdBy,
  1343. privacy: playlist.privacy
  1344. }
  1345. });
  1346. next();
  1347. });
  1348. CacheModule.runJob("PUB", {
  1349. channel: "playlist.updated",
  1350. value: { playlistId }
  1351. });
  1352. const message = `Done adding songs. Succesful: ${successful.length}, failed: ${
  1353. Object.keys(failed).length
  1354. }, existing: ${existing.length}.`;
  1355. this.publishProgress({
  1356. status: "success",
  1357. message
  1358. });
  1359. return cb({
  1360. status: "success",
  1361. message,
  1362. data: {
  1363. songs: playlist.songs,
  1364. stats: {
  1365. successful,
  1366. existing,
  1367. failed,
  1368. errors
  1369. }
  1370. }
  1371. });
  1372. }
  1373. );
  1374. }),
  1375. /**
  1376. * Removes songs from a playlist
  1377. *
  1378. * @param {object} session - the session object automatically added by the websocket
  1379. * @param {string} playlistId - the id of the playlist we are removing the songs from
  1380. * @param {Array} mediaSources - the media sources of the songs we are trying to remove
  1381. * @param {Function} cb - gets called with the result
  1382. */
  1383. removeSongsFromPlaylist: useHasPermission(
  1384. "playlists.songs.remove",
  1385. async function removeSongsFromPlaylist(session, playlistId, mediaSources, cb) {
  1386. const successful = [];
  1387. const notInPlaylist = [];
  1388. const failed = {};
  1389. const errors = {};
  1390. const lastYoutubeId = "none";
  1391. const addError = message => {
  1392. if (!errors[message]) errors[message] = 1;
  1393. else errors[message] += 1;
  1394. };
  1395. this.keepLongJob();
  1396. this.publishProgress({
  1397. status: "started",
  1398. title: "Bulk remove songs from playlist",
  1399. message: "Removing songs from playlist.",
  1400. id: this.toString()
  1401. });
  1402. await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
  1403. await CacheModule.runJob(
  1404. "PUB",
  1405. {
  1406. channel: "longJob.added",
  1407. value: { jobId: this.toString(), userId: session.userId }
  1408. },
  1409. this
  1410. );
  1411. async.waterfall(
  1412. [
  1413. next => {
  1414. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1415. .then(playlist => {
  1416. if (!playlist) return next("Playlist not found.");
  1417. return next(null, playlist);
  1418. })
  1419. .catch(next);
  1420. },
  1421. (playlist, next) => {
  1422. if (playlist.type !== "admin") return next("Playlist must be of type admin.");
  1423. return next();
  1424. },
  1425. next => {
  1426. async.eachLimit(
  1427. mediaSources,
  1428. 1,
  1429. (mediaSource, next) => {
  1430. this.publishProgress({ status: "update", message: `Removing song "${mediaSource}"` });
  1431. PlaylistsModule.runJob("REMOVE_FROM_PLAYLIST", { playlistId, mediaSource }, this)
  1432. .then(() => {
  1433. successful.push(mediaSource);
  1434. next();
  1435. })
  1436. .catch(async err => {
  1437. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1438. if (err === "That song is not currently in the playlist.") {
  1439. notInPlaylist.push(mediaSource);
  1440. next();
  1441. } else {
  1442. addError(err);
  1443. failed[mediaSource] = err;
  1444. next();
  1445. }
  1446. });
  1447. },
  1448. err => {
  1449. if (err) next(err);
  1450. else next();
  1451. }
  1452. );
  1453. },
  1454. next => {
  1455. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1456. .then(playlist => {
  1457. if (!playlist) return next("Playlist not found.");
  1458. return next(null, playlist);
  1459. })
  1460. .catch(next);
  1461. }
  1462. ],
  1463. async (err, playlist) => {
  1464. if (err) {
  1465. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1466. this.log(
  1467. "ERROR",
  1468. "PLAYLIST_REMOVE_SONGS",
  1469. `Removing songs from playlist "${playlistId}" failed for user "${
  1470. session.userId
  1471. }". "${err}". Stats: successful:${successful.length}, notInPlaylist:${
  1472. notInPlaylist.length
  1473. }, failed:${
  1474. Object.keys(failed).length
  1475. }, last mediaSource:${lastYoutubeId}, mediaSources length:${
  1476. mediaSources ? mediaSources.length : null
  1477. }`
  1478. );
  1479. return cb({
  1480. status: "error",
  1481. message: err,
  1482. data: {
  1483. stats: {
  1484. successful,
  1485. notInPlaylist,
  1486. failed,
  1487. errors
  1488. }
  1489. }
  1490. });
  1491. }
  1492. this.log(
  1493. "SUCCESS",
  1494. "PLAYLIST_REMOVE_SONGS",
  1495. `Successfully removed songs from playlist "${playlistId}" for user "${
  1496. session.userId
  1497. }". Stats: successful:${successful.length}, notInPlaylist:${notInPlaylist.length}, failed:${
  1498. Object.keys(failed).length
  1499. }, mediaSources length:${mediaSources ? mediaSources.length : null}`
  1500. );
  1501. CacheModule.runJob("PUB", {
  1502. channel: "playlist.updated",
  1503. value: { playlistId }
  1504. });
  1505. const message = `Done removing songs. Succesful: ${successful.length}, failed: ${
  1506. Object.keys(failed).length
  1507. }, not in playlist: ${notInPlaylist.length}.`;
  1508. this.publishProgress({
  1509. status: "success",
  1510. message
  1511. });
  1512. return cb({
  1513. status: "success",
  1514. message,
  1515. data: {
  1516. songs: playlist.songs,
  1517. stats: {
  1518. successful,
  1519. notInPlaylist,
  1520. failed,
  1521. errors
  1522. }
  1523. }
  1524. });
  1525. }
  1526. );
  1527. }
  1528. ),
  1529. /**
  1530. * Adds a set of songs to a private playlist
  1531. *
  1532. * @param {object} session - the session object automatically added by the websocket
  1533. * @param {string} url - the url of the the YouTube playlist
  1534. * @param {string} playlistId - the id of the playlist we are adding the set of songs to
  1535. * @param {boolean} musicOnly - whether to only add music to the playlist
  1536. * @param {Function} cb - gets called with the result
  1537. */
  1538. addYoutubeSetToPlaylist: isLoginRequired(async function addYoutubeSetToPlaylist(
  1539. session,
  1540. url,
  1541. playlistId,
  1542. musicOnly,
  1543. cb
  1544. ) {
  1545. let videosInPlaylistTotal = 0;
  1546. let songsInPlaylistTotal = 0;
  1547. let addSongsStats = null;
  1548. const addedSongs = [];
  1549. this.keepLongJob();
  1550. this.publishProgress({
  1551. status: "started",
  1552. title: "Import YouTube playlist",
  1553. message: "Importing YouTube playlist.",
  1554. id: this.toString()
  1555. });
  1556. await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
  1557. await CacheModule.runJob(
  1558. "PUB",
  1559. {
  1560. channel: "longJob.added",
  1561. value: { jobId: this.toString(), userId: session.userId }
  1562. },
  1563. this
  1564. );
  1565. async.waterfall(
  1566. [
  1567. next => {
  1568. DBModule.runJob("GET_MODEL", { modelName: "user" }, this).then(userModel => {
  1569. userModel.findOne({ _id: session.userId }, (err, user) => {
  1570. if (user && user.role === "admin") return next(null, true);
  1571. return next(null, false);
  1572. });
  1573. });
  1574. },
  1575. (isAdmin, next) => {
  1576. this.publishProgress({ status: "update", message: `Importing YouTube playlist (stage 1)` });
  1577. const playlistRegex = /[\\?&]list=([^&#]*)/;
  1578. const channelRegex =
  1579. /\.[\w]+\/(?:(?:channel\/(UC[0-9A-Za-z_-]{21}[AQgw]))|(?:user\/?([\w-]+))|(?:c\/?([\w-]+))|(?:\/?([\w-]+)))/;
  1580. if (playlistRegex.exec(url) || channelRegex.exec(url))
  1581. YouTubeModule.runJob(
  1582. playlistRegex.exec(url) ? "GET_PLAYLIST" : "GET_CHANNEL",
  1583. {
  1584. url,
  1585. musicOnly,
  1586. disableSearch: !isAdmin
  1587. },
  1588. this
  1589. )
  1590. .then(res => {
  1591. if (res.filteredSongs) {
  1592. videosInPlaylistTotal = res.songs.length;
  1593. songsInPlaylistTotal = res.filteredSongs.length;
  1594. } else {
  1595. songsInPlaylistTotal = videosInPlaylistTotal = res.songs.length;
  1596. }
  1597. next(null, res.songs);
  1598. })
  1599. .catch(next);
  1600. else next("Invalid YouTube URL.");
  1601. },
  1602. (mediaSources, next) => {
  1603. this.publishProgress({ status: "update", message: `Importing YouTube playlist (stage 2)` });
  1604. let successful = 0;
  1605. let failed = 0;
  1606. let alreadyInPlaylist = 0;
  1607. let alreadyInLikedPlaylist = 0;
  1608. let alreadyInDislikedPlaylist = 0;
  1609. if (mediaSources.length === 0) next();
  1610. async.eachLimit(
  1611. mediaSources,
  1612. 1,
  1613. (mediaSource, next) => {
  1614. WSModule.runJob(
  1615. "RUN_ACTION2",
  1616. {
  1617. session,
  1618. namespace: "playlists",
  1619. action: "addSongToPlaylist",
  1620. args: [true, mediaSource, playlistId]
  1621. },
  1622. this
  1623. )
  1624. .then(res => {
  1625. if (res.status === "success") {
  1626. successful += 1;
  1627. addedSongs.push(mediaSource);
  1628. } else failed += 1;
  1629. if (res.message === "That song is already in the playlist") alreadyInPlaylist += 1;
  1630. else if (
  1631. res.message ===
  1632. "That song is already in your Liked Songs playlist. " +
  1633. "A song cannot be in both the Liked Songs playlist" +
  1634. " and the Disliked Songs playlist at the same time."
  1635. )
  1636. alreadyInLikedPlaylist += 1;
  1637. else if (
  1638. res.message ===
  1639. "That song is already in your Disliked Songs playlist. " +
  1640. "A song cannot be in both the Liked Songs playlist " +
  1641. "and the Disliked Songs playlist at the same time."
  1642. )
  1643. alreadyInDislikedPlaylist += 1;
  1644. })
  1645. .catch(() => {
  1646. failed += 1;
  1647. })
  1648. .finally(() => next());
  1649. },
  1650. () => {
  1651. addSongsStats = {
  1652. successful,
  1653. failed,
  1654. alreadyInPlaylist,
  1655. alreadyInLikedPlaylist,
  1656. alreadyInDislikedPlaylist
  1657. };
  1658. next(null);
  1659. }
  1660. );
  1661. },
  1662. next => {
  1663. this.publishProgress({ status: "update", message: `Importing YouTube playlist (stage 3)` });
  1664. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1665. .then(playlist => next(null, playlist))
  1666. .catch(next);
  1667. },
  1668. (playlist, next) => {
  1669. this.publishProgress({ status: "update", message: `Importing YouTube playlist (stage 4)` });
  1670. if (!playlist) return next("Playlist not found.");
  1671. if (playlist.createdBy !== session.userId)
  1672. return hasPermission("playlists.songs.add", session)
  1673. .then(() => next(null, playlist))
  1674. .catch(() => next("Invalid permissions."));
  1675. return next(null, playlist);
  1676. }
  1677. ],
  1678. async (err, playlist) => {
  1679. if (err) {
  1680. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1681. this.log(
  1682. "ERROR",
  1683. "PLAYLIST_IMPORT",
  1684. `Importing a YouTube playlist to private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1685. );
  1686. this.publishProgress({
  1687. status: "error",
  1688. message: err
  1689. });
  1690. return cb({ status: "error", message: err });
  1691. }
  1692. if (playlist.privacy === "public")
  1693. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1694. userId: session.userId,
  1695. type: "playlist__import_playlist",
  1696. payload: {
  1697. message: `Imported ${addSongsStats.successful} songs to playlist <playlistId>${playlist.displayName}</playlistId>`,
  1698. playlistId
  1699. }
  1700. });
  1701. this.log(
  1702. "SUCCESS",
  1703. "PLAYLIST_IMPORT",
  1704. `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}.`
  1705. );
  1706. this.publishProgress({
  1707. status: "success",
  1708. message: `Playlist has been imported. ${addSongsStats.successful} were added successfully, ${addSongsStats.failed} failed (${addSongsStats.alreadyInPlaylist} were already in the playlist)`
  1709. });
  1710. return cb({
  1711. status: "success",
  1712. message: `Playlist has been imported. ${addSongsStats.successful} were added successfully, ${addSongsStats.failed} failed (${addSongsStats.alreadyInPlaylist} were already in the playlist)`,
  1713. data: {
  1714. songs: playlist.songs,
  1715. stats: {
  1716. videosInPlaylistTotal,
  1717. songsInPlaylistTotal,
  1718. alreadyInLikedPlaylist: addSongsStats.alreadyInLikedPlaylist,
  1719. alreadyInDislikedPlaylist: addSongsStats.alreadyInDislikedPlaylist
  1720. }
  1721. }
  1722. });
  1723. }
  1724. );
  1725. }),
  1726. /**
  1727. * Adds a set of Soundcloud songs to a private playlist
  1728. *
  1729. * @param {object} session - the session object automatically added by the websocket
  1730. * @param {string} url - the url of the the SoundCloud playlist
  1731. * @param {string} playlistId - the id of the playlist we are adding the set of songs to
  1732. * @param {Function} cb - gets called with the result
  1733. */
  1734. addSoundcloudSetToPlaylist: isLoginRequired(async function addSoundcloudSetToPlaylist(
  1735. session,
  1736. url,
  1737. playlistId,
  1738. cb
  1739. ) {
  1740. let songsInPlaylistTotal = 0;
  1741. let addSongsStats = null;
  1742. const addedSongs = [];
  1743. this.keepLongJob();
  1744. this.publishProgress({
  1745. status: "started",
  1746. title: "Import SoundCloud playlist",
  1747. message: "Importing SoundCloud playlist.",
  1748. id: this.toString()
  1749. });
  1750. await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
  1751. await CacheModule.runJob(
  1752. "PUB",
  1753. {
  1754. channel: "longJob.added",
  1755. value: { jobId: this.toString(), userId: session.userId }
  1756. },
  1757. this
  1758. );
  1759. async.waterfall(
  1760. [
  1761. next => {
  1762. DBModule.runJob("GET_MODEL", { modelName: "user" }, this).then(userModel => {
  1763. userModel.findOne({ _id: session.userId }, (err, user) => {
  1764. if (user && user.role === "admin") return next(null, true);
  1765. return next(null, false);
  1766. });
  1767. });
  1768. },
  1769. (isAdmin, next) => {
  1770. this.publishProgress({ status: "update", message: `Importing SoundCloud playlist (stage 1)` });
  1771. SoundcloudModule.runJob(
  1772. "GET_PLAYLIST",
  1773. {
  1774. url
  1775. },
  1776. this
  1777. )
  1778. .then(res => {
  1779. songsInPlaylistTotal = res.songs.length;
  1780. const mediaSources = res.songs.map(song => `soundcloud:${song}`);
  1781. next(null, mediaSources);
  1782. })
  1783. .catch(next);
  1784. },
  1785. (mediaSources, next) => {
  1786. this.publishProgress({ status: "update", message: `Importing SoundCloud playlist (stage 2)` });
  1787. let successful = 0;
  1788. let failed = 0;
  1789. let alreadyInPlaylist = 0;
  1790. let alreadyInLikedPlaylist = 0;
  1791. let alreadyInDislikedPlaylist = 0;
  1792. if (mediaSources.length === 0) next();
  1793. async.eachLimit(
  1794. mediaSources,
  1795. 1,
  1796. (mediaSource, next) => {
  1797. WSModule.runJob(
  1798. "RUN_ACTION2",
  1799. {
  1800. session,
  1801. namespace: "playlists",
  1802. action: "addSongToPlaylist",
  1803. args: [true, mediaSource, playlistId]
  1804. },
  1805. this
  1806. )
  1807. .then(res => {
  1808. if (res.status === "success") {
  1809. successful += 1;
  1810. addedSongs.push(mediaSource);
  1811. } else failed += 1;
  1812. if (res.message === "That song is already in the playlist") alreadyInPlaylist += 1;
  1813. else if (
  1814. res.message ===
  1815. "That song is already in your Liked Songs playlist. " +
  1816. "A song cannot be in both the Liked Songs playlist" +
  1817. " and the Disliked Songs playlist at the same time."
  1818. )
  1819. alreadyInLikedPlaylist += 1;
  1820. else if (
  1821. res.message ===
  1822. "That song is already in your Disliked Songs playlist. " +
  1823. "A song cannot be in both the Liked Songs playlist " +
  1824. "and the Disliked Songs playlist at the same time."
  1825. )
  1826. alreadyInDislikedPlaylist += 1;
  1827. })
  1828. .catch(() => {
  1829. failed += 1;
  1830. })
  1831. .finally(() => next());
  1832. },
  1833. () => {
  1834. addSongsStats = {
  1835. successful,
  1836. failed,
  1837. alreadyInPlaylist,
  1838. alreadyInLikedPlaylist,
  1839. alreadyInDislikedPlaylist
  1840. };
  1841. next(null);
  1842. }
  1843. );
  1844. },
  1845. next => {
  1846. this.publishProgress({ status: "update", message: `Importing SoundCloud playlist (stage 3)` });
  1847. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1848. .then(playlist => next(null, playlist))
  1849. .catch(next);
  1850. },
  1851. (playlist, next) => {
  1852. this.publishProgress({ status: "update", message: `Importing SoundCloud playlist (stage 4)` });
  1853. if (!playlist) return next("Playlist not found.");
  1854. if (playlist.createdBy !== session.userId)
  1855. return hasPermission("playlists.songs.add", session)
  1856. .then(() => next(null, playlist))
  1857. .catch(() => next("Invalid permissions."));
  1858. return next(null, playlist);
  1859. }
  1860. ],
  1861. async (err, playlist) => {
  1862. if (err) {
  1863. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1864. this.log(
  1865. "ERROR",
  1866. "PLAYLIST_IMPORT",
  1867. `Importing a SoundCloud playlist to private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1868. );
  1869. this.publishProgress({
  1870. status: "error",
  1871. message: err
  1872. });
  1873. return cb({ status: "error", message: err });
  1874. }
  1875. if (playlist.privacy === "public")
  1876. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1877. userId: session.userId,
  1878. type: "playlist__import_playlist",
  1879. payload: {
  1880. message: `Imported ${addSongsStats.successful} songs to playlist <playlistId>${playlist.displayName}</playlistId>`,
  1881. playlistId
  1882. }
  1883. });
  1884. this.log(
  1885. "SUCCESS",
  1886. "PLAYLIST_IMPORT",
  1887. `Successfully imported a SoundCloud playlist to private playlist "${playlistId}" for user "${session.userId}". 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}.`
  1888. );
  1889. this.publishProgress({
  1890. status: "success",
  1891. message: `Playlist has been imported. ${addSongsStats.successful} were added successfully, ${addSongsStats.failed} failed (${addSongsStats.alreadyInPlaylist} were already in the playlist)`
  1892. });
  1893. return cb({
  1894. status: "success",
  1895. message: `Playlist has been imported. ${addSongsStats.successful} were added successfully, ${addSongsStats.failed} failed (${addSongsStats.alreadyInPlaylist} were already in the playlist)`,
  1896. data: {
  1897. songs: playlist.songs,
  1898. stats: {
  1899. songsInPlaylistTotal,
  1900. alreadyInLikedPlaylist: addSongsStats.alreadyInLikedPlaylist,
  1901. alreadyInDislikedPlaylist: addSongsStats.alreadyInDislikedPlaylist
  1902. }
  1903. }
  1904. });
  1905. }
  1906. );
  1907. }),
  1908. /**
  1909. * Adds a set of Spotify songs to a private playlist
  1910. *
  1911. * @param {object} session - the session object automatically added by the websocket
  1912. * @param {string} url - the url of the the Spotify playlist
  1913. * @param {string} playlistId - the id of the playlist we are adding the set of songs to
  1914. * @param {Function} cb - gets called with the result
  1915. */
  1916. addSpotifySetToPlaylist: isLoginRequired(async function addSpotifySetToPlaylist(session, url, playlistId, cb) {
  1917. let songsInPlaylistTotal = 0;
  1918. let addSongsStats = null;
  1919. const addedSongs = [];
  1920. this.keepLongJob();
  1921. this.publishProgress({
  1922. status: "started",
  1923. title: "Import Spotify playlist",
  1924. message: "Importing Spotify playlist.",
  1925. id: this.toString()
  1926. });
  1927. await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
  1928. await CacheModule.runJob(
  1929. "PUB",
  1930. {
  1931. channel: "longJob.added",
  1932. value: { jobId: this.toString(), userId: session.userId }
  1933. },
  1934. this
  1935. );
  1936. async.waterfall(
  1937. [
  1938. next => {
  1939. DBModule.runJob("GET_MODEL", { modelName: "user" }, this).then(userModel => {
  1940. userModel.findOne({ _id: session.userId }, (err, user) => {
  1941. if (user && user.role === "admin") return next(null, true);
  1942. return next(null, false);
  1943. });
  1944. });
  1945. },
  1946. (isAdmin, next) => {
  1947. this.publishProgress({ status: "update", message: `Importing Spotify playlist (stage 1)` });
  1948. SpotifyModule.runJob(
  1949. "GET_PLAYLIST",
  1950. {
  1951. url
  1952. },
  1953. this
  1954. )
  1955. .then(res => {
  1956. songsInPlaylistTotal = res.songs.length;
  1957. const mediaSources = res.songs.map(song => `spotify:${song}`);
  1958. next(null, mediaSources);
  1959. })
  1960. .catch(next);
  1961. },
  1962. (mediaSources, next) => {
  1963. this.publishProgress({ status: "update", message: `Importing Spotify playlist (stage 2)` });
  1964. let successful = 0;
  1965. let failed = 0;
  1966. let alreadyInPlaylist = 0;
  1967. let alreadyInLikedPlaylist = 0;
  1968. let alreadyInDislikedPlaylist = 0;
  1969. if (mediaSources.length === 0) next();
  1970. async.eachLimit(
  1971. mediaSources,
  1972. 1,
  1973. (mediaSource, next) => {
  1974. WSModule.runJob(
  1975. "RUN_ACTION2",
  1976. {
  1977. session,
  1978. namespace: "playlists",
  1979. action: "addSongToPlaylist",
  1980. args: [true, mediaSource, playlistId]
  1981. },
  1982. this
  1983. )
  1984. .then(res => {
  1985. if (res.status === "success") {
  1986. successful += 1;
  1987. addedSongs.push(mediaSource);
  1988. } else failed += 1;
  1989. if (res.message === "That song is already in the playlist") alreadyInPlaylist += 1;
  1990. else if (
  1991. res.message ===
  1992. "That song is already in your Liked Songs playlist. " +
  1993. "A song cannot be in both the Liked Songs playlist" +
  1994. " and the Disliked Songs playlist at the same time."
  1995. )
  1996. alreadyInLikedPlaylist += 1;
  1997. else if (
  1998. res.message ===
  1999. "That song is already in your Disliked Songs playlist. " +
  2000. "A song cannot be in both the Liked Songs playlist " +
  2001. "and the Disliked Songs playlist at the same time."
  2002. )
  2003. alreadyInDislikedPlaylist += 1;
  2004. })
  2005. .catch(() => {
  2006. failed += 1;
  2007. })
  2008. .finally(() => next());
  2009. },
  2010. () => {
  2011. addSongsStats = {
  2012. successful,
  2013. failed,
  2014. alreadyInPlaylist,
  2015. alreadyInLikedPlaylist,
  2016. alreadyInDislikedPlaylist
  2017. };
  2018. next(null);
  2019. }
  2020. );
  2021. },
  2022. next => {
  2023. this.publishProgress({ status: "update", message: `Importing Spotify playlist (stage 3)` });
  2024. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  2025. .then(playlist => next(null, playlist))
  2026. .catch(next);
  2027. },
  2028. (playlist, next) => {
  2029. this.publishProgress({ status: "update", message: `Importing Spotify playlist (stage 4)` });
  2030. if (!playlist) return next("Playlist not found.");
  2031. if (playlist.createdBy !== session.userId)
  2032. return hasPermission("playlists.songs.add", session)
  2033. .then(() => next(null, playlist))
  2034. .catch(() => next("Invalid permissions."));
  2035. return next(null, playlist);
  2036. }
  2037. ],
  2038. async (err, playlist) => {
  2039. if (err) {
  2040. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2041. this.log(
  2042. "ERROR",
  2043. "PLAYLIST_IMPORT",
  2044. `Importing a Spotify playlist to private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  2045. );
  2046. this.publishProgress({
  2047. status: "error",
  2048. message: err
  2049. });
  2050. return cb({ status: "error", message: err });
  2051. }
  2052. if (playlist.privacy === "public")
  2053. ActivitiesModule.runJob("ADD_ACTIVITY", {
  2054. userId: session.userId,
  2055. type: "playlist__import_playlist",
  2056. payload: {
  2057. message: `Imported ${addSongsStats.successful} songs to playlist <playlistId>${playlist.displayName}</playlistId>`,
  2058. playlistId
  2059. }
  2060. });
  2061. this.log(
  2062. "SUCCESS",
  2063. "PLAYLIST_IMPORT",
  2064. `Successfully imported a Spotify playlist to private playlist "${playlistId}" for user "${session.userId}". 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}.`
  2065. );
  2066. this.publishProgress({
  2067. status: "success",
  2068. message: `Playlist has been imported. ${addSongsStats.successful} were added successfully, ${addSongsStats.failed} failed (${addSongsStats.alreadyInPlaylist} were already in the playlist)`
  2069. });
  2070. return cb({
  2071. status: "success",
  2072. message: `Playlist has been imported. ${addSongsStats.successful} were added successfully, ${addSongsStats.failed} failed (${addSongsStats.alreadyInPlaylist} were already in the playlist)`,
  2073. data: {
  2074. songs: playlist.songs,
  2075. stats: {
  2076. songsInPlaylistTotal,
  2077. alreadyInLikedPlaylist: addSongsStats.alreadyInLikedPlaylist,
  2078. alreadyInDislikedPlaylist: addSongsStats.alreadyInDislikedPlaylist
  2079. }
  2080. }
  2081. });
  2082. }
  2083. );
  2084. }),
  2085. /**
  2086. * Removes a song from a private playlist
  2087. *
  2088. * @param {object} session - the session object automatically added by the websocket
  2089. * @param {string} mediaSource - the media source of the song we are removing from the private playlist
  2090. * @param {string} playlistId - the id of the playlist we are removing the song from
  2091. * @param {Function} cb - gets called with the result
  2092. */
  2093. removeSongFromPlaylist: isLoginRequired(async function removeSongFromPlaylist(
  2094. session,
  2095. mediaSource,
  2096. playlistId,
  2097. cb
  2098. ) {
  2099. async.waterfall(
  2100. [
  2101. next => {
  2102. if (!mediaSource || typeof mediaSource !== "string") return next("Invalid song id.");
  2103. if (!playlistId || typeof mediaSource !== "string") return next("Invalid playlist id.");
  2104. return next();
  2105. },
  2106. next => {
  2107. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  2108. .then(playlist => {
  2109. if (!playlist) return next("Playlist not found.");
  2110. if (playlist.createdBy !== session.userId)
  2111. return hasPermission("playlists.songs.remove", session)
  2112. .then(() => next(null, playlist))
  2113. .catch(() => next("Invalid permissions."));
  2114. return next(null, playlist);
  2115. })
  2116. .catch(next);
  2117. },
  2118. (playlist, next) => {
  2119. MediaModule.runJob("GET_MEDIA", { mediaSource }, this)
  2120. .then(res =>
  2121. next(null, playlist, {
  2122. _id: res.song._id,
  2123. title: res.song.title,
  2124. thumbnail: res.song.thumbnail,
  2125. artists: res.song.artists,
  2126. mediaSource: res.song.mediaSource
  2127. })
  2128. )
  2129. .catch(next);
  2130. },
  2131. (playlist, newSong, next) => {
  2132. PlaylistsModule.runJob("REMOVE_FROM_PLAYLIST", { playlistId, mediaSource }, this)
  2133. .then(res => {
  2134. const { ratings } = res;
  2135. next(null, playlist, newSong, ratings);
  2136. })
  2137. .catch(next);
  2138. },
  2139. (playlist, newSong, ratings, next) => {
  2140. const { _id, title, artists, thumbnail } = newSong;
  2141. const songName = artists ? `${title} by ${artists.join(", ")}` : title;
  2142. if (playlist.type === "user" && playlist.privacy === "public") {
  2143. ActivitiesModule.runJob("ADD_ACTIVITY", {
  2144. userId: session.userId,
  2145. type: "playlist__remove_song",
  2146. payload: {
  2147. message: `Removed <mediaSource>${songName}</mediaSource> from playlist <playlistId>${playlist.displayName}</playlistId>`,
  2148. thumbnail,
  2149. playlistId,
  2150. mediaSource: newSong.mediaSource
  2151. }
  2152. });
  2153. }
  2154. if (ratings && (playlist.type === "user-liked" || playlist.type === "user-disliked")) {
  2155. const { likes, dislikes } = ratings;
  2156. if (_id) SongsModule.runJob("UPDATE_SONG", { songId: _id });
  2157. if (playlist.type === "user-liked") {
  2158. CacheModule.runJob("PUB", {
  2159. channel: "ratings.unlike",
  2160. value: JSON.stringify({
  2161. mediaSource: newSong.mediaSource,
  2162. userId: session.userId,
  2163. likes,
  2164. dislikes
  2165. })
  2166. });
  2167. ActivitiesModule.runJob("ADD_ACTIVITY", {
  2168. userId: session.userId,
  2169. type: "song__unlike",
  2170. payload: {
  2171. message: `Removed <mediaSource>${title} by ${artists.join(
  2172. ", "
  2173. )}</mediaSource> from your Liked Songs`,
  2174. mediaSource: newSong.mediaSource,
  2175. thumbnail
  2176. }
  2177. });
  2178. } else {
  2179. CacheModule.runJob("PUB", {
  2180. channel: "ratings.undislike",
  2181. value: JSON.stringify({
  2182. mediaSource: newSong.mediaSource,
  2183. userId: session.userId,
  2184. likes,
  2185. dislikes
  2186. })
  2187. });
  2188. ActivitiesModule.runJob("ADD_ACTIVITY", {
  2189. userId: session.userId,
  2190. type: "song__undislike",
  2191. payload: {
  2192. message: `Removed <mediaSource>${title} by ${artists.join(
  2193. ", "
  2194. )}</mediaSource> from your Disliked Songs`,
  2195. mediaSource: newSong.mediaSource,
  2196. thumbnail
  2197. }
  2198. });
  2199. }
  2200. }
  2201. return next(null, playlist);
  2202. }
  2203. ],
  2204. async (err, playlist) => {
  2205. if (err) {
  2206. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2207. this.log(
  2208. "ERROR",
  2209. "PLAYLIST_REMOVE_SONG",
  2210. `Removing song "${mediaSource}" from private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  2211. );
  2212. return cb({ status: "error", message: err });
  2213. }
  2214. this.log(
  2215. "SUCCESS",
  2216. "PLAYLIST_REMOVE_SONG",
  2217. `Successfully removed song "${mediaSource}" from private playlist "${playlistId}" for user "${session.userId}".`
  2218. );
  2219. CacheModule.runJob("PUB", {
  2220. channel: "playlist.removeSong",
  2221. value: {
  2222. playlistId: playlist._id,
  2223. mediaSource,
  2224. createdBy: playlist.createdBy,
  2225. privacy: playlist.privacy
  2226. }
  2227. });
  2228. return cb({
  2229. status: "success",
  2230. message: "Song has been successfully removed from playlist",
  2231. data: { songs: playlist.songs }
  2232. });
  2233. }
  2234. );
  2235. }),
  2236. /**
  2237. * Updates the displayName of a private playlist
  2238. *
  2239. * @param {object} session - the session object automatically added by the websocket
  2240. * @param {string} playlistId - the id of the playlist we are updating the displayName for
  2241. * @param {Function} cb - gets called with the result
  2242. */
  2243. updateDisplayName: isLoginRequired(async function updateDisplayName(session, playlistId, displayName, cb) {
  2244. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  2245. async.waterfall(
  2246. [
  2247. next => {
  2248. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  2249. .then(playlist => next(null, playlist))
  2250. .catch(next);
  2251. },
  2252. (playlist, next) => {
  2253. if (playlist.type === "admin")
  2254. hasPermission("playlists.update.displayName", session)
  2255. .then(() => next())
  2256. .catch(() => next("Invalid permissions."));
  2257. else if (playlist.type !== "user" || playlist.createdBy !== session.userId)
  2258. next("Playlist cannot be modified.");
  2259. else next(null);
  2260. },
  2261. next => {
  2262. playlistModel.updateOne(
  2263. { _id: playlistId },
  2264. { $set: { displayName } },
  2265. { runValidators: true },
  2266. next
  2267. );
  2268. },
  2269. (res, next) => {
  2270. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  2271. .then(playlist => next(null, playlist))
  2272. .catch(next);
  2273. }
  2274. ],
  2275. async (err, playlist) => {
  2276. if (err) {
  2277. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2278. this.log(
  2279. "ERROR",
  2280. "PLAYLIST_UPDATE_DISPLAY_NAME",
  2281. `Updating display name to "${displayName}" for playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  2282. );
  2283. return cb({ status: "error", message: err });
  2284. }
  2285. this.log(
  2286. "SUCCESS",
  2287. "PLAYLIST_UPDATE_DISPLAY_NAME",
  2288. `Successfully updated display name to "${displayName}" for playlist "${playlistId}" for user "${session.userId}".`
  2289. );
  2290. CacheModule.runJob("PUB", {
  2291. channel: "playlist.updateDisplayName",
  2292. value: {
  2293. playlistId,
  2294. displayName,
  2295. createdBy: playlist.createdBy,
  2296. privacy: playlist.privacy
  2297. }
  2298. });
  2299. CacheModule.runJob("PUB", {
  2300. channel: "playlist.updated",
  2301. value: { playlistId }
  2302. });
  2303. if (playlist.type !== "admin")
  2304. ActivitiesModule.runJob("ADD_ACTIVITY", {
  2305. userId: session.userId,
  2306. type: "playlist__edit_display_name",
  2307. payload: {
  2308. message: `Changed display name of playlist <playlistId>${displayName}</playlistId>`,
  2309. playlistId
  2310. }
  2311. });
  2312. return cb({
  2313. status: "success",
  2314. message: "Playlist has been successfully updated"
  2315. });
  2316. }
  2317. );
  2318. }),
  2319. /**
  2320. * Removes a user's own modifiable user playlist
  2321. *
  2322. * @param {object} session - the session object automatically added by the websocket
  2323. * @param {string} playlistId - the id of the playlist we are removing
  2324. * @param {Function} cb - gets called with the result
  2325. */
  2326. remove: isLoginRequired(async function remove(session, playlistId, cb) {
  2327. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  2328. async.waterfall(
  2329. [
  2330. next => {
  2331. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  2332. .then(playlist => next(null, playlist))
  2333. .catch(next);
  2334. },
  2335. (playlist, next) => {
  2336. if (playlist.createdBy !== session.userId) return next("You do not own this playlist.");
  2337. if (playlist.type !== "user") return next("Playlist cannot be removed.");
  2338. return next(null, playlist);
  2339. },
  2340. (playlist, next) => {
  2341. userModel.updateOne(
  2342. { _id: playlist.createdBy },
  2343. { $pull: { "preferences.orderOfPlaylists": playlist._id } },
  2344. err => next(err, playlist)
  2345. );
  2346. },
  2347. (playlist, next) => {
  2348. PlaylistsModule.runJob("DELETE_PLAYLIST", { playlistId }, this)
  2349. .then(() => next(null, playlist))
  2350. .catch(next);
  2351. }
  2352. ],
  2353. async (err, playlist) => {
  2354. if (err) {
  2355. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2356. this.log(
  2357. "ERROR",
  2358. "PLAYLIST_REMOVE",
  2359. `Removing private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  2360. );
  2361. return cb({ status: "error", message: err });
  2362. }
  2363. this.log(
  2364. "SUCCESS",
  2365. "PLAYLIST_REMOVE",
  2366. `Successfully removed private playlist "${playlistId}" for user "${session.userId}".`
  2367. );
  2368. CacheModule.runJob("PUB", {
  2369. channel: "playlist.delete",
  2370. value: {
  2371. createdBy: playlist.createdBy,
  2372. playlistId
  2373. }
  2374. });
  2375. ActivitiesModule.runJob("ADD_ACTIVITY", {
  2376. userId: playlist.createdBy,
  2377. type: "playlist__remove",
  2378. payload: {
  2379. message: `Removed playlist ${playlist.displayName}`
  2380. }
  2381. });
  2382. ActivitiesModule.runJob("REMOVE_ACTIVITY_REFERENCES", { type: "playlistId", playlistId });
  2383. return cb({
  2384. status: "success",
  2385. message: "Playlist successfully removed"
  2386. });
  2387. }
  2388. );
  2389. }),
  2390. /**
  2391. * Removes a user's modifiable user playlist as an admin
  2392. *
  2393. * @param {object} session - the session object automatically added by the websocket
  2394. * @param {string} playlistId - the id of the playlist we are removing
  2395. * @param {Function} cb - gets called with the result
  2396. */
  2397. removeAdmin: useHasPermission("playlists.removeAdmin", async function removeAdmin(session, playlistId, cb) {
  2398. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  2399. async.waterfall(
  2400. [
  2401. next => {
  2402. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  2403. .then(playlist => next(null, playlist))
  2404. .catch(next);
  2405. },
  2406. (playlist, next) => {
  2407. if (playlist.type !== "user" && playlist.type !== "admin")
  2408. return next("Playlist cannot be removed.");
  2409. return next(null, playlist);
  2410. },
  2411. (playlist, next) => {
  2412. if (playlist.type === "admin") next(null, null);
  2413. else
  2414. userModel.updateOne(
  2415. { _id: playlist.createdBy },
  2416. { $pull: { "preferences.orderOfPlaylists": playlist._id } },
  2417. err => next(err, playlist.createdBy)
  2418. );
  2419. },
  2420. (playlistCreator, next) => {
  2421. PlaylistsModule.runJob("DELETE_PLAYLIST", { playlistId }, this)
  2422. .then(() => next(null, playlistCreator))
  2423. .catch(next);
  2424. }
  2425. ],
  2426. async (err, playlistCreator) => {
  2427. if (err) {
  2428. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2429. this.log(
  2430. "ERROR",
  2431. "PLAYLIST_REMOVE_ADMIN",
  2432. `Removing playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  2433. );
  2434. return cb({ status: "error", message: err });
  2435. }
  2436. this.log(
  2437. "SUCCESS",
  2438. "PLAYLIST_REMOVE_ADMIN",
  2439. `Successfully removed playlist "${playlistId}" for user "${session.userId}".`
  2440. );
  2441. CacheModule.runJob("PUB", {
  2442. channel: "playlist.delete",
  2443. value: {
  2444. createdBy: playlistCreator,
  2445. playlistId
  2446. }
  2447. });
  2448. ActivitiesModule.runJob("REMOVE_ACTIVITY_REFERENCES", { type: "playlistId", playlistId });
  2449. return cb({
  2450. status: "success",
  2451. message: "Playlist successfully removed"
  2452. });
  2453. }
  2454. );
  2455. }),
  2456. /**
  2457. * Updates the privacy of a private playlist
  2458. *
  2459. * @param {object} session - the session object automatically added by the websocket
  2460. * @param {string} playlistId - the id of the playlist we are updating the privacy for
  2461. * @param {string} privacy - what the new privacy of the playlist should be e.g. public
  2462. * @param {Function} cb - gets called with the result
  2463. */
  2464. updatePrivacy: isLoginRequired(async function updatePrivacy(session, playlistId, privacy, cb) {
  2465. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  2466. async.waterfall(
  2467. [
  2468. next => {
  2469. playlistModel.updateOne(
  2470. { _id: playlistId, createdBy: session.userId },
  2471. { $set: { privacy } },
  2472. { runValidators: true },
  2473. next
  2474. );
  2475. },
  2476. (res, next) => {
  2477. if (res.n === 0) next("No user playlist found with that id and owned by you.");
  2478. else if (res.nModified === 0) next(`Nothing changed, the playlist was already ${privacy}.`);
  2479. else {
  2480. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  2481. .then(playlist => next(null, playlist))
  2482. .catch(next);
  2483. }
  2484. }
  2485. ],
  2486. async (err, playlist) => {
  2487. if (err) {
  2488. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2489. this.log(
  2490. "ERROR",
  2491. "PLAYLIST_UPDATE_PRIVACY",
  2492. `Updating privacy to "${privacy}" for private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  2493. );
  2494. return cb({ status: "error", message: err });
  2495. }
  2496. this.log(
  2497. "SUCCESS",
  2498. "PLAYLIST_UPDATE_PRIVACY",
  2499. `Successfully updated privacy to "${privacy}" for private playlist "${playlistId}" for user "${session.userId}".`
  2500. );
  2501. CacheModule.runJob("PUB", {
  2502. channel: "playlist.updatePrivacy",
  2503. value: {
  2504. createdBy: playlist.createdBy,
  2505. playlist
  2506. }
  2507. });
  2508. CacheModule.runJob("PUB", {
  2509. channel: "playlist.updated",
  2510. value: { playlistId }
  2511. });
  2512. ActivitiesModule.runJob("ADD_ACTIVITY", {
  2513. userId: session.userId,
  2514. type: "playlist__edit_privacy",
  2515. payload: {
  2516. message: `Changed privacy of playlist <playlistId>${playlist.displayName}</playlistId> to ${privacy}`,
  2517. playlistId
  2518. }
  2519. });
  2520. return cb({
  2521. status: "success",
  2522. message: "Playlist has been successfully updated"
  2523. });
  2524. }
  2525. );
  2526. }),
  2527. /**
  2528. * Updates the privacy of a playlist
  2529. *
  2530. * @param {object} session - the session object automatically added by the websocket
  2531. * @param {string} playlistId - the id of the playlist we are updating the privacy for
  2532. * @param {string} privacy - what the new privacy of the playlist should be e.g. public
  2533. * @param {Function} cb - gets called with the result
  2534. */
  2535. updatePrivacyAdmin: useHasPermission(
  2536. "playlists.update.privacy",
  2537. async function updatePrivacyAdmin(session, playlistId, privacy, cb) {
  2538. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  2539. async.waterfall(
  2540. [
  2541. next => {
  2542. playlistModel.updateOne(
  2543. { _id: playlistId },
  2544. { $set: { privacy } },
  2545. { runValidators: true },
  2546. next
  2547. );
  2548. },
  2549. (res, next) => {
  2550. if (res.n === 0) next("No playlist found with that id.");
  2551. else if (res.nModified === 0) next(`Nothing changed, the playlist was already ${privacy}.`);
  2552. else {
  2553. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  2554. .then(playlist => next(null, playlist))
  2555. .catch(next);
  2556. }
  2557. }
  2558. ],
  2559. async (err, playlist) => {
  2560. if (err) {
  2561. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2562. this.log(
  2563. "ERROR",
  2564. "PLAYLIST_UPDATE_PRIVACY_ADMIN",
  2565. `Updating privacy to "${privacy}" for playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  2566. );
  2567. return cb({ status: "error", message: err });
  2568. }
  2569. this.log(
  2570. "SUCCESS",
  2571. "PLAYLIST_UPDATE_PRIVACY_ADMIn",
  2572. `Successfully updated privacy to "${privacy}" for playlist "${playlistId}" for user "${session.userId}".`
  2573. );
  2574. if (playlist.type === "user") {
  2575. CacheModule.runJob("PUB", {
  2576. channel: "playlist.updatePrivacy",
  2577. value: {
  2578. userId: playlist.createdBy,
  2579. playlist
  2580. }
  2581. });
  2582. }
  2583. CacheModule.runJob("PUB", {
  2584. channel: "playlist.updated",
  2585. value: { playlistId }
  2586. });
  2587. return cb({
  2588. status: "success",
  2589. message: "Playlist has been successfully updated"
  2590. });
  2591. }
  2592. );
  2593. }
  2594. ),
  2595. /**
  2596. * Deletes all orphaned station playlists
  2597. *
  2598. * @param {object} session - the session object automatically added by socket.io
  2599. * @param {Function} cb - gets called with the result
  2600. */
  2601. deleteOrphanedStationPlaylists: useHasPermission("playlists.deleteOrphaned", async function index(session, cb) {
  2602. this.keepLongJob();
  2603. this.publishProgress({
  2604. status: "started",
  2605. title: "Delete orphaned station playlists",
  2606. message: "Deleting orphaned station playlists.",
  2607. id: this.toString()
  2608. });
  2609. await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
  2610. await CacheModule.runJob(
  2611. "PUB",
  2612. {
  2613. channel: "longJob.added",
  2614. value: { jobId: this.toString(), userId: session.userId }
  2615. },
  2616. this
  2617. );
  2618. async.waterfall(
  2619. [
  2620. next => {
  2621. PlaylistsModule.runJob("DELETE_ORPHANED_STATION_PLAYLISTS", {}, this)
  2622. .then(() => next())
  2623. .catch(next);
  2624. }
  2625. ],
  2626. async err => {
  2627. if (err) {
  2628. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2629. this.log(
  2630. "ERROR",
  2631. "PLAYLISTS_DELETE_ORPHANED_STATION_PLAYLISTS",
  2632. `Deleting orphaned station playlists failed. "${err}"`
  2633. );
  2634. this.publishProgress({
  2635. status: "error",
  2636. message: err
  2637. });
  2638. return cb({ status: "error", message: err });
  2639. }
  2640. this.log(
  2641. "SUCCESS",
  2642. "PLAYLISTS_DELETE_ORPHANED_STATION_PLAYLISTS",
  2643. "Deleting orphaned station playlists successful."
  2644. );
  2645. this.publishProgress({
  2646. status: "success",
  2647. message: "Successfully deleted orphaned station playlists."
  2648. });
  2649. return cb({ status: "success", message: "Successfully deleted orphaned station playlists." });
  2650. }
  2651. );
  2652. }),
  2653. /**
  2654. * Deletes all orphaned genre playlists
  2655. *
  2656. * @param {object} session - the session object automatically added by socket.io
  2657. * @param {Function} cb - gets called with the result
  2658. */
  2659. deleteOrphanedGenrePlaylists: useHasPermission("playlists.deleteOrphaned", async function index(session, cb) {
  2660. this.keepLongJob();
  2661. this.publishProgress({
  2662. status: "started",
  2663. title: "Delete orphaned genre playlists",
  2664. message: "Deleting orphaned genre playlists.",
  2665. id: this.toString()
  2666. });
  2667. await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
  2668. await CacheModule.runJob(
  2669. "PUB",
  2670. {
  2671. channel: "longJob.added",
  2672. value: { jobId: this.toString(), userId: session.userId }
  2673. },
  2674. this
  2675. );
  2676. async.waterfall(
  2677. [
  2678. next => {
  2679. PlaylistsModule.runJob("DELETE_ORPHANED_GENRE_PLAYLISTS", {}, this)
  2680. .then(() => next())
  2681. .catch(next);
  2682. }
  2683. ],
  2684. async err => {
  2685. if (err) {
  2686. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2687. this.log(
  2688. "ERROR",
  2689. "PLAYLISTS_DELETE_ORPHANED_GENRE_PLAYLISTS",
  2690. `Deleting orphaned genre playlists failed. "${err}"`
  2691. );
  2692. this.publishProgress({
  2693. status: "error",
  2694. message: err
  2695. });
  2696. return cb({ status: "error", message: err });
  2697. }
  2698. this.log(
  2699. "SUCCESS",
  2700. "PLAYLISTS_DELETE_ORPHANED_GENRE_PLAYLISTS",
  2701. "Deleting orphaned genre playlists successful."
  2702. );
  2703. this.publishProgress({
  2704. status: "success",
  2705. message: "Successfully deleted orphaned genre playlists."
  2706. });
  2707. return cb({ status: "success", message: "Successfully deleted orphaned genre playlists." });
  2708. }
  2709. );
  2710. }),
  2711. /**
  2712. * Requests orpahned playlist songs
  2713. *
  2714. * @param {object} session - the session object automatically added by socket.io
  2715. * @param {Function} cb - gets called with the result
  2716. */
  2717. requestOrphanedPlaylistSongs: useHasPermission(
  2718. "playlists.requestOrphanedPlaylistSongs",
  2719. async function index(session, cb) {
  2720. this.keepLongJob();
  2721. this.publishProgress({
  2722. status: "started",
  2723. title: "Request orphaned playlist songs",
  2724. message: "Requesting orphaned playlist songs.",
  2725. id: this.toString()
  2726. });
  2727. await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
  2728. await CacheModule.runJob(
  2729. "PUB",
  2730. {
  2731. channel: "longJob.added",
  2732. value: { jobId: this.toString(), userId: session.userId }
  2733. },
  2734. this
  2735. );
  2736. async.waterfall(
  2737. [
  2738. next => {
  2739. SongsModule.runJob("REQUEST_ORPHANED_PLAYLIST_SONGS", {}, this)
  2740. .then(() => next())
  2741. .catch(next);
  2742. }
  2743. ],
  2744. async err => {
  2745. if (err) {
  2746. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2747. this.log(
  2748. "ERROR",
  2749. "REQUEST_ORPHANED_PLAYLIST_SONGS",
  2750. `Requesting orphaned playlist songs failed. "${err}"`
  2751. );
  2752. this.publishProgress({
  2753. status: "error",
  2754. message: err
  2755. });
  2756. return cb({ status: "error", message: err });
  2757. }
  2758. this.log(
  2759. "SUCCESS",
  2760. "REQUEST_ORPHANED_PLAYLIST_SONGS",
  2761. "Requesting orphaned playlist songs was successful."
  2762. );
  2763. this.publishProgress({
  2764. status: "success",
  2765. message: "Successfully requested orphaned playlist songs."
  2766. });
  2767. return cb({ status: "success", message: "Successfully requested orphaned playlist songs." });
  2768. }
  2769. );
  2770. }
  2771. ),
  2772. /**
  2773. * Clears and refills a station playlist
  2774. *
  2775. * @param {object} session - the session object automatically added by socket.io
  2776. * @param {string} playlistId - the id of the playlist we are clearing and refilling
  2777. * @param {Function} cb - gets called with the result
  2778. */
  2779. clearAndRefillStationPlaylist: useHasPermission(
  2780. "playlists.clearAndRefill",
  2781. async function index(session, playlistId, cb) {
  2782. async.waterfall(
  2783. [
  2784. next => {
  2785. if (!playlistId) next("Please specify a playlist id");
  2786. else {
  2787. PlaylistsModule.runJob("CLEAR_AND_REFILL_STATION_PLAYLIST", { playlistId }, this)
  2788. .then(() => {
  2789. next();
  2790. })
  2791. .catch(err => {
  2792. next(err);
  2793. });
  2794. }
  2795. }
  2796. ],
  2797. async err => {
  2798. if (err) {
  2799. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2800. this.log(
  2801. "ERROR",
  2802. "PLAYLIST_CLEAR_AND_REFILL_STATION_PLAYLIST",
  2803. `Clearing and refilling station playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  2804. );
  2805. return cb({ status: "error", message: err });
  2806. }
  2807. this.log(
  2808. "SUCCESS",
  2809. "PLAYLIST_CLEAR_AND_REFILL_STATION_PLAYLIST",
  2810. `Successfully cleared and refilled station playlist "${playlistId}" for user "${session.userId}".`
  2811. );
  2812. return cb({
  2813. status: "success",
  2814. message: "Playlist has been successfully cleared and refilled"
  2815. });
  2816. }
  2817. );
  2818. }
  2819. ),
  2820. /**
  2821. * Clears and refills a genre playlist
  2822. *
  2823. * @param {object} session - the session object automatically added by socket.io
  2824. * @param {string} playlistId - the id of the playlist we are clearing and refilling
  2825. * @param {Function} cb - gets called with the result
  2826. */
  2827. clearAndRefillGenrePlaylist: useHasPermission(
  2828. "playlists.clearAndRefill",
  2829. async function index(session, playlistId, cb) {
  2830. async.waterfall(
  2831. [
  2832. next => {
  2833. if (!playlistId) next("Please specify a playlist id");
  2834. else {
  2835. PlaylistsModule.runJob("CLEAR_AND_REFILL_GENRE_PLAYLIST", { playlistId }, this)
  2836. .then(() => {
  2837. next();
  2838. })
  2839. .catch(err => {
  2840. next(err);
  2841. });
  2842. }
  2843. }
  2844. ],
  2845. async err => {
  2846. if (err) {
  2847. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2848. this.log(
  2849. "ERROR",
  2850. "PLAYLIST_CLEAR_AND_REFILL_GENRE_PLAYLIST",
  2851. `Clearing and refilling genre playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  2852. );
  2853. return cb({ status: "error", message: err });
  2854. }
  2855. this.log(
  2856. "SUCCESS",
  2857. "PLAYLIST_CLEAR_AND_REFILL_GENRE_PLAYLIST",
  2858. `Successfully cleared and refilled genre playlist "${playlistId}" for user "${session.userId}".`
  2859. );
  2860. return cb({
  2861. status: "success",
  2862. message: "Playlist has been successfully cleared and refilled"
  2863. });
  2864. }
  2865. );
  2866. }
  2867. ),
  2868. /**
  2869. * Clears and refills all station playlists
  2870. *
  2871. * @param {object} session - the session object automatically added by socket.io
  2872. * @param {Function} cb - gets called with the result
  2873. */
  2874. clearAndRefillAllStationPlaylists: useHasPermission(
  2875. "playlists.clearAndRefillAll",
  2876. async function index(session, cb) {
  2877. this.keepLongJob();
  2878. this.publishProgress({
  2879. status: "started",
  2880. title: "Clear and refill all station playlists",
  2881. message: "Clearing and refilling all station playlists.",
  2882. id: this.toString()
  2883. });
  2884. await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
  2885. await CacheModule.runJob(
  2886. "PUB",
  2887. {
  2888. channel: "longJob.added",
  2889. value: { jobId: this.toString(), userId: session.userId }
  2890. },
  2891. this
  2892. );
  2893. async.waterfall(
  2894. [
  2895. next => {
  2896. PlaylistsModule.runJob("GET_ALL_STATION_PLAYLISTS", {}, this)
  2897. .then(response => {
  2898. next(null, response.playlists);
  2899. })
  2900. .catch(err => {
  2901. next(err);
  2902. });
  2903. },
  2904. (playlists, next) => {
  2905. async.eachLimit(
  2906. playlists,
  2907. 1,
  2908. (playlist, next) => {
  2909. this.publishProgress({
  2910. status: "update",
  2911. message: `Clearing and refilling "${playlist._id}"`
  2912. });
  2913. PlaylistsModule.runJob(
  2914. "CLEAR_AND_REFILL_STATION_PLAYLIST",
  2915. { playlistId: playlist._id },
  2916. this
  2917. )
  2918. .then(() => {
  2919. next();
  2920. })
  2921. .catch(err => {
  2922. next(err);
  2923. });
  2924. },
  2925. next
  2926. );
  2927. }
  2928. ],
  2929. async err => {
  2930. if (err) {
  2931. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2932. this.log(
  2933. "ERROR",
  2934. "PLAYLIST_CLEAR_AND_REFILL_ALL_STATION_PLAYLISTS",
  2935. `Clearing and refilling all station playlists failed for user "${session.userId}". "${err}"`
  2936. );
  2937. this.publishProgress({
  2938. status: "error",
  2939. message: err
  2940. });
  2941. return cb({ status: "error", message: err });
  2942. }
  2943. this.log(
  2944. "SUCCESS",
  2945. "PLAYLIST_CLEAR_AND_REFILL_ALL_STATION_PLAYLISTS",
  2946. `Successfully cleared and refilled all station playlists for user "${session.userId}".`
  2947. );
  2948. this.publishProgress({
  2949. status: "success",
  2950. message: "Playlists have been successfully cleared and refilled."
  2951. });
  2952. return cb({
  2953. status: "success",
  2954. message: "Playlists have been successfully cleared and refilled"
  2955. });
  2956. }
  2957. );
  2958. }
  2959. ),
  2960. /**
  2961. * Clears and refills all genre playlists
  2962. *
  2963. * @param {object} session - the session object automatically added by socket.io
  2964. * @param {Function} cb - gets called with the result
  2965. */
  2966. clearAndRefillAllGenrePlaylists: useHasPermission("playlists.clearAndRefillAll", async function index(session, cb) {
  2967. this.keepLongJob();
  2968. this.publishProgress({
  2969. status: "started",
  2970. title: "Clear and refill all genre playlists",
  2971. message: "Clearing and refilling all genre playlists.",
  2972. id: this.toString()
  2973. });
  2974. await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
  2975. await CacheModule.runJob(
  2976. "PUB",
  2977. {
  2978. channel: "longJob.added",
  2979. value: { jobId: this.toString(), userId: session.userId }
  2980. },
  2981. this
  2982. );
  2983. async.waterfall(
  2984. [
  2985. next => {
  2986. PlaylistsModule.runJob("GET_ALL_GENRE_PLAYLISTS", {}, this)
  2987. .then(response => {
  2988. next(null, response.playlists);
  2989. })
  2990. .catch(err => {
  2991. next(err);
  2992. });
  2993. },
  2994. (playlists, next) => {
  2995. async.eachLimit(
  2996. playlists,
  2997. 1,
  2998. (playlist, next) => {
  2999. this.publishProgress({
  3000. status: "update",
  3001. message: `Clearing and refilling "${playlist._id}"`
  3002. });
  3003. PlaylistsModule.runJob(
  3004. "CLEAR_AND_REFILL_GENRE_PLAYLIST",
  3005. { playlistId: playlist._id },
  3006. this
  3007. )
  3008. .then(() => {
  3009. next();
  3010. })
  3011. .catch(err => {
  3012. next(err);
  3013. });
  3014. },
  3015. next
  3016. );
  3017. }
  3018. ],
  3019. async err => {
  3020. if (err) {
  3021. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  3022. this.log(
  3023. "ERROR",
  3024. "PLAYLIST_CLEAR_AND_REFILL_ALL_GENRE_PLAYLISTS",
  3025. `Clearing and refilling all genre playlists failed for user "${session.userId}". "${err}"`
  3026. );
  3027. this.publishProgress({
  3028. status: "error",
  3029. message: err
  3030. });
  3031. return cb({ status: "error", message: err });
  3032. }
  3033. this.log(
  3034. "SUCCESS",
  3035. "PLAYLIST_CLEAR_AND_REFILL_ALL_GENRE_PLAYLISTS",
  3036. `Successfully cleared and refilled all genre playlists for user "${session.userId}".`
  3037. );
  3038. this.publishProgress({
  3039. status: "success",
  3040. message: "Playlists have been successfully cleared and refilled."
  3041. });
  3042. return cb({
  3043. status: "success",
  3044. message: "Playlists have been successfully cleared and refilled"
  3045. });
  3046. }
  3047. );
  3048. }),
  3049. /**
  3050. * Create missing genre playlists
  3051. *
  3052. * @param {object} session - the session object automatically added by socket.io
  3053. * @param {Function} cb - gets called with the result
  3054. */
  3055. createMissingGenrePlaylists: useHasPermission("playlists.createMissing", async function index(session, cb) {
  3056. this.keepLongJob();
  3057. this.publishProgress({
  3058. status: "started",
  3059. title: "Create missing genre playlists",
  3060. message: "Creating missing genre playlists.",
  3061. id: this.toString()
  3062. });
  3063. await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
  3064. await CacheModule.runJob(
  3065. "PUB",
  3066. {
  3067. channel: "longJob.added",
  3068. value: { jobId: this.toString(), userId: session.userId }
  3069. },
  3070. this
  3071. );
  3072. async.waterfall(
  3073. [
  3074. next => {
  3075. PlaylistsModule.runJob("CREATE_MISSING_GENRE_PLAYLISTS", this)
  3076. .then(() => {
  3077. next();
  3078. })
  3079. .catch(err => {
  3080. next(err);
  3081. });
  3082. }
  3083. ],
  3084. async err => {
  3085. if (err) {
  3086. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  3087. this.log(
  3088. "ERROR",
  3089. "PLAYLIST_CREATE_MISSING_GENRE_PLAYLISTS",
  3090. `Creating missing genre playlists failed for user "${session.userId}". "${err}"`
  3091. );
  3092. this.publishProgress({
  3093. status: "error",
  3094. message: err
  3095. });
  3096. return cb({ status: "error", message: err });
  3097. }
  3098. this.log(
  3099. "SUCCESS",
  3100. "PLAYLIST_CREATE_MISSING_GENRE_PLAYLISTS",
  3101. `Successfully created missing genre playlists for user "${session.userId}".`
  3102. );
  3103. this.publishProgress({
  3104. status: "success",
  3105. message: "Missing genre playlists have been successfully created."
  3106. });
  3107. return cb({
  3108. status: "success",
  3109. message: "Missing genre playlists have been successfully created"
  3110. });
  3111. }
  3112. );
  3113. })
  3114. };