1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110 |
- import async from "async";
- import { isLoginRequired, isOwnerRequired } from "./hooks";
- import moduleManager from "../../index";
- const DBModule = moduleManager.modules.db;
- const UtilsModule = moduleManager.modules.utils;
- const IOModule = moduleManager.modules.io;
- const SongsModule = moduleManager.modules.songs;
- const CacheModule = moduleManager.modules.cache;
- const NotificationsModule = moduleManager.modules.notifications;
- const StationsModule = moduleManager.modules.stations;
- const ActivitiesModule = moduleManager.modules.activities;
- const YouTubeModule = moduleManager.modules.youtube;
- CacheModule.runJob("SUB", {
- channel: "station.updateUsers",
- cb: stationId => {
- const list = StationsModule.usersPerStation[stationId] || [];
- IOModule.runJob("EMIT_TO_ROOM", {
- room: `station.${stationId}`,
- args: ["event:users.updated", list]
- });
- }
- });
- CacheModule.runJob("SUB", {
- channel: "station.updateUserCount",
- cb: stationId => {
- const count = StationsModule.usersPerStationCount[stationId] || 0;
- IOModule.runJob("EMIT_TO_ROOM", {
- room: `station.${stationId}`,
- args: ["event:userCount.updated", count]
- });
- StationsModule.runJob("GET_STATION", { stationId }).then(async station => {
- if (station.privacy === "public")
- IOModule.runJob("EMIT_TO_ROOM", {
- room: "home",
- args: ["event:userCount.updated", stationId, count]
- });
- else {
- const sockets = await IOModule.runJob("GET_ROOM_SOCKETS", {
- room: "home"
- });
- Object.keys(sockets).forEach(socketKey => {
- const socket = sockets[socketKey];
- const { session } = socket;
- if (session.sessionId) {
- CacheModule.runJob("HGET", {
- table: "sessions",
- key: session.sessionId
- }).then(session => {
- if (session)
- DBModule.runJob("GET_MODEL", {
- modelName: "user"
- }).then(userModel =>
- userModel.findOne({ _id: session.userId }, (err, user) => {
- if (user.role === "admin")
- socket.emit("event:userCount.updated", stationId, count);
- else if (station.type === "community" && station.owner === session.userId)
- socket.emit("event:userCount.updated", stationId, count);
- })
- );
- });
- }
- });
- }
- });
- }
- });
- CacheModule.runJob("SUB", {
- channel: "station.queueLockToggled",
- cb: data => {
- IOModule.runJob("EMIT_TO_ROOM", {
- room: `station.${data.stationId}`,
- args: ["event:queueLockToggled", data.locked]
- });
- }
- });
- CacheModule.runJob("SUB", {
- channel: "station.updatePartyMode",
- cb: data => {
- IOModule.runJob("EMIT_TO_ROOM", {
- room: `station.${data.stationId}`,
- args: ["event:partyMode.updated", data.partyMode]
- });
- }
- });
- CacheModule.runJob("SUB", {
- channel: "privatePlaylist.selected",
- cb: data => {
- IOModule.runJob("EMIT_TO_ROOM", {
- room: `station.${data.stationId}`,
- args: ["event:privatePlaylist.selected", data.playlistId]
- });
- }
- });
- CacheModule.runJob("SUB", {
- channel: "station.pause",
- cb: stationId => {
- StationsModule.runJob("GET_STATION", { stationId }).then(station => {
- IOModule.runJob("EMIT_TO_ROOM", {
- room: `station.${stationId}`,
- args: ["event:stations.pause", { pausedAt: station.pausedAt }]
- });
- });
- }
- });
- CacheModule.runJob("SUB", {
- channel: "station.resume",
- cb: stationId => {
- StationsModule.runJob("GET_STATION", { stationId }).then(station => {
- IOModule.runJob("EMIT_TO_ROOM", {
- room: `station.${stationId}`,
- args: ["event:stations.resume", { timePaused: station.timePaused }]
- });
- });
- }
- });
- CacheModule.runJob("SUB", {
- channel: "station.queueUpdate",
- cb: stationId => {
- StationsModule.runJob("GET_STATION", { stationId }).then(station => {
- IOModule.runJob("EMIT_TO_ROOM", {
- room: `station.${stationId}`,
- args: ["event:queue.update", station.queue]
- });
- });
- }
- });
- CacheModule.runJob("SUB", {
- channel: "station.voteSkipSong",
- cb: stationId => {
- IOModule.runJob("EMIT_TO_ROOM", {
- room: `station.${stationId}`,
- args: ["event:song.voteSkipSong"]
- });
- }
- });
- CacheModule.runJob("SUB", {
- channel: "station.remove",
- cb: stationId => {
- IOModule.runJob("EMIT_TO_ROOM", {
- room: `station.${stationId}`,
- args: ["event:stations.remove"]
- });
- IOModule.runJob("EMIT_TO_ROOM", {
- room: "admin.stations",
- args: ["event:admin.station.removed", stationId]
- });
- }
- });
- CacheModule.runJob("SUB", {
- channel: "station.create",
- cb: async stationId => {
- const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" });
- StationsModule.runJob("INITIALIZE_STATION", { stationId }).then(async response => {
- const { station } = response;
- station.userCount = StationsModule.usersPerStationCount[stationId] || 0;
- IOModule.runJob("EMIT_TO_ROOM", {
- room: "admin.stations",
- args: ["event:admin.station.added", station]
- });
- // TODO If community, check if on whitelist
- if (station.privacy === "public")
- IOModule.runJob("EMIT_TO_ROOM", {
- room: "home",
- args: ["event:stations.created", station]
- });
- else {
- const sockets = await IOModule.runJob("GET_ROOM_SOCKETS", {
- room: "home"
- });
- Object.keys(sockets).forEach(socketKey => {
- const socket = sockets[socketKey];
- const { session } = socket;
- if (session.sessionId) {
- CacheModule.runJob("HGET", {
- table: "sessions",
- key: session.sessionId
- }).then(session => {
- if (session) {
- userModel.findOne({ _id: session.userId }, (err, user) => {
- if (user.role === "admin") socket.emit("event:stations.created", station);
- else if (station.type === "community" && station.owner === session.userId)
- socket.emit("event:stations.created", station);
- });
- }
- });
- }
- });
- }
- });
- }
- });
- export default {
- /**
- * Get a list of all the stations
- *
- * @param {object} session - user session
- * @param {Function} cb - callback
- */
- index(session, cb) {
- async.waterfall(
- [
- next => {
- CacheModule.runJob("HGETALL", { table: "stations" }, this).then(stations => {
- next(null, stations);
- });
- },
- (items, next) => {
- const filteredStations = [];
- async.each(
- items,
- (station, next) => {
- async.waterfall(
- [
- next => {
- StationsModule.runJob(
- "CAN_USER_VIEW_STATION",
- {
- station,
- userId: session.userId,
- hideUnlisted: true
- },
- this
- )
- .then(exists => {
- next(null, exists);
- })
- .catch(next);
- }
- ],
- (err, exists) => {
- if (err) this.log(err);
- station.userCount = StationsModule.usersPerStationCount[station._id] || 0;
- if (exists) filteredStations.push(station);
- next();
- }
- );
- },
- () => next(null, filteredStations)
- );
- }
- ],
- async (err, stations) => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log("ERROR", "STATIONS_INDEX", `Indexing stations failed. "${err}"`);
- return cb({ status: "failure", message: err });
- }
- this.log("SUCCESS", "STATIONS_INDEX", `Indexing stations successful.`, false);
- return cb({ status: "success", stations });
- }
- );
- },
- /**
- * Obtains basic metadata of a station in order to format an activity
- *
- * @param {object} session - user session
- * @param {string} stationId - the station id
- * @param {Function} cb - callback
- */
- getStationForActivity(session, stationId, cb) {
- async.waterfall(
- [
- next => {
- StationsModule.runJob("GET_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- }
- ],
- async (err, station) => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log(
- "ERROR",
- "STATIONS_GET_STATION_FOR_ACTIVITY",
- `Failed to obtain metadata of station ${stationId} for activity formatting. "${err}"`
- );
- return cb({ status: "failure", message: err });
- }
- this.log(
- "SUCCESS",
- "STATIONS_GET_STATION_FOR_ACTIVITY",
- `Obtained metadata of station ${stationId} for activity formatting successfully.`
- );
- return cb({
- status: "success",
- data: {
- title: station.displayName,
- thumbnail: station.currentSong ? station.currentSong.thumbnail : ""
- }
- });
- }
- );
- },
- /**
- * Verifies that a station exists
- *
- * @param {object} session - user session
- * @param {string} stationName - the station name
- * @param {Function} cb - callback
- */
- existsByName(session, stationName, cb) {
- async.waterfall(
- [
- next => {
- StationsModule.runJob("GET_STATION_BY_NAME", { stationName }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- },
- (station, next) => {
- if (!station) return next(null, false);
- return StationsModule.runJob(
- "CAN_USER_VIEW_STATION",
- {
- station,
- userId: session.userId
- },
- this
- )
- .then(exists => {
- next(null, exists);
- })
- .catch(next);
- }
- ],
- async (err, exists) => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log(
- "ERROR",
- "STATION_EXISTS_BY_NAME",
- `Checking if station "${stationName}" exists failed. "${err}"`
- );
- return cb({ status: "failure", message: err });
- }
- this.log(
- "SUCCESS",
- "STATION_EXISTS_BY_NAME",
- `Station "${stationName}" exists successfully.` /* , false */
- );
- return cb({ status: "success", exists });
- }
- );
- },
- /**
- * Gets the official playlist for a station
- *
- * @param {object} session - user session
- * @param {string} stationId - the station id
- * @param {Function} cb - callback
- */
- getPlaylist(session, stationId, cb) {
- async.waterfall(
- [
- next => {
- StationsModule.runJob("GET_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- },
- (station, next) => {
- StationsModule.runJob(
- "CAN_USER_VIEW_STATION",
- {
- station,
- userId: session.userId
- },
- this
- )
- .then(canView => {
- if (canView) return next(null, station);
- return next("Insufficient permissions.");
- })
- .catch(err => next(err));
- },
- (station, next) => {
- if (!station) return next("Station not found.");
- if (station.type !== "official") return next("This is not an official station.");
- return next();
- },
- next => {
- CacheModule.runJob(
- "HGET",
- {
- table: "officialPlaylists",
- key: stationId
- },
- this
- )
- .then(playlist => {
- next(null, playlist);
- })
- .catch(next);
- },
- (playlist, next) => {
- if (!playlist) return next("Playlist not found.");
- return next(null, playlist);
- }
- ],
- async (err, playlist) => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log(
- "ERROR",
- "STATIONS_GET_PLAYLIST",
- `Getting playlist for station "${stationId}" failed. "${err}"`
- );
- return cb({ status: "failure", message: err });
- }
- this.log(
- "SUCCESS",
- "STATIONS_GET_PLAYLIST",
- `Got playlist for station "${stationId}" successfully.`,
- false
- );
- return cb({ status: "success", data: playlist.songs });
- }
- );
- },
- /**
- * Joins the station by its name
- *
- * @param {object} session - user session
- * @param {string} stationName - the station name
- * @param {Function} cb - callback
- */
- join(session, stationName, cb) {
- async.waterfall(
- [
- next => {
- StationsModule.runJob("GET_STATION_BY_NAME", { stationName }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- },
- (station, next) => {
- if (!station) return next("Station not found.");
- return StationsModule.runJob(
- "CAN_USER_VIEW_STATION",
- {
- station,
- userId: session.userId
- },
- this
- )
- .then(canView => {
- if (!canView) next("Not allowed to join station.");
- else next(null, station);
- })
- .catch(err => next(err));
- },
- (station, next) => {
- IOModule.runJob("SOCKET_JOIN_ROOM", {
- socketId: session.socketId,
- room: `station.${station._id}`
- });
- const data = {
- _id: station._id,
- type: station.type,
- currentSong: station.currentSong,
- startedAt: station.startedAt,
- paused: station.paused,
- timePaused: station.timePaused,
- pausedAt: station.pausedAt,
- description: station.description,
- displayName: station.displayName,
- privacy: station.privacy,
- locked: station.locked,
- partyMode: station.partyMode,
- owner: station.owner,
- privatePlaylist: station.privatePlaylist,
- genres: station.genres,
- blacklistedGenres: station.blacklistedGenres
- };
- StationsModule.userList[session.socketId] = station._id;
- next(null, data);
- },
- (data, next) => {
- data = JSON.parse(JSON.stringify(data));
- data.userCount = StationsModule.usersPerStationCount[data._id] || 0;
- data.users = StationsModule.usersPerStation[data._id] || [];
- if (!data.currentSong || !data.currentSong.title) return next(null, data);
- IOModule.runJob("SOCKET_JOIN_SONG_ROOM", {
- socketId: session.socketId,
- room: `song.${data.currentSong.songId}`
- });
- data.currentSong.skipVotes = data.currentSong.skipVotes.length;
- return SongsModule.runJob(
- "GET_SONG_FROM_ID",
- {
- songId: data.currentSong.songId
- },
- this
- )
- .then(response => {
- const { song } = response;
- if (song) {
- data.currentSong.likes = song.likes;
- data.currentSong.dislikes = song.dislikes;
- } else {
- data.currentSong.likes = -1;
- data.currentSong.dislikes = -1;
- }
- })
- .catch(() => {
- data.currentSong.likes = -1;
- data.currentSong.dislikes = -1;
- })
- .finally(() => {
- next(null, data);
- });
- }
- ],
- async (err, data) => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log("ERROR", "STATIONS_JOIN", `Joining station "${stationName}" failed. "${err}"`);
- return cb({ status: "failure", message: err });
- }
- this.log("SUCCESS", "STATIONS_JOIN", `Joined station "${data._id}" successfully.`);
- return cb({ status: "success", data });
- }
- );
- },
- /**
- * Toggles if a station is locked
- *
- * @param session
- * @param stationId - the station id
- * @param cb
- */
- toggleLock: isOwnerRequired(async function toggleLock(session, stationId, cb) {
- const stationModel = await DBModule.runJob(
- "GET_MODEL",
- {
- modelName: "station"
- },
- this
- );
- async.waterfall(
- [
- next => {
- StationsModule.runJob("GET_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- },
- (station, next) => {
- stationModel.updateOne({ _id: stationId }, { $set: { locked: !station.locked } }, next);
- },
- (res, next) => {
- StationsModule.runJob("UPDATE_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- }
- ],
- async (err, station) => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log(
- "ERROR",
- "STATIONS_UPDATE_LOCKED_STATUS",
- `Toggling the queue lock for station "${stationId}" failed. "${err}"`
- );
- return cb({ status: "failure", message: err });
- }
- this.log(
- "SUCCESS",
- "STATIONS_UPDATE_LOCKED_STATUS",
- `Toggled the queue lock for station "${stationId}" successfully to "${station.locked}".`
- );
- CacheModule.runJob("PUB", {
- channel: "station.queueLockToggled",
- value: {
- stationId,
- locked: station.locked
- }
- });
- return cb({ status: "success", data: station.locked });
- }
- );
- }),
- /**
- * Votes to skip a station
- *
- * @param session
- * @param stationId - the station id
- * @param cb
- */
- voteSkip: isLoginRequired(async function voteSkip(session, stationId, cb) {
- const stationModel = await DBModule.runJob(
- "GET_MODEL",
- {
- modelName: "station"
- },
- this
- );
- let skipVotes = 0;
- let shouldSkip = false;
- async.waterfall(
- [
- next => {
- StationsModule.runJob("GET_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- },
- (station, next) => {
- if (!station) return next("Station not found.");
- return StationsModule.runJob(
- "CAN_USER_VIEW_STATION",
- {
- station,
- userId: session.userId
- },
- this
- )
- .then(canView => {
- if (canView) return next(null, station);
- return next("Insufficient permissions.");
- })
- .catch(err => next(err));
- },
- (station, next) => {
- if (!station.currentSong) return next("There is currently no song to skip.");
- if (station.currentSong.skipVotes.indexOf(session.userId) !== -1)
- return next("You have already voted to skip this song.");
- return next(null, station);
- },
- (station, next) => {
- stationModel.updateOne(
- { _id: stationId },
- { $push: { "currentSong.skipVotes": session.userId } },
- next
- );
- },
- (res, next) => {
- StationsModule.runJob("UPDATE_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- },
- (station, next) => {
- if (!station) return next("Station not found.");
- return next(null, station);
- },
- (station, next) => {
- skipVotes = station.currentSong.skipVotes.length;
- IOModule.runJob(
- "GET_ROOM_SOCKETS",
- {
- room: `station.${stationId}`
- },
- this
- )
- .then(sockets => {
- next(null, sockets);
- })
- .catch(next);
- },
- (sockets, next) => {
- if (sockets.length <= skipVotes) shouldSkip = true;
- next();
- }
- ],
- async err => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log("ERROR", "STATIONS_VOTE_SKIP", `Vote skipping station "${stationId}" failed. "${err}"`);
- return cb({ status: "failure", message: err });
- }
- this.log("SUCCESS", "STATIONS_VOTE_SKIP", `Vote skipping "${stationId}" successful.`);
- CacheModule.runJob("PUB", {
- channel: "station.voteSkipSong",
- value: stationId
- });
- if (shouldSkip) StationsModule.runJob("SKIP_STATION", { stationId });
- return cb({
- status: "success",
- message: "Successfully voted to skip the song."
- });
- }
- );
- }),
- /**
- * Force skips a station
- *
- * @param session
- * @param stationId - the station id
- * @param cb
- */
- forceSkip: isOwnerRequired(function forceSkip(session, stationId, cb) {
- async.waterfall(
- [
- next => {
- StationsModule.runJob("GET_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- },
- (station, next) => {
- if (!station) return next("Station not found.");
- return next();
- }
- ],
- async err => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log("ERROR", "STATIONS_FORCE_SKIP", `Force skipping station "${stationId}" failed. "${err}"`);
- return cb({ status: "failure", message: err });
- }
- StationsModule.runJob("SKIP_STATION", { stationId });
- this.log("SUCCESS", "STATIONS_FORCE_SKIP", `Force skipped station "${stationId}" successfully.`);
- return cb({
- status: "success",
- message: "Successfully skipped station."
- });
- }
- );
- }),
- /**
- * Leaves the user's current station
- *
- * @param {object} session - user session
- * @param {string} stationId - id of station to leave
- * @param {Function} cb - callback
- */
- leave(session, stationId, cb) {
- async.waterfall(
- [
- next => {
- StationsModule.runJob("GET_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- },
- (station, next) => {
- if (!station) return next("Station not found.");
- return next();
- }
- ],
- async (err, userCount) => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log("ERROR", "STATIONS_LEAVE", `Leaving station "${stationId}" failed. "${err}"`);
- return cb({ status: "failure", message: err });
- }
- this.log("SUCCESS", "STATIONS_LEAVE", `Left station "${stationId}" successfully.`);
- IOModule.runJob("SOCKET_LEAVE_ROOMS", { socketId: session });
- delete StationsModule.userList[session.socketId];
- return cb({
- status: "success",
- message: "Successfully left station.",
- userCount
- });
- }
- );
- },
- /**
- * Updates a station's name
- *
- * @param session
- * @param stationId - the station id
- * @param newName - the new station name
- * @param cb
- */
- updateName: isOwnerRequired(async function updateName(session, stationId, newName, cb) {
- const stationModel = await DBModule.runJob(
- "GET_MODEL",
- {
- modelName: "station"
- },
- this
- );
- async.waterfall(
- [
- next => {
- stationModel.updateOne(
- { _id: stationId },
- { $set: { name: newName } },
- { runValidators: true },
- next
- );
- },
- (res, next) => {
- StationsModule.runJob("UPDATE_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- }
- ],
- async err => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log(
- "ERROR",
- "STATIONS_UPDATE_NAME",
- `Updating station "${stationId}" name to "${newName}" failed. "${err}"`
- );
- return cb({ status: "failure", message: err });
- }
- this.log(
- "SUCCESS",
- "STATIONS_UPDATE_NAME",
- `Updated station "${stationId}" name to "${newName}" successfully.`
- );
- return cb({
- status: "success",
- message: "Successfully updated the name."
- });
- }
- );
- }),
- /**
- * Updates a station's display name
- *
- * @param session
- * @param stationId - the station id
- * @param newDisplayName - the new station display name
- * @param cb
- */
- updateDisplayName: isOwnerRequired(async function updateDisplayName(session, stationId, newDisplayName, cb) {
- const stationModel = await DBModule.runJob(
- "GET_MODEL",
- {
- modelName: "station"
- },
- this
- );
- async.waterfall(
- [
- next => {
- stationModel.updateOne(
- { _id: stationId },
- { $set: { displayName: newDisplayName } },
- { runValidators: true },
- next
- );
- },
- (res, next) => {
- StationsModule.runJob("UPDATE_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- }
- ],
- async err => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log(
- "ERROR",
- "STATIONS_UPDATE_DISPLAY_NAME",
- `Updating station "${stationId}" displayName to "${newDisplayName}" failed. "${err}"`
- );
- return cb({ status: "failure", message: err });
- }
- this.log(
- "SUCCESS",
- "STATIONS_UPDATE_DISPLAY_NAME",
- `Updated station "${stationId}" displayName to "${newDisplayName}" successfully.`
- );
- return cb({
- status: "success",
- message: "Successfully updated the display name."
- });
- }
- );
- }),
- /**
- * Updates a station's description
- *
- * @param session
- * @param stationId - the station id
- * @param newDescription - the new station description
- * @param cb
- */
- updateDescription: isOwnerRequired(async function updateDescription(session, stationId, newDescription, cb) {
- const stationModel = await DBModule.runJob(
- "GET_MODEL",
- {
- modelName: "station"
- },
- this
- );
- async.waterfall(
- [
- next => {
- stationModel.updateOne(
- { _id: stationId },
- { $set: { description: newDescription } },
- { runValidators: true },
- next
- );
- },
- (res, next) => {
- StationsModule.runJob("UPDATE_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- }
- ],
- async err => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log(
- "ERROR",
- "STATIONS_UPDATE_DESCRIPTION",
- `Updating station "${stationId}" description to "${newDescription}" failed. "${err}"`
- );
- return cb({ status: "failure", message: err });
- }
- this.log(
- "SUCCESS",
- "STATIONS_UPDATE_DESCRIPTION",
- `Updated station "${stationId}" description to "${newDescription}" successfully.`
- );
- return cb({
- status: "success",
- message: "Successfully updated the description."
- });
- }
- );
- }),
- /**
- * Updates a station's privacy
- *
- * @param session
- * @param stationId - the station id
- * @param newPrivacy - the new station privacy
- * @param cb
- */
- updatePrivacy: isOwnerRequired(async function updatePrivacy(session, stationId, newPrivacy, cb) {
- const stationModel = await DBModule.runJob(
- "GET_MODEL",
- {
- modelName: "station"
- },
- this
- );
- async.waterfall(
- [
- next => {
- stationModel.updateOne(
- { _id: stationId },
- { $set: { privacy: newPrivacy } },
- { runValidators: true },
- next
- );
- },
- (res, next) => {
- StationsModule.runJob("UPDATE_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- }
- ],
- async err => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log(
- "ERROR",
- "STATIONS_UPDATE_PRIVACY",
- `Updating station "${stationId}" privacy to "${newPrivacy}" failed. "${err}"`
- );
- return cb({ status: "failure", message: err });
- }
- this.log(
- "SUCCESS",
- "STATIONS_UPDATE_PRIVACY",
- `Updated station "${stationId}" privacy to "${newPrivacy}" successfully.`
- );
- return cb({
- status: "success",
- message: "Successfully updated the privacy."
- });
- }
- );
- }),
- /**
- * Updates a station's genres
- *
- * @param session
- * @param stationId - the station id
- * @param newGenres - the new station genres
- * @param cb
- */
- updateGenres: isOwnerRequired(async function updateGenres(session, stationId, newGenres, cb) {
- const stationModel = await DBModule.runJob(
- "GET_MODEL",
- {
- modelName: "station"
- },
- this
- );
- async.waterfall(
- [
- next => {
- stationModel.updateOne(
- { _id: stationId },
- { $set: { genres: newGenres } },
- { runValidators: true },
- next
- );
- },
- (res, next) => {
- StationsModule.runJob("UPDATE_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- }
- ],
- async err => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err });
- this.log(
- "ERROR",
- "STATIONS_UPDATE_GENRES",
- `Updating station "${stationId}" genres to "${newGenres}" failed. "${err}"`
- );
- return cb({ status: "failure", message: err });
- }
- this.log(
- "SUCCESS",
- "STATIONS_UPDATE_GENRES",
- `Updated station "${stationId}" genres to "${newGenres}" successfully.`
- );
- return cb({
- status: "success",
- message: "Successfully updated the genres."
- });
- }
- );
- }),
- /**
- * Updates a station's blacklisted genres
- *
- * @param session
- * @param stationId - the station id
- * @param newBlacklistedGenres - the new station blacklisted genres
- * @param cb
- */
- updateBlacklistedGenres: isOwnerRequired(async function updateBlacklistedGenres(
- session,
- stationId,
- newBlacklistedGenres,
- cb
- ) {
- const stationModel = await DBModule.runJob(
- "GET_MODEL",
- {
- modelName: "station"
- },
- this
- );
- async.waterfall(
- [
- next => {
- stationModel.updateOne(
- { _id: stationId },
- {
- $set: {
- blacklistedGenres: newBlacklistedGenres
- }
- },
- { runValidators: true },
- next
- );
- },
- (res, next) => {
- StationsModule.runJob("UPDATE_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- }
- ],
- async err => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log(
- "ERROR",
- "STATIONS_UPDATE_BLACKLISTED_GENRES",
- `Updating station "${stationId}" blacklisted genres to "${newBlacklistedGenres}" failed. "${err}"`
- );
- return cb({ status: "failure", message: err });
- }
- this.log(
- "SUCCESS",
- "STATIONS_UPDATE_BLACKLISTED_GENRES",
- `Updated station "${stationId}" blacklisted genres to "${newBlacklistedGenres}" successfully.`
- );
- return cb({
- status: "success",
- message: "Successfully updated the blacklisted genres."
- });
- }
- );
- }),
- /**
- * Updates a station's party mode
- *
- * @param session
- * @param stationId - the station id
- * @param newPartyMode - the new station party mode
- * @param cb
- */
- updatePartyMode: isOwnerRequired(async function updatePartyMode(session, stationId, newPartyMode, cb) {
- const stationModel = await DBModule.runJob(
- "GET_MODEL",
- {
- modelName: "station"
- },
- this
- );
- async.waterfall(
- [
- next => {
- StationsModule.runJob("GET_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- },
- (station, next) => {
- if (!station) return next("Station not found.");
- if (station.partyMode === newPartyMode)
- return next(`The party mode was already ${newPartyMode ? "enabled." : "disabled."}`);
- return stationModel.updateOne(
- { _id: stationId },
- { $set: { partyMode: newPartyMode } },
- { runValidators: true },
- next
- );
- },
- (res, next) => {
- StationsModule.runJob("UPDATE_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- }
- ],
- async err => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log(
- "ERROR",
- "STATIONS_UPDATE_PARTY_MODE",
- `Updating station "${stationId}" party mode to "${newPartyMode}" failed. "${err}"`
- );
- return cb({ status: "failure", message: err });
- }
- this.log(
- "SUCCESS",
- "STATIONS_UPDATE_PARTY_MODE",
- `Updated station "${stationId}" party mode to "${newPartyMode}" successfully.`
- );
- CacheModule.runJob("PUB", {
- channel: "station.updatePartyMode",
- value: {
- stationId,
- partyMode: newPartyMode
- }
- });
- StationsModule.runJob("SKIP_STATION", { stationId });
- return cb({
- status: "success",
- message: "Successfully updated the party mode."
- });
- }
- );
- }),
- /**
- * Pauses a station
- *
- * @param session
- * @param stationId - the station id
- * @param cb
- */
- pause: isOwnerRequired(async function pause(session, stationId, cb) {
- const stationModel = await DBModule.runJob(
- "GET_MODEL",
- {
- modelName: "station"
- },
- this
- );
- async.waterfall(
- [
- next => {
- StationsModule.runJob("GET_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- },
- (station, next) => {
- if (!station) return next("Station not found.");
- if (station.paused) return next("That station was already paused.");
- return stationModel.updateOne(
- { _id: stationId },
- { $set: { paused: true, pausedAt: Date.now() } },
- next
- );
- },
- (res, next) => {
- StationsModule.runJob("UPDATE_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- }
- ],
- async err => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log("ERROR", "STATIONS_PAUSE", `Pausing station "${stationId}" failed. "${err}"`);
- return cb({ status: "failure", message: err });
- }
- this.log("SUCCESS", "STATIONS_PAUSE", `Paused station "${stationId}" successfully.`);
- CacheModule.runJob("PUB", {
- channel: "station.pause",
- value: stationId
- });
- NotificationsModule.runJob("UNSCHEDULE", {
- name: `stations.nextSong?id=${stationId}`
- });
- return cb({
- status: "success",
- message: "Successfully paused."
- });
- }
- );
- }),
- /**
- * Resumes a station
- *
- * @param session
- * @param stationId - the station id
- * @param cb
- */
- resume: isOwnerRequired(async function resume(session, stationId, cb) {
- const stationModel = await DBModule.runJob(
- "GET_MODEL",
- {
- modelName: "station"
- },
- this
- );
- async.waterfall(
- [
- next => {
- StationsModule.runJob("GET_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- },
- (station, next) => {
- if (!station) return next("Station not found.");
- if (!station.paused) return next("That station is not paused.");
- station.timePaused += Date.now() - station.pausedAt;
- return stationModel.updateOne(
- { _id: stationId },
- {
- $set: { paused: false },
- $inc: { timePaused: Date.now() - station.pausedAt }
- },
- next
- );
- },
- (res, next) => {
- StationsModule.runJob("UPDATE_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- }
- ],
- async err => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log("ERROR", "STATIONS_RESUME", `Resuming station "${stationId}" failed. "${err}"`);
- return cb({ status: "failure", message: err });
- }
- this.log("SUCCESS", "STATIONS_RESUME", `Resuming station "${stationId}" successfully.`);
- CacheModule.runJob("PUB", {
- channel: "station.resume",
- value: stationId
- });
- return cb({
- status: "success",
- message: "Successfully resumed."
- });
- }
- );
- }),
- /**
- * Removes a station
- *
- * @param session
- * @param stationId - the station id
- * @param cb
- */
- remove: isOwnerRequired(async function remove(session, stationId, cb) {
- const stationModel = await DBModule.runJob(
- "GET_MODEL",
- {
- modelName: "station"
- },
- this
- );
- async.waterfall(
- [
- next => {
- stationModel.deleteOne({ _id: stationId }, err => next(err));
- },
- next => {
- CacheModule.runJob("HDEL", { table: "stations", key: stationId }, this).then(next).catch(next);
- }
- ],
- async err => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log("ERROR", "STATIONS_REMOVE", `Removing station "${stationId}" failed. "${err}"`);
- return cb({ status: "failure", message: err });
- }
- this.log("SUCCESS", "STATIONS_REMOVE", `Removing station "${stationId}" successfully.`);
- CacheModule.runJob("PUB", {
- channel: "station.remove",
- value: stationId
- });
- ActivitiesModule.runJob("ADD_ACTIVITY", {
- userId: session.userId,
- activityType: "deleted_station",
- payload: [stationId]
- });
- return cb({
- status: "success",
- message: "Successfully removed."
- });
- }
- );
- }),
- /**
- * Create a station
- *
- * @param session
- * @param data - the station data
- * @param cb
- */
- create: isLoginRequired(async function create(session, data, cb) {
- const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
- const stationModel = await DBModule.runJob(
- "GET_MODEL",
- {
- modelName: "station"
- },
- this
- );
- data.name = data.name.toLowerCase();
- const blacklist = [
- "country",
- "edm",
- "musare",
- "hip-hop",
- "rap",
- "top-hits",
- "todays-hits",
- "old-school",
- "christmas",
- "about",
- "support",
- "staff",
- "help",
- "news",
- "terms",
- "privacy",
- "profile",
- "c",
- "community",
- "tos",
- "login",
- "register",
- "p",
- "official",
- "o",
- "trap",
- "faq",
- "team",
- "donate",
- "buy",
- "shop",
- "forums",
- "explore",
- "settings",
- "admin",
- "auth",
- "reset_password"
- ];
- async.waterfall(
- [
- next => {
- if (!data) return next("Invalid data.");
- return next();
- },
- next => {
- stationModel.findOne(
- {
- $or: [
- { name: data.name },
- {
- displayName: new RegExp(`^${data.displayName}$`, "i")
- }
- ]
- },
- next
- );
- },
- // eslint-disable-next-line consistent-return
- (station, next) => {
- this.log(station);
- if (station) return next("A station with that name or display name already exists.");
- const { name, displayName, description, genres, playlist, type, blacklistedGenres } = data;
- if (type === "official") {
- return userModel.findOne({ _id: session.userId }, (err, user) => {
- if (err) return next(err);
- if (!user) return next("User not found.");
- if (user.role !== "admin") return next("Admin required.");
- return stationModel.create(
- {
- name,
- displayName,
- description,
- type,
- privacy: "private",
- playlist,
- genres,
- blacklistedGenres,
- currentSong: StationsModule.defaultSong
- },
- next
- );
- });
- }
- if (type === "community") {
- if (blacklist.indexOf(name) !== -1)
- return next("That name is blacklisted. Please use a different name.");
- return stationModel.create(
- {
- name,
- displayName,
- description,
- type,
- privacy: "private",
- owner: session.userId,
- queue: [],
- currentSong: null
- },
- next
- );
- }
- }
- ],
- async (err, station) => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log("ERROR", "STATIONS_CREATE", `Creating station failed. "${err}"`);
- return cb({ status: "failure", message: err });
- }
- this.log("SUCCESS", "STATIONS_CREATE", `Created station "${station._id}" successfully.`);
- CacheModule.runJob("PUB", {
- channel: "station.create",
- value: station._id
- });
- ActivitiesModule.runJob("ADD_ACTIVITY", {
- userId: session.userId,
- activityType: "created_station",
- payload: [station._id]
- });
- return cb({
- status: "success",
- message: "Successfully created station."
- });
- }
- );
- }),
- /**
- * Adds song to station queue
- *
- * @param session
- * @param stationId - the station id
- * @param songId - the song id
- * @param cb
- */
- addToQueue: isLoginRequired(async function addToQueue(session, stationId, songId, cb) {
- const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
- const stationModel = await DBModule.runJob(
- "GET_MODEL",
- {
- modelName: "station"
- },
- this
- );
- async.waterfall(
- [
- next => {
- StationsModule.runJob("GET_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- },
- (station, next) => {
- if (!station) return next("Station not found.");
- if (station.locked) {
- return userModel.findOne({ _id: session.userId }, (err, user) => {
- if (user.role !== "admin" && station.owner !== session.userId)
- return next("Only owners and admins can add songs to a locked queue.");
- return next(null, station);
- });
- }
- return next(null, station);
- },
- (station, next) => {
- if (station.type !== "community") return next("That station is not a community station.");
- return StationsModule.runJob(
- "CAN_USER_VIEW_STATION",
- {
- station,
- userId: session.userId
- },
- this
- )
- .then(canView => {
- if (canView) return next(null, station);
- return next("Insufficient permissions.");
- })
- .catch(err => next(err));
- },
- (station, next) => {
- if (station.currentSong && station.currentSong.songId === songId)
- return next("That song is currently playing.");
- return async.each(
- station.queue,
- (queueSong, next) => {
- if (queueSong.songId === songId) return next("That song is already in the queue.");
- return next();
- },
- err => next(err, station)
- );
- },
- (station, next) => {
- SongsModule.runJob("GET_SONG_FROM_ID", { songId }, this)
- .then(res => {
- if (res.song) return next(null, res.song, station);
- return YouTubeModule.runJob("GET_SONG", { songId }, this)
- .then(response => {
- const { song } = response;
- song.artists = [];
- song.skipDuration = 0;
- song.likes = -1;
- song.dislikes = -1;
- song.thumbnail = "empty";
- song.explicit = false;
- return next(null, song, station);
- })
- .catch(err => next(err));
- })
- .catch(err => next(err));
- },
- (song, station, next) => {
- const { queue } = station;
- song.requestedBy = session.userId;
- queue.push(song);
- let totalDuration = 0;
- queue.forEach(song => {
- totalDuration += song.duration;
- });
- if (totalDuration >= 3600 * 3) return next("The max length of the queue is 3 hours.");
- return next(null, song, station);
- },
- (song, station, next) => {
- const { queue } = station;
- if (queue.length === 0) return next(null, song, station);
- let totalDuration = 0;
- const userId = queue[queue.length - 1].requestedBy;
- station.queue.forEach(song => {
- if (userId === song.requestedBy) {
- totalDuration += song.duration;
- }
- });
- if (totalDuration >= 900) return next("The max length of songs per user is 15 minutes.");
- return next(null, song, station);
- },
- (song, station, next) => {
- const { queue } = station;
- if (queue.length === 0) return next(null, song);
- let totalSongs = 0;
- const userId = queue[queue.length - 1].requestedBy;
- queue.forEach(song => {
- if (userId === song.requestedBy) {
- totalSongs += 1;
- }
- });
- if (totalSongs <= 2) return next(null, song);
- if (totalSongs > 3)
- return next("The max amount of songs per user is 3, and only 2 in a row is allowed.");
- if (queue[queue.length - 2].requestedBy !== userId || queue[queue.length - 3] !== userId)
- return next("The max amount of songs per user is 3, and only 2 in a row is allowed.");
- return next(null, song);
- },
- (song, next) => {
- stationModel.updateOne(
- { _id: stationId },
- { $push: { queue: song } },
- { runValidators: true },
- next
- );
- },
- (res, next) => {
- StationsModule.runJob("UPDATE_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- }
- ],
- async err => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log(
- "ERROR",
- "STATIONS_ADD_SONG_TO_QUEUE",
- `Adding song "${songId}" to station "${stationId}" queue failed. "${err}"`
- );
- return cb({ status: "failure", message: err });
- }
- this.log(
- "SUCCESS",
- "STATIONS_ADD_SONG_TO_QUEUE",
- `Added song "${songId}" to station "${stationId}" successfully.`
- );
- CacheModule.runJob("PUB", {
- channel: "station.queueUpdate",
- value: stationId
- });
- return cb({
- status: "success",
- message: "Successfully added song to queue."
- });
- }
- );
- }),
- /**
- * Removes song from station queue
- *
- * @param session
- * @param stationId - the station id
- * @param songId - the song id
- * @param cb
- */
- removeFromQueue: isOwnerRequired(async function removeFromQueue(session, stationId, songId, cb) {
- const stationModel = await DBModule.runJob(
- "GET_MODEL",
- {
- modelName: "station"
- },
- this
- );
- async.waterfall(
- [
- next => {
- if (!songId) return next("Invalid song id.");
- return StationsModule.runJob("GET_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- },
- (station, next) => {
- if (!station) return next("Station not found.");
- if (station.type !== "community") return next("Station is not a community station.");
- return async.each(
- station.queue,
- (queueSong, next) => {
- if (queueSong.songId === songId) return next(true);
- return next();
- },
- err => {
- if (err === true) return next();
- return next("Song is not currently in the queue.");
- }
- );
- },
- next => {
- stationModel.updateOne({ _id: stationId }, { $pull: { queue: { songId } } }, next);
- },
- (res, next) => {
- StationsModule.runJob("UPDATE_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- }
- ],
- async err => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log(
- "ERROR",
- "STATIONS_REMOVE_SONG_TO_QUEUE",
- `Removing song "${songId}" from station "${stationId}" queue failed. "${err}"`
- );
- return cb({ status: "failure", message: err });
- }
- this.log(
- "SUCCESS",
- "STATIONS_REMOVE_SONG_TO_QUEUE",
- `Removed song "${songId}" from station "${stationId}" successfully.`
- );
- CacheModule.runJob("PUB", {
- channel: "station.queueUpdate",
- value: stationId
- });
- return cb({
- status: "success",
- message: "Successfully removed song from queue."
- });
- }
- );
- }),
- /**
- * Gets the queue from a station
- *
- * @param {object} session - user session
- * @param {string} stationId - the station id
- * @param {Function} cb - callback
- */
- getQueue(session, stationId, cb) {
- async.waterfall(
- [
- next => {
- StationsModule.runJob("GET_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- },
- (station, next) => {
- if (!station) return next("Station not found.");
- if (station.type !== "community") return next("Station is not a community station.");
- return next(null, station);
- },
- (station, next) => {
- StationsModule.runJob(
- "CAN_USER_VIEW_STATION",
- {
- station,
- userId: session.userId
- },
- this
- )
- .then(canView => {
- if (canView) return next(null, station);
- return next("Insufficient permissions.");
- })
- .catch(err => next(err));
- }
- ],
- async (err, station) => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log(
- "ERROR",
- "STATIONS_GET_QUEUE",
- `Getting queue for station "${stationId}" failed. "${err}"`
- );
- return cb({ status: "failure", message: err });
- }
- this.log("SUCCESS", "STATIONS_GET_QUEUE", `Got queue for station "${stationId}" successfully.`);
- return cb({
- status: "success",
- message: "Successfully got queue.",
- queue: station.queue
- });
- }
- );
- },
- /**
- * Selects a private playlist for a station
- *
- * @param session
- * @param stationId - the station id
- * @param playlistId - the private playlist id
- * @param cb
- */
- selectPrivatePlaylist: isOwnerRequired(async function selectPrivatePlaylist(session, stationId, playlistId, cb) {
- const stationModel = await DBModule.runJob(
- "GET_MODEL",
- {
- modelName: "station"
- },
- this
- );
- const playlistModel = await DBModule.runJob(
- "GET_MODEL",
- {
- modelName: "playlist"
- },
- this
- );
- async.waterfall(
- [
- next => {
- StationsModule.runJob("GET_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- },
- (station, next) => {
- if (!station) return next("Station not found.");
- if (station.type !== "community") return next("Station is not a community station.");
- if (station.privatePlaylist === playlistId)
- return next("That private playlist is already selected.");
- return playlistModel.findOne({ _id: playlistId }, next);
- },
- (playlist, next) => {
- if (!playlist) return next("Playlist not found.");
- const currentSongIndex = playlist.songs.length > 0 ? playlist.songs.length - 1 : 0;
- return stationModel.updateOne(
- { _id: stationId },
- {
- $set: {
- privatePlaylist: playlistId,
- currentSongIndex
- }
- },
- { runValidators: true },
- next
- );
- },
- (res, next) => {
- StationsModule.runJob("UPDATE_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- }
- ],
- async (err, station) => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log(
- "ERROR",
- "STATIONS_SELECT_PRIVATE_PLAYLIST",
- `Selecting private playlist "${playlistId}" for station "${stationId}" failed. "${err}"`
- );
- return cb({ status: "failure", message: err });
- }
- this.log(
- "SUCCESS",
- "STATIONS_SELECT_PRIVATE_PLAYLIST",
- `Selected private playlist "${playlistId}" for station "${stationId}" successfully.`
- );
- NotificationsModule.runJob("UNSCHEDULE", {
- name: `stations.nextSong?id${stationId}`
- });
- if (!station.partyMode) StationsModule.runJob("SKIP_STATION", { stationId });
- CacheModule.runJob("PUB", {
- channel: "privatePlaylist.selected",
- value: {
- playlistId,
- stationId
- }
- });
- return cb({
- status: "success",
- message: "Successfully selected playlist."
- });
- }
- );
- }),
- favoriteStation: isLoginRequired(async function favoriteStation(session, stationId, cb) {
- const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
- async.waterfall(
- [
- next => {
- StationsModule.runJob("GET_STATION", { stationId }, this)
- .then(station => {
- next(null, station);
- })
- .catch(next);
- },
- (station, next) => {
- if (!station) return next("Station not found.");
- return StationsModule.runJob(
- "CAN_USER_VIEW_STATION",
- {
- station,
- userId: session.userId
- },
- this
- )
- .then(canView => {
- if (canView) return next();
- return next("Insufficient permissions.");
- })
- .catch(err => next(err));
- },
- next => {
- userModel.updateOne({ _id: session.userId }, { $addToSet: { favoriteStations: stationId } }, next);
- },
- (res, next) => {
- if (res.nModified === 0) return next("The station was already favorited.");
- return next();
- }
- ],
- async err => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log("ERROR", "FAVORITE_STATION", `Favoriting station "${stationId}" failed. "${err}"`);
- return cb({ status: "failure", message: err });
- }
- this.log("SUCCESS", "FAVORITE_STATION", `Favorited station "${stationId}" successfully.`);
- CacheModule.runJob("PUB", {
- channel: "user.favoritedStation",
- value: {
- userId: session.userId,
- stationId
- }
- });
- return cb({
- status: "success",
- message: "Succesfully favorited station."
- });
- }
- );
- }),
- unfavoriteStation: isLoginRequired(async function unfavoriteStation(session, stationId, cb) {
- const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
- async.waterfall(
- [
- next => {
- userModel.updateOne({ _id: session.userId }, { $pull: { favoriteStations: stationId } }, next);
- },
- (res, next) => {
- if (res.nModified === 0) return next("The station wasn't favorited.");
- return next();
- }
- ],
- async err => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log("ERROR", "UNFAVORITE_STATION", `Unfavoriting station "${stationId}" failed. "${err}"`);
- return cb({ status: "failure", message: err });
- }
- this.log("SUCCESS", "UNFAVORITE_STATION", `Unfavorited station "${stationId}" successfully.`);
- CacheModule.runJob("PUB", {
- channel: "user.unfavoritedStation",
- value: {
- userId: session.userId,
- stationId
- }
- });
- return cb({
- status: "success",
- message: "Succesfully unfavorited station."
- });
- }
- );
- })
- };
|