stations.js 87 KB

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