stations.js 56 KB

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