stations.js 88 KB

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