stations.js 52 KB

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