stations.js 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111
  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. song.requestedAt = Date.now();
  1593. queue.push(song);
  1594. let totalDuration = 0;
  1595. queue.forEach(song => {
  1596. totalDuration += song.duration;
  1597. });
  1598. if (totalDuration >= 3600 * 3) return next("The max length of the queue is 3 hours.");
  1599. return next(null, song, station);
  1600. },
  1601. (song, station, next) => {
  1602. const { queue } = station;
  1603. if (queue.length === 0) return next(null, song, station);
  1604. let totalDuration = 0;
  1605. const userId = queue[queue.length - 1].requestedBy;
  1606. station.queue.forEach(song => {
  1607. if (userId === song.requestedBy) {
  1608. totalDuration += song.duration;
  1609. }
  1610. });
  1611. if (totalDuration >= 900) return next("The max length of songs per user is 15 minutes.");
  1612. return next(null, song, station);
  1613. },
  1614. (song, station, next) => {
  1615. const { queue } = station;
  1616. if (queue.length === 0) return next(null, song);
  1617. let totalSongs = 0;
  1618. const userId = queue[queue.length - 1].requestedBy;
  1619. queue.forEach(song => {
  1620. if (userId === song.requestedBy) {
  1621. totalSongs += 1;
  1622. }
  1623. });
  1624. if (totalSongs <= 2) return next(null, song);
  1625. if (totalSongs > 3)
  1626. return next("The max amount of songs per user is 3, and only 2 in a row is allowed.");
  1627. if (queue[queue.length - 2].requestedBy !== userId || queue[queue.length - 3] !== userId)
  1628. return next("The max amount of songs per user is 3, and only 2 in a row is allowed.");
  1629. return next(null, song);
  1630. },
  1631. (song, next) => {
  1632. stationModel.updateOne(
  1633. { _id: stationId },
  1634. { $push: { queue: song } },
  1635. { runValidators: true },
  1636. next
  1637. );
  1638. },
  1639. (res, next) => {
  1640. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  1641. .then(station => {
  1642. next(null, station);
  1643. })
  1644. .catch(next);
  1645. }
  1646. ],
  1647. async err => {
  1648. if (err) {
  1649. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1650. this.log(
  1651. "ERROR",
  1652. "STATIONS_ADD_SONG_TO_QUEUE",
  1653. `Adding song "${songId}" to station "${stationId}" queue failed. "${err}"`
  1654. );
  1655. return cb({ status: "failure", message: err });
  1656. }
  1657. this.log(
  1658. "SUCCESS",
  1659. "STATIONS_ADD_SONG_TO_QUEUE",
  1660. `Added song "${songId}" to station "${stationId}" successfully.`
  1661. );
  1662. CacheModule.runJob("PUB", {
  1663. channel: "station.queueUpdate",
  1664. value: stationId
  1665. });
  1666. return cb({
  1667. status: "success",
  1668. message: "Successfully added song to queue."
  1669. });
  1670. }
  1671. );
  1672. }),
  1673. /**
  1674. * Removes song from station queue
  1675. *
  1676. * @param session
  1677. * @param stationId - the station id
  1678. * @param songId - the song id
  1679. * @param cb
  1680. */
  1681. removeFromQueue: isOwnerRequired(async function removeFromQueue(session, stationId, songId, cb) {
  1682. const stationModel = await DBModule.runJob(
  1683. "GET_MODEL",
  1684. {
  1685. modelName: "station"
  1686. },
  1687. this
  1688. );
  1689. async.waterfall(
  1690. [
  1691. next => {
  1692. if (!songId) return next("Invalid song id.");
  1693. return StationsModule.runJob("GET_STATION", { stationId }, this)
  1694. .then(station => {
  1695. next(null, station);
  1696. })
  1697. .catch(next);
  1698. },
  1699. (station, next) => {
  1700. if (!station) return next("Station not found.");
  1701. if (station.type !== "community") return next("Station is not a community station.");
  1702. return async.each(
  1703. station.queue,
  1704. (queueSong, next) => {
  1705. if (queueSong.songId === songId) return next(true);
  1706. return next();
  1707. },
  1708. err => {
  1709. if (err === true) return next();
  1710. return next("Song is not currently in the queue.");
  1711. }
  1712. );
  1713. },
  1714. next => {
  1715. stationModel.updateOne({ _id: stationId }, { $pull: { queue: { songId } } }, next);
  1716. },
  1717. (res, next) => {
  1718. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  1719. .then(station => {
  1720. next(null, station);
  1721. })
  1722. .catch(next);
  1723. }
  1724. ],
  1725. async err => {
  1726. if (err) {
  1727. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1728. this.log(
  1729. "ERROR",
  1730. "STATIONS_REMOVE_SONG_TO_QUEUE",
  1731. `Removing song "${songId}" from station "${stationId}" queue failed. "${err}"`
  1732. );
  1733. return cb({ status: "failure", message: err });
  1734. }
  1735. this.log(
  1736. "SUCCESS",
  1737. "STATIONS_REMOVE_SONG_TO_QUEUE",
  1738. `Removed song "${songId}" from station "${stationId}" successfully.`
  1739. );
  1740. CacheModule.runJob("PUB", {
  1741. channel: "station.queueUpdate",
  1742. value: stationId
  1743. });
  1744. return cb({
  1745. status: "success",
  1746. message: "Successfully removed song from queue."
  1747. });
  1748. }
  1749. );
  1750. }),
  1751. /**
  1752. * Gets the queue from a station
  1753. *
  1754. * @param {object} session - user session
  1755. * @param {string} stationId - the station id
  1756. * @param {Function} cb - callback
  1757. */
  1758. getQueue(session, stationId, cb) {
  1759. async.waterfall(
  1760. [
  1761. next => {
  1762. StationsModule.runJob("GET_STATION", { stationId }, this)
  1763. .then(station => {
  1764. next(null, station);
  1765. })
  1766. .catch(next);
  1767. },
  1768. (station, next) => {
  1769. if (!station) return next("Station not found.");
  1770. if (station.type !== "community") return next("Station is not a community station.");
  1771. return next(null, station);
  1772. },
  1773. (station, next) => {
  1774. StationsModule.runJob(
  1775. "CAN_USER_VIEW_STATION",
  1776. {
  1777. station,
  1778. userId: session.userId
  1779. },
  1780. this
  1781. )
  1782. .then(canView => {
  1783. if (canView) return next(null, station);
  1784. return next("Insufficient permissions.");
  1785. })
  1786. .catch(err => next(err));
  1787. }
  1788. ],
  1789. async (err, station) => {
  1790. if (err) {
  1791. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1792. this.log(
  1793. "ERROR",
  1794. "STATIONS_GET_QUEUE",
  1795. `Getting queue for station "${stationId}" failed. "${err}"`
  1796. );
  1797. return cb({ status: "failure", message: err });
  1798. }
  1799. this.log("SUCCESS", "STATIONS_GET_QUEUE", `Got queue for station "${stationId}" successfully.`);
  1800. return cb({
  1801. status: "success",
  1802. message: "Successfully got queue.",
  1803. queue: station.queue
  1804. });
  1805. }
  1806. );
  1807. },
  1808. /**
  1809. * Selects a private playlist for a station
  1810. *
  1811. * @param session
  1812. * @param stationId - the station id
  1813. * @param playlistId - the private playlist id
  1814. * @param cb
  1815. */
  1816. selectPrivatePlaylist: isOwnerRequired(async function selectPrivatePlaylist(session, stationId, playlistId, cb) {
  1817. const stationModel = await DBModule.runJob(
  1818. "GET_MODEL",
  1819. {
  1820. modelName: "station"
  1821. },
  1822. this
  1823. );
  1824. const playlistModel = await DBModule.runJob(
  1825. "GET_MODEL",
  1826. {
  1827. modelName: "playlist"
  1828. },
  1829. this
  1830. );
  1831. async.waterfall(
  1832. [
  1833. next => {
  1834. StationsModule.runJob("GET_STATION", { stationId }, this)
  1835. .then(station => {
  1836. next(null, station);
  1837. })
  1838. .catch(next);
  1839. },
  1840. (station, next) => {
  1841. if (!station) return next("Station not found.");
  1842. if (station.type !== "community") return next("Station is not a community station.");
  1843. if (station.privatePlaylist === playlistId)
  1844. return next("That private playlist is already selected.");
  1845. return playlistModel.findOne({ _id: playlistId }, next);
  1846. },
  1847. (playlist, next) => {
  1848. if (!playlist) return next("Playlist not found.");
  1849. const currentSongIndex = playlist.songs.length > 0 ? playlist.songs.length - 1 : 0;
  1850. return stationModel.updateOne(
  1851. { _id: stationId },
  1852. {
  1853. $set: {
  1854. privatePlaylist: playlistId,
  1855. currentSongIndex
  1856. }
  1857. },
  1858. { runValidators: true },
  1859. next
  1860. );
  1861. },
  1862. (res, next) => {
  1863. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  1864. .then(station => {
  1865. next(null, station);
  1866. })
  1867. .catch(next);
  1868. }
  1869. ],
  1870. async (err, station) => {
  1871. if (err) {
  1872. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1873. this.log(
  1874. "ERROR",
  1875. "STATIONS_SELECT_PRIVATE_PLAYLIST",
  1876. `Selecting private playlist "${playlistId}" for station "${stationId}" failed. "${err}"`
  1877. );
  1878. return cb({ status: "failure", message: err });
  1879. }
  1880. this.log(
  1881. "SUCCESS",
  1882. "STATIONS_SELECT_PRIVATE_PLAYLIST",
  1883. `Selected private playlist "${playlistId}" for station "${stationId}" successfully.`
  1884. );
  1885. NotificationsModule.runJob("UNSCHEDULE", {
  1886. name: `stations.nextSong?id${stationId}`
  1887. });
  1888. if (!station.partyMode) StationsModule.runJob("SKIP_STATION", { stationId });
  1889. CacheModule.runJob("PUB", {
  1890. channel: "privatePlaylist.selected",
  1891. value: {
  1892. playlistId,
  1893. stationId
  1894. }
  1895. });
  1896. return cb({
  1897. status: "success",
  1898. message: "Successfully selected playlist."
  1899. });
  1900. }
  1901. );
  1902. }),
  1903. favoriteStation: isLoginRequired(async function favoriteStation(session, stationId, cb) {
  1904. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  1905. async.waterfall(
  1906. [
  1907. next => {
  1908. StationsModule.runJob("GET_STATION", { stationId }, this)
  1909. .then(station => {
  1910. next(null, station);
  1911. })
  1912. .catch(next);
  1913. },
  1914. (station, next) => {
  1915. if (!station) return next("Station not found.");
  1916. return StationsModule.runJob(
  1917. "CAN_USER_VIEW_STATION",
  1918. {
  1919. station,
  1920. userId: session.userId
  1921. },
  1922. this
  1923. )
  1924. .then(canView => {
  1925. if (canView) return next();
  1926. return next("Insufficient permissions.");
  1927. })
  1928. .catch(err => next(err));
  1929. },
  1930. next => {
  1931. userModel.updateOne({ _id: session.userId }, { $addToSet: { favoriteStations: stationId } }, next);
  1932. },
  1933. (res, next) => {
  1934. if (res.nModified === 0) return next("The station was already favorited.");
  1935. return next();
  1936. }
  1937. ],
  1938. async err => {
  1939. if (err) {
  1940. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1941. this.log("ERROR", "FAVORITE_STATION", `Favoriting station "${stationId}" failed. "${err}"`);
  1942. return cb({ status: "failure", message: err });
  1943. }
  1944. this.log("SUCCESS", "FAVORITE_STATION", `Favorited station "${stationId}" successfully.`);
  1945. CacheModule.runJob("PUB", {
  1946. channel: "user.favoritedStation",
  1947. value: {
  1948. userId: session.userId,
  1949. stationId
  1950. }
  1951. });
  1952. return cb({
  1953. status: "success",
  1954. message: "Succesfully favorited station."
  1955. });
  1956. }
  1957. );
  1958. }),
  1959. unfavoriteStation: isLoginRequired(async function unfavoriteStation(session, stationId, cb) {
  1960. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  1961. async.waterfall(
  1962. [
  1963. next => {
  1964. userModel.updateOne({ _id: session.userId }, { $pull: { favoriteStations: stationId } }, next);
  1965. },
  1966. (res, next) => {
  1967. if (res.nModified === 0) return next("The station wasn't favorited.");
  1968. return next();
  1969. }
  1970. ],
  1971. async err => {
  1972. if (err) {
  1973. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1974. this.log("ERROR", "UNFAVORITE_STATION", `Unfavoriting station "${stationId}" failed. "${err}"`);
  1975. return cb({ status: "failure", message: err });
  1976. }
  1977. this.log("SUCCESS", "UNFAVORITE_STATION", `Unfavorited station "${stationId}" successfully.`);
  1978. CacheModule.runJob("PUB", {
  1979. channel: "user.unfavoritedStation",
  1980. value: {
  1981. userId: session.userId,
  1982. stationId
  1983. }
  1984. });
  1985. return cb({
  1986. status: "success",
  1987. message: "Succesfully unfavorited station."
  1988. });
  1989. }
  1990. );
  1991. })
  1992. };