stations.js 68 KB

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