stations.js 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290
  1. import async from "async";
  2. import config from "config";
  3. import CoreClass from "../core";
  4. import { hasPermission } from "./hooks/hasPermission";
  5. let StationsModule;
  6. let CacheModule;
  7. let DBModule;
  8. let UtilsModule;
  9. let WSModule;
  10. let PlaylistsModule;
  11. let NotificationsModule;
  12. let MediaModule;
  13. let SongsModule;
  14. class _StationsModule extends CoreClass {
  15. // eslint-disable-next-line require-jsdoc
  16. constructor() {
  17. super("stations");
  18. StationsModule = this;
  19. }
  20. /**
  21. * Initialises the stations module
  22. *
  23. * @returns {Promise} - returns promise (reject, resolve)
  24. */
  25. async initialize() {
  26. CacheModule = this.moduleManager.modules.cache;
  27. DBModule = this.moduleManager.modules.db;
  28. UtilsModule = this.moduleManager.modules.utils;
  29. WSModule = this.moduleManager.modules.ws;
  30. PlaylistsModule = this.moduleManager.modules.playlists;
  31. NotificationsModule = this.moduleManager.modules.notifications;
  32. MediaModule = this.moduleManager.modules.media;
  33. SongsModule = this.moduleManager.modules.songs;
  34. this.userList = {};
  35. this.usersPerStation = {};
  36. this.usersPerStationCount = {};
  37. // TEMP
  38. CacheModule.runJob("SUB", {
  39. channel: "station.pause",
  40. cb: async stationId => {
  41. NotificationsModule.runJob("REMOVE", {
  42. subscription: `stations.nextSong?id=${stationId}`
  43. }).then();
  44. }
  45. });
  46. CacheModule.runJob("SUB", {
  47. channel: "station.resume",
  48. cb: async stationId => {
  49. StationsModule.runJob("INITIALIZE_STATION", { stationId }).then();
  50. }
  51. });
  52. CacheModule.runJob("SUB", {
  53. channel: "station.queueUpdate",
  54. cb: async stationId => {
  55. StationsModule.runJob("GET_STATION", { stationId }).then(station => {
  56. if (!station.currentSong && station.queue.length > 0) {
  57. StationsModule.runJob("INITIALIZE_STATION", {
  58. stationId
  59. }).then();
  60. }
  61. WSModule.runJob("EMIT_TO_ROOM", {
  62. room: `station.${stationId}`,
  63. args: ["event:station.queue.updated", { data: { queue: station.queue } }]
  64. });
  65. WSModule.runJob("EMIT_TO_ROOM", {
  66. room: `manage-station.${stationId}`,
  67. args: ["event:manageStation.queue.updated", { data: { stationId, queue: station.queue } }]
  68. });
  69. });
  70. }
  71. });
  72. const userModel = (this.userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }));
  73. CacheModule.runJob("SUB", {
  74. channel: "station.djs.added",
  75. cb: async ({ stationId, userId }) => {
  76. userModel.findOne({ _id: userId }, (err, user) => {
  77. if (!err && user) {
  78. const { _id, name, username, avatar } = user;
  79. const data = { data: { user: { _id, name, username, avatar }, stationId } };
  80. WSModule.runJob("EMIT_TO_ROOMS", {
  81. rooms: [`station.${stationId}`, "home"],
  82. args: ["event:station.djs.added", data]
  83. });
  84. WSModule.runJob("EMIT_TO_ROOM", {
  85. room: `manage-station.${stationId}`,
  86. args: ["event:manageStation.djs.added", data]
  87. });
  88. }
  89. });
  90. }
  91. });
  92. CacheModule.runJob("SUB", {
  93. channel: "station.djs.removed",
  94. cb: async ({ stationId, userId }) => {
  95. userModel.findOne({ _id: userId }, (err, user) => {
  96. if (!err && user) {
  97. const { _id, name, username, avatar } = user;
  98. const data = { data: { user: { _id, name, username, avatar }, stationId } };
  99. WSModule.runJob("EMIT_TO_ROOMS", {
  100. rooms: [`station.${stationId}`, "home"],
  101. args: ["event:station.djs.removed", data]
  102. });
  103. WSModule.runJob("EMIT_TO_ROOM", {
  104. room: `manage-station.${stationId}`,
  105. args: ["event:manageStation.djs.removed", data]
  106. });
  107. }
  108. });
  109. }
  110. });
  111. const stationModel = (this.stationModel = await DBModule.runJob("GET_MODEL", { modelName: "station" }));
  112. const stationSchema = (this.stationSchema = await CacheModule.runJob("GET_SCHEMA", { schemaName: "station" }));
  113. const stationHistoryModel = (this.stationHistoryModel = await DBModule.runJob("GET_MODEL", { modelName: "stationHistory" }));
  114. const stationHistorySchema = (this.stationHistorySchema = await CacheModule.runJob("GET_SCHEMA", { schemaName: "stationHistory" }));
  115. return new Promise((resolve, reject) => {
  116. async.waterfall(
  117. [
  118. next => {
  119. this.setStage(2);
  120. CacheModule.runJob("HGETALL", { table: "stations" })
  121. .then(stations => {
  122. next(null, stations);
  123. })
  124. .catch(next);
  125. },
  126. (stations, next) => {
  127. this.setStage(3);
  128. if (!stations) return next();
  129. const stationIds = Object.keys(stations);
  130. return async.each(
  131. stationIds,
  132. (stationId, next) => {
  133. stationModel.findOne({ _id: stationId }, (err, station) => {
  134. if (err) next(err);
  135. else if (!station) {
  136. CacheModule.runJob("HDEL", {
  137. table: "stations",
  138. key: stationId
  139. })
  140. .then(() => {
  141. next();
  142. })
  143. .catch(next);
  144. } else next();
  145. });
  146. },
  147. next
  148. );
  149. },
  150. next => {
  151. this.setStage(4);
  152. stationModel.find({}, next);
  153. },
  154. (stations, next) => {
  155. this.setStage(5);
  156. async.each(
  157. stations,
  158. (station, next2) => {
  159. async.waterfall(
  160. [
  161. next => {
  162. CacheModule.runJob("HSET", {
  163. table: "stations",
  164. key: station._id,
  165. value: stationSchema(station)
  166. })
  167. .then(station => next(null, station))
  168. .catch(next);
  169. },
  170. (station, next) => {
  171. StationsModule.runJob(
  172. "INITIALIZE_STATION",
  173. {
  174. stationId: station._id
  175. },
  176. null,
  177. -1
  178. )
  179. .then(() => {
  180. next();
  181. })
  182. .catch(next);
  183. }
  184. ],
  185. err => {
  186. next2(err);
  187. }
  188. );
  189. },
  190. next
  191. );
  192. }
  193. ],
  194. async err => {
  195. if (err) {
  196. err = await UtilsModule.runJob("GET_ERROR", {
  197. error: err
  198. });
  199. reject(new Error(err));
  200. } else {
  201. resolve();
  202. }
  203. }
  204. );
  205. });
  206. }
  207. /**
  208. * Initialises a station
  209. *
  210. * @param {object} payload - object that contains the payload
  211. * @param {string} payload.stationId - id of the station to initialise
  212. * @returns {Promise} - returns a promise (resolve, reject)
  213. */
  214. INITIALIZE_STATION(payload) {
  215. return new Promise((resolve, reject) => {
  216. async.waterfall(
  217. [
  218. next => {
  219. StationsModule.runJob(
  220. "GET_STATION",
  221. {
  222. stationId: payload.stationId
  223. },
  224. this
  225. )
  226. .then(station => {
  227. next(null, station);
  228. })
  229. .catch(next);
  230. },
  231. (station, next) => {
  232. if (!station) return next("Station not found.");
  233. return NotificationsModule.runJob(
  234. "UNSCHEDULE",
  235. {
  236. name: `stations.nextSong?id=${station._id}`
  237. },
  238. this
  239. )
  240. .then()
  241. .catch()
  242. .finally(() => {
  243. NotificationsModule.runJob("SUBSCRIBE", {
  244. name: `stations.nextSong?id=${station._id}`,
  245. cb: () =>
  246. StationsModule.runJob("SKIP_STATION", {
  247. stationId: station._id,
  248. natural: true,
  249. skipReason: "natural"
  250. }),
  251. unique: true,
  252. station
  253. })
  254. .then()
  255. .catch();
  256. if (station.paused) return next(true, station);
  257. return next(null, station);
  258. });
  259. },
  260. (station, next) => {
  261. if (!station.currentSong) {
  262. return StationsModule.runJob(
  263. "SKIP_STATION",
  264. {
  265. stationId: station._id,
  266. natural: false,
  267. skipReason: "other"
  268. },
  269. this
  270. )
  271. .then(station => {
  272. next(true, station);
  273. })
  274. .catch(next)
  275. .finally(() => {});
  276. }
  277. let timeLeft =
  278. station.currentSong.duration * 1000 - (Date.now() - station.startedAt - station.timePaused);
  279. if (Number.isNaN(timeLeft)) timeLeft = -1;
  280. if (station.currentSong.duration * 1000 < timeLeft || timeLeft < 0) {
  281. return StationsModule.runJob(
  282. "SKIP_STATION",
  283. {
  284. stationId: station._id,
  285. natural: false,
  286. skipReason: "other"
  287. },
  288. this
  289. )
  290. .then(station => {
  291. next(null, station);
  292. })
  293. .catch(next);
  294. }
  295. // name, time, cb, station
  296. NotificationsModule.runJob("SCHEDULE", {
  297. name: `stations.nextSong?id=${station._id}`,
  298. time: timeLeft,
  299. station
  300. });
  301. return next(null, station);
  302. }
  303. ],
  304. async (err, station) => {
  305. if (err && err !== true) {
  306. err = await UtilsModule.runJob(
  307. "GET_ERROR",
  308. {
  309. error: err
  310. },
  311. this
  312. );
  313. reject(new Error(err));
  314. } else resolve(station);
  315. }
  316. );
  317. });
  318. }
  319. /**
  320. * Attempts to get the station from Redis. If it's not in Redis, get it from Mongo and add it to Redis.
  321. *
  322. * @param {object} payload - object that contains the payload
  323. * @param {string} payload.stationId - id of the station
  324. * @returns {Promise} - returns a promise (resolve, reject)
  325. */
  326. GET_STATION(payload) {
  327. return new Promise((resolve, reject) => {
  328. async.waterfall(
  329. [
  330. next => {
  331. CacheModule.runJob("HGET", { table: "stations", key: payload.stationId }, this)
  332. .then(station => next(null, station))
  333. .catch(next);
  334. },
  335. (station, next) => {
  336. if (station) return next(true, station);
  337. return StationsModule.stationModel.findOne({ _id: payload.stationId }, next);
  338. },
  339. (station, next) => {
  340. if (station) {
  341. station = StationsModule.stationSchema(station);
  342. CacheModule.runJob("HSET", {
  343. table: "stations",
  344. key: payload.stationId,
  345. value: station
  346. })
  347. .then()
  348. .catch();
  349. next(true, station);
  350. } else next("Station not found");
  351. }
  352. ],
  353. async (err, station) => {
  354. if (err && err !== true) {
  355. err = await UtilsModule.runJob(
  356. "GET_ERROR",
  357. {
  358. error: err
  359. },
  360. this
  361. );
  362. reject(new Error(err));
  363. } else resolve(station);
  364. }
  365. );
  366. });
  367. }
  368. /**
  369. * Attempts to get a station by name, firstly from Redis. If it's not in Redis, get it from Mongo and add it to Redis.
  370. *
  371. * @param {object} payload - object that contains the payload
  372. * @param {string} payload.stationName - the unique name of the station
  373. * @returns {Promise} - returns a promise (resolve, reject)
  374. */
  375. async GET_STATION_BY_NAME(payload) {
  376. return new Promise((resolve, reject) => {
  377. async.waterfall(
  378. [
  379. next => {
  380. StationsModule.stationModel.findOne({ name: payload.stationName }, next);
  381. },
  382. (station, next) => {
  383. if (station) {
  384. station = StationsModule.stationSchema(station);
  385. CacheModule.runJob("HSET", {
  386. table: "stations",
  387. key: station._id,
  388. value: station
  389. });
  390. next(true, station);
  391. } else next("Station not found");
  392. }
  393. ],
  394. (err, station) => {
  395. if (err && err !== true) return reject(new Error(err));
  396. return resolve(station);
  397. }
  398. );
  399. });
  400. }
  401. /**
  402. * Updates the station in cache from mongo or deletes station in cache if no longer in mongo.
  403. *
  404. * @param {object} payload - object that contains the payload
  405. * @param {string} payload.stationId - the id of the station to update
  406. * @returns {Promise} - returns a promise (resolve, reject)
  407. */
  408. UPDATE_STATION(payload) {
  409. return new Promise((resolve, reject) => {
  410. async.waterfall(
  411. [
  412. next => {
  413. StationsModule.stationModel.findOne({ _id: payload.stationId }, next);
  414. },
  415. (station, next) => {
  416. if (!station) {
  417. CacheModule.runJob("HDEL", {
  418. table: "stations",
  419. key: payload.stationId
  420. })
  421. .then()
  422. .catch();
  423. return next("Station not found");
  424. }
  425. return CacheModule.runJob(
  426. "HSET",
  427. {
  428. table: "stations",
  429. key: payload.stationId,
  430. value: station
  431. },
  432. this
  433. )
  434. .then(station => {
  435. next(null, station);
  436. })
  437. .catch(next);
  438. }
  439. ],
  440. async (err, station) => {
  441. if (err && err !== true) {
  442. err = await UtilsModule.runJob(
  443. "GET_ERROR",
  444. {
  445. error: err
  446. },
  447. this
  448. );
  449. reject(new Error(err));
  450. } else resolve(station);
  451. }
  452. );
  453. });
  454. }
  455. /**
  456. * Autofill station queue from station playlist
  457. *
  458. * @param {object} payload - object that contains the payload
  459. * @param {string} payload.stationId - the id of the station
  460. * @param {string} payload.ignoreExistingQueue - ignore the existing queue songs, replacing the old queue with a completely fresh one
  461. * @returns {Promise} - returns a promise (resolve, reject)
  462. */
  463. AUTOFILL_STATION(payload) {
  464. return new Promise((resolve, reject) => {
  465. const { stationId, ignoreExistingQueue } = payload;
  466. async.waterfall(
  467. [
  468. next => {
  469. PlaylistsModule.runJob("GET_STATION_PLAYLIST", { stationId, includeSongs: true }, this)
  470. .then(response => {
  471. next(null, response.playlist);
  472. })
  473. .catch(next);
  474. },
  475. (playlist, next) => {
  476. StationsModule.runJob("GET_STATION", { stationId }, this)
  477. .then(station => {
  478. if (!station.autofill.enabled) return next("Autofill is disabled in this station");
  479. if (
  480. !ignoreExistingQueue &&
  481. station.autofill.limit <= station.queue.filter(song => !song.requestedBy).length
  482. )
  483. return next("Autofill limit reached");
  484. if (ignoreExistingQueue) station.queue = [];
  485. return next(null, playlist, station);
  486. })
  487. .catch(next);
  488. },
  489. (playlist, station, next) => {
  490. if (station.autofill.mode === "random") {
  491. UtilsModule.runJob("SHUFFLE", { array: playlist.songs }, this)
  492. .then(response => {
  493. next(null, response.array, station);
  494. })
  495. .catch(next);
  496. } else next(null, playlist.songs, station);
  497. },
  498. async (_playlistSongs, station) => {
  499. let playlistSongs = JSON.parse(JSON.stringify(_playlistSongs));
  500. if (station.autofill.mode === "sequential") {
  501. if (station.currentSongIndex <= playlistSongs.length) {
  502. const songsToAddToEnd = playlistSongs.splice(0, station.currentSongIndex);
  503. playlistSongs = [...playlistSongs, ...songsToAddToEnd];
  504. }
  505. }
  506. const currentRequests = station.queue.filter(song => !song.requestedBy).length;
  507. const songsStillNeeded = station.autofill.limit - currentRequests;
  508. const currentSongs = station.queue;
  509. let currentYoutubeIds = station.queue.map(song => song.youtubeId);
  510. const songsToAdd = [];
  511. let lastSongAdded = null;
  512. if (station.currentSong && station.currentSong.youtubeId)
  513. currentYoutubeIds.push(station.currentSong.youtubeId);
  514. // Block for experiment: queue_autofill_skip_last_x_played
  515. if (config.has(`experimental.queue_autofill_skip_last_x_played.${stationId}`)) {
  516. const redisList = `experimental:queue_autofill_skip_last_x_played:${stationId}`;
  517. // Get list of last x youtube video's played, to make sure they can't be autofilled
  518. const listOfYoutubeIds = await CacheModule.runJob("LRANGE", { key: redisList }, this);
  519. currentYoutubeIds = [...currentYoutubeIds, ...listOfYoutubeIds];
  520. }
  521. // Block for experiment: weight_stations
  522. if (
  523. config.has(`experimental.weight_stations.${stationId}`) &&
  524. !!config.get(`experimental.weight_stations.${stationId}`)
  525. ) {
  526. const weightTagName =
  527. config.get(`experimental.weight_stations.${stationId}`) === true
  528. ? "weight"
  529. : config.get(`experimental.weight_stations.${stationId}`);
  530. const weightMap = {};
  531. const getYoutubeIds = playlistSongs
  532. .map(playlistSong => playlistSong.youtubeId)
  533. .filter(youtubeId => currentYoutubeIds.indexOf(youtubeId) === -1);
  534. const { songs } = await SongsModule.runJob("GET_SONGS", { youtubeIds: getYoutubeIds });
  535. const weightRegex = new RegExp(`${weightTagName}\\[(\\d+)\\]`);
  536. songs.forEach(song => {
  537. let weight = 1;
  538. song.tags.forEach(tag => {
  539. const regexResponse = weightRegex.exec(tag);
  540. if (regexResponse) weight = Number(regexResponse[1]);
  541. });
  542. if (Number.isNaN(weight)) weight = 1;
  543. weight = Math.round(weight);
  544. weight = Math.max(1, weight);
  545. weight = Math.min(10000, weight);
  546. weightMap[song.youtubeId] = weight;
  547. });
  548. const adjustedPlaylistSongs = [];
  549. playlistSongs.forEach(playlistSong => {
  550. Array.from({ length: weightMap[playlistSong.youtubeId] }).forEach(() => {
  551. adjustedPlaylistSongs.push(playlistSong);
  552. });
  553. });
  554. const { array } = await UtilsModule.runJob(
  555. "SHUFFLE",
  556. { array: adjustedPlaylistSongs },
  557. this
  558. );
  559. playlistSongs = array;
  560. }
  561. playlistSongs.every(song => {
  562. if (
  563. songsToAdd.length < songsStillNeeded &&
  564. currentYoutubeIds.indexOf(song.youtubeId) === -1 &&
  565. !songsToAdd.find(songToAdd => songToAdd.youtubeId === song.youtubeId)
  566. ) {
  567. lastSongAdded = song;
  568. songsToAdd.push(song);
  569. return true;
  570. }
  571. if (songsToAdd.length >= songsStillNeeded) return false;
  572. return true;
  573. });
  574. let { currentSongIndex } = station;
  575. if (station.autofill.mode === "sequential" && lastSongAdded) {
  576. const indexOfLastSong = _playlistSongs
  577. .map(song => song.youtubeId)
  578. .indexOf(lastSongAdded.youtubeId);
  579. if (indexOfLastSong !== -1) currentSongIndex = indexOfLastSong;
  580. }
  581. return { currentSongs, songsToAdd, currentSongIndex };
  582. },
  583. ({ currentSongs, songsToAdd, currentSongIndex }, next) => {
  584. const songs = [];
  585. async.eachLimit(
  586. songsToAdd.map(song => song.youtubeId),
  587. 2,
  588. (youtubeId, next) => {
  589. MediaModule.runJob("GET_MEDIA", { youtubeId }, this)
  590. .then(response => {
  591. const { song } = response;
  592. const { _id, title, artists, thumbnail, duration, skipDuration, verified } =
  593. song;
  594. songs.push({
  595. _id,
  596. youtubeId,
  597. title,
  598. artists,
  599. thumbnail,
  600. duration,
  601. skipDuration,
  602. verified
  603. });
  604. next();
  605. })
  606. .catch(next);
  607. },
  608. err => {
  609. if (err) next(err);
  610. else {
  611. const newSongsToAdd = songsToAdd.map(song =>
  612. songs.find(newSong => newSong.youtubeId === song.youtubeId)
  613. );
  614. next(null, currentSongs, newSongsToAdd, currentSongIndex);
  615. }
  616. }
  617. );
  618. },
  619. (currentSongs, songsToAdd, currentSongIndex, next) => {
  620. const newPlaylist = [...currentSongs, ...songsToAdd].map(song => {
  621. if (!song._id) song._id = null;
  622. if (!song.requestedAt) song.requestedAt = Date.now();
  623. return song;
  624. });
  625. next(null, newPlaylist, currentSongIndex);
  626. },
  627. (newPlaylist, currentSongIndex, next) => {
  628. StationsModule.stationModel.updateOne(
  629. { _id: stationId },
  630. { $set: { queue: newPlaylist, currentSongIndex } },
  631. { runValidators: true },
  632. err => {
  633. if (err) next(err);
  634. else
  635. StationsModule.runJob(
  636. "UPDATE_STATION",
  637. {
  638. stationId
  639. },
  640. this
  641. )
  642. .then(() => {
  643. next(null);
  644. })
  645. .catch(next);
  646. }
  647. );
  648. }
  649. ],
  650. err => {
  651. if (err) reject(err);
  652. else resolve();
  653. }
  654. );
  655. });
  656. }
  657. /**
  658. * Gets next station song
  659. *
  660. * @param {object} payload - object that contains the payload
  661. * @param {string} payload.stationId - the id of the station
  662. * @returns {Promise} - returns a promise (resolve, reject)
  663. */
  664. GET_NEXT_STATION_SONG(payload) {
  665. return new Promise((resolve, reject) => {
  666. const { stationId } = payload;
  667. async.waterfall(
  668. [
  669. next => {
  670. StationsModule.runJob("GET_STATION", { stationId }, this)
  671. .then(station => {
  672. next(null, station);
  673. })
  674. .catch(next);
  675. },
  676. (station, next) => {
  677. if (station.queue.length === 0) next("No songs available.");
  678. else {
  679. next(null, station.queue[0]);
  680. }
  681. },
  682. (queueSong, next) => {
  683. MediaModule.runJob(
  684. "GET_MEDIA",
  685. {
  686. youtubeId: queueSong.youtubeId
  687. },
  688. this
  689. )
  690. .then(response => {
  691. const { song } = response;
  692. const { _id, youtubeId, title, skipDuration, artists, thumbnail, duration, verified } =
  693. song;
  694. next(null, {
  695. _id,
  696. youtubeId,
  697. title,
  698. skipDuration,
  699. artists,
  700. thumbnail,
  701. duration,
  702. verified,
  703. requestedAt: queueSong.requestedAt,
  704. requestedBy: queueSong.requestedBy,
  705. likes: song.likes || 0,
  706. dislikes: song.dislikes || 0
  707. });
  708. })
  709. .catch(next);
  710. }
  711. ],
  712. (err, song) => {
  713. if (err) reject(err);
  714. else resolve({ song });
  715. }
  716. );
  717. });
  718. }
  719. /**
  720. * Removes first station queue song
  721. *
  722. * @param {object} payload - object that contains the payload
  723. * @param {string} payload.stationId - the id of the station
  724. * @returns {Promise} - returns a promise (resolve, reject)
  725. */
  726. REMOVE_FIRST_QUEUE_SONG(payload) {
  727. return new Promise((resolve, reject) => {
  728. const { stationId } = payload;
  729. async.waterfall(
  730. [
  731. next => {
  732. StationsModule.stationModel.updateOne(
  733. { _id: stationId },
  734. { $pop: { queue: -1 } },
  735. { runValidators: true },
  736. err => {
  737. if (err) next(err);
  738. else
  739. StationsModule.runJob(
  740. "UPDATE_STATION",
  741. {
  742. stationId
  743. },
  744. this
  745. )
  746. .then(() => {
  747. next(null);
  748. })
  749. .catch(next);
  750. }
  751. );
  752. }
  753. ],
  754. err => {
  755. if (err) reject(err);
  756. else resolve();
  757. }
  758. );
  759. });
  760. }
  761. /**
  762. * Process vote to skips for a station
  763. *
  764. * @param {object} payload - object that contains the payload
  765. * @param {string} payload.stationId - the id of the station to process
  766. * @returns {Promise} - returns a promise (resolve, reject)
  767. */
  768. PROCESS_SKIP_VOTES(payload) {
  769. return new Promise((resolve, reject) => {
  770. StationsModule.log("INFO", `Processing skip votes for station ${payload.stationId}.`);
  771. async.waterfall(
  772. [
  773. next => {
  774. StationsModule.runJob(
  775. "GET_STATION",
  776. {
  777. stationId: payload.stationId
  778. },
  779. this
  780. )
  781. .then(station => next(null, station))
  782. .catch(next);
  783. },
  784. (station, next) => {
  785. if (!station) return next("Station not found.");
  786. return next(null, station);
  787. },
  788. (station, next) => {
  789. WSModule.runJob("GET_SOCKETS_FOR_ROOM", { room: `station.${station._id}` }, this)
  790. .then(sockets => next(null, station, sockets))
  791. .catch(next);
  792. },
  793. (station, sockets, next) => {
  794. const skipVotes =
  795. station.currentSong && station.currentSong.skipVotes
  796. ? station.currentSong.skipVotes.length
  797. : 0;
  798. let shouldSkip = false;
  799. if (skipVotes === 0) {
  800. if (!station.paused && !station.currentSong && station.queue.length > 0) shouldSkip = true;
  801. return next(null, shouldSkip);
  802. }
  803. const users = [];
  804. return async.each(
  805. sockets,
  806. (socketId, next) => {
  807. WSModule.runJob("SOCKET_FROM_SOCKET_ID", { socketId }, this)
  808. .then(socket => {
  809. if (socket && socket.session && socket.session.userId) {
  810. if (!users.includes(socket.session.userId))
  811. users.push(socket.session.userId);
  812. }
  813. return next();
  814. })
  815. .catch(next);
  816. },
  817. err => {
  818. if (err) return next(err);
  819. if (
  820. !station.paused &&
  821. Math.min(skipVotes, users.length) / users.length >= station.skipVoteThreshold / 100
  822. )
  823. shouldSkip = true;
  824. return next(null, shouldSkip);
  825. }
  826. );
  827. },
  828. (shouldSkip, next) => {
  829. if (shouldSkip)
  830. StationsModule.runJob(
  831. "SKIP_STATION",
  832. {
  833. stationId: payload.stationId,
  834. natural: false,
  835. skipReason: "vote_skip"
  836. },
  837. this
  838. )
  839. .then(() => next())
  840. .catch(next);
  841. else next();
  842. }
  843. ],
  844. err => {
  845. if (err) reject(err);
  846. else resolve();
  847. }
  848. );
  849. });
  850. }
  851. /**
  852. *
  853. *
  854. * @param {*} payload
  855. */
  856. async ADD_STATION_HISTORY_ITEM(payload) {
  857. const { stationId, currentSong, skipReason, skippedAt } = payload;
  858. let document = await StationsModule.stationHistoryModel.create({
  859. stationId,
  860. type: "song_played",
  861. payload: {
  862. song: currentSong,
  863. skippedAt,
  864. skipReason
  865. },
  866. documentVersion: 1
  867. });
  868. if (!document) return;
  869. document = document._doc;
  870. delete document.__v;
  871. delete document.documentVersion;
  872. WSModule.runJob("EMIT_TO_ROOM", {
  873. room: `station.${stationId}`,
  874. args: ["event:station.history.new", { data: { historyItem: document } }]
  875. });
  876. WSModule.runJob("EMIT_TO_ROOM", {
  877. room: `manage-station.${stationId}`,
  878. args: ["event:manageStation.history.new", { data: { stationId, historyItem: document } }]
  879. });
  880. }
  881. /**
  882. * Skips a station
  883. *
  884. * @param {object} payload - object that contains the payload
  885. * @param {string} payload.stationId - the id of the station to skip
  886. * @param {string} payload.natural - whether to skip naturally or forcefully
  887. * @param {string} payload.skipReason - if it was skipped via force skip or via vote skipping or if it was natural
  888. * @returns {Promise} - returns a promise (resolve, reject)
  889. */
  890. SKIP_STATION(payload) {
  891. return new Promise((resolve, reject) => {
  892. StationsModule.log("INFO", `Skipping station ${payload.stationId}.`);
  893. StationsModule.log("STATION_ISSUE", `SKIP_STATION_CB - Station ID: ${payload.stationId}.`);
  894. async.waterfall(
  895. [
  896. // Clears up any existing timers that would skip the station if the song ends
  897. next => {
  898. NotificationsModule.runJob("UNSCHEDULE", {
  899. name: `stations.nextSong?id=${payload.stationId}`
  900. })
  901. .then(() => {
  902. next();
  903. })
  904. .catch(next);
  905. },
  906. // Gets the station object
  907. next => {
  908. StationsModule.runJob(
  909. "GET_STATION",
  910. {
  911. stationId: payload.stationId
  912. },
  913. this
  914. )
  915. .then(station => next(null, station))
  916. .catch(next);
  917. },
  918. (station, next) => {
  919. if (!station) return next("Station not found.");
  920. const { currentSong } = station;
  921. if (!currentSong) return next(null, station);
  922. const stationId = station._id;
  923. const skippedAt = new Date();
  924. const { skipReason } = payload;
  925. return StationsModule.runJob(
  926. "ADD_STATION_HISTORY_ITEM",
  927. {
  928. stationId,
  929. currentSong,
  930. skippedAt,
  931. skipReason
  932. },
  933. this
  934. ).finally(() => {
  935. next(null, station);
  936. });
  937. },
  938. (station, next) => {
  939. if (station.autofill.enabled)
  940. return StationsModule.runJob("AUTOFILL_STATION", { stationId: station._id }, this)
  941. .then(() => next(null, station))
  942. .catch(err => {
  943. if (
  944. err === "Autofill is disabled in this station" ||
  945. err === "Autofill limit reached"
  946. )
  947. return next(null, station);
  948. return next(err);
  949. });
  950. return next(null, station);
  951. },
  952. (station, next) => {
  953. StationsModule.runJob("GET_NEXT_STATION_SONG", { stationId: station._id }, this)
  954. .then(response => {
  955. StationsModule.runJob("REMOVE_FIRST_QUEUE_SONG", { stationId: station._id }, this)
  956. .then(() => {
  957. next(null, response.song, station);
  958. })
  959. .catch(next);
  960. })
  961. .catch(err => {
  962. if (err === "No songs available.") next(null, null, station);
  963. else next(err);
  964. });
  965. },
  966. async (song, station) => {
  967. const $set = {};
  968. if (song === null) $set.currentSong = null;
  969. else {
  970. // Block for experiment: queue_autofill_skip_last_x_played
  971. if (config.has(`experimental.queue_autofill_skip_last_x_played.${payload.stationId}`)) {
  972. const redisList = `experimental:queue_autofill_skip_last_x_played:${payload.stationId}`;
  973. const maxListLength = Number(
  974. config.get(`experimental.queue_autofill_skip_last_x_played.${payload.stationId}`)
  975. );
  976. // Add youtubeId to list for this station in Redis list
  977. await CacheModule.runJob(
  978. "LPUSH",
  979. {
  980. key: `experimental:queue_autofill_skip_last_x_played:${payload.stationId}`,
  981. value: song.youtubeId
  982. },
  983. this
  984. );
  985. const currentListLength = await CacheModule.runJob("LLEN", { key: redisList }, this);
  986. // Removes oldest youtubeId from list for this station in Redis list
  987. if (currentListLength > maxListLength) {
  988. const amount = currentListLength - maxListLength;
  989. const promises = Array.from({ length: amount }).map(() =>
  990. CacheModule.runJob(
  991. "RPOP",
  992. {
  993. key: `experimental:queue_autofill_skip_last_x_played:${payload.stationId}`
  994. },
  995. this
  996. )
  997. );
  998. await Promise.all(promises);
  999. }
  1000. }
  1001. $set.currentSong = {
  1002. _id: song._id,
  1003. youtubeId: song.youtubeId,
  1004. title: song.title,
  1005. artists: song.artists,
  1006. duration: song.duration,
  1007. skipDuration: song.skipDuration,
  1008. thumbnail: song.thumbnail,
  1009. requestedAt: song.requestedAt,
  1010. requestedBy: song.requestedBy,
  1011. verified: song.verified
  1012. };
  1013. }
  1014. $set.startedAt = Date.now();
  1015. $set.timePaused = 0;
  1016. if (station.paused) $set.pausedAt = Date.now();
  1017. return { $set, station };
  1018. },
  1019. ({ $set, station }, next) => {
  1020. StationsModule.stationModel.updateOne({ _id: station._id }, { $set }, err => {
  1021. if (err) return next(err);
  1022. return StationsModule.runJob("UPDATE_STATION", { stationId: station._id }, this)
  1023. .then(station => {
  1024. next(null, station);
  1025. })
  1026. .catch(next);
  1027. });
  1028. },
  1029. (station, next) => {
  1030. if (station.currentSong !== null && station.currentSong.youtubeId !== undefined) {
  1031. station.currentSong.skipVotes = 0;
  1032. }
  1033. next(null, station);
  1034. },
  1035. (station, next) => {
  1036. if (station.autofill.enabled)
  1037. return StationsModule.runJob("AUTOFILL_STATION", { stationId: station._id }, this)
  1038. .then(() => next(null, station))
  1039. .catch(err => {
  1040. if (
  1041. err === "Autofill is disabled in this station" ||
  1042. err === "Autofill limit reached"
  1043. )
  1044. return next(null, station);
  1045. return next(err);
  1046. });
  1047. return next(null, station);
  1048. },
  1049. (station, next) =>
  1050. StationsModule.runJob("UPDATE_STATION", { stationId: station._id }, this)
  1051. .then(station => {
  1052. CacheModule.runJob("PUB", {
  1053. channel: "station.queueUpdate",
  1054. value: payload.stationId
  1055. })
  1056. .then()
  1057. .catch();
  1058. next(null, station);
  1059. })
  1060. .catch(next)
  1061. ],
  1062. async (err, station) => {
  1063. if (err === "Autofill limit reached") return resolve({ station });
  1064. if (err) {
  1065. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1066. StationsModule.log("ERROR", `Skipping station "${payload.stationId}" failed. "${err}"`);
  1067. return reject(new Error(err));
  1068. }
  1069. // TODO Pub/Sub this
  1070. const { currentSong } = station;
  1071. WSModule.runJob("EMIT_TO_ROOM", {
  1072. room: `station.${station._id}`,
  1073. args: [
  1074. "event:station.nextSong",
  1075. {
  1076. data: {
  1077. currentSong,
  1078. startedAt: station.startedAt,
  1079. paused: station.paused,
  1080. timePaused: 0,
  1081. natural: payload.natural
  1082. }
  1083. }
  1084. ]
  1085. });
  1086. WSModule.runJob("EMIT_TO_ROOM", {
  1087. room: `manage-station.${station._id}`,
  1088. args: ["event:station.nextSong", { data: { stationId: station._id, currentSong } }]
  1089. });
  1090. if (station.privacy === "public")
  1091. WSModule.runJob("EMIT_TO_ROOM", {
  1092. room: "home",
  1093. args: ["event:station.nextSong", { data: { stationId: station._id, currentSong } }]
  1094. });
  1095. else {
  1096. const sockets = await WSModule.runJob("GET_SOCKETS_FOR_ROOM", { room: "home" }, this);
  1097. sockets.forEach(async socketId => {
  1098. const socket = await WSModule.runJob("SOCKET_FROM_SOCKET_ID", { socketId });
  1099. if (!socket) return;
  1100. const { session } = socket;
  1101. if (session.sessionId) {
  1102. CacheModule.runJob("HGET", { table: "sessions", key: session.sessionId }).then(
  1103. session => {
  1104. const dispatchNextSong = () => {
  1105. socket.dispatch("event:station.nextSong", {
  1106. data: {
  1107. stationId: station._id,
  1108. currentSong
  1109. }
  1110. });
  1111. };
  1112. hasPermission("stations.index", session, station._id)
  1113. .then(() => dispatchNextSong())
  1114. .catch(() => {
  1115. hasPermission("stations.index.other", session)
  1116. .then(() => dispatchNextSong())
  1117. .catch(() => {});
  1118. });
  1119. }
  1120. );
  1121. }
  1122. });
  1123. }
  1124. WSModule.runJob("GET_SOCKETS_FOR_ROOM", { room: `station.${station._id}` }).then(sockets => {
  1125. if (station.currentSong !== null && station.currentSong.youtubeId !== undefined) {
  1126. WSModule.runJob("SOCKETS_JOIN_SONG_ROOM", {
  1127. sockets,
  1128. room: `song.${station.currentSong.youtubeId}`
  1129. });
  1130. if (!station.paused) {
  1131. NotificationsModule.runJob("SCHEDULE", {
  1132. name: `stations.nextSong?id=${station._id}`,
  1133. time: station.currentSong.duration * 1000,
  1134. station
  1135. });
  1136. }
  1137. } else WSModule.runJob("SOCKETS_LEAVE_SONG_ROOMS", { sockets });
  1138. });
  1139. return resolve({ station });
  1140. }
  1141. );
  1142. });
  1143. }
  1144. /**
  1145. * Checks if a user can view/access a station
  1146. *
  1147. * @param {object} payload - object that contains the payload
  1148. * @param {object} payload.station - the station object of the station in question
  1149. * @param {string} payload.userId - the id of the user in question
  1150. * @returns {Promise} - returns a promise (resolve, reject)
  1151. */
  1152. CAN_USER_VIEW_STATION(payload) {
  1153. return new Promise((resolve, reject) => {
  1154. async.waterfall(
  1155. [
  1156. next => {
  1157. if (payload.station.privacy === "public" || payload.station.privacy === "unlisted")
  1158. return next(true);
  1159. if (!payload.userId) return next("Not allowed");
  1160. return next();
  1161. },
  1162. next => {
  1163. hasPermission("stations.view", payload.userId, payload.station._id)
  1164. .then(() => next(true))
  1165. .catch(() => next("Not allowed"));
  1166. }
  1167. ],
  1168. async errOrResult => {
  1169. if (errOrResult !== true && errOrResult !== "Not allowed") {
  1170. errOrResult = await UtilsModule.runJob(
  1171. "GET_ERROR",
  1172. {
  1173. error: errOrResult
  1174. },
  1175. this
  1176. );
  1177. reject(new Error(errOrResult));
  1178. } else {
  1179. resolve(errOrResult === true);
  1180. }
  1181. }
  1182. );
  1183. });
  1184. }
  1185. /**
  1186. * Checks if a user has favorited a station or not
  1187. *
  1188. * @param {object} payload - object that contains the payload
  1189. * @param {object} payload.stationId - the id of the station in question
  1190. * @param {string} payload.userId - the id of the user in question
  1191. * @returns {Promise} - returns a promise (resolve, reject)
  1192. */
  1193. HAS_USER_FAVORITED_STATION(payload) {
  1194. return new Promise((resolve, reject) => {
  1195. async.waterfall(
  1196. [
  1197. next => {
  1198. DBModule.runJob("GET_MODEL", { modelName: "user" }, this).then(userModel => {
  1199. userModel.findOne({ _id: payload.userId }, next);
  1200. });
  1201. },
  1202. (user, next) => {
  1203. if (!user) return next("User not found.");
  1204. if (user.favoriteStations.indexOf(payload.stationId) !== -1) return next(null, true);
  1205. return next(null, false);
  1206. }
  1207. ],
  1208. async (err, isStationFavorited) => {
  1209. if (err && err !== true) {
  1210. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1211. return reject(new Error(err));
  1212. }
  1213. return resolve(isStationFavorited);
  1214. }
  1215. );
  1216. });
  1217. }
  1218. /**
  1219. * Returns a list of sockets in a room that can and can't know about a station
  1220. *
  1221. * @param {object} payload - the payload object
  1222. * @param {object} payload.station - the station object
  1223. * @param {string} payload.room - the websockets room to get the sockets from
  1224. * @returns {Promise} - returns a promise (resolve, reject)
  1225. */
  1226. GET_SOCKETS_THAT_CAN_KNOW_ABOUT_STATION(payload) {
  1227. return new Promise((resolve, reject) => {
  1228. WSModule.runJob("GET_SOCKETS_FOR_ROOM", { room: payload.room }, this)
  1229. .then(socketIds => {
  1230. const sockets = [];
  1231. async.eachLimit(
  1232. socketIds,
  1233. 1,
  1234. (socketId, next) => {
  1235. WSModule.runJob("SOCKET_FROM_SOCKET_ID", { socketId }, this)
  1236. .then(socket => {
  1237. if (socket) sockets.push(socket);
  1238. next();
  1239. })
  1240. .catch(err => {
  1241. reject(err);
  1242. });
  1243. },
  1244. err => {
  1245. if (err) reject(err);
  1246. else {
  1247. let socketsThatCan = [];
  1248. const socketsThatCannot = [];
  1249. if (payload.station.privacy === "public") {
  1250. socketsThatCan = sockets;
  1251. resolve({ socketsThatCan, socketsThatCannot });
  1252. } else {
  1253. async.eachLimit(
  1254. sockets,
  1255. 1,
  1256. (socket, next) => {
  1257. if (!(socket.session && socket.session.sessionId)) {
  1258. socketsThatCannot.push(socket);
  1259. next();
  1260. } else
  1261. hasPermission("stations.view", socket.session, payload.station._id)
  1262. .then(() => {
  1263. socketsThatCan.push(socket);
  1264. next();
  1265. })
  1266. .catch(() => {
  1267. socketsThatCannot.push(socket);
  1268. next();
  1269. });
  1270. },
  1271. err => {
  1272. if (err) reject(err);
  1273. else resolve({ socketsThatCan, socketsThatCannot });
  1274. }
  1275. );
  1276. }
  1277. }
  1278. }
  1279. );
  1280. })
  1281. .catch(reject);
  1282. });
  1283. }
  1284. /**
  1285. * Adds a playlist to autofill a station
  1286. *
  1287. * @param {object} payload - object that contains the payload
  1288. * @param {object} payload.stationId - the id of the station
  1289. * @param {object} payload.playlistId - the id of the playlist
  1290. * @returns {Promise} - returns a promise (resolve, reject)
  1291. */
  1292. AUTOFILL_PLAYLIST(payload) {
  1293. return new Promise((resolve, reject) => {
  1294. async.waterfall(
  1295. [
  1296. next => {
  1297. if (!payload.stationId) next("Please specify a station id");
  1298. else if (!payload.playlistId) next("Please specify a playlist id");
  1299. else next();
  1300. },
  1301. next => {
  1302. StationsModule.runJob("GET_STATION", { stationId: payload.stationId }, this)
  1303. .then(station => {
  1304. next(null, station);
  1305. })
  1306. .catch(next);
  1307. },
  1308. (station, next) => {
  1309. if (station.playlist === payload.playlistId) next("You cannot autofill the station playlist");
  1310. else if (station.autofill.playlists.indexOf(payload.playlistId) !== -1)
  1311. next("This playlist is already autofilling");
  1312. else if (station.blacklist.indexOf(payload.playlistId) !== -1)
  1313. next("This playlist is currently blacklisted");
  1314. else
  1315. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId: payload.playlistId }, this)
  1316. .then(() => {
  1317. next(null);
  1318. })
  1319. .catch(next);
  1320. },
  1321. next => {
  1322. StationsModule.stationModel.updateOne(
  1323. { _id: payload.stationId },
  1324. { $push: { "autofill.playlists": payload.playlistId } },
  1325. next
  1326. );
  1327. },
  1328. (res, next) => {
  1329. StationsModule.runJob(
  1330. "UPDATE_STATION",
  1331. {
  1332. stationId: payload.stationId
  1333. },
  1334. this
  1335. )
  1336. .then(() => {
  1337. next();
  1338. })
  1339. .catch(next);
  1340. }
  1341. ],
  1342. async err => {
  1343. if (err && err !== true) {
  1344. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1345. return reject(new Error(err));
  1346. }
  1347. return resolve();
  1348. }
  1349. );
  1350. });
  1351. }
  1352. /**
  1353. * Removes a playlist from autofill
  1354. *
  1355. * @param {object} payload - object that contains the payload
  1356. * @param {object} payload.stationId - the id of the station
  1357. * @param {object} payload.playlistId - the id of the playlist
  1358. * @returns {Promise} - returns a promise (resolve, reject)
  1359. */
  1360. REMOVE_AUTOFILL_PLAYLIST(payload) {
  1361. return new Promise((resolve, reject) => {
  1362. async.waterfall(
  1363. [
  1364. next => {
  1365. if (!payload.stationId) next("Please specify a station id");
  1366. else if (!payload.playlistId) next("Please specify a playlist id");
  1367. else next();
  1368. },
  1369. next => {
  1370. StationsModule.runJob("GET_STATION", { stationId: payload.stationId }, this)
  1371. .then(station => {
  1372. next(null, station);
  1373. })
  1374. .catch(next);
  1375. },
  1376. (station, next) => {
  1377. if (station.autofill.playlists.indexOf(payload.playlistId) === -1)
  1378. next("This playlist isn't autofilling");
  1379. else next();
  1380. },
  1381. next => {
  1382. StationsModule.stationModel.updateOne(
  1383. { _id: payload.stationId },
  1384. { $pull: { "autofill.playlists": payload.playlistId } },
  1385. next
  1386. );
  1387. },
  1388. (res, next) => {
  1389. StationsModule.runJob(
  1390. "UPDATE_STATION",
  1391. {
  1392. stationId: payload.stationId
  1393. },
  1394. this
  1395. )
  1396. .then(() => {
  1397. next();
  1398. })
  1399. .catch(next);
  1400. }
  1401. ],
  1402. async err => {
  1403. if (err && err !== true) {
  1404. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1405. return reject(new Error(err));
  1406. }
  1407. return resolve();
  1408. }
  1409. );
  1410. });
  1411. }
  1412. /**
  1413. * Add a playlist to station blacklist
  1414. *
  1415. * @param {object} payload - object that contains the payload
  1416. * @param {object} payload.stationId - the id of the station
  1417. * @param {object} payload.playlistId - the id of the playlist
  1418. * @returns {Promise} - returns a promise (resolve, reject)
  1419. */
  1420. BLACKLIST_PLAYLIST(payload) {
  1421. return new Promise((resolve, reject) => {
  1422. async.waterfall(
  1423. [
  1424. next => {
  1425. if (!payload.stationId) next("Please specify a station id");
  1426. else if (!payload.playlistId) next("Please specify a playlist id");
  1427. else next();
  1428. },
  1429. next => {
  1430. StationsModule.runJob("GET_STATION", { stationId: payload.stationId }, this)
  1431. .then(station => {
  1432. next(null, station);
  1433. })
  1434. .catch(next);
  1435. },
  1436. (station, next) => {
  1437. if (station.playlist === payload.playlistId) next("You cannot blacklist the station playlist");
  1438. else if (station.blacklist.indexOf(payload.playlistId) !== -1)
  1439. next("This playlist is already blacklisted");
  1440. else if (station.autofill.playlists.indexOf(payload.playlistId) !== -1)
  1441. next(
  1442. "This playlist is currently autofilling, please remove it from there before blacklisting it"
  1443. );
  1444. else
  1445. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId: payload.playlistId }, this)
  1446. .then(() => {
  1447. next(null);
  1448. })
  1449. .catch(next);
  1450. },
  1451. next => {
  1452. StationsModule.stationModel.updateOne(
  1453. { _id: payload.stationId },
  1454. { $push: { blacklist: payload.playlistId } },
  1455. next
  1456. );
  1457. },
  1458. (res, next) => {
  1459. StationsModule.runJob(
  1460. "UPDATE_STATION",
  1461. {
  1462. stationId: payload.stationId
  1463. },
  1464. this
  1465. )
  1466. .then(() => {
  1467. next();
  1468. })
  1469. .catch(next);
  1470. }
  1471. ],
  1472. async err => {
  1473. if (err && err !== true) {
  1474. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1475. return reject(new Error(err));
  1476. }
  1477. return resolve();
  1478. }
  1479. );
  1480. });
  1481. }
  1482. /**
  1483. * Remove a playlist from station blacklist
  1484. *
  1485. * @param {object} payload - object that contains the payload
  1486. * @param {object} payload.stationId - the id of the station
  1487. * @param {object} payload.playlistId - the id of the playlist
  1488. * @returns {Promise} - returns a promise (resolve, reject)
  1489. */
  1490. REMOVE_BLACKLISTED_PLAYLIST(payload) {
  1491. return new Promise((resolve, reject) => {
  1492. async.waterfall(
  1493. [
  1494. next => {
  1495. if (!payload.stationId) next("Please specify a station id");
  1496. else if (!payload.playlistId) next("Please specify a playlist id");
  1497. else next();
  1498. },
  1499. next => {
  1500. StationsModule.runJob("GET_STATION", { stationId: payload.stationId }, this)
  1501. .then(station => {
  1502. next(null, station);
  1503. })
  1504. .catch(next);
  1505. },
  1506. (station, next) => {
  1507. if (station.blacklist.indexOf(payload.playlistId) === -1)
  1508. next("This playlist isn't blacklisted");
  1509. else next();
  1510. },
  1511. next => {
  1512. StationsModule.stationModel.updateOne(
  1513. { _id: payload.stationId },
  1514. { $pull: { blacklist: payload.playlistId } },
  1515. next
  1516. );
  1517. },
  1518. (res, next) => {
  1519. StationsModule.runJob(
  1520. "UPDATE_STATION",
  1521. {
  1522. stationId: payload.stationId
  1523. },
  1524. this
  1525. )
  1526. .then(() => {
  1527. next();
  1528. })
  1529. .catch(next);
  1530. }
  1531. ],
  1532. async err => {
  1533. if (err && err !== true) {
  1534. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1535. return reject(new Error(err));
  1536. }
  1537. return resolve();
  1538. }
  1539. );
  1540. });
  1541. }
  1542. /**
  1543. * Removes autofilled or blacklisted playlist from a station
  1544. *
  1545. * @param {object} payload - object that contains the payload
  1546. * @param {string} payload.playlistId - the playlist id
  1547. * @returns {Promise} - returns promise (reject, resolve)
  1548. */
  1549. REMOVE_AUTOFILLED_OR_BLACKLISTED_PLAYLIST_FROM_STATIONS(payload) {
  1550. return new Promise((resolve, reject) => {
  1551. async.waterfall(
  1552. [
  1553. next => {
  1554. if (!payload.playlistId) next("Please specify a playlist id");
  1555. else next();
  1556. },
  1557. next => {
  1558. StationsModule.stationModel.updateMany(
  1559. {
  1560. $or: [{ "autofill.playlists": payload.playlistId }, { blacklist: payload.playlistId }]
  1561. },
  1562. {
  1563. $pull: {
  1564. "autofill.playlists": payload.playlistId,
  1565. blacklist: payload.playlistId
  1566. }
  1567. },
  1568. err => {
  1569. if (err) next(err);
  1570. else next();
  1571. }
  1572. );
  1573. }
  1574. ],
  1575. async err => {
  1576. if (err && err !== true) {
  1577. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1578. return reject(new Error(err));
  1579. }
  1580. return resolve();
  1581. }
  1582. );
  1583. });
  1584. }
  1585. /**
  1586. * Gets stations that autofill or blacklist a specific playlist
  1587. *
  1588. * @param {object} payload - object that contains the payload
  1589. * @param {string} payload.playlistId - the playlist id
  1590. * @returns {Promise} - returns promise (reject, resolve)
  1591. */
  1592. GET_STATIONS_THAT_AUTOFILL_OR_BLACKLIST_PLAYLIST(payload) {
  1593. return new Promise((resolve, reject) => {
  1594. DBModule.runJob(
  1595. "GET_MODEL",
  1596. {
  1597. modelName: "station"
  1598. },
  1599. this
  1600. ).then(stationModel => {
  1601. stationModel.find(
  1602. {
  1603. $or: [{ "autofill.playlists": payload.playlistId }, { blacklist: payload.playlistId }]
  1604. },
  1605. (err, stations) => {
  1606. if (err) reject(err);
  1607. else resolve({ stationIds: stations.map(station => station._id) });
  1608. }
  1609. );
  1610. });
  1611. });
  1612. }
  1613. /**
  1614. * Clears every queue
  1615. *
  1616. * @returns {Promise} - returns a promise (resolve, reject)
  1617. */
  1618. CLEAR_EVERY_STATION_QUEUE() {
  1619. return new Promise((resolve, reject) => {
  1620. async.waterfall(
  1621. [
  1622. next => {
  1623. StationsModule.stationModel.updateMany({}, { $set: { queue: [] } }, err => {
  1624. if (err) next(err);
  1625. else {
  1626. StationsModule.stationModel.find({}, (err, stations) => {
  1627. if (err) next(err);
  1628. else {
  1629. async.eachLimit(
  1630. stations,
  1631. 1,
  1632. (station, next) => {
  1633. this.publishProgress({
  1634. status: "update",
  1635. message: `Updating station "${station._id}"`
  1636. });
  1637. StationsModule.runJob("UPDATE_STATION", {
  1638. stationId: station._id
  1639. })
  1640. .then(() => next())
  1641. .catch(next);
  1642. CacheModule.runJob("PUB", {
  1643. channel: "station.queueUpdate",
  1644. value: station._id
  1645. })
  1646. .then()
  1647. .catch();
  1648. },
  1649. next
  1650. );
  1651. }
  1652. });
  1653. }
  1654. });
  1655. }
  1656. ],
  1657. err => {
  1658. if (err) reject(err);
  1659. else resolve();
  1660. }
  1661. );
  1662. });
  1663. }
  1664. /**
  1665. * Resets a station queue
  1666. *
  1667. * @param {object} payload - object that contains the payload
  1668. * @param {string} payload.stationId - the station id
  1669. * @returns {Promise} - returns a promise (resolve, reject)
  1670. */
  1671. RESET_QUEUE(payload) {
  1672. return new Promise((resolve, reject) => {
  1673. async.waterfall(
  1674. [
  1675. next => {
  1676. StationsModule.runJob(
  1677. "AUTOFILL_STATION",
  1678. { stationId: payload.stationId, ignoreExistingQueue: true },
  1679. this
  1680. )
  1681. .then(() => {
  1682. CacheModule.runJob("PUB", {
  1683. channel: "station.queueUpdate",
  1684. value: payload.stationId
  1685. })
  1686. .then()
  1687. .catch();
  1688. next();
  1689. })
  1690. .catch(err => {
  1691. if (err === "Autofill is disabled in this station" || err === "Autofill limit reached")
  1692. StationsModule.stationModel
  1693. .updateOne({ _id: payload.stationId }, { $set: { queue: [] } }, this)
  1694. .then(() => next())
  1695. .catch(next);
  1696. else next(err);
  1697. });
  1698. },
  1699. next => {
  1700. StationsModule.runJob("UPDATE_STATION", { stationId: payload.stationId }, this)
  1701. .then(() => next())
  1702. .catch(next);
  1703. },
  1704. next => {
  1705. CacheModule.runJob("PUB", {
  1706. channel: "station.queueUpdate",
  1707. value: payload.stationId
  1708. })
  1709. .then(() => next())
  1710. .catch(next);
  1711. }
  1712. ],
  1713. err => {
  1714. if (err) reject(err);
  1715. else resolve();
  1716. }
  1717. );
  1718. });
  1719. }
  1720. /**
  1721. * Add to a station queue
  1722. *
  1723. * @param {object} payload - object that contains the payload
  1724. * @param {string} payload.stationId - the station id
  1725. * @param {string} payload.youtubeId - the youtube id
  1726. * @param {string} payload.requestUser - the requesting user id
  1727. * @returns {Promise} - returns a promise (resolve, reject)
  1728. */
  1729. ADD_TO_QUEUE(payload) {
  1730. return new Promise((resolve, reject) => {
  1731. const { stationId, youtubeId, requestUser } = payload;
  1732. async.waterfall(
  1733. [
  1734. next => {
  1735. StationsModule.runJob("GET_STATION", { stationId }, this)
  1736. .then(station => {
  1737. next(null, station);
  1738. })
  1739. .catch(next);
  1740. },
  1741. (station, next) => {
  1742. if (!station) return next("Station not found.");
  1743. if (!station.requests.enabled) return next("Requests are disabled in this station.");
  1744. if (station.currentSong && station.currentSong.youtubeId === youtubeId)
  1745. return next("That song is currently playing.");
  1746. if (station.queue.find(song => song.youtubeId === youtubeId))
  1747. return next("That song is already in the queue.");
  1748. return next(null, station);
  1749. },
  1750. (station, next) => {
  1751. MediaModule.runJob("GET_MEDIA", { youtubeId }, this)
  1752. .then(response => {
  1753. const { song } = response;
  1754. const { _id, title, skipDuration, artists, thumbnail, duration, verified } = song;
  1755. next(
  1756. null,
  1757. {
  1758. _id,
  1759. youtubeId,
  1760. title,
  1761. skipDuration,
  1762. artists,
  1763. thumbnail,
  1764. duration,
  1765. verified
  1766. },
  1767. station
  1768. );
  1769. })
  1770. .catch(next);
  1771. },
  1772. (song, station, next) => {
  1773. const blacklist = [];
  1774. async.eachLimit(
  1775. station.blacklist,
  1776. 1,
  1777. (playlistId, next) => {
  1778. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1779. .then(playlist => {
  1780. blacklist.push(playlist);
  1781. next();
  1782. })
  1783. .catch(next);
  1784. },
  1785. err => {
  1786. next(err, song, station, blacklist);
  1787. }
  1788. );
  1789. },
  1790. (song, station, blacklist, next) => {
  1791. const blacklistedSongs = blacklist
  1792. .flatMap(blacklistedPlaylist => blacklistedPlaylist.songs)
  1793. .reduce(
  1794. (items, item) =>
  1795. items.find(x => x.youtubeId === item.youtubeId) ? [...items] : [...items, item],
  1796. []
  1797. );
  1798. if (blacklistedSongs.find(blacklistedSong => blacklistedSong.youtubeId === song.youtubeId))
  1799. next("That song is in an blacklisted playlist and cannot be played.");
  1800. else next(null, song, station);
  1801. },
  1802. (song, station, next) => {
  1803. song.requestedBy = requestUser;
  1804. song.requestedAt = Date.now();
  1805. if (station.queue.length === 0) return next(null, song, station);
  1806. if (
  1807. requestUser &&
  1808. station.queue.filter(queueSong => queueSong.requestedBy === song.requestedBy).length >=
  1809. station.requests.limit
  1810. )
  1811. return next(`The max amount of songs per user is ${station.requests.limit}.`);
  1812. return next(null, song, station);
  1813. },
  1814. // (song, station, next) => {
  1815. // song.requestedBy = session.userId;
  1816. // song.requestedAt = Date.now();
  1817. // let totalDuration = 0;
  1818. // station.queue.forEach(song => {
  1819. // totalDuration += song.duration;
  1820. // });
  1821. // if (totalDuration >= 3600 * 3) return next("The max length of the queue is 3 hours.");
  1822. // return next(null, song, station);
  1823. // },
  1824. // (song, station, next) => {
  1825. // if (station.queue.length === 0) return next(null, song, station);
  1826. // let totalDuration = 0;
  1827. // const userId = station.queue[station.queue.length - 1].requestedBy;
  1828. // station.queue.forEach(song => {
  1829. // if (userId === song.requestedBy) {
  1830. // totalDuration += song.duration;
  1831. // }
  1832. // });
  1833. // if (totalDuration >= 900) return next("The max length of songs per user is 15 minutes.");
  1834. // return next(null, song, station);
  1835. // },
  1836. // (song, station, next) => {
  1837. // if (station.queue.length === 0) return next(null, song);
  1838. // let totalSongs = 0;
  1839. // const userId = station.queue[station.queue.length - 1].requestedBy;
  1840. // station.queue.forEach(song => {
  1841. // if (userId === song.requestedBy) {
  1842. // totalSongs += 1;
  1843. // }
  1844. // });
  1845. // if (totalSongs <= 2) return next(null, song);
  1846. // if (totalSongs > 3)
  1847. // return next("The max amount of songs per user is 3, and only 2 in a row is allowed.");
  1848. // if (
  1849. // station.queue[station.queue.length - 2].requestedBy !== userId ||
  1850. // station.queue[station.queue.length - 3] !== userId
  1851. // )
  1852. // return next("The max amount of songs per user is 3, and only 2 in a row is allowed.");
  1853. // return next(null, song);
  1854. // },
  1855. (song, station, next) => {
  1856. if (config.has(`experimental.queue_add_before_autofilled`)) {
  1857. const queueAddBeforeAutofilled = config.get(`experimental.queue_add_before_autofilled`);
  1858. if (
  1859. queueAddBeforeAutofilled === true ||
  1860. (Array.isArray(queueAddBeforeAutofilled) &&
  1861. queueAddBeforeAutofilled.indexOf(stationId) !== -1)
  1862. ) {
  1863. let position = station.queue.length;
  1864. if (station.autofill.enabled && station.queue.length >= station.autofill.limit) {
  1865. position = -station.autofill.limit;
  1866. }
  1867. StationsModule.stationModel.updateOne(
  1868. { _id: stationId },
  1869. {
  1870. $push: {
  1871. queue: {
  1872. $each: [song],
  1873. $position: position
  1874. }
  1875. }
  1876. },
  1877. { runValidators: true },
  1878. next
  1879. );
  1880. return;
  1881. }
  1882. }
  1883. StationsModule.stationModel.updateOne(
  1884. { _id: stationId },
  1885. { $push: { queue: song } },
  1886. { runValidators: true },
  1887. next
  1888. );
  1889. },
  1890. (res, next) => {
  1891. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  1892. .then(() => next())
  1893. .catch(next);
  1894. },
  1895. next => {
  1896. CacheModule.runJob(
  1897. "PUB",
  1898. {
  1899. channel: "station.queueUpdate",
  1900. value: stationId
  1901. },
  1902. this
  1903. )
  1904. .then(() => next())
  1905. .catch(next);
  1906. }
  1907. ],
  1908. err => {
  1909. if (err) reject(err);
  1910. else resolve();
  1911. }
  1912. );
  1913. });
  1914. }
  1915. /**
  1916. * Remove from a station queue
  1917. *
  1918. * @param {object} payload - object that contains the payload
  1919. * @param {string} payload.stationId - the station id
  1920. * @param {string} payload.youtubeId - the youtube id
  1921. * @returns {Promise} - returns a promise (resolve, reject)
  1922. */
  1923. REMOVE_FROM_QUEUE(payload) {
  1924. return new Promise((resolve, reject) => {
  1925. const { stationId, youtubeId } = payload;
  1926. async.waterfall(
  1927. [
  1928. next => {
  1929. StationsModule.runJob("GET_STATION", { stationId }, this)
  1930. .then(station => {
  1931. next(null, station);
  1932. })
  1933. .catch(next);
  1934. },
  1935. (station, next) => {
  1936. if (!station) return next("Station not found.");
  1937. if (!station.queue.find(song => song.youtubeId === youtubeId))
  1938. return next("That song is not currently in the queue.");
  1939. return StationsModule.stationModel.updateOne(
  1940. { _id: stationId },
  1941. { $pull: { queue: { youtubeId } } },
  1942. next
  1943. );
  1944. },
  1945. (res, next) => {
  1946. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  1947. .then(station => {
  1948. if (station.autofill.enabled)
  1949. StationsModule.runJob("AUTOFILL_STATION", { stationId }, this)
  1950. .then(() => next())
  1951. .catch(err => {
  1952. if (
  1953. err === "Autofill is disabled in this station" ||
  1954. err === "Autofill limit reached"
  1955. )
  1956. return next();
  1957. return next(err);
  1958. });
  1959. else next();
  1960. })
  1961. .catch(next);
  1962. },
  1963. next =>
  1964. CacheModule.runJob(
  1965. "PUB",
  1966. {
  1967. channel: "station.queueUpdate",
  1968. value: stationId
  1969. },
  1970. this
  1971. )
  1972. .then(() => next())
  1973. .catch(next)
  1974. ],
  1975. err => {
  1976. if (err) reject(err);
  1977. else resolve();
  1978. }
  1979. );
  1980. });
  1981. }
  1982. /**
  1983. * Add DJ to station
  1984. *
  1985. * @param {object} payload - object that contains the payload
  1986. * @param {string} payload.stationId - the station id
  1987. * @param {string} payload.userId - the dj user id
  1988. * @returns {Promise} - returns a promise (resolve, reject)
  1989. */
  1990. ADD_DJ(payload) {
  1991. return new Promise((resolve, reject) => {
  1992. const { stationId, userId } = payload;
  1993. async.waterfall(
  1994. [
  1995. next => {
  1996. StationsModule.runJob("GET_STATION", { stationId }, this)
  1997. .then(station => {
  1998. next(null, station);
  1999. })
  2000. .catch(next);
  2001. },
  2002. (station, next) => {
  2003. if (!station) return next("Station not found.");
  2004. if (station.djs.find(dj => dj === userId)) return next("That user is already a DJ.");
  2005. return StationsModule.stationModel.updateOne(
  2006. { _id: stationId },
  2007. { $push: { djs: userId } },
  2008. next
  2009. );
  2010. },
  2011. (res, next) => {
  2012. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  2013. .then(() => next())
  2014. .catch(next);
  2015. },
  2016. next =>
  2017. CacheModule.runJob(
  2018. "PUB",
  2019. {
  2020. channel: "station.djs.added",
  2021. value: { stationId, userId }
  2022. },
  2023. this
  2024. )
  2025. .then(() => next())
  2026. .catch(next)
  2027. ],
  2028. err => {
  2029. if (err) reject(err);
  2030. else resolve();
  2031. }
  2032. );
  2033. });
  2034. }
  2035. /**
  2036. * Remove DJ from station
  2037. *
  2038. * @param {object} payload - object that contains the payload
  2039. * @param {string} payload.stationId - the station id
  2040. * @param {string} payload.userId - the dj user id
  2041. * @returns {Promise} - returns a promise (resolve, reject)
  2042. */
  2043. REMOVE_DJ(payload) {
  2044. return new Promise((resolve, reject) => {
  2045. const { stationId, userId } = payload;
  2046. async.waterfall(
  2047. [
  2048. next => {
  2049. StationsModule.runJob("GET_STATION", { stationId }, this)
  2050. .then(station => {
  2051. next(null, station);
  2052. })
  2053. .catch(next);
  2054. },
  2055. (station, next) => {
  2056. if (!station) return next("Station not found.");
  2057. if (!station.djs.find(dj => dj === userId)) return next("That user is not currently a DJ.");
  2058. return StationsModule.stationModel.updateOne(
  2059. { _id: stationId },
  2060. { $pull: { djs: userId } },
  2061. next
  2062. );
  2063. },
  2064. (res, next) => {
  2065. StationsModule.runJob("UPDATE_STATION", { stationId }, this)
  2066. .then(() => next())
  2067. .catch(next);
  2068. },
  2069. next =>
  2070. CacheModule.runJob(
  2071. "PUB",
  2072. {
  2073. channel: "station.djs.removed",
  2074. value: { stationId, userId }
  2075. },
  2076. this
  2077. )
  2078. .then(() => next())
  2079. .catch(next)
  2080. ],
  2081. err => {
  2082. if (err) reject(err);
  2083. else resolve();
  2084. }
  2085. );
  2086. });
  2087. }
  2088. }
  2089. export default new _StationsModule();