stations.js 62 KB

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