stations.js 62 KB

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