stations.js 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090
  1. import async from "async";
  2. import mongoose from "mongoose";
  3. import { isLoginRequired, isOwnerRequired } from "./hooks";
  4. import moduleManager from "../../index";
  5. const DBModule = moduleManager.modules.db;
  6. const UtilsModule = moduleManager.modules.utils;
  7. const IOModule = moduleManager.modules.io;
  8. const SongsModule = moduleManager.modules.songs;
  9. const PlaylistsModule = moduleManager.modules.playlists;
  10. const CacheModule = moduleManager.modules.cache;
  11. const NotificationsModule = moduleManager.modules.notifications;
  12. const StationsModule = moduleManager.modules.stations;
  13. const ActivitiesModule = moduleManager.modules.activities;
  14. const YouTubeModule = moduleManager.modules.youtube;
  15. CacheModule.runJob("SUB", {
  16. channel: "station.updateUsers",
  17. cb: ({ stationId, usersPerStation }) => {
  18. IOModule.runJob("EMIT_TO_ROOM", {
  19. room: `station.${stationId}`,
  20. args: ["event:users.updated", usersPerStation]
  21. });
  22. }
  23. });
  24. CacheModule.runJob("SUB", {
  25. channel: "station.updateUserCount",
  26. cb: ({ stationId, usersPerStationCount }) => {
  27. const count = usersPerStationCount || 0;
  28. IOModule.runJob("EMIT_TO_ROOM", {
  29. room: `station.${stationId}`,
  30. args: ["event:userCount.updated", count]
  31. });
  32. StationsModule.runJob("GET_STATION", { stationId }).then(async station => {
  33. if (station.privacy === "public")
  34. IOModule.runJob("EMIT_TO_ROOM", {
  35. room: "home",
  36. args: ["event:userCount.updated", stationId, count]
  37. });
  38. else {
  39. const sockets = await IOModule.runJob("GET_ROOM_SOCKETS", {
  40. room: "home"
  41. });
  42. Object.keys(sockets).forEach(socketKey => {
  43. const socket = sockets[socketKey];
  44. const { session } = socket;
  45. if (session.sessionId) {
  46. CacheModule.runJob("HGET", {
  47. table: "sessions",
  48. key: session.sessionId
  49. }).then(session => {
  50. if (session)
  51. DBModule.runJob("GET_MODEL", {
  52. modelName: "user"
  53. }).then(userModel =>
  54. userModel.findOne({ _id: session.userId }, (err, user) => {
  55. if (user.role === "admin")
  56. socket.emit("event:userCount.updated", stationId, count);
  57. else if (station.type === "community" && station.owner === session.userId)
  58. socket.emit("event:userCount.updated", stationId, count);
  59. })
  60. );
  61. });
  62. }
  63. });
  64. }
  65. });
  66. }
  67. });
  68. CacheModule.runJob("SUB", {
  69. channel: "station.updateTheme",
  70. cb: data => {
  71. IOModule.runJob("EMIT_TO_ROOM", {
  72. room: `station.${data.stationId}`,
  73. args: ["event:theme.updated", data.theme]
  74. });
  75. }
  76. });
  77. CacheModule.runJob("SUB", {
  78. channel: "station.queueLockToggled",
  79. cb: data => {
  80. IOModule.runJob("EMIT_TO_ROOM", {
  81. room: `station.${data.stationId}`,
  82. args: ["event:queueLockToggled", data.locked]
  83. });
  84. }
  85. });
  86. CacheModule.runJob("SUB", {
  87. channel: "station.updatePartyMode",
  88. cb: data => {
  89. IOModule.runJob("EMIT_TO_ROOM", {
  90. room: `station.${data.stationId}`,
  91. args: ["event:partyMode.updated", data.partyMode]
  92. });
  93. }
  94. });
  95. CacheModule.runJob("SUB", {
  96. channel: "privatePlaylist.selected",
  97. cb: data => {
  98. IOModule.runJob("EMIT_TO_ROOM", {
  99. room: `station.${data.stationId}`,
  100. args: ["event:privatePlaylist.selected", data.playlistId]
  101. });
  102. }
  103. });
  104. CacheModule.runJob("SUB", {
  105. channel: "privatePlaylist.deselected",
  106. cb: data => {
  107. IOModule.runJob("EMIT_TO_ROOM", {
  108. room: `station.${data.stationId}`,
  109. args: ["event:privatePlaylist.deselected"]
  110. });
  111. }
  112. });
  113. CacheModule.runJob("SUB", {
  114. channel: "station.pause",
  115. cb: stationId => {
  116. StationsModule.runJob("GET_STATION", { stationId }).then(station => {
  117. IOModule.runJob("EMIT_TO_ROOM", {
  118. room: `station.${stationId}`,
  119. args: ["event:stations.pause", { pausedAt: station.pausedAt }]
  120. });
  121. StationsModule.runJob("GET_SOCKETS_THAT_CAN_KNOW_ABOUT_STATION", {
  122. room: `home`,
  123. station
  124. }).then(response => {
  125. const { socketsThatCan } = response;
  126. socketsThatCan.forEach(socket => {
  127. socket.emit("event:station.pause", { stationId });
  128. });
  129. });
  130. });
  131. }
  132. });
  133. CacheModule.runJob("SUB", {
  134. channel: "station.resume",
  135. cb: stationId => {
  136. StationsModule.runJob("GET_STATION", { stationId }).then(station => {
  137. IOModule.runJob("EMIT_TO_ROOM", {
  138. room: `station.${stationId}`,
  139. args: ["event:stations.resume", { timePaused: station.timePaused }]
  140. });
  141. StationsModule.runJob("GET_SOCKETS_THAT_CAN_KNOW_ABOUT_STATION", {
  142. room: `home`,
  143. station
  144. })
  145. .then(response => {
  146. const { socketsThatCan } = response;
  147. socketsThatCan.forEach(socket => {
  148. socket.emit("event:station.resume", { stationId });
  149. });
  150. })
  151. .catch(console.log);
  152. });
  153. }
  154. });
  155. CacheModule.runJob("SUB", {
  156. channel: "station.privacyUpdate",
  157. cb: response => {
  158. const { stationId, previousPrivacy } = response;
  159. StationsModule.runJob("GET_STATION", { stationId }).then(station => {
  160. if (previousPrivacy !== station.privacy) {
  161. if (station.privacy === "public") {
  162. // Station became public
  163. IOModule.runJob("EMIT_TO_ROOM", {
  164. room: "home",
  165. args: ["event:stations.created", station]
  166. });
  167. } else if (previousPrivacy === "public") {
  168. // Station became hidden
  169. StationsModule.runJob("GET_SOCKETS_THAT_CAN_KNOW_ABOUT_STATION", {
  170. room: `home`,
  171. station
  172. }).then(response => {
  173. const { socketsThatCan, socketsThatCannot } = response;
  174. socketsThatCan.forEach(socket => {
  175. socket.emit("event:station.updatePrivacy", { stationId, privacy: station.privacy });
  176. });
  177. socketsThatCannot.forEach(socket => {
  178. socket.emit("event:station.removed", { stationId });
  179. });
  180. });
  181. } else {
  182. // Station was hidden and is still hidden
  183. StationsModule.runJob("GET_SOCKETS_THAT_CAN_KNOW_ABOUT_STATION", {
  184. room: `home`,
  185. station
  186. }).then(response => {
  187. const { socketsThatCan } = response;
  188. socketsThatCan.forEach(socket => {
  189. socket.emit("event:station.updatePrivacy", { stationId, privacy: station.privacy });
  190. });
  191. });
  192. }
  193. }
  194. });
  195. }
  196. });
  197. CacheModule.runJob("SUB", {
  198. channel: "station.nameUpdate",
  199. cb: res => {
  200. const { stationId, name } = res;
  201. StationsModule.runJob("GET_STATION", { stationId }).then(station =>
  202. StationsModule.runJob("GET_SOCKETS_THAT_CAN_KNOW_ABOUT_STATION", {
  203. room: `home`,
  204. station
  205. }).then(response => {
  206. const { socketsThatCan } = response;
  207. socketsThatCan.forEach(socket => socket.emit("event:station.updateName", { stationId, name }));
  208. })
  209. );
  210. IOModule.runJob("EMIT_TO_ROOM", {
  211. room: `station.${stationId}`,
  212. args: ["event:station.updateName", { stationId, name }]
  213. });
  214. }
  215. });
  216. CacheModule.runJob("SUB", {
  217. channel: "station.displayNameUpdate",
  218. cb: response => {
  219. const { stationId, displayName } = response;
  220. StationsModule.runJob("GET_STATION", { stationId }).then(station =>
  221. StationsModule.runJob("GET_SOCKETS_THAT_CAN_KNOW_ABOUT_STATION", {
  222. room: `home`,
  223. station
  224. }).then(response => {
  225. const { socketsThatCan } = response;
  226. socketsThatCan.forEach(socket =>
  227. socket.emit("event:station.updateDisplayName", { stationId, displayName })
  228. );
  229. })
  230. );
  231. IOModule.runJob("EMIT_TO_ROOM", {
  232. room: `station.${stationId}`,
  233. args: ["event:station.updateDisplayName", { stationId, displayName }]
  234. });
  235. }
  236. });
  237. CacheModule.runJob("SUB", {
  238. channel: "station.descriptionUpdate",
  239. cb: response => {
  240. const { stationId, description } = response;
  241. StationsModule.runJob("GET_STATION", { stationId }).then(station =>
  242. StationsModule.runJob("GET_SOCKETS_THAT_CAN_KNOW_ABOUT_STATION", {
  243. room: `home`,
  244. station
  245. }).then(response => {
  246. const { socketsThatCan } = response;
  247. socketsThatCan.forEach(socket =>
  248. socket.emit("event:station.updateDescription", { stationId, description })
  249. );
  250. })
  251. );
  252. IOModule.runJob("EMIT_TO_ROOM", {
  253. room: `station.${stationId}`,
  254. args: ["event:station.updateDescription", { stationId, description }]
  255. });
  256. }
  257. });
  258. CacheModule.runJob("SUB", {
  259. channel: "station.themeUpdate",
  260. cb: response => {
  261. const { stationId } = response;
  262. StationsModule.runJob("GET_STATION", { stationId }).then(station => {
  263. IOModule.runJob("EMIT_TO_ROOM", {
  264. room: `station.${stationId}`,
  265. args: ["event:station.themeUpdated", station.theme]
  266. });
  267. StationsModule.runJob("GET_SOCKETS_THAT_CAN_KNOW_ABOUT_STATION", {
  268. room: `home`,
  269. station
  270. }).then(response => {
  271. const { socketsThatCan } = response;
  272. socketsThatCan.forEach(socket => {
  273. socket.emit("event:station.themeUpdated", { stationId, theme: station.theme });
  274. });
  275. });
  276. });
  277. }
  278. });
  279. CacheModule.runJob("SUB", {
  280. channel: "station.queueUpdate",
  281. cb: stationId => {
  282. StationsModule.runJob("GET_STATION", { stationId }).then(station => {
  283. IOModule.runJob("EMIT_TO_ROOM", {
  284. room: `station.${stationId}`,
  285. args: ["event:queue.update", station.queue]
  286. });
  287. });
  288. }
  289. });
  290. CacheModule.runJob("SUB", {
  291. channel: "station.voteSkipSong",
  292. cb: stationId => {
  293. IOModule.runJob("EMIT_TO_ROOM", {
  294. room: `station.${stationId}`,
  295. args: ["event:song.voteSkipSong"]
  296. });
  297. }
  298. });
  299. CacheModule.runJob("SUB", {
  300. channel: "station.remove",
  301. cb: stationId => {
  302. IOModule.runJob("EMIT_TO_ROOM", {
  303. room: `station.${stationId}`,
  304. args: ["event:stations.remove"]
  305. });
  306. console.log(111, "REMOVED");
  307. IOModule.runJob("EMIT_TO_ROOM", {
  308. room: `home`,
  309. args: ["event:station.removed", { stationId }]
  310. });
  311. IOModule.runJob("EMIT_TO_ROOM", {
  312. room: "admin.stations",
  313. args: ["event:admin.station.removed", stationId]
  314. });
  315. }
  316. });
  317. CacheModule.runJob("SUB", {
  318. channel: "station.create",
  319. cb: async stationId => {
  320. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" });
  321. StationsModule.runJob("INITIALIZE_STATION", { stationId }).then(async response => {
  322. const { station } = response;
  323. station.userCount = StationsModule.usersPerStationCount[stationId] || 0;
  324. IOModule.runJob("EMIT_TO_ROOM", {
  325. room: "admin.stations",
  326. args: ["event:admin.station.added", station]
  327. });
  328. // TODO If community, check if on whitelist
  329. if (station.privacy === "public")
  330. IOModule.runJob("EMIT_TO_ROOM", {
  331. room: "home",
  332. args: ["event:stations.created", station]
  333. });
  334. else {
  335. const sockets = await IOModule.runJob("GET_ROOM_SOCKETS", {
  336. room: "home"
  337. });
  338. Object.keys(sockets).forEach(socketKey => {
  339. const socket = sockets[socketKey];
  340. const { session } = socket;
  341. if (session.sessionId) {
  342. CacheModule.runJob("HGET", {
  343. table: "sessions",
  344. key: session.sessionId
  345. }).then(session => {
  346. if (session) {
  347. userModel.findOne({ _id: session.userId }, (err, user) => {
  348. if (user.role === "admin") socket.emit("event:stations.created", station);
  349. else if (station.type === "community" && station.owner === session.userId)
  350. socket.emit("event:stations.created", station);
  351. });
  352. }
  353. });
  354. }
  355. });
  356. }
  357. });
  358. }
  359. });
  360. export default {
  361. /**
  362. * Get a list of all the stations
  363. *
  364. * @param {object} session - user session
  365. * @param {Function} cb - callback
  366. */
  367. index(session, cb) {
  368. async.waterfall(
  369. [
  370. next => {
  371. CacheModule.runJob("HGETALL", { table: "stations" }, this).then(stations => {
  372. next(null, stations);
  373. });
  374. },
  375. (items, next) => {
  376. const filteredStations = [];
  377. async.each(
  378. items,
  379. (station, nextStation) => {
  380. async.waterfall(
  381. [
  382. callback => {
  383. // only relevant if user logged in
  384. if (session.userId) {
  385. return StationsModule.runJob(
  386. "HAS_USER_FAVORITED_STATION",
  387. {
  388. userId: session.userId,
  389. stationId: station._id
  390. },
  391. this
  392. )
  393. .then(isStationFavorited => {
  394. station.isFavorited = isStationFavorited;
  395. return callback();
  396. })
  397. .catch(err => callback(err));
  398. }
  399. return callback();
  400. },
  401. callback => {
  402. StationsModule.runJob(
  403. "CAN_USER_VIEW_STATION",
  404. {
  405. station,
  406. userId: session.userId,
  407. hideUnlisted: true
  408. },
  409. this
  410. )
  411. .then(exists => callback(null, exists))
  412. .catch(callback);
  413. }
  414. ],
  415. (err, exists) => {
  416. if (err) return this.log("ERROR", "STATIONS_INDEX", err);
  417. station.userCount = StationsModule.usersPerStationCount[station._id] || 0;
  418. if (exists) filteredStations.push(station);
  419. return nextStation();
  420. }
  421. );
  422. },
  423. () => next(null, filteredStations)
  424. );
  425. }
  426. ],
  427. async (err, stations) => {
  428. if (err) {
  429. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  430. this.log("ERROR", "STATIONS_INDEX", `Indexing stations failed. "${err}"`);
  431. return cb({ status: "failure", message: err });
  432. }
  433. this.log("SUCCESS", "STATIONS_INDEX", `Indexing stations successful.`, false);
  434. return cb({ status: "success", stations });
  435. }
  436. );
  437. },
  438. /**
  439. * Obtains basic metadata of a station in order to format an activity
  440. *
  441. * @param {object} session - user session
  442. * @param {string} stationId - the station id
  443. * @param {Function} cb - callback
  444. */
  445. getStationForActivity(session, stationId, cb) {
  446. async.waterfall(
  447. [
  448. next => {
  449. StationsModule.runJob("GET_STATION", { stationId }, this)
  450. .then(station => {
  451. next(null, station);
  452. })
  453. .catch(next);
  454. }
  455. ],
  456. async (err, station) => {
  457. if (err) {
  458. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  459. this.log(
  460. "ERROR",
  461. "STATIONS_GET_STATION_FOR_ACTIVITY",
  462. `Failed to obtain metadata of station ${stationId} for activity formatting. "${err}"`
  463. );
  464. return cb({ status: "failure", message: err });
  465. }
  466. this.log(
  467. "SUCCESS",
  468. "STATIONS_GET_STATION_FOR_ACTIVITY",
  469. `Obtained metadata of station ${stationId} for activity formatting successfully.`
  470. );
  471. return cb({
  472. status: "success",
  473. data: {
  474. title: station.displayName,
  475. thumbnail: station.currentSong ? station.currentSong.thumbnail : ""
  476. }
  477. });
  478. }
  479. );
  480. },
  481. /**
  482. * Verifies that a station exists
  483. *
  484. * @param {object} session - user session
  485. * @param {string} stationName - the station name
  486. * @param {Function} cb - callback
  487. */
  488. existsByName(session, stationName, cb) {
  489. async.waterfall(
  490. [
  491. next => {
  492. StationsModule.runJob("GET_STATION_BY_NAME", { stationName }, this)
  493. .then(station => {
  494. next(null, station);
  495. })
  496. .catch(next);
  497. },
  498. (station, next) => {
  499. if (!station) return next(null, false);
  500. return StationsModule.runJob(
  501. "CAN_USER_VIEW_STATION",
  502. {
  503. station,
  504. userId: session.userId
  505. },
  506. this
  507. )
  508. .then(exists => {
  509. next(null, exists);
  510. })
  511. .catch(next);
  512. }
  513. ],
  514. async (err, exists) => {
  515. if (err) {
  516. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  517. this.log(
  518. "ERROR",
  519. "STATION_EXISTS_BY_NAME",
  520. `Checking if station "${stationName}" exists failed. "${err}"`
  521. );
  522. return cb({ status: "failure", message: err });
  523. }
  524. this.log(
  525. "SUCCESS",
  526. "STATION_EXISTS_BY_NAME",
  527. `Station "${stationName}" exists successfully.` /* , false */
  528. );
  529. return cb({ status: "success", exists });
  530. }
  531. );
  532. },
  533. /**
  534. * Gets the official playlist for a station
  535. *
  536. * @param {object} session - user session
  537. * @param {string} stationId - the station id
  538. * @param {Function} cb - callback
  539. */
  540. getPlaylist(session, stationId, cb) {
  541. async.waterfall(
  542. [
  543. next => {
  544. StationsModule.runJob("GET_STATION", { stationId }, this)
  545. .then(station => {
  546. next(null, station);
  547. })
  548. .catch(next);
  549. },
  550. (station, next) => {
  551. StationsModule.runJob(
  552. "CAN_USER_VIEW_STATION",
  553. {
  554. station,
  555. userId: session.userId
  556. },
  557. this
  558. )
  559. .then(canView => {
  560. if (canView) return next(null, station);
  561. return next("Insufficient permissions.");
  562. })
  563. .catch(err => next(err));
  564. },
  565. (station, next) => {
  566. if (!station) return next("Station not found.");
  567. if (station.type !== "official") return next("This is not an official station.");
  568. return next();
  569. },
  570. next => {
  571. CacheModule.runJob(
  572. "HGET",
  573. {
  574. table: "officialPlaylists",
  575. key: stationId
  576. },
  577. this
  578. )
  579. .then(playlist => {
  580. next(null, playlist);
  581. })
  582. .catch(next);
  583. },
  584. (playlist, next) => {
  585. if (!playlist) return next("Playlist not found.");
  586. return next(null, playlist);
  587. }
  588. ],
  589. async (err, playlist) => {
  590. if (err) {
  591. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  592. this.log(
  593. "ERROR",
  594. "STATIONS_GET_PLAYLIST",
  595. `Getting playlist for station "${stationId}" failed. "${err}"`
  596. );
  597. return cb({ status: "failure", message: err });
  598. }
  599. this.log(
  600. "SUCCESS",
  601. "STATIONS_GET_PLAYLIST",
  602. `Got playlist for station "${stationId}" successfully.`,
  603. false
  604. );
  605. return cb({ status: "success", data: playlist.songs });
  606. }
  607. );
  608. },
  609. /**
  610. * Joins the station by its name
  611. *
  612. * @param {object} session - user session
  613. * @param {string} stationName - the station name
  614. * @param {Function} cb - callback
  615. */
  616. join(session, stationName, cb) {
  617. async.waterfall(
  618. [
  619. next => {
  620. StationsModule.runJob("GET_STATION_BY_NAME", { stationName }, this)
  621. .then(station => {
  622. next(null, station);
  623. })
  624. .catch(next);
  625. },
  626. (station, next) => {
  627. if (!station) return next("Station not found.");
  628. return StationsModule.runJob(
  629. "CAN_USER_VIEW_STATION",
  630. {
  631. station,
  632. userId: session.userId
  633. },
  634. this
  635. )
  636. .then(canView => {
  637. if (!canView) next("Not allowed to join station.");
  638. else next(null, station);
  639. })
  640. .catch(err => next(err));
  641. },
  642. (station, next) => {
  643. IOModule.runJob("SOCKET_JOIN_ROOM", {
  644. socketId: session.socketId,
  645. room: `station.${station._id}`
  646. });
  647. const data = {
  648. _id: station._id,
  649. type: station.type,
  650. currentSong: station.currentSong,
  651. startedAt: station.startedAt,
  652. paused: station.paused,
  653. timePaused: station.timePaused,
  654. pausedAt: station.pausedAt,
  655. description: station.description,
  656. displayName: station.displayName,
  657. privacy: station.privacy,
  658. locked: station.locked,
  659. partyMode: station.partyMode,
  660. owner: station.owner,
  661. privatePlaylist: station.privatePlaylist,
  662. genres: station.genres,
  663. blacklistedGenres: station.blacklistedGenres,
  664. theme: station.theme
  665. };
  666. StationsModule.userList[session.socketId] = station._id;
  667. next(null, data);
  668. },
  669. (data, next) => {
  670. data = JSON.parse(JSON.stringify(data));
  671. data.userCount = StationsModule.usersPerStationCount[data._id] || 0;
  672. data.users = StationsModule.usersPerStation[data._id] || [];
  673. if (!data.currentSong || !data.currentSong.title) return next(null, data);
  674. IOModule.runJob("SOCKET_JOIN_SONG_ROOM", {
  675. socketId: session.socketId,
  676. room: `song.${data.currentSong.songId}`
  677. });
  678. data.currentSong.skipVotes = data.currentSong.skipVotes.length;
  679. return SongsModule.runJob(
  680. "GET_SONG_FROM_ID",
  681. {
  682. songId: data.currentSong.songId
  683. },
  684. this
  685. )
  686. .then(response => {
  687. const { song } = response;
  688. if (song) {
  689. data.currentSong.likes = song.likes;
  690. data.currentSong.dislikes = song.dislikes;
  691. } else {
  692. data.currentSong.likes = -1;
  693. data.currentSong.dislikes = -1;
  694. }
  695. })
  696. .catch(() => {
  697. data.currentSong.likes = -1;
  698. data.currentSong.dislikes = -1;
  699. })
  700. .finally(() => next(null, data));
  701. },
  702. (data, next) => {
  703. // only relevant if user logged in
  704. if (session.userId) {
  705. return StationsModule.runJob(
  706. "HAS_USER_FAVORITED_STATION",
  707. {
  708. userId: session.userId,
  709. stationId: data._id
  710. },
  711. this
  712. )
  713. .then(isStationFavorited => {
  714. data.isFavorited = isStationFavorited;
  715. return next(null, data);
  716. })
  717. .catch(err => next(err));
  718. }
  719. return next(null, data);
  720. }
  721. ],
  722. async (err, data) => {
  723. if (err) {
  724. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  725. this.log("ERROR", "STATIONS_JOIN", `Joining station "${stationName}" failed. "${err}"`);
  726. return cb({ status: "failure", message: err });
  727. }
  728. this.log("SUCCESS", "STATIONS_JOIN", `Joined station "${data._id}" successfully.`);
  729. return cb({ status: "success", data });
  730. }
  731. );
  732. },
  733. /**
  734. * Gets a station by id
  735. *
  736. * @param {object} session - user session
  737. * @param {string} stationId - the station id
  738. * @param {Function} cb - callback
  739. */
  740. getStationById(session, stationId, cb) {
  741. async.waterfall(
  742. [
  743. next => {
  744. StationsModule.runJob("GET_STATION", { stationId }, this)
  745. .then(station => {
  746. next(null, station);
  747. })
  748. .catch(next);
  749. },
  750. (station, next) => {
  751. if (!station) return next("Station not found.");
  752. return StationsModule.runJob(
  753. "CAN_USER_VIEW_STATION",
  754. {
  755. station,
  756. userId: session.userId
  757. },
  758. this
  759. )
  760. .then(canView => {
  761. if (!canView) next("Not allowed to get station.");
  762. else next(null, station);
  763. })
  764. .catch(err => next(err));
  765. },
  766. (station, next) => {
  767. const data = {
  768. _id: station._id,
  769. type: station.type,
  770. description: station.description,
  771. displayName: station.displayName,
  772. name: station.name,
  773. privacy: station.privacy,
  774. locked: station.locked,
  775. partyMode: station.partyMode,
  776. owner: station.owner,
  777. privatePlaylist: station.privatePlaylist,
  778. genres: station.genres,
  779. blacklistedGenres: station.blacklistedGenres,
  780. theme: station.theme
  781. };
  782. next(null, data);
  783. }
  784. ],
  785. async (err, data) => {
  786. if (err) {
  787. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  788. this.log("ERROR", "GET_STATION_BY_ID", `Getting station "${stationId}" failed. "${err}"`);
  789. return cb({ status: "failure", message: err });
  790. }
  791. this.log("SUCCESS", "GET_STATION_BY_ID", `Got station "${stationId}" successfully.`);
  792. return cb({ status: "success", station: data });
  793. }
  794. );
  795. },
  796. getStationIncludedPlaylistsById(session, stationId, cb) {
  797. async.waterfall(
  798. [
  799. next => {
  800. StationsModule.runJob("GET_STATION", { stationId }, this)
  801. .then(station => {
  802. next(null, station);
  803. })
  804. .catch(next);
  805. },
  806. (station, next) => {
  807. if (!station) return next("Station not found.");
  808. return StationsModule.runJob(
  809. "CAN_USER_VIEW_STATION",
  810. {
  811. station,
  812. userId: session.userId
  813. },
  814. this
  815. )
  816. .then(canView => {
  817. if (!canView) next("Not allowed to get station.");
  818. else next(null, station);
  819. })
  820. .catch(err => next(err));
  821. },
  822. (station, next) => {
  823. const playlists = [];
  824. async.eachLimit(
  825. station.includedPlaylists,
  826. 1,
  827. (playlistId, next) => {
  828. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  829. .then(playlist => {
  830. playlists.push(playlist);
  831. next();
  832. })
  833. .catch(() => {
  834. playlists.push(null);
  835. next();
  836. });
  837. },
  838. err => {
  839. next(err, playlists);
  840. }
  841. );
  842. }
  843. ],
  844. async (err, playlists) => {
  845. if (err) {
  846. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  847. this.log(
  848. "ERROR",
  849. "GET_STATION_INCLUDED_PLAYLISTS_BY_ID",
  850. `Getting station "${stationId}"'s included playlists failed. "${err}"`
  851. );
  852. return cb({ status: "failure", message: err });
  853. }
  854. this.log(
  855. "SUCCESS",
  856. "GET_STATION_INCLUDED_PLAYLISTS_BY_ID",
  857. `Got station "${stationId}"'s included playlists successfully.`
  858. );
  859. return cb({ status: "success", playlists });
  860. }
  861. );
  862. },
  863. getStationExcludedPlaylistsById(session, stationId, cb) {
  864. async.waterfall(
  865. [
  866. next => {
  867. StationsModule.runJob("GET_STATION", { stationId }, this)
  868. .then(station => {
  869. next(null, station);
  870. })
  871. .catch(next);
  872. },
  873. (station, next) => {
  874. if (!station) return next("Station not found.");
  875. return StationsModule.runJob(
  876. "CAN_USER_VIEW_STATION",
  877. {
  878. station,
  879. userId: session.userId
  880. },
  881. this
  882. )
  883. .then(canView => {
  884. if (!canView) next("Not allowed to get station.");
  885. else next(null, station);
  886. })
  887. .catch(err => next(err));
  888. },
  889. (station, next) => {
  890. const playlists = [];
  891. async.eachLimit(
  892. station.excludedPlaylists,
  893. 1,
  894. (playlistId, next) => {
  895. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  896. .then(playlist => {
  897. playlists.push(playlist);
  898. next();
  899. })
  900. .catch(() => {
  901. playlists.push(null);
  902. next();
  903. });
  904. },
  905. err => {
  906. next(err, playlists);
  907. }
  908. );
  909. }
  910. ],
  911. async (err, playlists) => {
  912. if (err) {
  913. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  914. this.log(
  915. "ERROR",
  916. "GET_STATION_EXCLUDED_PLAYLISTS_BY_ID",
  917. `Getting station "${stationId}"'s excluded playlists failed. "${err}"`
  918. );
  919. return cb({ status: "failure", message: err });
  920. }
  921. this.log(
  922. "SUCCESS",
  923. "GET_STATION_EXCLUDED_PLAYLISTS_BY_ID",
  924. `Got station "${stationId}"'s excluded playlists successfully.`
  925. );
  926. return cb({ status: "success", playlists });
  927. }
  928. );
  929. },
  930. /**
  931. * Toggles if a station is locked
  932. *
  933. * @param session
  934. * @param stationId - the station id
  935. * @param cb
  936. */
  937. toggleLock: isOwnerRequired(async function toggleLock(session, stationId, cb) {
  938. const stationModel = await DBModule.runJob(
  939. "GET_MODEL",
  940. {
  941. modelName: "station"
  942. },
  943. this
  944. );
  945. async.waterfall(
  946. [
  947. next => {
  948. StationsModule.runJob("GET_STATION", { stationId }, this)
  949. .then(station => {
  950. next(null, station);
  951. })
  952. .catch(next);
  953. },
  954. (station, next) => {
  955. stationModel.updateOne({ _id: stationId }, { $set: { locked: !station.locked } }, next);
  956. },
  957. (res, next) => {
  958. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  959. .then(station => {
  960. next(null, station);
  961. })
  962. .catch(next);
  963. }
  964. ],
  965. async (err, station) => {
  966. if (err) {
  967. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  968. this.log(
  969. "ERROR",
  970. "STATIONS_UPDATE_LOCKED_STATUS",
  971. `Toggling the queue lock for station "${stationId}" failed. "${err}"`
  972. );
  973. return cb({ status: "failure", message: err });
  974. }
  975. this.log(
  976. "SUCCESS",
  977. "STATIONS_UPDATE_LOCKED_STATUS",
  978. `Toggled the queue lock for station "${stationId}" successfully to "${station.locked}".`
  979. );
  980. CacheModule.runJob("PUB", {
  981. channel: "station.queueLockToggled",
  982. value: {
  983. stationId,
  984. locked: station.locked
  985. }
  986. });
  987. return cb({ status: "success", data: station.locked });
  988. }
  989. );
  990. }),
  991. /**
  992. * Votes to skip a station
  993. *
  994. * @param session
  995. * @param stationId - the station id
  996. * @param cb
  997. */
  998. voteSkip: isLoginRequired(async function voteSkip(session, stationId, cb) {
  999. const stationModel = await DBModule.runJob(
  1000. "GET_MODEL",
  1001. {
  1002. modelName: "station"
  1003. },
  1004. this
  1005. );
  1006. let skipVotes = 0;
  1007. let shouldSkip = false;
  1008. async.waterfall(
  1009. [
  1010. next => {
  1011. StationsModule.runJob("GET_STATION", { stationId }, this)
  1012. .then(station => {
  1013. next(null, station);
  1014. })
  1015. .catch(next);
  1016. },
  1017. (station, next) => {
  1018. if (!station) return next("Station not found.");
  1019. return StationsModule.runJob(
  1020. "CAN_USER_VIEW_STATION",
  1021. {
  1022. station,
  1023. userId: session.userId
  1024. },
  1025. this
  1026. )
  1027. .then(canView => {
  1028. if (canView) return next(null, station);
  1029. return next("Insufficient permissions.");
  1030. })
  1031. .catch(err => next(err));
  1032. },
  1033. (station, next) => {
  1034. if (!station.currentSong) return next("There is currently no song to skip.");
  1035. if (station.currentSong.skipVotes.indexOf(session.userId) !== -1)
  1036. return next("You have already voted to skip this song.");
  1037. return next(null, station);
  1038. },
  1039. (station, next) => {
  1040. stationModel.updateOne(
  1041. { _id: stationId },
  1042. { $push: { "currentSong.skipVotes": session.userId } },
  1043. next
  1044. );
  1045. },
  1046. (res, next) => {
  1047. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  1048. .then(station => {
  1049. next(null, station);
  1050. })
  1051. .catch(next);
  1052. },
  1053. (station, next) => {
  1054. if (!station) return next("Station not found.");
  1055. return next(null, station);
  1056. },
  1057. (station, next) => {
  1058. skipVotes = station.currentSong.skipVotes.length;
  1059. IOModule.runJob(
  1060. "GET_ROOM_SOCKETS",
  1061. {
  1062. room: `station.${stationId}`
  1063. },
  1064. this
  1065. )
  1066. .then(sockets => {
  1067. next(null, sockets);
  1068. })
  1069. .catch(next);
  1070. },
  1071. (sockets, next) => {
  1072. if (sockets.length <= skipVotes) shouldSkip = true;
  1073. next();
  1074. }
  1075. ],
  1076. async err => {
  1077. if (err) {
  1078. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1079. this.log("ERROR", "STATIONS_VOTE_SKIP", `Vote skipping station "${stationId}" failed. "${err}"`);
  1080. return cb({ status: "failure", message: err });
  1081. }
  1082. this.log("SUCCESS", "STATIONS_VOTE_SKIP", `Vote skipping "${stationId}" successful.`);
  1083. CacheModule.runJob("PUB", {
  1084. channel: "station.voteSkipSong",
  1085. value: stationId
  1086. });
  1087. if (shouldSkip) StationsModule.runJob("SKIP_STATION", { stationId });
  1088. return cb({
  1089. status: "success",
  1090. message: "Successfully voted to skip the song."
  1091. });
  1092. }
  1093. );
  1094. }),
  1095. /**
  1096. * Force skips a station
  1097. *
  1098. * @param session
  1099. * @param stationId - the station id
  1100. * @param cb
  1101. */
  1102. forceSkip: isOwnerRequired(function forceSkip(session, stationId, cb) {
  1103. async.waterfall(
  1104. [
  1105. next => {
  1106. StationsModule.runJob("GET_STATION", { stationId }, this)
  1107. .then(station => {
  1108. next(null, station);
  1109. })
  1110. .catch(next);
  1111. },
  1112. (station, next) => {
  1113. if (!station) return next("Station not found.");
  1114. return next();
  1115. }
  1116. ],
  1117. async err => {
  1118. if (err) {
  1119. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1120. this.log("ERROR", "STATIONS_FORCE_SKIP", `Force skipping station "${stationId}" failed. "${err}"`);
  1121. return cb({ status: "failure", message: err });
  1122. }
  1123. StationsModule.runJob("SKIP_STATION", { stationId });
  1124. this.log("SUCCESS", "STATIONS_FORCE_SKIP", `Force skipped station "${stationId}" successfully.`);
  1125. return cb({
  1126. status: "success",
  1127. message: "Successfully skipped station."
  1128. });
  1129. }
  1130. );
  1131. }),
  1132. /**
  1133. * Leaves the user's current station
  1134. *
  1135. * @param {object} session - user session
  1136. * @param {string} stationId - id of station to leave
  1137. * @param {Function} cb - callback
  1138. */
  1139. leave(session, stationId, cb) {
  1140. async.waterfall(
  1141. [
  1142. next => {
  1143. StationsModule.runJob("GET_STATION", { stationId }, this)
  1144. .then(station => {
  1145. next(null, station);
  1146. })
  1147. .catch(next);
  1148. },
  1149. (station, next) => {
  1150. if (!station) return next("Station not found.");
  1151. return next();
  1152. }
  1153. ],
  1154. async (err, userCount) => {
  1155. if (err) {
  1156. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1157. this.log("ERROR", "STATIONS_LEAVE", `Leaving station "${stationId}" failed. "${err}"`);
  1158. return cb({ status: "failure", message: err });
  1159. }
  1160. this.log("SUCCESS", "STATIONS_LEAVE", `Left station "${stationId}" successfully.`);
  1161. IOModule.runJob("SOCKET_LEAVE_ROOMS", { socketId: session });
  1162. delete StationsModule.userList[session.socketId];
  1163. return cb({
  1164. status: "success",
  1165. message: "Successfully left station.",
  1166. userCount
  1167. });
  1168. }
  1169. );
  1170. },
  1171. /**
  1172. * Updates a station's name
  1173. *
  1174. * @param session
  1175. * @param stationId - the station id
  1176. * @param newName - the new station name
  1177. * @param cb
  1178. */
  1179. updateName: isOwnerRequired(async function updateName(session, stationId, newName, cb) {
  1180. const stationModel = await DBModule.runJob("GET_MODEL", { modelName: "station" }, this);
  1181. async.waterfall(
  1182. [
  1183. next => {
  1184. stationModel.updateOne(
  1185. { _id: stationId },
  1186. { $set: { name: newName } },
  1187. { runValidators: true },
  1188. next
  1189. );
  1190. },
  1191. (res, next) => {
  1192. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  1193. .then(station => next(null, station))
  1194. .catch(next);
  1195. }
  1196. ],
  1197. async (err, station) => {
  1198. if (err) {
  1199. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1200. this.log(
  1201. "ERROR",
  1202. "STATIONS_UPDATE_NAME",
  1203. `Updating station "${stationId}" name to "${newName}" failed. "${err}"`
  1204. );
  1205. return cb({ status: "failure", message: err });
  1206. }
  1207. this.log(
  1208. "SUCCESS",
  1209. "STATIONS_UPDATE_NAME",
  1210. `Updated station "${stationId}" name to "${newName}" successfully.`
  1211. );
  1212. CacheModule.runJob("PUB", {
  1213. channel: "station.nameUpdate",
  1214. value: { stationId, name: newName }
  1215. });
  1216. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1217. userId: session.userId,
  1218. type: "station__edit_name",
  1219. payload: {
  1220. message: `Changed name of station <stationId>${station.displayName}</stationId> to ${newName}`,
  1221. stationId
  1222. }
  1223. });
  1224. return cb({
  1225. status: "success",
  1226. message: "Successfully updated the name."
  1227. });
  1228. }
  1229. );
  1230. }),
  1231. /**
  1232. * Updates a station's display name
  1233. *
  1234. * @param session
  1235. * @param stationId - the station id
  1236. * @param newDisplayName - the new station display name
  1237. * @param cb
  1238. */
  1239. updateDisplayName: isOwnerRequired(async function updateDisplayName(session, stationId, newDisplayName, cb) {
  1240. const stationModel = await DBModule.runJob("GET_MODEL", { modelName: "station" }, this);
  1241. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  1242. async.waterfall(
  1243. [
  1244. next => {
  1245. stationModel.updateOne(
  1246. { _id: stationId },
  1247. { $set: { displayName: newDisplayName } },
  1248. { runValidators: true },
  1249. next
  1250. );
  1251. },
  1252. (res, next) => {
  1253. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  1254. .then(station => next(null, station))
  1255. .catch(next);
  1256. },
  1257. (station, next) => {
  1258. playlistModel.updateOne(
  1259. { _id: station.playlist2 },
  1260. { $set: { displayName: `Station - ${station.displayName}` } },
  1261. err => {
  1262. next(err, station);
  1263. }
  1264. );
  1265. }
  1266. ],
  1267. async err => {
  1268. if (err) {
  1269. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1270. this.log(
  1271. "ERROR",
  1272. "STATIONS_UPDATE_DISPLAY_NAME",
  1273. `Updating station "${stationId}" displayName to "${newDisplayName}" failed. "${err}"`
  1274. );
  1275. return cb({ status: "failure", message: err });
  1276. }
  1277. this.log(
  1278. "SUCCESS",
  1279. "STATIONS_UPDATE_DISPLAY_NAME",
  1280. `Updated station "${stationId}" displayName to "${newDisplayName}" successfully.`
  1281. );
  1282. CacheModule.runJob("PUB", {
  1283. channel: "station.displayNameUpdate",
  1284. value: { stationId, displayName: newDisplayName }
  1285. });
  1286. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1287. userId: session.userId,
  1288. type: "station__edit_display_name",
  1289. payload: {
  1290. message: `Changed display name of station <stationId>${newDisplayName}</stationId>`,
  1291. stationId
  1292. }
  1293. });
  1294. return cb({
  1295. status: "success",
  1296. message: "Successfully updated the display name."
  1297. });
  1298. }
  1299. );
  1300. }),
  1301. /**
  1302. * Updates a station's description
  1303. *
  1304. * @param session
  1305. * @param stationId - the station id
  1306. * @param newDescription - the new station description
  1307. * @param cb
  1308. */
  1309. updateDescription: isOwnerRequired(async function updateDescription(session, stationId, newDescription, cb) {
  1310. const stationModel = await DBModule.runJob("GET_MODEL", { modelName: "station" }, this);
  1311. async.waterfall(
  1312. [
  1313. next => {
  1314. stationModel.updateOne(
  1315. { _id: stationId },
  1316. { $set: { description: newDescription } },
  1317. { runValidators: true },
  1318. next
  1319. );
  1320. },
  1321. (res, next) => {
  1322. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  1323. .then(station => next(null, station))
  1324. .catch(next);
  1325. }
  1326. ],
  1327. async (err, station) => {
  1328. if (err) {
  1329. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1330. this.log(
  1331. "ERROR",
  1332. "STATIONS_UPDATE_DESCRIPTION",
  1333. `Updating station "${stationId}" description to "${newDescription}" failed. "${err}"`
  1334. );
  1335. return cb({ status: "failure", message: err });
  1336. }
  1337. this.log(
  1338. "SUCCESS",
  1339. "STATIONS_UPDATE_DESCRIPTION",
  1340. `Updated station "${stationId}" description to "${newDescription}" successfully.`
  1341. );
  1342. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1343. userId: session.userId,
  1344. type: "station__edit_description",
  1345. payload: {
  1346. message: `Changed description of station <stationId>${station.displayName}</stationId> to ${newDescription}`,
  1347. stationId
  1348. }
  1349. });
  1350. CacheModule.runJob("PUB", {
  1351. channel: "station.descriptionUpdate",
  1352. value: { stationId, description: newDescription }
  1353. });
  1354. return cb({
  1355. status: "success",
  1356. message: "Successfully updated the description."
  1357. });
  1358. }
  1359. );
  1360. }),
  1361. /**
  1362. * Updates a station's privacy
  1363. *
  1364. * @param session
  1365. * @param stationId - the station id
  1366. * @param newPrivacy - the new station privacy
  1367. * @param cb
  1368. */
  1369. updatePrivacy: isOwnerRequired(async function updatePrivacy(session, stationId, newPrivacy, cb) {
  1370. const stationModel = await DBModule.runJob("GET_MODEL", { modelName: "station" }, this);
  1371. let previousPrivacy = null;
  1372. async.waterfall(
  1373. [
  1374. next => {
  1375. stationModel.findOne({ _id: stationId }, next);
  1376. },
  1377. (station, next) => {
  1378. if (!station) next("No station found.");
  1379. else {
  1380. previousPrivacy = station.privacy;
  1381. next();
  1382. }
  1383. },
  1384. next => {
  1385. stationModel.updateOne(
  1386. { _id: stationId },
  1387. { $set: { privacy: newPrivacy } },
  1388. { runValidators: true },
  1389. next
  1390. );
  1391. },
  1392. (res, next) => {
  1393. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  1394. .then(station => next(null, station))
  1395. .catch(next);
  1396. }
  1397. ],
  1398. async (err, station) => {
  1399. if (err) {
  1400. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1401. this.log(
  1402. "ERROR",
  1403. "STATIONS_UPDATE_PRIVACY",
  1404. `Updating station "${stationId}" privacy to "${newPrivacy}" failed. "${err}"`
  1405. );
  1406. return cb({ status: "failure", message: err });
  1407. }
  1408. this.log(
  1409. "SUCCESS",
  1410. "STATIONS_UPDATE_PRIVACY",
  1411. `Updated station "${stationId}" privacy to "${newPrivacy}" successfully.`
  1412. );
  1413. CacheModule.runJob("PUB", {
  1414. channel: "station.privacyUpdate",
  1415. value: { stationId, previousPrivacy }
  1416. });
  1417. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1418. userId: session.userId,
  1419. type: "station__edit_privacy",
  1420. payload: {
  1421. message: `Changed privacy of station <stationId>${station.displayName}</stationId> to ${newPrivacy}`,
  1422. stationId
  1423. }
  1424. });
  1425. return cb({
  1426. status: "success",
  1427. message: "Successfully updated the privacy."
  1428. });
  1429. }
  1430. );
  1431. }),
  1432. /**
  1433. * Updates a station's genres
  1434. *
  1435. * @param session
  1436. * @param stationId - the station id
  1437. * @param newGenres - the new station genres
  1438. * @param cb
  1439. */
  1440. updateGenres: isOwnerRequired(async function updateGenres(session, stationId, newGenres, cb) {
  1441. async.waterfall(
  1442. [
  1443. next => {
  1444. StationsModule.runJob("GET_STATION", { stationId }, this)
  1445. .then(station => {
  1446. next(null, station);
  1447. })
  1448. .catch(next);
  1449. },
  1450. (station, next) => {
  1451. const playlists = [];
  1452. async.eachLimit(
  1453. newGenres,
  1454. 1,
  1455. (genre, next) => {
  1456. PlaylistsModule.runJob("GET_GENRE_PLAYLIST", { genre, includeSongs: false }, this).then(
  1457. response => {
  1458. playlists.push(response.playlist);
  1459. next();
  1460. }
  1461. );
  1462. },
  1463. err => {
  1464. next(
  1465. err,
  1466. station,
  1467. playlists.map(playlist => playlist._id.toString())
  1468. );
  1469. }
  1470. );
  1471. },
  1472. (station, playlists, next) => {
  1473. const playlistsToRemoveFromExcluded = playlists.filter(
  1474. playlistId => station.excludedPlaylists.indexOf(playlistId) !== -1
  1475. );
  1476. console.log(
  1477. `playlistsToRemoveFromExcluded: ${playlistsToRemoveFromExcluded.length}`,
  1478. playlistsToRemoveFromExcluded
  1479. );
  1480. async.eachLimit(
  1481. playlistsToRemoveFromExcluded,
  1482. 1,
  1483. (playlistId, next) => {
  1484. StationsModule.runJob("REMOVE_EXCLUDED_PLAYLIST", { stationId, playlistId }, this)
  1485. .then(() => {
  1486. next();
  1487. })
  1488. .catch(next);
  1489. },
  1490. err => {
  1491. next(err, station, playlists);
  1492. }
  1493. );
  1494. },
  1495. (station, playlists, next) => {
  1496. const playlistsToRemoveFromIncluded = station.includedPlaylists.filter(
  1497. playlistId => playlists.indexOf(playlistId) === -1
  1498. );
  1499. console.log(
  1500. `playlistsToRemoveFromIncluded: ${playlistsToRemoveFromIncluded.length}`,
  1501. playlistsToRemoveFromIncluded
  1502. );
  1503. async.eachLimit(
  1504. playlistsToRemoveFromIncluded,
  1505. 1,
  1506. (playlistId, next) => {
  1507. StationsModule.runJob("REMOVE_INCLUDED_PLAYLIST", { stationId, playlistId }, this)
  1508. .then(() => {
  1509. next();
  1510. })
  1511. .catch(next);
  1512. },
  1513. err => {
  1514. next(err, station, playlists);
  1515. }
  1516. );
  1517. },
  1518. (station, playlists, next) => {
  1519. const playlistsToAddToIncluded = playlists.filter(
  1520. playlistId => station.includedPlaylists.indexOf(playlistId) === -1
  1521. );
  1522. console.log(
  1523. `playlistsToAddToIncluded: ${playlistsToAddToIncluded.length}`,
  1524. playlistsToAddToIncluded
  1525. );
  1526. async.eachLimit(
  1527. playlistsToAddToIncluded,
  1528. 1,
  1529. (playlistId, next) => {
  1530. StationsModule.runJob("INCLUDE_PLAYLIST", { stationId, playlistId }, this)
  1531. .then(() => {
  1532. next();
  1533. })
  1534. .catch(next);
  1535. },
  1536. err => {
  1537. next(err);
  1538. }
  1539. );
  1540. },
  1541. next => {
  1542. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  1543. .then(station => next(null, station))
  1544. .catch(next);
  1545. }
  1546. ],
  1547. async (err, station) => {
  1548. if (err) {
  1549. err = await UtilsModule.runJob("GET_ERROR", { error: err });
  1550. this.log(
  1551. "ERROR",
  1552. "STATIONS_UPDATE_GENRES",
  1553. `Updating station "${stationId}" genres to "${newGenres}" failed. "${err}"`
  1554. );
  1555. return cb({ status: "failure", message: err });
  1556. }
  1557. this.log(
  1558. "SUCCESS",
  1559. "STATIONS_UPDATE_GENRES",
  1560. `Updated station "${stationId}" genres to "${newGenres}" successfully.`
  1561. );
  1562. if (newGenres.length > 0) {
  1563. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1564. userId: session.userId,
  1565. type: "station__edit_genres",
  1566. payload: {
  1567. message: `Updated genres of station <stationId>${station.displayName}</stationId> to
  1568. ${newGenres.join(", ")}`,
  1569. stationId
  1570. }
  1571. });
  1572. } else {
  1573. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1574. userId: session.userId,
  1575. type: "station__edit_genres",
  1576. payload: {
  1577. message: `Removed all genres of station <stationId>${station.displayName}</stationId>`,
  1578. stationId
  1579. }
  1580. });
  1581. }
  1582. return cb({
  1583. status: "success",
  1584. message: "Successfully updated the genres."
  1585. });
  1586. }
  1587. );
  1588. }),
  1589. /**
  1590. * Updates a station's blacklisted genres
  1591. *
  1592. * @param session
  1593. * @param stationId - the station id
  1594. * @param newBlacklistedGenres - the new station blacklisted genres
  1595. * @param cb
  1596. */
  1597. updateBlacklistedGenres: isOwnerRequired(async function updateBlacklistedGenres(
  1598. session,
  1599. stationId,
  1600. newBlacklistedGenres,
  1601. cb
  1602. ) {
  1603. async.waterfall(
  1604. [
  1605. next => {
  1606. StationsModule.runJob("GET_STATION", { stationId }, this)
  1607. .then(station => {
  1608. next(null, station);
  1609. })
  1610. .catch(next);
  1611. },
  1612. (station, next) => {
  1613. const playlists = [];
  1614. async.eachLimit(
  1615. newBlacklistedGenres,
  1616. 1,
  1617. (genre, next) => {
  1618. PlaylistsModule.runJob("GET_GENRE_PLAYLIST", { genre, includeSongs: false }, this).then(
  1619. response => {
  1620. playlists.push(response.playlist);
  1621. next();
  1622. }
  1623. );
  1624. },
  1625. err => {
  1626. next(
  1627. err,
  1628. station,
  1629. playlists.map(playlist => playlist._id.toString())
  1630. );
  1631. }
  1632. );
  1633. },
  1634. (station, playlists, next) => {
  1635. const playlistsToRemoveFromIncluded = playlists.filter(
  1636. playlistId => station.includedPlaylists.indexOf(playlistId) !== -1
  1637. );
  1638. console.log(
  1639. `playlistsToRemoveFromIncluded: ${playlistsToRemoveFromIncluded.length}`,
  1640. playlistsToRemoveFromIncluded
  1641. );
  1642. async.eachLimit(
  1643. playlistsToRemoveFromIncluded,
  1644. 1,
  1645. (playlistId, next) => {
  1646. StationsModule.runJob("REMOVE_INCLUDED_PLAYLIST", { stationId, playlistId }, this)
  1647. .then(() => {
  1648. next();
  1649. })
  1650. .catch(next);
  1651. },
  1652. err => {
  1653. next(err, station, playlists);
  1654. }
  1655. );
  1656. },
  1657. (station, playlists, next) => {
  1658. const playlistsToRemoveFromExcluded = station.excludedPlaylists.filter(
  1659. playlistId => playlists.indexOf(playlistId) === -1
  1660. );
  1661. console.log(
  1662. `playlistsToRemoveFromExcluded: ${playlistsToRemoveFromExcluded.length}`,
  1663. playlistsToRemoveFromExcluded
  1664. );
  1665. async.eachLimit(
  1666. playlistsToRemoveFromExcluded,
  1667. 1,
  1668. (playlistId, next) => {
  1669. StationsModule.runJob("REMOVE_EXCLUDED_PLAYLIST", { stationId, playlistId }, this)
  1670. .then(() => {
  1671. next();
  1672. })
  1673. .catch(next);
  1674. },
  1675. err => {
  1676. next(err, station, playlists);
  1677. }
  1678. );
  1679. },
  1680. (station, playlists, next) => {
  1681. const playlistsToAddToExcluded = playlists.filter(
  1682. playlistId => station.excludedPlaylists.indexOf(playlistId) === -1
  1683. );
  1684. console.log(
  1685. `playlistsToAddToExcluded: ${playlistsToAddToExcluded.length}`,
  1686. playlistsToAddToExcluded
  1687. );
  1688. async.eachLimit(
  1689. playlistsToAddToExcluded,
  1690. 1,
  1691. (playlistId, next) => {
  1692. StationsModule.runJob("EXCLUDE_PLAYLIST", { stationId, playlistId }, this)
  1693. .then(() => {
  1694. next();
  1695. })
  1696. .catch(next);
  1697. },
  1698. err => {
  1699. next(err);
  1700. }
  1701. );
  1702. },
  1703. next => {
  1704. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  1705. .then(station => next(null, station))
  1706. .catch(next);
  1707. }
  1708. ],
  1709. async (err, station) => {
  1710. if (err) {
  1711. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1712. this.log(
  1713. "ERROR",
  1714. "STATIONS_UPDATE_BLACKLISTED_GENRES",
  1715. `Updating station "${stationId}" blacklisted genres to "${newBlacklistedGenres}" failed. "${err}"`
  1716. );
  1717. return cb({ status: "failure", message: err });
  1718. }
  1719. this.log(
  1720. "SUCCESS",
  1721. "STATIONS_UPDATE_BLACKLISTED_GENRES",
  1722. `Updated station "${stationId}" blacklisted genres to "${newBlacklistedGenres}" successfully.`
  1723. );
  1724. if (newBlacklistedGenres.length > 0) {
  1725. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1726. userId: session.userId,
  1727. type: "station__edit_blacklisted_genres",
  1728. payload: {
  1729. message: `Updated blacklisted genres of station <stationId>${
  1730. station.displayName
  1731. }</stationId> to ${newBlacklistedGenres.join(", ")}`,
  1732. stationId
  1733. }
  1734. });
  1735. } else {
  1736. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1737. userId: session.userId,
  1738. type: "station__edit_blacklisted_genres",
  1739. payload: {
  1740. message: `Removed all blacklisted genres of station <stationId>${station.displayName}</stationId>`,
  1741. stationId
  1742. }
  1743. });
  1744. }
  1745. return cb({
  1746. status: "success",
  1747. message: "Successfully updated the blacklisted genres."
  1748. });
  1749. }
  1750. );
  1751. }),
  1752. /**
  1753. * Updates a station's party mode
  1754. *
  1755. * @param session
  1756. * @param stationId - the station id
  1757. * @param newPartyMode - the new station party mode
  1758. * @param cb
  1759. */
  1760. updatePartyMode: isOwnerRequired(async function updatePartyMode(session, stationId, newPartyMode, cb) {
  1761. const stationModel = await DBModule.runJob("GET_MODEL", { modelName: "station" }, this);
  1762. async.waterfall(
  1763. [
  1764. next => {
  1765. StationsModule.runJob("GET_STATION", { stationId }, this)
  1766. .then(station => {
  1767. next(null, station);
  1768. })
  1769. .catch(next);
  1770. },
  1771. (station, next) => {
  1772. if (!station) return next("Station not found.");
  1773. if (station.partyMode === newPartyMode)
  1774. return next(`The party mode was already ${newPartyMode ? "enabled." : "disabled."}`);
  1775. return stationModel.updateOne(
  1776. { _id: stationId },
  1777. { $set: { partyMode: newPartyMode } },
  1778. { runValidators: true },
  1779. next
  1780. );
  1781. },
  1782. (res, next) => {
  1783. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  1784. .then(station => {
  1785. next(null, station);
  1786. })
  1787. .catch(next);
  1788. }
  1789. ],
  1790. async err => {
  1791. if (err) {
  1792. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1793. this.log(
  1794. "ERROR",
  1795. "STATIONS_UPDATE_PARTY_MODE",
  1796. `Updating station "${stationId}" party mode to "${newPartyMode}" failed. "${err}"`
  1797. );
  1798. return cb({ status: "failure", message: err });
  1799. }
  1800. this.log(
  1801. "SUCCESS",
  1802. "STATIONS_UPDATE_PARTY_MODE",
  1803. `Updated station "${stationId}" party mode to "${newPartyMode}" successfully.`
  1804. );
  1805. CacheModule.runJob("PUB", {
  1806. channel: "station.updatePartyMode",
  1807. value: {
  1808. stationId,
  1809. partyMode: newPartyMode
  1810. }
  1811. });
  1812. StationsModule.runJob("SKIP_STATION", { stationId });
  1813. return cb({
  1814. status: "success",
  1815. message: "Successfully updated the party mode."
  1816. });
  1817. }
  1818. );
  1819. }),
  1820. /**
  1821. * Updates a station's theme
  1822. *
  1823. * @param session
  1824. * @param stationId - the station id
  1825. * @param newTheme - the new station theme
  1826. * @param cb
  1827. */
  1828. updateTheme: isOwnerRequired(async function updateTheme(session, stationId, newTheme, cb) {
  1829. const stationModel = await DBModule.runJob("GET_MODEL", { modelName: "station" }, this);
  1830. async.waterfall(
  1831. [
  1832. next => {
  1833. StationsModule.runJob("GET_STATION", { stationId }, this)
  1834. .then(station => {
  1835. next(null, station);
  1836. })
  1837. .catch(next);
  1838. },
  1839. (station, next) => {
  1840. if (!station) return next("Station not found.");
  1841. if (station.theme === newTheme) return next("No change in theme.");
  1842. return stationModel.updateOne(
  1843. { _id: stationId },
  1844. { $set: { theme: newTheme } },
  1845. { runValidators: true },
  1846. next
  1847. );
  1848. },
  1849. (res, next) => {
  1850. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  1851. .then(station => next(null, station))
  1852. .catch(next);
  1853. }
  1854. ],
  1855. async (err, station) => {
  1856. if (err) {
  1857. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1858. this.log(
  1859. "ERROR",
  1860. "STATIONS_UPDATE_THEME",
  1861. `Updating station "${stationId}" theme to "${newTheme}" failed. "${err}"`
  1862. );
  1863. return cb({ status: "failure", message: err });
  1864. }
  1865. this.log(
  1866. "SUCCESS",
  1867. "STATIONS_UPDATE_THEME",
  1868. `Updated station "${stationId}" theme to "${newTheme}" successfully.`
  1869. );
  1870. CacheModule.runJob("PUB", {
  1871. channel: "station.themeUpdate",
  1872. value: { stationId }
  1873. });
  1874. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1875. userId: session.userId,
  1876. type: "station__edit_theme",
  1877. payload: {
  1878. message: `Changed theme of station <stationId>${station.displayName}</stationId> to ${newTheme}`,
  1879. stationId
  1880. }
  1881. });
  1882. return cb({
  1883. status: "success",
  1884. message: "Successfully updated the theme."
  1885. });
  1886. }
  1887. );
  1888. }),
  1889. /**
  1890. * Pauses a station
  1891. *
  1892. * @param session
  1893. * @param stationId - the station id
  1894. * @param cb
  1895. */
  1896. pause: isOwnerRequired(async function pause(session, stationId, cb) {
  1897. const stationModel = await DBModule.runJob(
  1898. "GET_MODEL",
  1899. {
  1900. modelName: "station"
  1901. },
  1902. this
  1903. );
  1904. async.waterfall(
  1905. [
  1906. next => {
  1907. StationsModule.runJob("GET_STATION", { stationId }, this)
  1908. .then(station => {
  1909. next(null, station);
  1910. })
  1911. .catch(next);
  1912. },
  1913. (station, next) => {
  1914. if (!station) return next("Station not found.");
  1915. if (station.paused) return next("That station was already paused.");
  1916. return stationModel.updateOne(
  1917. { _id: stationId },
  1918. { $set: { paused: true, pausedAt: Date.now() } },
  1919. next
  1920. );
  1921. },
  1922. (res, next) => {
  1923. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  1924. .then(station => {
  1925. next(null, station);
  1926. })
  1927. .catch(next);
  1928. }
  1929. ],
  1930. async err => {
  1931. if (err) {
  1932. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1933. this.log("ERROR", "STATIONS_PAUSE", `Pausing station "${stationId}" failed. "${err}"`);
  1934. return cb({ status: "failure", message: err });
  1935. }
  1936. this.log("SUCCESS", "STATIONS_PAUSE", `Paused station "${stationId}" successfully.`);
  1937. CacheModule.runJob("PUB", {
  1938. channel: "station.pause",
  1939. value: stationId
  1940. });
  1941. NotificationsModule.runJob("UNSCHEDULE", {
  1942. name: `stations.nextSong?id=${stationId}`
  1943. });
  1944. return cb({
  1945. status: "success",
  1946. message: "Successfully paused."
  1947. });
  1948. }
  1949. );
  1950. }),
  1951. /**
  1952. * Resumes a station
  1953. *
  1954. * @param session
  1955. * @param stationId - the station id
  1956. * @param cb
  1957. */
  1958. resume: isOwnerRequired(async function resume(session, stationId, cb) {
  1959. const stationModel = await DBModule.runJob(
  1960. "GET_MODEL",
  1961. {
  1962. modelName: "station"
  1963. },
  1964. this
  1965. );
  1966. async.waterfall(
  1967. [
  1968. next => {
  1969. StationsModule.runJob("GET_STATION", { stationId }, this)
  1970. .then(station => {
  1971. next(null, station);
  1972. })
  1973. .catch(next);
  1974. },
  1975. (station, next) => {
  1976. if (!station) return next("Station not found.");
  1977. if (!station.paused) return next("That station is not paused.");
  1978. station.timePaused += Date.now() - station.pausedAt;
  1979. return stationModel.updateOne(
  1980. { _id: stationId },
  1981. {
  1982. $set: { paused: false },
  1983. $inc: { timePaused: Date.now() - station.pausedAt }
  1984. },
  1985. next
  1986. );
  1987. },
  1988. (res, next) => {
  1989. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  1990. .then(station => {
  1991. next(null, station);
  1992. })
  1993. .catch(next);
  1994. }
  1995. ],
  1996. async err => {
  1997. if (err) {
  1998. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1999. this.log("ERROR", "STATIONS_RESUME", `Resuming station "${stationId}" failed. "${err}"`);
  2000. return cb({ status: "failure", message: err });
  2001. }
  2002. this.log("SUCCESS", "STATIONS_RESUME", `Resuming station "${stationId}" successfully.`);
  2003. CacheModule.runJob("PUB", {
  2004. channel: "station.resume",
  2005. value: stationId
  2006. });
  2007. return cb({
  2008. status: "success",
  2009. message: "Successfully resumed."
  2010. });
  2011. }
  2012. );
  2013. }),
  2014. /**
  2015. * Removes a station
  2016. *
  2017. * @param session
  2018. * @param stationId - the station id
  2019. * @param cb
  2020. */
  2021. remove: isOwnerRequired(async function remove(session, stationId, cb) {
  2022. const stationModel = await DBModule.runJob("GET_MODEL", { modelName: "station" }, this);
  2023. async.waterfall(
  2024. [
  2025. next => {
  2026. stationModel.findById(stationId, (err, station) => {
  2027. if (err) return next(err);
  2028. return next(null, station);
  2029. });
  2030. },
  2031. (station, next) => {
  2032. stationModel.deleteOne({ _id: stationId }, err => next(err, station));
  2033. },
  2034. (station, next) => {
  2035. CacheModule.runJob("HDEL", { table: "stations", key: stationId }, this)
  2036. .then(next(null, station))
  2037. .catch(next);
  2038. },
  2039. (station, next) => {
  2040. if (station.playlist2)
  2041. PlaylistsModule.runJob("DELETE_PLAYLIST", { playlistId: station.playlist2 }).then().catch();
  2042. next(null, station);
  2043. }
  2044. ],
  2045. async (err, station) => {
  2046. if (err) {
  2047. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2048. this.log("ERROR", "STATIONS_REMOVE", `Removing station "${stationId}" failed. "${err}"`);
  2049. return cb({ status: "failure", message: err });
  2050. }
  2051. this.log("SUCCESS", "STATIONS_REMOVE", `Removing station "${stationId}" successfully.`);
  2052. CacheModule.runJob("PUB", {
  2053. channel: "station.remove",
  2054. value: stationId
  2055. });
  2056. ActivitiesModule.runJob("ADD_ACTIVITY", {
  2057. userId: session.userId,
  2058. type: "station__remove",
  2059. payload: { message: `Removed a station named <stationId>${station.displayName}</stationId>` }
  2060. });
  2061. return cb({
  2062. status: "success",
  2063. message: "Successfully removed."
  2064. });
  2065. }
  2066. );
  2067. }),
  2068. /**
  2069. * Create a station
  2070. *
  2071. * @param session
  2072. * @param data - the station data
  2073. * @param cb
  2074. */
  2075. create: isLoginRequired(async function create(session, data, cb) {
  2076. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  2077. const stationModel = await DBModule.runJob("GET_MODEL", { modelName: "station" }, this);
  2078. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  2079. data.name = data.name.toLowerCase();
  2080. const blacklist = [
  2081. "country",
  2082. "edm",
  2083. "musare",
  2084. "hip-hop",
  2085. "rap",
  2086. "top-hits",
  2087. "todays-hits",
  2088. "old-school",
  2089. "christmas",
  2090. "about",
  2091. "support",
  2092. "staff",
  2093. "help",
  2094. "news",
  2095. "terms",
  2096. "privacy",
  2097. "profile",
  2098. "c",
  2099. "community",
  2100. "tos",
  2101. "login",
  2102. "register",
  2103. "p",
  2104. "official",
  2105. "o",
  2106. "trap",
  2107. "faq",
  2108. "team",
  2109. "donate",
  2110. "buy",
  2111. "shop",
  2112. "forums",
  2113. "explore",
  2114. "settings",
  2115. "admin",
  2116. "auth",
  2117. "reset_password"
  2118. ];
  2119. async.waterfall(
  2120. [
  2121. next => {
  2122. if (!data) return next("Invalid data.");
  2123. return next();
  2124. },
  2125. next => {
  2126. stationModel.findOne(
  2127. {
  2128. $or: [
  2129. { name: data.name },
  2130. {
  2131. displayName: new RegExp(`^${data.displayName}$`, "i")
  2132. }
  2133. ]
  2134. },
  2135. next
  2136. );
  2137. },
  2138. // eslint-disable-next-line consistent-return
  2139. (station, next) => {
  2140. this.log(station);
  2141. if (station) return next("A station with that name or display name already exists.");
  2142. const { name, displayName, description, genres, playlist, type, blacklistedGenres } = data;
  2143. const stationId = mongoose.Types.ObjectId();
  2144. if (type === "official") {
  2145. return userModel.findOne({ _id: session.userId }, (err, user) => {
  2146. if (err) return next(err);
  2147. if (!user) return next("User not found.");
  2148. if (user.role !== "admin") return next("Admin required.");
  2149. return playlistModel.create(
  2150. {
  2151. isUserModifiable: false,
  2152. displayName: `Station - ${displayName}`,
  2153. songs: [],
  2154. createdBy: "Musare",
  2155. createdFor: `${stationId}`,
  2156. createdAt: Date.now(),
  2157. type: "station"
  2158. },
  2159. (err, playlist2) => {
  2160. if (err) next(err);
  2161. else {
  2162. stationModel.create(
  2163. {
  2164. _id: stationId,
  2165. name,
  2166. displayName,
  2167. description,
  2168. type,
  2169. privacy: "private",
  2170. playlist2: playlist2._id,
  2171. playlist,
  2172. genres,
  2173. blacklistedGenres,
  2174. currentSong: StationsModule.defaultSong
  2175. },
  2176. next
  2177. );
  2178. }
  2179. }
  2180. );
  2181. });
  2182. }
  2183. if (type === "community") {
  2184. if (blacklist.indexOf(name) !== -1)
  2185. return next("That name is blacklisted. Please use a different name.");
  2186. return playlistModel.create(
  2187. {
  2188. isUserModifiable: false,
  2189. displayName: `Station - ${name}`,
  2190. songs: [],
  2191. createdBy: session.userId,
  2192. createdFor: `${stationId}`,
  2193. createdAt: Date.now(),
  2194. type: "station"
  2195. },
  2196. (err, playlist2) => {
  2197. if (err) next(err);
  2198. else {
  2199. stationModel.create(
  2200. {
  2201. name,
  2202. displayName,
  2203. description,
  2204. playlist2: playlist2._id,
  2205. type,
  2206. privacy: "private",
  2207. owner: session.userId,
  2208. queue: [],
  2209. currentSong: null
  2210. },
  2211. next
  2212. );
  2213. }
  2214. }
  2215. );
  2216. }
  2217. }
  2218. ],
  2219. async (err, station) => {
  2220. if (err) {
  2221. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2222. this.log("ERROR", "STATIONS_CREATE", `Creating station failed. "${err}"`);
  2223. return cb({ status: "failure", message: err });
  2224. }
  2225. this.log("SUCCESS", "STATIONS_CREATE", `Created station "${station._id}" successfully.`);
  2226. CacheModule.runJob("PUB", {
  2227. channel: "station.create",
  2228. value: station._id
  2229. });
  2230. ActivitiesModule.runJob("ADD_ACTIVITY", {
  2231. userId: session.userId,
  2232. type: "station__create",
  2233. payload: {
  2234. message: `Created a station named <stationId>${station.displayName}</stationId>`,
  2235. stationId: station._id
  2236. }
  2237. });
  2238. return cb({
  2239. status: "success",
  2240. message: "Successfully created station."
  2241. });
  2242. }
  2243. );
  2244. }),
  2245. /**
  2246. * Adds song to station queue
  2247. *
  2248. * @param session
  2249. * @param stationId - the station id
  2250. * @param songId - the song id
  2251. * @param cb
  2252. */
  2253. addToQueue: isLoginRequired(async function addToQueue(session, stationId, songId, cb) {
  2254. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  2255. const playlistModel = await DBModule.runJob(
  2256. "GET_MODEL",
  2257. {
  2258. modelName: "playlist"
  2259. },
  2260. this
  2261. );
  2262. async.waterfall(
  2263. [
  2264. next => {
  2265. StationsModule.runJob("GET_STATION", { stationId }, this)
  2266. .then(station => {
  2267. next(null, station);
  2268. })
  2269. .catch(next);
  2270. },
  2271. (station, next) => {
  2272. if (!station) return next("Station not found.");
  2273. if (!station.partyMode) return next("Station is not in party mode.");
  2274. if (station.locked) {
  2275. return userModel.findOne({ _id: session.userId }, (err, user) => {
  2276. if (user.role !== "admin" && station.owner !== session.userId)
  2277. return next("Only owners and admins can add songs to a locked queue.");
  2278. return next(null, station);
  2279. });
  2280. }
  2281. return next(null, station);
  2282. },
  2283. (station, next) => {
  2284. if (station.type !== "community") return next("That station is not a community station.");
  2285. return StationsModule.runJob(
  2286. "CAN_USER_VIEW_STATION",
  2287. {
  2288. station,
  2289. userId: session.userId
  2290. },
  2291. this
  2292. )
  2293. .then(canView => {
  2294. if (canView) return next(null, station);
  2295. return next("Insufficient permissions.");
  2296. })
  2297. .catch(err => next(err));
  2298. },
  2299. (station, next) => {
  2300. if (station.currentSong && station.currentSong.songId === songId)
  2301. return next("That song is currently playing.");
  2302. return playlistModel.findOne({ _id: station.playlist2 }, (err, playlist) => {
  2303. console.log(111, station, err, playlist);
  2304. next(err, station, playlist);
  2305. });
  2306. },
  2307. (station, playlist, next) => {
  2308. async.each(
  2309. playlist.songs,
  2310. (song, next) => {
  2311. if (song.songId === songId) return next("That song is already in the queue.");
  2312. return next();
  2313. },
  2314. err => next(err, station, playlist)
  2315. );
  2316. },
  2317. (station, playlist, next) => {
  2318. SongsModule.runJob("GET_SONG_FROM_ID", { songId }, this)
  2319. .then(res => {
  2320. if (res.song) return next(null, res.song, station, playlist);
  2321. return YouTubeModule.runJob("GET_SONG", { songId }, this)
  2322. .then(response => {
  2323. const { song } = response;
  2324. song.artists = [];
  2325. song.skipDuration = 0;
  2326. song.likes = -1;
  2327. song.dislikes = -1;
  2328. song.thumbnail = "empty";
  2329. song.explicit = false;
  2330. return next(null, song, station, playlist);
  2331. })
  2332. .catch(err => {
  2333. console.log(11111, err);
  2334. next(err);
  2335. });
  2336. })
  2337. .catch(err => {
  2338. console.log(11122, err);
  2339. next(err);
  2340. });
  2341. },
  2342. (song, station, playlist, next) => {
  2343. song.requestedBy = session.userId;
  2344. song.requestedAt = Date.now();
  2345. let totalDuration = 0;
  2346. playlist.songs.forEach(song => {
  2347. totalDuration += song.duration;
  2348. });
  2349. if (totalDuration >= 3600 * 3) return next("The max length of the queue is 3 hours.");
  2350. return next(null, song, station, playlist);
  2351. },
  2352. (song, station, playlist, next) => {
  2353. console.log(333, song, station, playlist);
  2354. if (playlist.songs.length === 0) return next(null, song, station, playlist);
  2355. let totalDuration = 0;
  2356. const userId = playlist.songs[playlist.songs.length - 1].requestedBy;
  2357. playlist.songs.forEach(song => {
  2358. if (userId === song.requestedBy) {
  2359. totalDuration += song.duration;
  2360. }
  2361. });
  2362. if (totalDuration >= 900) return next("The max length of songs per user is 15 minutes.");
  2363. return next(null, song, station, playlist);
  2364. },
  2365. (song, station, playlist, next) => {
  2366. console.log(444, song, station, playlist);
  2367. if (playlist.songs.length === 0) return next(null, song, station);
  2368. let totalSongs = 0;
  2369. const userId = playlist.songs[playlist.songs.length - 1].requestedBy;
  2370. playlist.songs.forEach(song => {
  2371. if (userId === song.requestedBy) {
  2372. totalSongs += 1;
  2373. }
  2374. });
  2375. if (totalSongs <= 2) return next(null, song, station);
  2376. if (totalSongs > 3)
  2377. return next("The max amount of songs per user is 3, and only 2 in a row is allowed.");
  2378. if (
  2379. playlist.songs[playlist.songs.length - 2].requestedBy !== userId ||
  2380. playlist.songs[playlist.songs.length - 3] !== userId
  2381. )
  2382. return next("The max amount of songs per user is 3, and only 2 in a row is allowed.");
  2383. return next(null, song, station);
  2384. },
  2385. (song, station, next) => {
  2386. playlistModel.updateOne(
  2387. { _id: station.playlist2 },
  2388. { $push: { songs: song } },
  2389. { runValidators: true },
  2390. next
  2391. );
  2392. }
  2393. // (res, next) => {
  2394. // StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  2395. // .then(station => next(null, station))
  2396. // .catch(next);
  2397. // }
  2398. // (res, next) => {
  2399. // StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  2400. // .then(station => {
  2401. // next(null, station);
  2402. // })
  2403. // .catch(next);
  2404. // }
  2405. ],
  2406. async err => {
  2407. if (err) {
  2408. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2409. this.log(
  2410. "ERROR",
  2411. "STATIONS_ADD_SONG_TO_QUEUE",
  2412. `Adding song "${songId}" to station "${stationId}" queue failed. "${err}"`
  2413. );
  2414. return cb({ status: "failure", message: err });
  2415. }
  2416. this.log(
  2417. "SUCCESS",
  2418. "STATIONS_ADD_SONG_TO_QUEUE",
  2419. `Added song "${songId}" to station "${stationId}" successfully.`
  2420. );
  2421. CacheModule.runJob("PUB", {
  2422. channel: "station.queueUpdate",
  2423. value: stationId
  2424. });
  2425. return cb({
  2426. status: "success",
  2427. message: "Successfully added song to queue."
  2428. });
  2429. }
  2430. );
  2431. }),
  2432. /**
  2433. * Removes song from station queue
  2434. *
  2435. * @param session
  2436. * @param stationId - the station id
  2437. * @param songId - the song id
  2438. * @param cb
  2439. */
  2440. removeFromQueue: isOwnerRequired(async function removeFromQueue(session, stationId, songId, cb) {
  2441. const stationModel = await DBModule.runJob("GET_MODEL", { modelName: "station" }, this);
  2442. async.waterfall(
  2443. [
  2444. next => {
  2445. if (!songId) return next("Invalid song id.");
  2446. return StationsModule.runJob("GET_STATION", { stationId }, this)
  2447. .then(station => next(null, station))
  2448. .catch(next);
  2449. },
  2450. (station, next) => {
  2451. if (!station) return next("Station not found.");
  2452. if (station.type !== "community") return next("Station is not a community station.");
  2453. return async.each(
  2454. station.queue,
  2455. (queueSong, next) => {
  2456. if (queueSong.songId === songId) return next(true);
  2457. return next();
  2458. },
  2459. err => {
  2460. if (err === true) return next();
  2461. return next("Song is not currently in the queue.");
  2462. }
  2463. );
  2464. },
  2465. next => {
  2466. stationModel.updateOne({ _id: stationId }, { $pull: { queue: { songId } } }, next);
  2467. },
  2468. (res, next) => {
  2469. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  2470. .then(station => next(null, station))
  2471. .catch(next);
  2472. }
  2473. ],
  2474. async err => {
  2475. if (err) {
  2476. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2477. this.log(
  2478. "ERROR",
  2479. "STATIONS_REMOVE_SONG_TO_QUEUE",
  2480. `Removing song "${songId}" from station "${stationId}" queue failed. "${err}"`
  2481. );
  2482. return cb({ status: "failure", message: err });
  2483. }
  2484. this.log(
  2485. "SUCCESS",
  2486. "STATIONS_REMOVE_SONG_TO_QUEUE",
  2487. `Removed song "${songId}" from station "${stationId}" successfully.`
  2488. );
  2489. CacheModule.runJob("PUB", {
  2490. channel: "station.queueUpdate",
  2491. value: stationId
  2492. });
  2493. return cb({
  2494. status: "success",
  2495. message: "Successfully removed song from queue."
  2496. });
  2497. }
  2498. );
  2499. }),
  2500. /**
  2501. * Gets the queue from a station
  2502. *
  2503. * @param {object} session - user session
  2504. * @param {string} stationId - the station id
  2505. * @param {Function} cb - callback
  2506. */
  2507. getQueue(session, stationId, cb) {
  2508. async.waterfall(
  2509. [
  2510. next => {
  2511. StationsModule.runJob("GET_STATION", { stationId }, this)
  2512. .then(station => next(null, station))
  2513. .catch(next);
  2514. },
  2515. (station, next) => {
  2516. if (!station) return next("Station not found.");
  2517. if (station.type !== "community") return next("Station is not a community station.");
  2518. return next(null, station);
  2519. },
  2520. (station, next) => {
  2521. StationsModule.runJob("CAN_USER_VIEW_STATION", { station, userId: session.userId }, this)
  2522. .then(canView => {
  2523. if (canView) return next(null, station);
  2524. return next("Insufficient permissions.");
  2525. })
  2526. .catch(err => next(err));
  2527. }
  2528. ],
  2529. async (err, station) => {
  2530. if (err) {
  2531. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2532. this.log(
  2533. "ERROR",
  2534. "STATIONS_GET_QUEUE",
  2535. `Getting queue for station "${stationId}" failed. "${err}"`
  2536. );
  2537. return cb({ status: "failure", message: err });
  2538. }
  2539. this.log("SUCCESS", "STATIONS_GET_QUEUE", `Got queue for station "${stationId}" successfully.`);
  2540. return cb({
  2541. status: "success",
  2542. message: "Successfully got queue.",
  2543. queue: station.queue
  2544. });
  2545. }
  2546. );
  2547. },
  2548. /**
  2549. * Selects a private playlist for a station
  2550. *
  2551. * @param session
  2552. * @param stationId - the station id
  2553. * @param playlistId - the private playlist id
  2554. * @param cb
  2555. */
  2556. selectPrivatePlaylist: isOwnerRequired(async function selectPrivatePlaylist(session, stationId, playlistId, cb) {
  2557. const stationModel = await DBModule.runJob("GET_MODEL", { modelName: "station" }, this);
  2558. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  2559. async.waterfall(
  2560. [
  2561. next => {
  2562. StationsModule.runJob("GET_STATION", { stationId }, this)
  2563. .then(station => next(null, station))
  2564. .catch(next);
  2565. },
  2566. (station, next) => {
  2567. if (!station) return next("Station not found.");
  2568. if (station.type !== "community") return next("Station is not a community station.");
  2569. if (station.privatePlaylist === playlistId)
  2570. return next("That private playlist is already selected.");
  2571. return playlistModel.findOne({ _id: playlistId }, next);
  2572. },
  2573. (playlist, next) => {
  2574. if (!playlist) return next("Playlist not found.");
  2575. const currentSongIndex = playlist.songs.length > 0 ? playlist.songs.length - 1 : 0;
  2576. return stationModel.updateOne(
  2577. { _id: stationId },
  2578. {
  2579. $set: {
  2580. privatePlaylist: playlistId,
  2581. currentSongIndex
  2582. }
  2583. },
  2584. { runValidators: true },
  2585. next
  2586. );
  2587. },
  2588. (res, next) => {
  2589. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  2590. .then(station => next(null, station))
  2591. .catch(next);
  2592. }
  2593. ],
  2594. async (err, station) => {
  2595. if (err) {
  2596. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2597. this.log(
  2598. "ERROR",
  2599. "STATIONS_SELECT_PRIVATE_PLAYLIST",
  2600. `Selecting private playlist "${playlistId}" for station "${stationId}" failed. "${err}"`
  2601. );
  2602. return cb({ status: "failure", message: err });
  2603. }
  2604. this.log(
  2605. "SUCCESS",
  2606. "STATIONS_SELECT_PRIVATE_PLAYLIST",
  2607. `Selected private playlist "${playlistId}" for station "${stationId}" successfully.`
  2608. );
  2609. NotificationsModule.runJob("UNSCHEDULE", {
  2610. name: `stations.nextSong?id${stationId}`
  2611. });
  2612. if (!station.partyMode) StationsModule.runJob("SKIP_STATION", { stationId });
  2613. CacheModule.runJob("PUB", {
  2614. channel: "privatePlaylist.selected",
  2615. value: {
  2616. playlistId,
  2617. stationId
  2618. }
  2619. });
  2620. return cb({
  2621. status: "success",
  2622. message: "Successfully selected playlist."
  2623. });
  2624. }
  2625. );
  2626. }),
  2627. /**
  2628. * Deselects the private playlist selected in a station
  2629. *
  2630. * @param session
  2631. * @param stationId - the station id
  2632. * @param cb
  2633. */
  2634. deselectPrivatePlaylist: isOwnerRequired(async function deselectPrivatePlaylist(session, stationId, cb) {
  2635. const stationModel = await DBModule.runJob(
  2636. "GET_MODEL",
  2637. {
  2638. modelName: "station"
  2639. },
  2640. this
  2641. );
  2642. async.waterfall(
  2643. [
  2644. next => {
  2645. StationsModule.runJob("GET_STATION", { stationId }, this)
  2646. .then(station => {
  2647. next(null, station);
  2648. })
  2649. .catch(next);
  2650. },
  2651. (station, next) => {
  2652. if (!station) return next("Station not found.");
  2653. if (station.type !== "community") return next("Station is not a community station.");
  2654. if (!station.privatePlaylist) return next("No private playlist is currently selected.");
  2655. return stationModel.updateOne(
  2656. { _id: stationId },
  2657. {
  2658. $set: {
  2659. privatePlaylist: null,
  2660. currentSongIndex: 0
  2661. }
  2662. },
  2663. { runValidators: true },
  2664. next
  2665. );
  2666. },
  2667. (res, next) => {
  2668. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  2669. .then(station => {
  2670. next(null, station);
  2671. })
  2672. .catch(next);
  2673. }
  2674. ],
  2675. async (err, station) => {
  2676. if (err) {
  2677. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2678. this.log(
  2679. "ERROR",
  2680. "STATIONS_DESELECT_PRIVATE_PLAYLIST",
  2681. `Deselecting private playlist for station "${stationId}" failed. "${err}"`
  2682. );
  2683. return cb({ status: "failure", message: err });
  2684. }
  2685. this.log(
  2686. "SUCCESS",
  2687. "STATIONS_DESELECT_PRIVATE_PLAYLIST",
  2688. `Deselected private playlist for station "${stationId}" successfully.`
  2689. );
  2690. NotificationsModule.runJob("UNSCHEDULE", {
  2691. name: `stations.nextSong?id${stationId}`
  2692. });
  2693. if (!station.partyMode) StationsModule.runJob("SKIP_STATION", { stationId });
  2694. CacheModule.runJob("PUB", {
  2695. channel: "privatePlaylist.deselected",
  2696. value: {
  2697. stationId
  2698. }
  2699. });
  2700. return cb({
  2701. status: "success",
  2702. message: "Successfully deselected playlist."
  2703. });
  2704. }
  2705. );
  2706. }),
  2707. favoriteStation: isLoginRequired(async function favoriteStation(session, stationId, cb) {
  2708. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  2709. async.waterfall(
  2710. [
  2711. next => {
  2712. StationsModule.runJob("GET_STATION", { stationId }, this)
  2713. .then(station => next(null, station))
  2714. .catch(next);
  2715. },
  2716. (station, next) => {
  2717. if (!station) return next("Station not found.");
  2718. return StationsModule.runJob("CAN_USER_VIEW_STATION", { station, userId: session.userId }, this)
  2719. .then(canView => {
  2720. if (canView) return next(null, station);
  2721. return next("Insufficient permissions.");
  2722. })
  2723. .catch(err => next(err));
  2724. },
  2725. (station, next) => {
  2726. userModel.updateOne(
  2727. { _id: session.userId },
  2728. { $addToSet: { favoriteStations: stationId } },
  2729. (err, res) => next(err, station, res)
  2730. );
  2731. },
  2732. (station, res, next) => {
  2733. if (res.nModified === 0) return next("The station was already favorited.");
  2734. return next(null, station);
  2735. }
  2736. ],
  2737. async (err, station) => {
  2738. if (err) {
  2739. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2740. this.log("ERROR", "FAVORITE_STATION", `Favoriting station "${stationId}" failed. "${err}"`);
  2741. return cb({ status: "failure", message: err });
  2742. }
  2743. this.log("SUCCESS", "FAVORITE_STATION", `Favorited station "${stationId}" successfully.`);
  2744. CacheModule.runJob("PUB", {
  2745. channel: "user.favoritedStation",
  2746. value: {
  2747. userId: session.userId,
  2748. stationId
  2749. }
  2750. });
  2751. ActivitiesModule.runJob("ADD_ACTIVITY", {
  2752. userId: session.userId,
  2753. type: "station__favorite",
  2754. payload: {
  2755. message: `Favorited station <stationId>${station.displayName}</stationId>`,
  2756. stationId
  2757. }
  2758. });
  2759. return cb({
  2760. status: "success",
  2761. message: "Succesfully favorited station."
  2762. });
  2763. }
  2764. );
  2765. }),
  2766. unfavoriteStation: isLoginRequired(async function unfavoriteStation(session, stationId, cb) {
  2767. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  2768. async.waterfall(
  2769. [
  2770. next => {
  2771. userModel.updateOne({ _id: session.userId }, { $pull: { favoriteStations: stationId } }, next);
  2772. },
  2773. (res, next) => {
  2774. if (res.nModified === 0) return next("The station wasn't favorited.");
  2775. return next();
  2776. },
  2777. next => {
  2778. StationsModule.runJob("GET_STATION", { stationId }, this)
  2779. .then(station => next(null, station))
  2780. .catch(next);
  2781. }
  2782. ],
  2783. async (err, station) => {
  2784. if (err) {
  2785. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2786. this.log("ERROR", "UNFAVORITE_STATION", `Unfavoriting station "${stationId}" failed. "${err}"`);
  2787. return cb({ status: "failure", message: err });
  2788. }
  2789. this.log("SUCCESS", "UNFAVORITE_STATION", `Unfavorited station "${stationId}" successfully.`);
  2790. CacheModule.runJob("PUB", {
  2791. channel: "user.unfavoritedStation",
  2792. value: {
  2793. userId: session.userId,
  2794. stationId
  2795. }
  2796. });
  2797. ActivitiesModule.runJob("ADD_ACTIVITY", {
  2798. userId: session.userId,
  2799. type: "station__unfavorite",
  2800. payload: {
  2801. message: `Unfavorited station <stationId>${station.displayName}</stationId>`,
  2802. stationId
  2803. }
  2804. });
  2805. return cb({
  2806. status: "success",
  2807. message: "Succesfully unfavorited station."
  2808. });
  2809. }
  2810. );
  2811. })
  2812. };