stations.js 64 KB

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