stations.js 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661
  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("GET_MODEL", { modelName: "station" }, this);
  1045. async.waterfall(
  1046. [
  1047. next => {
  1048. stationModel.updateOne(
  1049. { _id: stationId },
  1050. { $set: { name: newName } },
  1051. { runValidators: true },
  1052. next
  1053. );
  1054. },
  1055. (res, next) => {
  1056. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  1057. .then(station => next(null, station))
  1058. .catch(next);
  1059. }
  1060. ],
  1061. async (err, station) => {
  1062. if (err) {
  1063. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1064. this.log(
  1065. "ERROR",
  1066. "STATIONS_UPDATE_NAME",
  1067. `Updating station "${stationId}" name to "${newName}" failed. "${err}"`
  1068. );
  1069. return cb({ status: "failure", message: err });
  1070. }
  1071. this.log(
  1072. "SUCCESS",
  1073. "STATIONS_UPDATE_NAME",
  1074. `Updated station "${stationId}" name to "${newName}" successfully.`
  1075. );
  1076. CacheModule.runJob("PUB", {
  1077. channel: "station.nameUpdate",
  1078. value: { stationId, name: newName }
  1079. });
  1080. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1081. userId: session.userId,
  1082. type: "station__edit_name",
  1083. payload: {
  1084. message: `Changed name of station <stationId>${station.displayName}</stationId> to ${newName}`,
  1085. stationId
  1086. }
  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("GET_MODEL", { modelName: "station" }, this);
  1105. async.waterfall(
  1106. [
  1107. next => {
  1108. stationModel.updateOne(
  1109. { _id: stationId },
  1110. { $set: { displayName: newDisplayName } },
  1111. { runValidators: true },
  1112. next
  1113. );
  1114. },
  1115. (res, next) => {
  1116. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  1117. .then(station => next(null, station))
  1118. .catch(next);
  1119. }
  1120. ],
  1121. async err => {
  1122. if (err) {
  1123. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1124. this.log(
  1125. "ERROR",
  1126. "STATIONS_UPDATE_DISPLAY_NAME",
  1127. `Updating station "${stationId}" displayName to "${newDisplayName}" failed. "${err}"`
  1128. );
  1129. return cb({ status: "failure", message: err });
  1130. }
  1131. this.log(
  1132. "SUCCESS",
  1133. "STATIONS_UPDATE_DISPLAY_NAME",
  1134. `Updated station "${stationId}" displayName to "${newDisplayName}" successfully.`
  1135. );
  1136. CacheModule.runJob("PUB", {
  1137. channel: "station.displayNameUpdate",
  1138. value: { stationId, displayName: newDisplayName }
  1139. });
  1140. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1141. userId: session.userId,
  1142. type: "station__edit_display_name",
  1143. payload: {
  1144. message: `Changed display name of station <stationId>${newDisplayName}</stationId>`,
  1145. stationId
  1146. }
  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("GET_MODEL", { modelName: "station" }, this);
  1165. async.waterfall(
  1166. [
  1167. next => {
  1168. stationModel.updateOne(
  1169. { _id: stationId },
  1170. { $set: { description: newDescription } },
  1171. { runValidators: true },
  1172. next
  1173. );
  1174. },
  1175. (res, next) => {
  1176. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  1177. .then(station => next(null, station))
  1178. .catch(next);
  1179. }
  1180. ],
  1181. async (err, station) => {
  1182. if (err) {
  1183. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1184. this.log(
  1185. "ERROR",
  1186. "STATIONS_UPDATE_DESCRIPTION",
  1187. `Updating station "${stationId}" description to "${newDescription}" failed. "${err}"`
  1188. );
  1189. return cb({ status: "failure", message: err });
  1190. }
  1191. this.log(
  1192. "SUCCESS",
  1193. "STATIONS_UPDATE_DESCRIPTION",
  1194. `Updated station "${stationId}" description to "${newDescription}" successfully.`
  1195. );
  1196. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1197. userId: session.userId,
  1198. type: "station__edit_description",
  1199. payload: {
  1200. message: `Changed description of station <stationId>${station.displayName}</stationId> to ${newDescription}`,
  1201. stationId
  1202. }
  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("GET_MODEL", { modelName: "station" }, this);
  1225. let previousPrivacy = null;
  1226. async.waterfall(
  1227. [
  1228. next => {
  1229. stationModel.findOne({ _id: stationId }, next);
  1230. },
  1231. (station, next) => {
  1232. if (!station) next("No station found.");
  1233. else {
  1234. previousPrivacy = station.privacy;
  1235. next();
  1236. }
  1237. },
  1238. next => {
  1239. stationModel.updateOne(
  1240. { _id: stationId },
  1241. { $set: { privacy: newPrivacy } },
  1242. { runValidators: true },
  1243. next
  1244. );
  1245. },
  1246. (res, next) => {
  1247. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  1248. .then(station => next(null, station))
  1249. .catch(next);
  1250. }
  1251. ],
  1252. async (err, station) => {
  1253. if (err) {
  1254. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1255. this.log(
  1256. "ERROR",
  1257. "STATIONS_UPDATE_PRIVACY",
  1258. `Updating station "${stationId}" privacy to "${newPrivacy}" failed. "${err}"`
  1259. );
  1260. return cb({ status: "failure", message: err });
  1261. }
  1262. this.log(
  1263. "SUCCESS",
  1264. "STATIONS_UPDATE_PRIVACY",
  1265. `Updated station "${stationId}" privacy to "${newPrivacy}" successfully.`
  1266. );
  1267. CacheModule.runJob("PUB", {
  1268. channel: "station.privacyUpdate",
  1269. value: { stationId, previousPrivacy }
  1270. });
  1271. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1272. userId: session.userId,
  1273. type: "station__edit_privacy",
  1274. payload: {
  1275. message: `Changed privacy of station <stationId>${station.displayName}</stationId> to ${newPrivacy}`,
  1276. stationId
  1277. }
  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 => next(null, station))
  1315. .catch(next);
  1316. }
  1317. ],
  1318. async (err, station) => {
  1319. if (err) {
  1320. err = await UtilsModule.runJob("GET_ERROR", { error: err });
  1321. this.log(
  1322. "ERROR",
  1323. "STATIONS_UPDATE_GENRES",
  1324. `Updating station "${stationId}" genres to "${newGenres}" failed. "${err}"`
  1325. );
  1326. return cb({ status: "failure", message: err });
  1327. }
  1328. this.log(
  1329. "SUCCESS",
  1330. "STATIONS_UPDATE_GENRES",
  1331. `Updated station "${stationId}" genres to "${newGenres}" successfully.`
  1332. );
  1333. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1334. userId: session.userId,
  1335. type: "station__edit_genres",
  1336. payload: {
  1337. message: `Updated genres of station <stationId>${station.displayName}</stationId> to
  1338. ${newGenres.join(", ")}`,
  1339. stationId
  1340. }
  1341. });
  1342. return cb({
  1343. status: "success",
  1344. message: "Successfully updated the genres."
  1345. });
  1346. }
  1347. );
  1348. }),
  1349. /**
  1350. * Updates a station's blacklisted genres
  1351. *
  1352. * @param session
  1353. * @param stationId - the station id
  1354. * @param newBlacklistedGenres - the new station blacklisted genres
  1355. * @param cb
  1356. */
  1357. updateBlacklistedGenres: isOwnerRequired(async function updateBlacklistedGenres(
  1358. session,
  1359. stationId,
  1360. newBlacklistedGenres,
  1361. cb
  1362. ) {
  1363. const stationModel = await DBModule.runJob("GET_MODEL", { modelName: "station" }, this);
  1364. async.waterfall(
  1365. [
  1366. next => {
  1367. stationModel.updateOne(
  1368. { _id: stationId },
  1369. {
  1370. $set: {
  1371. blacklistedGenres: newBlacklistedGenres
  1372. }
  1373. },
  1374. { runValidators: true },
  1375. next
  1376. );
  1377. },
  1378. (res, next) => {
  1379. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  1380. .then(station => next(null, station))
  1381. .catch(next);
  1382. }
  1383. ],
  1384. async (err, station) => {
  1385. if (err) {
  1386. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1387. this.log(
  1388. "ERROR",
  1389. "STATIONS_UPDATE_BLACKLISTED_GENRES",
  1390. `Updating station "${stationId}" blacklisted genres to "${newBlacklistedGenres}" failed. "${err}"`
  1391. );
  1392. return cb({ status: "failure", message: err });
  1393. }
  1394. this.log(
  1395. "SUCCESS",
  1396. "STATIONS_UPDATE_BLACKLISTED_GENRES",
  1397. `Updated station "${stationId}" blacklisted genres to "${newBlacklistedGenres}" successfully.`
  1398. );
  1399. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1400. userId: session.userId,
  1401. type: "station__edit_blacklisted_genres",
  1402. payload: {
  1403. message: `Updated blacklisted genres of station <stationId>${
  1404. station.displayName
  1405. }</stationId> to ${newBlacklistedGenres.join(", ")}`,
  1406. stationId
  1407. }
  1408. });
  1409. return cb({
  1410. status: "success",
  1411. message: "Successfully updated the blacklisted genres."
  1412. });
  1413. }
  1414. );
  1415. }),
  1416. /**
  1417. * Updates a station's party mode
  1418. *
  1419. * @param session
  1420. * @param stationId - the station id
  1421. * @param newPartyMode - the new station party mode
  1422. * @param cb
  1423. */
  1424. updatePartyMode: isOwnerRequired(async function updatePartyMode(session, stationId, newPartyMode, cb) {
  1425. const stationModel = await DBModule.runJob("GET_MODEL", { modelName: "station" }, this);
  1426. async.waterfall(
  1427. [
  1428. next => {
  1429. StationsModule.runJob("GET_STATION", { stationId }, this)
  1430. .then(station => {
  1431. next(null, station);
  1432. })
  1433. .catch(next);
  1434. },
  1435. (station, next) => {
  1436. if (!station) return next("Station not found.");
  1437. if (station.partyMode === newPartyMode)
  1438. return next(`The party mode was already ${newPartyMode ? "enabled." : "disabled."}`);
  1439. return stationModel.updateOne(
  1440. { _id: stationId },
  1441. { $set: { partyMode: newPartyMode } },
  1442. { runValidators: true },
  1443. next
  1444. );
  1445. },
  1446. (res, next) => {
  1447. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  1448. .then(station => {
  1449. next(null, station);
  1450. })
  1451. .catch(next);
  1452. }
  1453. ],
  1454. async err => {
  1455. if (err) {
  1456. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1457. this.log(
  1458. "ERROR",
  1459. "STATIONS_UPDATE_PARTY_MODE",
  1460. `Updating station "${stationId}" party mode to "${newPartyMode}" failed. "${err}"`
  1461. );
  1462. return cb({ status: "failure", message: err });
  1463. }
  1464. this.log(
  1465. "SUCCESS",
  1466. "STATIONS_UPDATE_PARTY_MODE",
  1467. `Updated station "${stationId}" party mode to "${newPartyMode}" successfully.`
  1468. );
  1469. CacheModule.runJob("PUB", {
  1470. channel: "station.updatePartyMode",
  1471. value: {
  1472. stationId,
  1473. partyMode: newPartyMode
  1474. }
  1475. });
  1476. StationsModule.runJob("SKIP_STATION", { stationId });
  1477. return cb({
  1478. status: "success",
  1479. message: "Successfully updated the party mode."
  1480. });
  1481. }
  1482. );
  1483. }),
  1484. /**
  1485. * Updates a station's theme
  1486. *
  1487. * @param session
  1488. * @param stationId - the station id
  1489. * @param newTheme - the new station theme
  1490. * @param cb
  1491. */
  1492. updateTheme: isOwnerRequired(async function updateTheme(session, stationId, newTheme, cb) {
  1493. const stationModel = await DBModule.runJob("GET_MODEL", { modelName: "station" }, this);
  1494. async.waterfall(
  1495. [
  1496. next => {
  1497. StationsModule.runJob("GET_STATION", { stationId }, this)
  1498. .then(station => {
  1499. next(null, station);
  1500. })
  1501. .catch(next);
  1502. },
  1503. (station, next) => {
  1504. if (!station) return next("Station not found.");
  1505. if (station.theme === newTheme) return next("No change in theme.");
  1506. return stationModel.updateOne(
  1507. { _id: stationId },
  1508. { $set: { theme: newTheme } },
  1509. { runValidators: true },
  1510. next
  1511. );
  1512. },
  1513. (res, next) => {
  1514. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  1515. .then(station => next(null, station))
  1516. .catch(next);
  1517. }
  1518. ],
  1519. async (err, station) => {
  1520. if (err) {
  1521. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1522. this.log(
  1523. "ERROR",
  1524. "STATIONS_UPDATE_THEME",
  1525. `Updating station "${stationId}" theme to "${newTheme}" failed. "${err}"`
  1526. );
  1527. return cb({ status: "failure", message: err });
  1528. }
  1529. this.log(
  1530. "SUCCESS",
  1531. "STATIONS_UPDATE_THEME",
  1532. `Updated station "${stationId}" theme to "${newTheme}" successfully.`
  1533. );
  1534. CacheModule.runJob("PUB", {
  1535. channel: "station.themeUpdate",
  1536. value: { stationId }
  1537. });
  1538. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1539. userId: session.userId,
  1540. type: "station__edit_theme",
  1541. payload: {
  1542. message: `Changed theme of station <stationId>${station.displayName}</stationId> to ${newTheme}`,
  1543. stationId
  1544. }
  1545. });
  1546. return cb({
  1547. status: "success",
  1548. message: "Successfully updated the theme."
  1549. });
  1550. }
  1551. );
  1552. }),
  1553. /**
  1554. * Pauses a station
  1555. *
  1556. * @param session
  1557. * @param stationId - the station id
  1558. * @param cb
  1559. */
  1560. pause: isOwnerRequired(async function pause(session, stationId, cb) {
  1561. const stationModel = await DBModule.runJob(
  1562. "GET_MODEL",
  1563. {
  1564. modelName: "station"
  1565. },
  1566. this
  1567. );
  1568. async.waterfall(
  1569. [
  1570. next => {
  1571. StationsModule.runJob("GET_STATION", { stationId }, this)
  1572. .then(station => {
  1573. next(null, station);
  1574. })
  1575. .catch(next);
  1576. },
  1577. (station, next) => {
  1578. if (!station) return next("Station not found.");
  1579. if (station.paused) return next("That station was already paused.");
  1580. return stationModel.updateOne(
  1581. { _id: stationId },
  1582. { $set: { paused: true, pausedAt: Date.now() } },
  1583. next
  1584. );
  1585. },
  1586. (res, next) => {
  1587. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  1588. .then(station => {
  1589. next(null, station);
  1590. })
  1591. .catch(next);
  1592. }
  1593. ],
  1594. async err => {
  1595. if (err) {
  1596. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1597. this.log("ERROR", "STATIONS_PAUSE", `Pausing station "${stationId}" failed. "${err}"`);
  1598. return cb({ status: "failure", message: err });
  1599. }
  1600. this.log("SUCCESS", "STATIONS_PAUSE", `Paused station "${stationId}" successfully.`);
  1601. CacheModule.runJob("PUB", {
  1602. channel: "station.pause",
  1603. value: stationId
  1604. });
  1605. NotificationsModule.runJob("UNSCHEDULE", {
  1606. name: `stations.nextSong?id=${stationId}`
  1607. });
  1608. return cb({
  1609. status: "success",
  1610. message: "Successfully paused."
  1611. });
  1612. }
  1613. );
  1614. }),
  1615. /**
  1616. * Resumes a station
  1617. *
  1618. * @param session
  1619. * @param stationId - the station id
  1620. * @param cb
  1621. */
  1622. resume: isOwnerRequired(async function resume(session, stationId, cb) {
  1623. const stationModel = await DBModule.runJob(
  1624. "GET_MODEL",
  1625. {
  1626. modelName: "station"
  1627. },
  1628. this
  1629. );
  1630. async.waterfall(
  1631. [
  1632. next => {
  1633. StationsModule.runJob("GET_STATION", { stationId }, this)
  1634. .then(station => {
  1635. next(null, station);
  1636. })
  1637. .catch(next);
  1638. },
  1639. (station, next) => {
  1640. if (!station) return next("Station not found.");
  1641. if (!station.paused) return next("That station is not paused.");
  1642. station.timePaused += Date.now() - station.pausedAt;
  1643. return stationModel.updateOne(
  1644. { _id: stationId },
  1645. {
  1646. $set: { paused: false },
  1647. $inc: { timePaused: Date.now() - station.pausedAt }
  1648. },
  1649. next
  1650. );
  1651. },
  1652. (res, next) => {
  1653. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  1654. .then(station => {
  1655. next(null, station);
  1656. })
  1657. .catch(next);
  1658. }
  1659. ],
  1660. async err => {
  1661. if (err) {
  1662. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1663. this.log("ERROR", "STATIONS_RESUME", `Resuming station "${stationId}" failed. "${err}"`);
  1664. return cb({ status: "failure", message: err });
  1665. }
  1666. this.log("SUCCESS", "STATIONS_RESUME", `Resuming station "${stationId}" successfully.`);
  1667. CacheModule.runJob("PUB", {
  1668. channel: "station.resume",
  1669. value: stationId
  1670. });
  1671. return cb({
  1672. status: "success",
  1673. message: "Successfully resumed."
  1674. });
  1675. }
  1676. );
  1677. }),
  1678. /**
  1679. * Removes a station
  1680. *
  1681. * @param session
  1682. * @param stationId - the station id
  1683. * @param cb
  1684. */
  1685. remove: isOwnerRequired(async function remove(session, stationId, cb) {
  1686. const stationModel = await DBModule.runJob("GET_MODEL", { modelName: "station" }, this);
  1687. async.waterfall(
  1688. [
  1689. next => {
  1690. stationModel.findById(stationId, (err, station) => {
  1691. if (err) return next(err);
  1692. return next(null, station);
  1693. });
  1694. },
  1695. (station, next) => {
  1696. stationModel.deleteOne({ _id: stationId }, err => next(err, station));
  1697. },
  1698. (station, next) => {
  1699. CacheModule.runJob("HDEL", { table: "stations", key: stationId }, this)
  1700. .then(next(null, station))
  1701. .catch(next);
  1702. }
  1703. ],
  1704. async (err, station) => {
  1705. if (err) {
  1706. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1707. this.log("ERROR", "STATIONS_REMOVE", `Removing station "${stationId}" failed. "${err}"`);
  1708. return cb({ status: "failure", message: err });
  1709. }
  1710. this.log("SUCCESS", "STATIONS_REMOVE", `Removing station "${stationId}" successfully.`);
  1711. CacheModule.runJob("PUB", {
  1712. channel: "station.remove",
  1713. value: stationId
  1714. });
  1715. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1716. userId: session.userId,
  1717. type: "station__remove",
  1718. payload: { message: `Removed a station named <stationId>${station.displayName}</stationId>` }
  1719. });
  1720. return cb({
  1721. status: "success",
  1722. message: "Successfully removed."
  1723. });
  1724. }
  1725. );
  1726. }),
  1727. /**
  1728. * Create a station
  1729. *
  1730. * @param session
  1731. * @param data - the station data
  1732. * @param cb
  1733. */
  1734. create: isLoginRequired(async function create(session, data, cb) {
  1735. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  1736. const stationModel = await DBModule.runJob("GET_MODEL", { modelName: "station" }, this);
  1737. data.name = data.name.toLowerCase();
  1738. const blacklist = [
  1739. "country",
  1740. "edm",
  1741. "musare",
  1742. "hip-hop",
  1743. "rap",
  1744. "top-hits",
  1745. "todays-hits",
  1746. "old-school",
  1747. "christmas",
  1748. "about",
  1749. "support",
  1750. "staff",
  1751. "help",
  1752. "news",
  1753. "terms",
  1754. "privacy",
  1755. "profile",
  1756. "c",
  1757. "community",
  1758. "tos",
  1759. "login",
  1760. "register",
  1761. "p",
  1762. "official",
  1763. "o",
  1764. "trap",
  1765. "faq",
  1766. "team",
  1767. "donate",
  1768. "buy",
  1769. "shop",
  1770. "forums",
  1771. "explore",
  1772. "settings",
  1773. "admin",
  1774. "auth",
  1775. "reset_password"
  1776. ];
  1777. async.waterfall(
  1778. [
  1779. next => {
  1780. if (!data) return next("Invalid data.");
  1781. return next();
  1782. },
  1783. next => {
  1784. stationModel.findOne(
  1785. {
  1786. $or: [
  1787. { name: data.name },
  1788. {
  1789. displayName: new RegExp(`^${data.displayName}$`, "i")
  1790. }
  1791. ]
  1792. },
  1793. next
  1794. );
  1795. },
  1796. // eslint-disable-next-line consistent-return
  1797. (station, next) => {
  1798. this.log(station);
  1799. if (station) return next("A station with that name or display name already exists.");
  1800. const { name, displayName, description, genres, playlist, type, blacklistedGenres } = data;
  1801. if (type === "official") {
  1802. return userModel.findOne({ _id: session.userId }, (err, user) => {
  1803. if (err) return next(err);
  1804. if (!user) return next("User not found.");
  1805. if (user.role !== "admin") return next("Admin required.");
  1806. return stationModel.create(
  1807. {
  1808. name,
  1809. displayName,
  1810. description,
  1811. type,
  1812. privacy: "private",
  1813. playlist,
  1814. genres,
  1815. blacklistedGenres,
  1816. currentSong: StationsModule.defaultSong
  1817. },
  1818. next
  1819. );
  1820. });
  1821. }
  1822. if (type === "community") {
  1823. if (blacklist.indexOf(name) !== -1)
  1824. return next("That name is blacklisted. Please use a different name.");
  1825. return stationModel.create(
  1826. {
  1827. name,
  1828. displayName,
  1829. description,
  1830. type,
  1831. privacy: "private",
  1832. owner: session.userId,
  1833. queue: [],
  1834. currentSong: null
  1835. },
  1836. next
  1837. );
  1838. }
  1839. }
  1840. ],
  1841. async (err, station) => {
  1842. if (err) {
  1843. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1844. this.log("ERROR", "STATIONS_CREATE", `Creating station failed. "${err}"`);
  1845. return cb({ status: "failure", message: err });
  1846. }
  1847. this.log("SUCCESS", "STATIONS_CREATE", `Created station "${station._id}" successfully.`);
  1848. CacheModule.runJob("PUB", {
  1849. channel: "station.create",
  1850. value: station._id
  1851. });
  1852. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1853. userId: session.userId,
  1854. type: "station__create",
  1855. payload: {
  1856. message: `Created a station named <stationId>${station.displayName}</stationId>`,
  1857. stationId: station._id
  1858. }
  1859. });
  1860. return cb({
  1861. status: "success",
  1862. message: "Successfully created station."
  1863. });
  1864. }
  1865. );
  1866. }),
  1867. /**
  1868. * Adds song to station queue
  1869. *
  1870. * @param session
  1871. * @param stationId - the station id
  1872. * @param songId - the song id
  1873. * @param cb
  1874. */
  1875. addToQueue: isLoginRequired(async function addToQueue(session, stationId, songId, cb) {
  1876. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  1877. const stationModel = await DBModule.runJob(
  1878. "GET_MODEL",
  1879. {
  1880. modelName: "station"
  1881. },
  1882. this
  1883. );
  1884. async.waterfall(
  1885. [
  1886. next => {
  1887. StationsModule.runJob("GET_STATION", { stationId }, this)
  1888. .then(station => {
  1889. next(null, station);
  1890. })
  1891. .catch(next);
  1892. },
  1893. (station, next) => {
  1894. if (!station) return next("Station not found.");
  1895. if (station.locked) {
  1896. return userModel.findOne({ _id: session.userId }, (err, user) => {
  1897. if (user.role !== "admin" && station.owner !== session.userId)
  1898. return next("Only owners and admins can add songs to a locked queue.");
  1899. return next(null, station);
  1900. });
  1901. }
  1902. return next(null, station);
  1903. },
  1904. (station, next) => {
  1905. if (station.type !== "community") return next("That station is not a community station.");
  1906. return StationsModule.runJob(
  1907. "CAN_USER_VIEW_STATION",
  1908. {
  1909. station,
  1910. userId: session.userId
  1911. },
  1912. this
  1913. )
  1914. .then(canView => {
  1915. if (canView) return next(null, station);
  1916. return next("Insufficient permissions.");
  1917. })
  1918. .catch(err => next(err));
  1919. },
  1920. (station, next) => {
  1921. if (station.currentSong && station.currentSong.songId === songId)
  1922. return next("That song is currently playing.");
  1923. return async.each(
  1924. station.queue,
  1925. (queueSong, next) => {
  1926. if (queueSong.songId === songId) return next("That song is already in the queue.");
  1927. return next();
  1928. },
  1929. err => next(err, station)
  1930. );
  1931. },
  1932. (station, next) => {
  1933. SongsModule.runJob("GET_SONG_FROM_ID", { songId }, this)
  1934. .then(res => {
  1935. if (res.song) return next(null, res.song, station);
  1936. return YouTubeModule.runJob("GET_SONG", { songId }, this)
  1937. .then(response => {
  1938. const { song } = response;
  1939. song.artists = [];
  1940. song.skipDuration = 0;
  1941. song.likes = -1;
  1942. song.dislikes = -1;
  1943. song.thumbnail = "empty";
  1944. song.explicit = false;
  1945. return next(null, song, station);
  1946. })
  1947. .catch(err => next(err));
  1948. })
  1949. .catch(err => next(err));
  1950. },
  1951. (song, station, next) => {
  1952. const { queue } = station;
  1953. song.requestedBy = session.userId;
  1954. song.requestedAt = Date.now();
  1955. queue.push(song);
  1956. let totalDuration = 0;
  1957. queue.forEach(song => {
  1958. totalDuration += song.duration;
  1959. });
  1960. if (totalDuration >= 3600 * 3) return next("The max length of the queue is 3 hours.");
  1961. return next(null, song, station);
  1962. },
  1963. (song, station, next) => {
  1964. const { queue } = station;
  1965. if (queue.length === 0) return next(null, song, station);
  1966. let totalDuration = 0;
  1967. const userId = queue[queue.length - 1].requestedBy;
  1968. station.queue.forEach(song => {
  1969. if (userId === song.requestedBy) {
  1970. totalDuration += song.duration;
  1971. }
  1972. });
  1973. if (totalDuration >= 900) return next("The max length of songs per user is 15 minutes.");
  1974. return next(null, song, station);
  1975. },
  1976. (song, station, next) => {
  1977. const { queue } = station;
  1978. if (queue.length === 0) return next(null, song);
  1979. let totalSongs = 0;
  1980. const userId = queue[queue.length - 1].requestedBy;
  1981. queue.forEach(song => {
  1982. if (userId === song.requestedBy) {
  1983. totalSongs += 1;
  1984. }
  1985. });
  1986. if (totalSongs <= 2) return next(null, song);
  1987. if (totalSongs > 3)
  1988. return next("The max amount of songs per user is 3, and only 2 in a row is allowed.");
  1989. if (queue[queue.length - 2].requestedBy !== userId || queue[queue.length - 3] !== userId)
  1990. return next("The max amount of songs per user is 3, and only 2 in a row is allowed.");
  1991. return next(null, song);
  1992. },
  1993. (song, next) => {
  1994. stationModel.updateOne(
  1995. { _id: stationId },
  1996. { $pushr: { queue: song } },
  1997. { runValidators: true },
  1998. next
  1999. );
  2000. },
  2001. (res, next) => {
  2002. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  2003. .then(station => next(null, station))
  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("GET_MODEL", { modelName: "station" }, this);
  2043. async.waterfall(
  2044. [
  2045. next => {
  2046. if (!songId) return next("Invalid song id.");
  2047. return StationsModule.runJob("GET_STATION", { stationId }, this)
  2048. .then(station => next(null, station))
  2049. .catch(next);
  2050. },
  2051. (station, next) => {
  2052. if (!station) return next("Station not found.");
  2053. if (station.type !== "community") return next("Station is not a community station.");
  2054. return async.each(
  2055. station.queue,
  2056. (queueSong, next) => {
  2057. if (queueSong.songId === songId) return next(true);
  2058. return next();
  2059. },
  2060. err => {
  2061. if (err === true) return next();
  2062. return next("Song is not currently in the queue.");
  2063. }
  2064. );
  2065. },
  2066. next => {
  2067. stationModel.updateOne({ _id: stationId }, { $pull: { queue: { songId } } }, next);
  2068. },
  2069. (res, next) => {
  2070. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  2071. .then(station => next(null, station))
  2072. .catch(next);
  2073. }
  2074. ],
  2075. async err => {
  2076. if (err) {
  2077. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2078. this.log(
  2079. "ERROR",
  2080. "STATIONS_REMOVE_SONG_TO_QUEUE",
  2081. `Removing song "${songId}" from station "${stationId}" queue failed. "${err}"`
  2082. );
  2083. return cb({ status: "failure", message: err });
  2084. }
  2085. this.log(
  2086. "SUCCESS",
  2087. "STATIONS_REMOVE_SONG_TO_QUEUE",
  2088. `Removed song "${songId}" from station "${stationId}" successfully.`
  2089. );
  2090. CacheModule.runJob("PUB", {
  2091. channel: "station.queueUpdate",
  2092. value: stationId
  2093. });
  2094. return cb({
  2095. status: "success",
  2096. message: "Successfully removed song from queue."
  2097. });
  2098. }
  2099. );
  2100. }),
  2101. /**
  2102. * Gets the queue from a station
  2103. *
  2104. * @param {object} session - user session
  2105. * @param {string} stationId - the station id
  2106. * @param {Function} cb - callback
  2107. */
  2108. getQueue(session, stationId, cb) {
  2109. async.waterfall(
  2110. [
  2111. next => {
  2112. StationsModule.runJob("GET_STATION", { stationId }, this)
  2113. .then(station => next(null, station))
  2114. .catch(next);
  2115. },
  2116. (station, next) => {
  2117. if (!station) return next("Station not found.");
  2118. if (station.type !== "community") return next("Station is not a community station.");
  2119. return next(null, station);
  2120. },
  2121. (station, next) => {
  2122. StationsModule.runJob("CAN_USER_VIEW_STATION", { station, userId: session.userId }, this)
  2123. .then(canView => {
  2124. if (canView) return next(null, station);
  2125. return next("Insufficient permissions.");
  2126. })
  2127. .catch(err => next(err));
  2128. }
  2129. ],
  2130. async (err, station) => {
  2131. if (err) {
  2132. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2133. this.log(
  2134. "ERROR",
  2135. "STATIONS_GET_QUEUE",
  2136. `Getting queue for station "${stationId}" failed. "${err}"`
  2137. );
  2138. return cb({ status: "failure", message: err });
  2139. }
  2140. this.log("SUCCESS", "STATIONS_GET_QUEUE", `Got queue for station "${stationId}" successfully.`);
  2141. return cb({
  2142. status: "success",
  2143. message: "Successfully got queue.",
  2144. queue: station.queue
  2145. });
  2146. }
  2147. );
  2148. },
  2149. /**
  2150. * Selects a private playlist for a station
  2151. *
  2152. * @param session
  2153. * @param stationId - the station id
  2154. * @param playlistId - the private playlist id
  2155. * @param cb
  2156. */
  2157. selectPrivatePlaylist: isOwnerRequired(async function selectPrivatePlaylist(session, stationId, playlistId, cb) {
  2158. const stationModel = await DBModule.runJob("GET_MODEL", { modelName: "station" }, this);
  2159. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  2160. async.waterfall(
  2161. [
  2162. next => {
  2163. StationsModule.runJob("GET_STATION", { stationId }, this)
  2164. .then(station => next(null, station))
  2165. .catch(next);
  2166. },
  2167. (station, next) => {
  2168. if (!station) return next("Station not found.");
  2169. if (station.type !== "community") return next("Station is not a community station.");
  2170. if (station.privatePlaylist === playlistId)
  2171. return next("That private playlist is already selected.");
  2172. return playlistModel.findOne({ _id: playlistId }, next);
  2173. },
  2174. (playlist, next) => {
  2175. if (!playlist) return next("Playlist not found.");
  2176. const currentSongIndex = playlist.songs.length > 0 ? playlist.songs.length - 1 : 0;
  2177. return stationModel.updateOne(
  2178. { _id: stationId },
  2179. {
  2180. $set: {
  2181. privatePlaylist: playlistId,
  2182. currentSongIndex
  2183. }
  2184. },
  2185. { runValidators: true },
  2186. next
  2187. );
  2188. },
  2189. (res, next) => {
  2190. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  2191. .then(station => next(null, station))
  2192. .catch(next);
  2193. }
  2194. ],
  2195. async (err, station) => {
  2196. if (err) {
  2197. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2198. this.log(
  2199. "ERROR",
  2200. "STATIONS_SELECT_PRIVATE_PLAYLIST",
  2201. `Selecting private playlist "${playlistId}" for station "${stationId}" failed. "${err}"`
  2202. );
  2203. return cb({ status: "failure", message: err });
  2204. }
  2205. this.log(
  2206. "SUCCESS",
  2207. "STATIONS_SELECT_PRIVATE_PLAYLIST",
  2208. `Selected private playlist "${playlistId}" for station "${stationId}" successfully.`
  2209. );
  2210. NotificationsModule.runJob("UNSCHEDULE", {
  2211. name: `stations.nextSong?id${stationId}`
  2212. });
  2213. if (!station.partyMode) StationsModule.runJob("SKIP_STATION", { stationId });
  2214. CacheModule.runJob("PUB", {
  2215. channel: "privatePlaylist.selected",
  2216. value: {
  2217. playlistId,
  2218. stationId
  2219. }
  2220. });
  2221. return cb({
  2222. status: "success",
  2223. message: "Successfully selected playlist."
  2224. });
  2225. }
  2226. );
  2227. }),
  2228. /**
  2229. * Deselects the private playlist selected in a station
  2230. *
  2231. * @param session
  2232. * @param stationId - the station id
  2233. * @param cb
  2234. */
  2235. deselectPrivatePlaylist: isOwnerRequired(async function deselectPrivatePlaylist(session, stationId, cb) {
  2236. const stationModel = await DBModule.runJob(
  2237. "GET_MODEL",
  2238. {
  2239. modelName: "station"
  2240. },
  2241. this
  2242. );
  2243. async.waterfall(
  2244. [
  2245. next => {
  2246. StationsModule.runJob("GET_STATION", { stationId }, this)
  2247. .then(station => {
  2248. next(null, station);
  2249. })
  2250. .catch(next);
  2251. },
  2252. (station, next) => {
  2253. if (!station) return next("Station not found.");
  2254. if (station.type !== "community") return next("Station is not a community station.");
  2255. if (!station.privatePlaylist) return next("No private playlist is currently selected.");
  2256. return stationModel.updateOne(
  2257. { _id: stationId },
  2258. {
  2259. $set: {
  2260. privatePlaylist: null,
  2261. currentSongIndex: 0
  2262. }
  2263. },
  2264. { runValidators: true },
  2265. next
  2266. );
  2267. },
  2268. (res, next) => {
  2269. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  2270. .then(station => {
  2271. next(null, station);
  2272. })
  2273. .catch(next);
  2274. }
  2275. ],
  2276. async (err, station) => {
  2277. if (err) {
  2278. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2279. this.log(
  2280. "ERROR",
  2281. "STATIONS_DESELECT_PRIVATE_PLAYLIST",
  2282. `Deselecting private playlist for station "${stationId}" failed. "${err}"`
  2283. );
  2284. return cb({ status: "failure", message: err });
  2285. }
  2286. this.log(
  2287. "SUCCESS",
  2288. "STATIONS_DESELECT_PRIVATE_PLAYLIST",
  2289. `Deselected private playlist for station "${stationId}" successfully.`
  2290. );
  2291. NotificationsModule.runJob("UNSCHEDULE", {
  2292. name: `stations.nextSong?id${stationId}`
  2293. });
  2294. if (!station.partyMode) StationsModule.runJob("SKIP_STATION", { stationId });
  2295. CacheModule.runJob("PUB", {
  2296. channel: "privatePlaylist.deselected",
  2297. value: {
  2298. stationId
  2299. }
  2300. });
  2301. return cb({
  2302. status: "success",
  2303. message: "Successfully deselected playlist."
  2304. });
  2305. }
  2306. );
  2307. }),
  2308. favoriteStation: isLoginRequired(async function favoriteStation(session, stationId, cb) {
  2309. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  2310. async.waterfall(
  2311. [
  2312. next => {
  2313. StationsModule.runJob("GET_STATION", { stationId }, this)
  2314. .then(station => next(null, station))
  2315. .catch(next);
  2316. },
  2317. (station, next) => {
  2318. if (!station) return next("Station not found.");
  2319. return StationsModule.runJob("CAN_USER_VIEW_STATION", { station, userId: session.userId }, this)
  2320. .then(canView => {
  2321. if (canView) return next(null, station);
  2322. return next("Insufficient permissions.");
  2323. })
  2324. .catch(err => next(err));
  2325. },
  2326. (station, next) => {
  2327. userModel.updateOne(
  2328. { _id: session.userId },
  2329. { $addToSet: { favoriteStations: stationId } },
  2330. (err, res) => next(err, station, res)
  2331. );
  2332. },
  2333. (station, res, next) => {
  2334. if (res.nModified === 0) return next("The station was already favorited.");
  2335. return next(null, station);
  2336. }
  2337. ],
  2338. async (err, station) => {
  2339. if (err) {
  2340. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2341. this.log("ERROR", "FAVORITE_STATION", `Favoriting station "${stationId}" failed. "${err}"`);
  2342. return cb({ status: "failure", message: err });
  2343. }
  2344. this.log("SUCCESS", "FAVORITE_STATION", `Favorited station "${stationId}" successfully.`);
  2345. CacheModule.runJob("PUB", {
  2346. channel: "user.favoritedStation",
  2347. value: {
  2348. userId: session.userId,
  2349. stationId
  2350. }
  2351. });
  2352. ActivitiesModule.runJob("ADD_ACTIVITY", {
  2353. userId: session.userId,
  2354. type: "station__favorite",
  2355. payload: {
  2356. message: `Favorited station <stationId>${station.displayName}</stationId>`,
  2357. stationId
  2358. }
  2359. });
  2360. return cb({
  2361. status: "success",
  2362. message: "Succesfully favorited station."
  2363. });
  2364. }
  2365. );
  2366. }),
  2367. unfavoriteStation: isLoginRequired(async function unfavoriteStation(session, stationId, cb) {
  2368. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  2369. async.waterfall(
  2370. [
  2371. next => {
  2372. userModel.updateOne({ _id: session.userId }, { $pull: { favoriteStations: stationId } }, next);
  2373. },
  2374. (res, next) => {
  2375. if (res.nModified === 0) return next("The station wasn't favorited.");
  2376. return next();
  2377. },
  2378. next => {
  2379. StationsModule.runJob("GET_STATION", { stationId }, this)
  2380. .then(station => next(null, station))
  2381. .catch(next);
  2382. }
  2383. ],
  2384. async (err, station) => {
  2385. if (err) {
  2386. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  2387. this.log("ERROR", "UNFAVORITE_STATION", `Unfavoriting station "${stationId}" failed. "${err}"`);
  2388. return cb({ status: "failure", message: err });
  2389. }
  2390. this.log("SUCCESS", "UNFAVORITE_STATION", `Unfavorited station "${stationId}" successfully.`);
  2391. CacheModule.runJob("PUB", {
  2392. channel: "user.unfavoritedStation",
  2393. value: {
  2394. userId: session.userId,
  2395. stationId
  2396. }
  2397. });
  2398. ActivitiesModule.runJob("ADD_ACTIVITY", {
  2399. userId: session.userId,
  2400. type: "station__unfavorite",
  2401. payload: {
  2402. message: `Unfavorited station <stationId>${station.displayName}</stationId>`,
  2403. stationId
  2404. }
  2405. });
  2406. return cb({
  2407. status: "success",
  2408. message: "Succesfully unfavorited station."
  2409. });
  2410. }
  2411. );
  2412. })
  2413. };