stations.js 76 KB

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