stations.js 63 KB

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