stations.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. 'use strict';
  2. const async = require('async'),
  3. request = require('request'),
  4. config = require('config');
  5. const io = require('../io');
  6. const db = require('../db');
  7. const cache = require('../cache');
  8. const notifications = require('../notifications');
  9. const utils = require('../utils');
  10. const logger = require('../logger');
  11. const stations = require('../stations');
  12. const songs = require('../songs');
  13. const hooks = require('./hooks');
  14. cache.sub('station.updatePartyMode', data => {
  15. utils.emitToRoom(`station.${data.stationId}`, "event:partyMode.updated", data.partyMode);
  16. });
  17. cache.sub('privatePlaylist.selected', data => {
  18. utils.emitToRoom(`station.${data.stationId}`, "event:privatePlaylist.selected", data.playlistId);
  19. });
  20. cache.sub('station.pause', stationId => {
  21. utils.emitToRoom(`station.${stationId}`, "event:stations.pause");
  22. });
  23. cache.sub('station.resume', stationId => {
  24. stations.getStation(stationId, (err, station) => {
  25. utils.emitToRoom(`station.${stationId}`, "event:stations.resume", { timePaused: station.timePaused });
  26. });
  27. });
  28. cache.sub('station.queueUpdate', stationId => {
  29. stations.getStation(stationId, (err, station) => {
  30. if (!err) utils.emitToRoom(`station.${stationId}`, "event:queue.update", station.queue);
  31. });
  32. });
  33. cache.sub('station.voteSkipSong', stationId => {
  34. utils.emitToRoom(`station.${stationId}`, "event:song.voteSkipSong");
  35. });
  36. cache.sub('station.remove', stationId => {
  37. utils.emitToRoom('admin.stations', 'event:admin.station.removed', stationId);
  38. });
  39. cache.sub('station.create', stationId => {
  40. stations.initializeStation(stationId, (err, station) => {
  41. if (err) console.error(err);
  42. utils.emitToRoom('admin.stations', 'event:admin.station.added', station);
  43. // TODO If community, check if on whitelist
  44. if (station.privacy === 'public') utils.emitToRoom('home', "event:stations.created", station);
  45. else {
  46. let sockets = utils.getRoomSockets('home');
  47. for (let socketId in sockets) {
  48. let socket = sockets[socketId];
  49. let session = sockets[socketId].session;
  50. if (session.sessionId) {
  51. cache.hget('sessions', session.sessionId, (err, session) => {
  52. if (!err && session) {
  53. db.models.user.findOne({_id: session.userId}, (err, user) => {
  54. if (user.role === 'admin') socket.emit("event:stations.created", station);
  55. else if (station.type === "community" && station.owner === session.userId) socket.emit("event:stations.created", station);
  56. });
  57. }
  58. });
  59. }
  60. }
  61. }
  62. });
  63. });
  64. module.exports = {
  65. /**
  66. * Get a list of all the stations
  67. *
  68. * @param session
  69. * @param cb
  70. * @return {{ status: String, stations: Array }}
  71. */
  72. index: (session, cb) => {
  73. async.waterfall([
  74. (next) => {
  75. cache.hgetall('stations', next);
  76. },
  77. (stations, next) => {
  78. let resultStations = [];
  79. for (let id in stations) {
  80. resultStations.push(stations[id]);
  81. }
  82. next(null, stations);
  83. },
  84. (stations, next) => {
  85. let resultStations = [];
  86. async.each(stations, (station, next) => {
  87. async.waterfall([
  88. (next) => {
  89. if (station.privacy === 'public') return next(true);
  90. if (!session.sessionId) return next(false);
  91. cache.hget('sessions', session.sessionId, next);
  92. },
  93. (session, next) => {
  94. if (!session) return next(false);
  95. db.models.user.findOne({_id: session.userId}, next);
  96. },
  97. (user, next) => {
  98. if (!user) return next(false);
  99. if (user.role === 'admin') return next(true);
  100. if (station.type === 'official') return next(false);
  101. if (station.owner === session.userId) return next(true);
  102. next(false);
  103. }
  104. ], (err) => {
  105. if (err === true) resultStations.push(station);
  106. next();
  107. });
  108. }, () => {
  109. next(null, resultStations);
  110. });
  111. }
  112. ], (err, stations) => {
  113. if (err) {
  114. err = utils.getError(err);
  115. logger.error("STATIONS_INDEX", `Indexing stations failed. "${err}"`);
  116. return cb({'status': 'failure', 'message': err});
  117. }
  118. logger.success("STATIONS_INDEX", `Indexing stations successful.`);
  119. return cb({'status': 'success', 'stations': stations});
  120. });
  121. },
  122. find: (session, stationId, cb) => {
  123. async.waterfall([
  124. (next) => {
  125. stations.getStation(stationId, next);
  126. },
  127. (station, next) => {
  128. if (!station) return next('Station not found.');
  129. next(null, station);
  130. }
  131. ], (err, station) => {
  132. if (err) {
  133. err = utils.getError(err);
  134. logger.error("STATIONS_FIND", `Finding station "${stationId}" failed. "${err}"`);
  135. return cb({'status': 'failure', 'message': err});
  136. }
  137. logger.success("STATIONS_FIND", `Found station "${stationId}" successfully.`);
  138. cb({status: 'success', data: station});
  139. });
  140. },
  141. getPlaylist: (session, stationId, cb) => {
  142. async.waterfall([
  143. (next) => {
  144. stations.getStation(stationId, next);
  145. },
  146. (station, next) => {
  147. if (!station) return next('Station not found.');
  148. if (station.type !== 'official') return next('This is not an official station.');
  149. next();
  150. },
  151. (next) => {
  152. cache.hget("officialPlaylists", stationId, next);
  153. },
  154. (playlist, next) => {
  155. if (!playlist) return next('Playlist not found.');
  156. next(null, playlist);
  157. }
  158. ], (err, playlist) => {
  159. if (err) {
  160. err = utils.getError(err);
  161. logger.error("STATIONS_GET_PLAYLIST", `Getting playlist for station "${stationId}" failed. "${err}"`);
  162. return cb({'status': 'failure', 'message': err});
  163. }
  164. logger.success("STATIONS_GET_PLAYLIST", `Got playlist for station "${stationId}" successfully.`);
  165. cb({status: 'success', data: playlist.songs})
  166. });
  167. },
  168. /**
  169. * Joins the station by its id
  170. *
  171. * @param session
  172. * @param stationId - the station id
  173. * @param cb
  174. * @return {{ status: String, userCount: Integer }}
  175. */
  176. join: (session, stationId, cb) => {
  177. async.waterfall([
  178. (next) => {
  179. stations.getStation(stationId, next);
  180. },
  181. (station, next) => {
  182. if (!station) return next('Station not found.');
  183. async.waterfall([
  184. (next) => {
  185. if (station.privacy !== 'private') return next(true);
  186. if (!session.userId) return next('An error occurred while joining the station.');
  187. next();
  188. },
  189. (next) => {
  190. db.models.user.findOne({_id: session.userId}, next);
  191. },
  192. (user, next) => {
  193. if (!user) return next('An error occurred while joining the station.');
  194. if (user.role === 'admin') return next(true);
  195. if (station.type === 'official') return next('An error occurred while joining the station.');
  196. if (station.owner === session.userId) return next(true);
  197. next('An error occurred while joining the station.');
  198. }
  199. ], (err) => {
  200. if (err === true) return next(null, station);
  201. next(utils.getError(err));
  202. });
  203. },
  204. (station, next) => {
  205. utils.socketJoinRoom(session.socketId, `station.${stationId}`);
  206. let data = {
  207. type: station.type,
  208. currentSong: station.currentSong,
  209. startedAt: station.startedAt,
  210. paused: station.paused,
  211. timePaused: station.timePaused,
  212. description: station.description,
  213. displayName: station.displayName,
  214. privacy: station.privacy,
  215. partyMode: station.partyMode,
  216. owner: station.owner,
  217. privatePlaylist: station.privatePlaylist
  218. };
  219. next(null, data);
  220. },
  221. (data, next) => {
  222. if (!data.currentSong) return next(null, data);
  223. utils.socketJoinSongRoom(session.socketId, `song.${data.currentSong._id}`);
  224. data.currentSong.skipVotes = data.currentSong.skipVotes.length;
  225. songs.getSong(data.currentSong._id, (err, song) => {
  226. if (!err && song) {
  227. data.currentSong.likes = song.likes;
  228. data.currentSong.dislikes = song.dislikes;
  229. } else {
  230. data.currentSong.likes = -1;
  231. data.currentSong.dislikes = -1;
  232. }
  233. next(null, data);
  234. });
  235. }
  236. ], (err, data) => {
  237. if (err) {
  238. err = utils.getError(err);
  239. logger.error("STATIONS_JOIN", `Joining station "${stationId}" failed. "${err}"`);
  240. return cb({'status': 'failure', 'message': err});
  241. }
  242. logger.success("STATIONS_JOIN", `Joined station "${stationId}" successfully.`);
  243. cb({status: 'success', data});
  244. });
  245. },
  246. /**
  247. * Skips the users current station
  248. *
  249. * @param session
  250. * @param stationId - the station id
  251. * @param cb
  252. */
  253. voteSkip: hooks.loginRequired((session, stationId, cb, userId) => {
  254. async.waterfall([
  255. (next) => {
  256. stations.getStation(stationId, next);
  257. },
  258. (station, next) => {
  259. if (!station) return next('Station not found.');
  260. if (!station.currentSong) return next('There is currently no song to skip.');
  261. if (station.currentSong.skipVotes.indexOf(userId) !== -1) return next('You have already voted to skip this song.');
  262. next(null, station);
  263. },
  264. (station, next) => {
  265. db.models.station.update({_id: stationId}, {$push: {"currentSong.skipVotes": userId}}, next)
  266. },
  267. (res, next) => {
  268. stations.updateStation(stationId, next);
  269. },
  270. (station, next) => {
  271. if (!station) return next('Station not found.');
  272. next(null, station);
  273. }
  274. ], (err, station) => {
  275. if (err) {
  276. err = utils.getError(err);
  277. logger.error("STATIONS_VOTE_SKIP", `Vote skipping station "${stationId}" failed. "${err}"`);
  278. return cb({'status': 'failure', 'message': err});
  279. }
  280. logger.success("STATIONS_VOTE_SKIP", `Vote skipping "${stationId}" successful.`);
  281. cache.pub('station.voteSkipSong', stationId);
  282. if (station.currentSong && station.currentSong.skipVotes.length >= 3) stations.skipStation(stationId)();
  283. cb({ status: 'success', message: 'Successfully voted to skip the song.' });
  284. });
  285. }),
  286. forceSkip: hooks.ownerRequired((session, stationId, cb) => {
  287. async.waterfall([
  288. (next) => {
  289. stations.getStation(stationId, next);
  290. },
  291. (station, next) => {
  292. if (!station) return next('Station not found.');
  293. next();
  294. }
  295. ], (err) => {
  296. if (err) {
  297. err = utils.getError(err);
  298. logger.error("STATIONS_FORCE_SKIP", `Force skipping station "${stationId}" failed. "${err}"`);
  299. return cb({'status': 'failure', 'message': err});
  300. }
  301. notifications.unschedule(`stations.nextSong?id=${stationId}`);
  302. stations.skipStation(stationId)();
  303. logger.success("STATIONS_FORCE_SKIP", `Force skipped station "${stationId}" successfully.`);
  304. return cb({'status': 'success', 'message': 'Successfully skipped station.'});
  305. });
  306. }),
  307. /**
  308. * Leaves the users current station
  309. *
  310. * @param session
  311. * @param stationId
  312. * @param cb
  313. * @return {{ status: String, userCount: Integer }}
  314. */
  315. leave: (session, stationId, cb) => {
  316. async.waterfall([
  317. (next) => {
  318. stations.getStation(stationId, next);
  319. },
  320. (station, next) => {
  321. if (!station) return next('Station not found.');
  322. next();
  323. },
  324. (next) => {
  325. cache.client.hincrby('station.userCounts', stationId, -1, next);
  326. }
  327. ], (err, userCount) => {
  328. if (err) {
  329. err = utils.getError(err);
  330. logger.error("STATIONS_LEAVE", `Leaving station "${stationId}" failed. "${err}"`);
  331. return cb({'status': 'failure', 'message': err});
  332. }
  333. logger.success("STATIONS_LEAVE", `Left station "${stationId}" successfully.`);
  334. utils.socketLeaveRooms(session);
  335. return cb({'status': 'success', 'message': 'Successfully left station.', userCount});
  336. });
  337. },
  338. updateDisplayName: hooks.ownerRequired((session, stationId, newDisplayName, cb) => {
  339. async.waterfall([
  340. (next) => {
  341. db.models.station.update({_id: stationId}, {$set: {displayName: newDisplayName}}, next);
  342. },
  343. (res, next) => {
  344. stations.updateStation(stationId, next);
  345. }
  346. ], (err) => {
  347. if (err) {
  348. err = utils.getError(err);
  349. logger.error("STATIONS_UPDATE_DISPLAY_NAME", `Updating station "${stationId}" displayName to "${newDisplayName}" failed. "${err}"`);
  350. return cb({'status': 'failure', 'message': err});
  351. }
  352. logger.success("STATIONS_UPDATE_DISPLAY_NAME", `Updated station "${stationId}" displayName to "${newDisplayName}" successfully.`);
  353. return cb({'status': 'success', 'message': 'Successfully updated the display name.'});
  354. });
  355. }),
  356. updateDescription: hooks.ownerRequired((session, stationId, newDescription, cb) => {
  357. async.waterfall([
  358. (next) => {
  359. db.models.station.update({_id: stationId}, {$set: {description: newDescription}}, next);
  360. },
  361. (res, next) => {
  362. stations.updateStation(stationId, next);
  363. }
  364. ], (err) => {
  365. if (err) {
  366. err = utils.getError(err);
  367. logger.error("STATIONS_UPDATE_DESCRIPTION", `Updating station "${stationId}" description to "${newDescription}" failed. "${err}"`);
  368. return cb({'status': 'failure', 'message': err});
  369. }
  370. logger.success("STATIONS_UPDATE_DESCRIPTION", `Updated station "${stationId}" description to "${newDescription}" successfully.`);
  371. return cb({'status': 'success', 'message': 'Successfully updated the description.'});
  372. });
  373. }),
  374. updatePrivacy: hooks.ownerRequired((session, stationId, newPrivacy, cb) => {
  375. async.waterfall([
  376. (next) => {
  377. db.models.station.update({_id: stationId}, {$set: {privacy: newPrivacy}}, next);
  378. },
  379. (res, next) => {
  380. stations.updateStation(stationId, next);
  381. }
  382. ], (err) => {
  383. if (err) {
  384. err = utils.getError(err);
  385. logger.error("STATIONS_UPDATE_PRIVACY", `Updating station "${stationId}" privacy to "${newPrivacy}" failed. "${err}"`);
  386. return cb({'status': 'failure', 'message': err});
  387. }
  388. logger.success("STATIONS_UPDATE_PRIVACY", `Updated station "${stationId}" privacy to "${newPrivacy}" successfully.`);
  389. return cb({'status': 'success', 'message': 'Successfully updated the privacy.'});
  390. });
  391. }),
  392. updatePartyMode: hooks.ownerRequired((session, stationId, newPartyMode, cb) => {
  393. async.waterfall([
  394. (next) => {
  395. stations.getStation(stationId, next);
  396. },
  397. (station, next) => {
  398. if (!station) return next('Station not found.');
  399. if (station.partyMode === newPartyMode) return next('The party mode was already ' + ((newPartyMode) ? 'enabled.' : 'disabled.'));
  400. db.models.station.update({_id: stationId}, {$set: {partyMode: newPartyMode}}, next);
  401. },
  402. (res, next) => {
  403. stations.updateStation(stationId, next);
  404. }
  405. ], (err) => {
  406. if (err) {
  407. err = utils.getError(err);
  408. logger.error("STATIONS_UPDATE_PARTY_MODE", `Updating station "${stationId}" party mode to "${newPartyMode}" failed. "${err}"`);
  409. return cb({'status': 'failure', 'message': err});
  410. }
  411. logger.success("STATIONS_UPDATE_PARTY_MODE", `Updated station "${stationId}" party mode to "${newPartyMode}" successfully.`);
  412. cache.pub('station.updatePartyMode', {stationId: stationId, partyMode: newPartyMode});
  413. stations.skipStation(stationId)();
  414. return cb({'status': 'success', 'message': 'Successfully updated the party mode.'});
  415. });
  416. }),
  417. pause: hooks.ownerRequired((session, stationId, cb) => {
  418. async.waterfall([
  419. (next) => {
  420. stations.getStation(stationId, next);
  421. },
  422. (station, next) => {
  423. if (!station) return next('Station not found.');
  424. if (station.paused) return next('That station was already paused.');
  425. db.models.station.update({_id: stationId}, {$set: {paused: true, pausedAt: Date.now()}}, next);
  426. },
  427. (res, next) => {
  428. stations.updateStation(stationId, next);
  429. }
  430. ], (err) => {
  431. if (err) {
  432. err = utils.getError(err);
  433. logger.error("STATIONS_PAUSE", `Pausing station "${stationId}" failed. "${err}"`);
  434. return cb({'status': 'failure', 'message': err});
  435. }
  436. logger.success("STATIONS_PAUSE", `Paused station "${stationId}" successfully.`);
  437. cache.pub('station.pause', stationId);
  438. notifications.unschedule(`stations.nextSong?id=${stationId}`);
  439. return cb({'status': 'success', 'message': 'Successfully paused.'});
  440. });
  441. }),
  442. resume: hooks.ownerRequired((session, stationId, cb) => {
  443. async.waterfall([
  444. (next) => {
  445. stations.getStation(stationId, next);
  446. },
  447. (station, next) => {
  448. if (!station) return next('Station not found.');
  449. if (!station.paused) return next('That station is not paused.');
  450. station.timePaused += (Date.now() - station.pausedAt);
  451. db.models.station.update({_id: stationId}, {$set: {paused: false}, $inc: {timePaused: Date.now() - station.pausedAt}}, next);
  452. },
  453. (next) => {
  454. stations.updateStation(stationId, next);
  455. }
  456. ], (err) => {
  457. if (err) {
  458. err = utils.getError(err);
  459. logger.error("STATIONS_RESUME", `Resuming station "${stationId}" failed. "${err}"`);
  460. return cb({'status': 'failure', 'message': err});
  461. }
  462. logger.success("STATIONS_RESUME", `Resuming station "${stationId}" successfully.`);
  463. cache.pub('station.resume', stationId);
  464. return cb({'status': 'success', 'message': 'Successfully resumed.'});
  465. });
  466. }),
  467. remove: hooks.ownerRequired((session, stationId, cb) => {
  468. async.waterfall([
  469. (next) => {
  470. db.models.station.remove({ _id: stationId }, next);
  471. },
  472. (next) => {
  473. cache.hdel('stations', stationId, next);
  474. }
  475. ], (err) => {
  476. if (err) {
  477. err = utils.getError(err);
  478. logger.error("STATIONS_REMOVE", `Removing station "${stationId}" failed. "${err}"`);
  479. return cb({'status': 'failure', 'message': err});
  480. }
  481. logger.success("STATIONS_REMOVE", `Removing station "${stationId}" successfully.`);
  482. cache.pub('station.remove', stationId);
  483. return cb({'status': 'success', 'message': 'Successfully removed.'});
  484. });
  485. }),
  486. create: hooks.loginRequired((session, data, cb, userId) => {
  487. data._id = data._id.toLowerCase();
  488. let 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"];
  489. async.waterfall([
  490. (next) => {
  491. if (!data) return next('Invalid data.');
  492. next();
  493. },
  494. (next) => {
  495. db.models.station.findOne({ $or: [{_id: data._id}, {displayName: new RegExp(`^${data.displayName}$`, 'i')}] }, next);
  496. },
  497. (station, next) => {
  498. if (station) return next('A station with that name or display name already exists.');
  499. const { _id, displayName, description, genres, playlist, type, blacklistedGenres } = data;
  500. if (type === 'official') {
  501. db.models.user.findOne({_id: userId}, (err, user) => {
  502. if (err) return next(err);
  503. if (!user) return next('User not found.');
  504. if (user.role !== 'admin') return next('Admin required.');
  505. db.models.station.create({
  506. _id,
  507. displayName,
  508. description,
  509. type,
  510. privacy: 'private',
  511. playlist,
  512. genres,
  513. blacklistedGenres,
  514. currentSong: stations.defaultSong
  515. }, next);
  516. });
  517. } else if (type === 'community') {
  518. if (blacklist.indexOf(_id) !== -1) return next('That id is blacklisted. Please use a different id.');
  519. db.models.station.create({
  520. _id,
  521. displayName,
  522. description,
  523. type,
  524. privacy: 'private',
  525. owner: userId,
  526. queue: [],
  527. currentSong: null
  528. }, next);
  529. }
  530. }
  531. ], (err, station) => {
  532. if (err) {
  533. err = utils.getError(err);
  534. logger.error("STATIONS_CREATE", `Creating station failed. "${err}"`);
  535. return cb({'status': 'failure', 'message': err});
  536. }
  537. logger.success("STATIONS_CREATE", `Created station "${station._id}" successfully.`);
  538. cache.pub('station.create', station._id);
  539. return cb({'status': 'success', 'message': 'Successfully created station.'});
  540. });
  541. }),
  542. addToQueue: hooks.loginRequired((session, stationId, songId, cb, userId) => {
  543. async.waterfall([
  544. (next) => {
  545. stations.getStation(stationId, next);
  546. },
  547. (station, next) => {
  548. if (!station) return next('Station not found.');
  549. if (station.type !== 'community') return next('That station is not a community station.');
  550. if (station.currentSong && station.currentSong._id === songId) return next('That song is currently playing.');
  551. async.each(station.queue, (queueSong, next) => {
  552. if (queueSong._id === songId) return next('That song is already in the queue.');
  553. next();
  554. }, (err) => {
  555. next(err, station);
  556. });
  557. },
  558. (station, next) => {
  559. songs.getSong(songId, (err, song) => {
  560. if (!err && song) return next(null, song);
  561. utils.getSongFromYouTube(songId, (song) => {
  562. song.artists = [];
  563. song.skipDuration = 0;
  564. song.likes = -1;
  565. song.dislikes = -1;
  566. song.thumbnail = "empty";
  567. song.explicit = false;
  568. next(null, song);
  569. });
  570. });
  571. },
  572. (song, next) => {
  573. song.requestedBy = userId;
  574. db.models.station.update({_id: stationId}, {$push: {queue: song}}, next);
  575. },
  576. (res, next) => {
  577. stations.updateStation(stationId, next);
  578. }
  579. ], (err, station) => {
  580. if (err) {
  581. err = utils.getError(err);
  582. logger.error("STATIONS_ADD_SONG_TO_QUEUE", `Adding song "${songId}" to station "${stationId}" queue failed. "${err}"`);
  583. return cb({'status': 'failure', 'message': err});
  584. }
  585. logger.success("STATIONS_ADD_SONG_TO_QUEUE", `Added song "${songId}" to station "${stationId}" successfully.`);
  586. cache.pub('station.queueUpdate', stationId);
  587. return cb({'status': 'success', 'message': 'Successfully added song to queue.'});
  588. });
  589. }),
  590. removeFromQueue: hooks.ownerRequired((session, stationId, songId, cb, userId) => {
  591. async.waterfall([
  592. (next) => {
  593. if (!songId) return next('Invalid song id.');
  594. stations.getStation(stationId, next);
  595. },
  596. (station, next) => {
  597. if (!station) return next('Station not found.');
  598. if (station.type !== 'community') return next('Station is not a community station.');
  599. async.each(station.queue, (queueSong, next) => {
  600. if (queueSong._id === songId) return next(true);
  601. next();
  602. }, (err) => {
  603. if (err === true) return next();
  604. next('Song is not currently in the queue.');
  605. });
  606. },
  607. (next) => {
  608. db.models.update({_id: stationId}, {$pull: {queue: {songId: songId}}}, next);
  609. },
  610. (next) => {
  611. stations.updateStation(stationId, next);
  612. }
  613. ], (err, station) => {
  614. if (err) {
  615. err = utils.getError(err);
  616. logger.error("STATIONS_REMOVE_SONG_TO_QUEUE", `Removing song "${songId}" from station "${stationId}" queue failed. "${err}"`);
  617. return cb({'status': 'failure', 'message': err});
  618. }
  619. logger.success("STATIONS_REMOVE_SONG_TO_QUEUE", `Removed song "${songId}" from station "${stationId}" successfully.`);
  620. cache.pub('station.queueUpdate', stationId);
  621. return cb({'status': 'success', 'message': 'Successfully removed song from queue.'});
  622. });
  623. }),
  624. getQueue: hooks.adminRequired((session, stationId, cb) => {
  625. async.waterfall([
  626. (next) => {
  627. stations.getStation(stationId, next);
  628. },
  629. (station, next) => {
  630. if (!station) return next('Station not found.');
  631. if (station.type !== 'community') return next('Station is not a community station.');
  632. next(null, station);
  633. }
  634. ], (err, station) => {
  635. if (err) {
  636. err = utils.getError(err);
  637. logger.error("STATIONS_GET_QUEUE", `Getting queue for station "${stationId}" failed. "${err}"`);
  638. return cb({'status': 'failure', 'message': err});
  639. }
  640. logger.success("STATIONS_GET_QUEUE", `Got queue for station "${stationId}" successfully.`);
  641. return cb({'status': 'success', 'message': 'Successfully got queue.', queue: station.queue});
  642. });
  643. }),
  644. selectPrivatePlaylist: hooks.ownerRequired((session, stationId, playlistId, cb, userId) => {
  645. async.waterfall([
  646. (next) => {
  647. stations.getStation(stationId, next);
  648. },
  649. (station, next) => {
  650. if (!station) return next('Station not found.');
  651. if (station.type !== 'community') return next('Station is not a community station.');
  652. if (station.privatePlaylist === playlistId) return next('That private playlist is already selected.');
  653. db.models.playlist.findOne({_id: playlistId}, next);
  654. },
  655. (playlist, next) => {
  656. if (!playlist) return next('Playlist not found.');
  657. let currentSongIndex = (playlist.songs.length > 0) ? playlist.songs.length - 1 : 0;
  658. db.models.station.update({_id: stationId}, {$set: {privatePlaylist: playlistId, currentSongIndex: currentSongIndex}}, next);
  659. },
  660. (res, next) => {
  661. stations.updateStation(stationId, next);
  662. }
  663. ], (err, station) => {
  664. if (err) {
  665. err = utils.getError(err);
  666. logger.error("STATIONS_SELECT_PRIVATE_PLAYLIST", `Selecting private playlist "${playlistId}" for station "${stationId}" failed. "${err}"`);
  667. return cb({'status': 'failure', 'message': err});
  668. }
  669. logger.success("STATIONS_SELECT_PRIVATE_PLAYLIST", `Selected private playlist "${playlistId}" for station "${stationId}" successfully.`);
  670. if (!station.partyMode) stations.skipStation(stationId)();
  671. cache.pub('privatePlaylist.selected', {playlistId, stationId});
  672. return cb({'status': 'success', 'message': 'Successfully got queue.'});
  673. });
  674. }),
  675. };