stations.js 57 KB

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