stations.js 59 KB

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