stations.js 62 KB

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