stations.js 88 KB

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