server.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. Meteor.startup(function() {
  2. reCAPTCHA.config({
  3. privatekey: '6LcVxg0TAAAAAI2fgIEEWHFxwNXeVIs8mzq5cfRM'
  4. });
  5. var stations = [{tag: "edm", display: "EDM"}, {tag: "pop", display: "Pop"}]; //Rooms to be set on server startup
  6. for(var i in stations){
  7. if(Rooms.find({type: stations[i]}).count() === 0){
  8. createRoom(stations[i].display, stations[i].tag);
  9. }
  10. }
  11. });
  12. Alerts.update({active: true}, {$set: {active: false}}, { multi: true });
  13. var stations = [];
  14. var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_";
  15. function createUniqueSongId() {
  16. var code = "";
  17. for (var i = 0; i < 6; i++) {
  18. code += chars[Math.floor(Math.random() * chars.length)];
  19. }
  20. if (Playlists.find({"songs.mid": code}).count() > 0) {
  21. return createUniqueSongId();
  22. } else {
  23. return code;
  24. }
  25. }
  26. function checkUsersPR() {
  27. var output = {};
  28. var connections = Meteor.server.stream_server.open_sockets;
  29. _.each(connections,function(connection){
  30. // named subscriptions
  31. var subs = connection._meteorSession._namedSubs;
  32. //var ip = connection.remoteAddress;
  33. var used_subs = [];
  34. for(var sub in subs){
  35. var mySubName = subs[sub]._name;
  36. if(subs[sub]._params.length>0){
  37. mySubName += subs[sub]._params[0]; // assume one id parameter for now
  38. }
  39. if (used_subs.indexOf(mySubName) === -1) {
  40. used_subs.push(mySubName);
  41. if(!output[mySubName]){
  42. output[mySubName] = 1;
  43. }else{
  44. output[mySubName] += 1;
  45. }
  46. }
  47. }
  48. // there are also these 'universal subscriptions'
  49. //not sure what these are, i count none in my tests
  50. //var usubs = connection._meteorSession._universalSubs;
  51. });
  52. for (var key in output) {
  53. getStation(key, function() {
  54. Rooms.update({type: key}, {$set: {users: output[key]}});
  55. });
  56. }
  57. return output;
  58. }
  59. function getStation(type, cb) {
  60. stations.forEach(function(station) {
  61. if (station.type === type) {
  62. cb(station);
  63. return;
  64. }
  65. });
  66. }
  67. function createRoom(display, tag) {
  68. var type = tag;
  69. if (Rooms.find({type: type}).count() === 0) {
  70. Rooms.insert({display: display, type: type, users: 0}, function(err) {
  71. if (err) {
  72. throw err;
  73. } else {
  74. if (Playlists.find({type: type}).count() === 1) {
  75. stations.push(new Station(type));
  76. } else {
  77. Playlists.insert({type: type, songs: getSongsByType(type)}, function (err2) {
  78. if (err2) {
  79. throw err2;
  80. } else {
  81. stations.push(new Station(type));
  82. }
  83. });
  84. }
  85. }
  86. });
  87. } else {
  88. return "Room already exists";
  89. }
  90. }
  91. function Station(type) {
  92. Meteor.publish(type, function() {
  93. return undefined;
  94. });
  95. var _this = this;
  96. var startedAt = Date.now();
  97. var playlist = Playlists.findOne({type: type});
  98. var songs = playlist.songs;
  99. if (playlist.lastSong === undefined) {
  100. Playlists.update({type: type}, {$set: {lastSong: 0}});
  101. playlist = Playlists.findOne({type: type});
  102. songs = playlist.songs;
  103. }
  104. var currentSong = playlist.lastSong;
  105. if (currentSong < (songs.length - 1)) {
  106. currentSong++;
  107. } else currentSong = 0;
  108. var currentTitle = songs[currentSong].title;
  109. Rooms.update({type: type}, {$set: {currentSong: {song: songs[currentSong], started: startedAt}, users: 0}});
  110. this.skipSong = function() {
  111. songs = Playlists.findOne({type: type}).songs;
  112. songs.forEach(function(song, index) {
  113. if (song.title === currentTitle) {
  114. currentSong = index;
  115. }
  116. });
  117. if (currentSong < (songs.length - 1)) {
  118. currentSong++;
  119. } else currentSong = 0;
  120. if (songs);
  121. if (currentSong === 0) {
  122. this.shufflePlaylist();
  123. } else {
  124. if (songs[currentSong].mid === undefined) {
  125. var newSong = songs[currentSong];
  126. newSong.mid = createUniqueSongId();
  127. songs[currentSong].mid = newSong.mid;
  128. Playlists.update({type: type, "songs": songs[currentSong]}, {$set: {"songs.$": newSong}});
  129. }
  130. currentTitle = songs[currentSong].title;
  131. Playlists.update({type: type}, {$set: {lastSong: currentSong}});
  132. Rooms.update({type: type}, {$set: {timePaused: 0}});
  133. this.songTimer();
  134. Rooms.update({type: type}, {$set: {currentSong: {song: songs[currentSong], started: startedAt}}});
  135. }
  136. };
  137. this.shufflePlaylist = function() {
  138. songs = Playlists.findOne({type: type}).songs;
  139. currentSong = 0;
  140. Playlists.update({type: type}, {$set: {"songs": []}});
  141. songs = shuffle(songs);
  142. songs.forEach(function(song) {
  143. if (song.mid === undefined) {
  144. song.mid = createUniqueSongId();
  145. }
  146. Playlists.update({type: type}, {$push: {"songs": song}});
  147. });
  148. currentTitle = songs[currentSong].title;
  149. Playlists.update({type: type}, {$set: {lastSong: currentSong}});
  150. Rooms.update({type: type}, {$set: {timePaused: 0}});
  151. this.songTimer();
  152. Rooms.update({type: type}, {$set: {currentSong: {song: songs[currentSong], started: startedAt}}});
  153. };
  154. Rooms.update({type: type}, {$set: {timePaused: 0}});
  155. var timer;
  156. this.songTimer = function() {
  157. startedAt = Date.now();
  158. if (timer !== undefined) {
  159. timer.pause();
  160. }
  161. timer = new Timer(function() {
  162. _this.skipSong();
  163. }, songs[currentSong].duration * 1000);
  164. };
  165. var state = Rooms.findOne({type: type}).state;
  166. this.pauseRoom = function() {
  167. if (state !== "paused") {
  168. timer.pause();
  169. Rooms.update({type: type}, {$set: {state: "paused"}});
  170. state = "paused";
  171. }
  172. };
  173. this.resumeRoom = function() {
  174. if (state !== "playing") {
  175. timer.resume();
  176. Rooms.update({type: type}, {$set: {state: "playing", timePaused: timer.timeWhenPaused()}});
  177. state = "playing";
  178. }
  179. };
  180. this.cancelTimer = function() {
  181. timer.pause();
  182. };
  183. this.getState = function() {
  184. return state;
  185. };
  186. this.type = type;
  187. this.songTimer();
  188. }
  189. function shuffle(array) {
  190. var currentIndex = array.length, temporaryValue, randomIndex ;
  191. // While there remain elements to shuffle...
  192. while (0 !== currentIndex) {
  193. // Pick a remaining element...
  194. randomIndex = Math.floor(Math.random() * currentIndex);
  195. currentIndex -= 1;
  196. // And swap it with the current element.
  197. temporaryValue = array[currentIndex];
  198. array[currentIndex] = array[randomIndex];
  199. array[randomIndex] = temporaryValue;
  200. }
  201. return array;
  202. }
  203. function Timer(callback, delay) {
  204. var timerId, start, remaining = delay;
  205. var timeWhenPaused = 0;
  206. var timePaused = new Date();
  207. this.pause = function() {
  208. Meteor.clearTimeout(timerId);
  209. remaining -= new Date() - start;
  210. timePaused = new Date();
  211. };
  212. this.resume = function() {
  213. start = new Date();
  214. Meteor.clearTimeout(timerId);
  215. timerId = Meteor.setTimeout(callback, remaining);
  216. timeWhenPaused += new Date() - timePaused;
  217. };
  218. this.timeWhenPaused = function() {
  219. return timeWhenPaused;
  220. };
  221. this.resume();
  222. }
  223. Meteor.users.deny({update: function () { return true; }});
  224. Meteor.users.deny({insert: function () { return true; }});
  225. Meteor.users.deny({remove: function () { return true; }});
  226. function getSongDuration(query, artistName){
  227. var duration;
  228. var search = query;
  229. query = query.toLowerCase().split(" ").join("%20");
  230. var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + query + '&type=track');
  231. for(var i in res.data){
  232. for(var j in res.data[i].items){
  233. if(search.indexOf(res.data[i].items[j].name) !== -1 && artistName.indexOf(res.data[i].items[j].artists[0].name) !== -1){
  234. duration = res.data[i].items[j].duration_ms / 1000;
  235. return duration;
  236. }
  237. }
  238. }
  239. }
  240. function getSongAlbumArt(query, artistName){
  241. var albumart;
  242. var search = query;
  243. query = query.toLowerCase().split(" ").join("%20");
  244. var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + query + '&type=track');
  245. for(var i in res.data){
  246. for(var j in res.data[i].items){
  247. if(search.indexOf(res.data[i].items[j].name) !== -1 && artistName.indexOf(res.data[i].items[j].artists[0].name) !== -1){
  248. albumart = res.data[i].items[j].album.images[1].url
  249. return albumart;
  250. }
  251. }
  252. }
  253. }
  254. //var room_types = ["edm", "nightcore"];
  255. var songsArr = [];
  256. function getSongsByType(type) {
  257. if (type === "edm") {
  258. return [
  259. {id: "aE2GCa-_nyU", mid: "fh6_Gf", title: "Radioactive", duration: getSongDuration("Radioactive - Lindsey Stirling and Pentatonix", "Lindsey Stirling, Pentatonix"), artist: "Lindsey Stirling, Pentatonix", type: "YouTube", img: "https://i.scdn.co/image/62167a9007cef2e8ef13ab1d93019312b9b03655"},
  260. {id: "aHjpOzsQ9YI", mid: "goG88g", title: "Crystallize", artist: "Lindsey Stirling", duration: getSongDuration("Crystallize", "Lindsey Stirling"), type: "YouTube", img: "https://i.scdn.co/image/b0c1ccdd0cd7bcda741ccc1c3e036f4ed2e52312"}
  261. ];
  262. } else if (type === "nightcore") {
  263. return [{id: "f7RKOP87tt4", mid: "5pGGog", title: "Monster (DotEXE Remix)", duration: getSongDuration("Monster (DotEXE Remix)", "Meg & Dia"), artist: "Meg & Dia", type: "YouTube", img: "https://i.scdn.co/image/35ecdfba9c31a6c54ee4c73dcf1ad474c560cd00"}];
  264. } else {
  265. return [{id: "dQw4w9WgXcQ", mid: "6_fdr4", title: "Never Gonna Give You Up", duration: getSongDuration("Never Gonna Give You Up", "Rick Astley"), artist: "Rick Astley", type: "YouTube", img: "https://i.scdn.co/image/5246898e19195715e65e261899baba890a2c1ded"}];
  266. }
  267. }
  268. Rooms.find({}).fetch().forEach(function(room) {
  269. var type = room.type;
  270. if (Playlists.find({type: type}).count() === 0) {
  271. if (type === "edm") {
  272. Playlists.insert({type: type, songs: getSongsByType(type)});
  273. } else if (type === "nightcore") {
  274. Playlists.insert({type: type, songs: getSongsByType(type)});
  275. } else {
  276. Playlists.insert({type: type, songs: getSongsByType(type)});
  277. }
  278. }
  279. if (Playlists.findOne({type: type}).songs.length === 0) {
  280. // Add a global video to Playlist so it can proceed
  281. } else {
  282. stations.push(new Station(type));
  283. }
  284. });
  285. Accounts.validateNewUser(function(user) {
  286. var username;
  287. if (user.services) {
  288. if (user.services.github) {
  289. username = user.services.github.username;
  290. } else if (user.services.facebook) {
  291. username = user.services.facebook.first_name;
  292. } else if (user.services.password) {
  293. username = user.username;
  294. }
  295. }
  296. if (Meteor.users.find({"profile.usernameL": username.toLowerCase()}).count() !== 0) {
  297. throw new Meteor.Error(403, "An account with that username already exists.");
  298. } else {
  299. return true;
  300. }
  301. });
  302. Accounts.onCreateUser(function(options, user) {
  303. var username;
  304. if (user.services) {
  305. if (user.services.github) {
  306. username = user.services.github.username;
  307. } else if (user.services.facebook) {
  308. username = user.services.facebook.first_name;
  309. } else if (user.services.password) {
  310. username = user.username;
  311. }
  312. }
  313. user.profile = {username: username, usernameL: username.toLowerCase(), rank: "default", liked: [], disliked: []};
  314. return user;
  315. });
  316. ServiceConfiguration.configurations.remove({
  317. service: "facebook"
  318. });
  319. ServiceConfiguration.configurations.insert({
  320. service: "facebook",
  321. appId: "1496014310695890",
  322. secret: "9a039f254a08a1488c08bb0737dbd2a6"
  323. });
  324. ServiceConfiguration.configurations.remove({
  325. service: "github"
  326. });
  327. ServiceConfiguration.configurations.insert({
  328. service: "github",
  329. clientId: "dcecd720f47c0e4001f7",
  330. secret: "375939d001ef1a0ca67c11dbf8fb9aeaa551e01b"
  331. });
  332. Meteor.publish("alerts", function() {
  333. return Alerts.find({active: true})
  334. });
  335. Meteor.publish("allAlerts", function() {
  336. return Alerts.find({active: false})
  337. });
  338. Meteor.publish("playlists", function() {
  339. return Playlists.find({})
  340. });
  341. Meteor.publish("rooms", function() {
  342. return Rooms.find({});
  343. });
  344. Meteor.publish("queues", function() {
  345. return Queues.find({});
  346. });
  347. Meteor.publish("reports", function() {
  348. return Reports.find({});
  349. });
  350. Meteor.publish("chat", function() {
  351. return Chat.find({});
  352. });
  353. Meteor.publish("userProfiles", function() {
  354. return Meteor.users.find({}, {fields: {profile: 1, createdAt: 1}});
  355. });
  356. Meteor.publish("isAdmin", function() {
  357. return Meteor.users.find({_id: this.userId, "profile.rank": "admin"});
  358. });
  359. function isAdmin() {
  360. var userData = Meteor.users.find(Meteor.userId());
  361. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
  362. return true;
  363. } else {
  364. return false;
  365. }
  366. }
  367. Meteor.methods({
  368. resetRating: function() {
  369. if (isAdmin()) {
  370. stations.forEach(function (station) {
  371. var type = station.type;
  372. var temp_songs = Playlists.findOne({type: type}).songs;
  373. Playlists.update({type: type}, {$set: {"songs": []}});
  374. temp_songs.forEach(function (song) {
  375. song.likes = 0;
  376. song.dislikes = 0;
  377. Playlists.update({type: type}, {$push: {"songs": song}});
  378. });
  379. });
  380. Meteor.users.update({}, {$set: {"profile.liked": [], "profile.disliked": []}}, {multi: true});
  381. } else {
  382. throw Meteor.Error(403, "Invalid permissions.");
  383. }
  384. },
  385. removeAlerts: function() {
  386. if (isAdmin()) {
  387. Alerts.update({active: true}, {$set: {active: false}}, { multi: true });
  388. } else {
  389. throw Meteor.Error(403, "Invalid permissions.");
  390. }
  391. },
  392. addAlert: function(description, priority) {
  393. if (isAdmin()) {
  394. if (description.length > 0 && description.length < 400) {
  395. var username = Meteor.user().profile.username;
  396. if (["danger", "warning", "success", "primary"].indexOf(priority) === -1) {
  397. priority = "warning";
  398. }
  399. Alerts.insert({description: description, priority: priority, active: true, createdBy: username});
  400. return true;
  401. } else {
  402. throw Meteor.Error(403, "Invalid description length.");
  403. }
  404. } else {
  405. throw Meteor.Error(403, "Invalid permissions.");
  406. }
  407. },
  408. sendMessage: function(type, message) {
  409. if (Meteor.userId()) {
  410. var user = Meteor.user();
  411. var time = new Date();
  412. var rawrank = user.profile.rank;
  413. var username = user.profile.username;
  414. var rank = user.profile.rank;
  415. if (message.length === 0) {
  416. throw new Meteor.Error(406, "Message length cannot be 0.");
  417. }
  418. if (message.length > 300) {
  419. throw new Meteor.Error(406, "Message length cannot be more than 300 characters long..");
  420. }
  421. if (user.profile.rank = "admin") {
  422. Chat.insert({type: type, rawrank: rawrank, rank: "[A]", message: message, time: time, username: username});
  423. return true;
  424. } else if (user.profile.rank = "mod") {
  425. Chat.insert({type: type, rank: "[M]", message: message, time: time, username: username});
  426. return true;
  427. }
  428. else {
  429. Chat.insert({type: type, rawrank: rawrank, message: message, time: time, username: username});
  430. return true;
  431. }
  432. } else {
  433. throw new Meteor.Error(403, "Invalid permissions.");
  434. }
  435. },
  436. likeSong: function(mid) {
  437. if (Meteor.userId()) {
  438. var user = Meteor.user();
  439. if (user.profile.liked.indexOf(mid) === -1) {
  440. Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.liked": mid}});
  441. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": 1}})
  442. } else {
  443. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
  444. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": -1}})
  445. }
  446. if (user.profile.disliked.indexOf(mid) !== -1) {
  447. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
  448. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": -1}})
  449. }
  450. return true;
  451. } else {
  452. throw new Meteor.Error(403, "Invalid permissions.");
  453. }
  454. },
  455. dislikeSong: function(mid) {
  456. if (Meteor.userId()) {
  457. var user = Meteor.user();
  458. if (user.profile.disliked.indexOf(mid) === -1) {
  459. Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.disliked": mid}});
  460. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": 1}});
  461. } else {
  462. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
  463. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": -1}});
  464. }
  465. if (user.profile.liked.indexOf(mid) !== -1) {
  466. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
  467. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": -1}});
  468. }
  469. return true;
  470. } else {
  471. throw new Meteor.Error(403, "Invalid permissions.");
  472. }
  473. },
  474. submitReport: function(report, id) {
  475. var obj = report;
  476. obj.id = id;
  477. Reports.insert(obj);
  478. },
  479. shufflePlaylist: function(type) {
  480. if (isAdmin()) {
  481. getStation(type, function(station) {
  482. if (station === undefined) {
  483. throw new Meteor.Error(404, "Station not found.");
  484. } else {
  485. station.cancelTimer();
  486. station.shufflePlaylist();
  487. }
  488. });
  489. }
  490. },
  491. skipSong: function(type) {
  492. if (isAdmin()) {
  493. getStation(type, function(station) {
  494. if (station === undefined) {
  495. throw new Meteor.Error(404, "Station not found.");
  496. } else {
  497. station.skipSong();
  498. }
  499. });
  500. }
  501. },
  502. pauseRoom: function(type) {
  503. if (isAdmin()) {
  504. getStation(type, function(station) {
  505. if (station === undefined) {
  506. throw new Meteor.Error(403, "Room doesn't exist.");
  507. } else {
  508. station.pauseRoom();
  509. }
  510. });
  511. } else {
  512. throw new Meteor.Error(403, "Invalid permissions.");
  513. }
  514. },
  515. resumeRoom: function(type) {
  516. if (isAdmin()) {
  517. getStation(type, function(station) {
  518. if (station === undefined) {
  519. throw new Meteor.Error(403, "Room doesn't exist.");
  520. } else {
  521. station.resumeRoom();
  522. }
  523. });
  524. } else {
  525. throw new Meteor.Error(403, "Invalid permissions.");
  526. }
  527. },
  528. createUserMethod: function(formData, captchaData) {
  529. var verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(this.connection.clientAddress, captchaData);
  530. if (!verifyCaptchaResponse.success) {
  531. console.log('reCAPTCHA check failed!', verifyCaptchaResponse);
  532. throw new Meteor.Error(422, 'reCAPTCHA Failed: ' + verifyCaptchaResponse.error);
  533. } else {
  534. console.log('reCAPTCHA verification passed!');
  535. Accounts.createUser({
  536. username: formData.username,
  537. email: formData.email,
  538. password: formData.password
  539. });
  540. }
  541. return true;
  542. },
  543. addSongToQueue: function(type, songData) {
  544. if (Meteor.userId()) {
  545. type = type.toLowerCase();
  546. if (Rooms.find({type: type}).count() === 1) {
  547. if (Queues.find({type: type}).count() === 0) {
  548. Queues.insert({type: type, songs: []});
  549. }
  550. if (songData !== undefined && Object.keys(songData).length === 5 && songData.type !== undefined && songData.title !== undefined && songData.title !== undefined && songData.artist !== undefined && songData.img !== undefined) {
  551. songData.duration = getSongDuration(songData.title, songData.artist);
  552. songData.img = getSongAlbumArt(songData.title, songData.artist);
  553. songData.skipDuration = 0;
  554. var mid = createUniqueSongId();
  555. if (mid !== undefined) {
  556. songData.mid = mid;
  557. Queues.update({type: type}, {
  558. $push: {
  559. songs: {
  560. id: songData.id,
  561. mid: songData.mid,
  562. title: songData.title,
  563. artist: songData.artist,
  564. duration: songData.duration,
  565. skipDuration: songData.skipDuration,
  566. img: songData.img,
  567. type: songData.type
  568. }
  569. }
  570. });
  571. return true;
  572. } else {
  573. throw new Meteor.Error(500, "Am error occured.");
  574. }
  575. } else {
  576. throw new Meteor.Error(403, "Invalid data.");
  577. }
  578. } else {
  579. throw new Meteor.Error(403, "Invalid genre.");
  580. }
  581. } else {
  582. throw new Meteor.Error(403, "Invalid permissions.");
  583. }
  584. },
  585. updateQueueSong: function(genre, oldSong, newSong) {
  586. if (isAdmin()) {
  587. newSong.mid = oldSong.mid;
  588. Queues.update({type: genre, "songs": oldSong}, {$set: {"songs.$": newSong}});
  589. return true;
  590. } else {
  591. throw new Meteor.Error(403, "Invalid permissions.");
  592. }
  593. },
  594. updatePlaylistSong: function(genre, oldSong, newSong) {
  595. if (isAdmin()) {
  596. newSong.mid = oldSong.mid;
  597. Playlists.update({type: genre, "songs": oldSong}, {$set: {"songs.$": newSong}});
  598. return true;
  599. } else {
  600. throw new Meteor.Error(403, "Invalid permissions.");
  601. }
  602. },
  603. removeSongFromQueue: function(type, mid) {
  604. if (isAdmin()) {
  605. type = type.toLowerCase();
  606. Queues.update({type: type}, {$pull: {songs: {mid: mid}}});
  607. } else {
  608. throw new Meteor.Error(403, "Invalid permissions.");
  609. }
  610. },
  611. removeSongFromPlaylist: function(type, mid) {
  612. if (isAdmin()) {
  613. type = type.toLowerCase();
  614. Playlists.update({type: type}, {$pull: {songs: {mid: mid}}});
  615. } else {
  616. throw new Meteor.Error(403, "Invalid permissions.");
  617. }
  618. },
  619. addSongToPlaylist: function(type, songData) {
  620. if (isAdmin()) {
  621. type = type.toLowerCase();
  622. if (Rooms.find({type: type}).count() === 1) {
  623. if (Playlists.find({type: type}).count() === 0) {
  624. Playlists.insert({type: type, songs: []});
  625. }
  626. console.log(songData);
  627. if (songData !== undefined && Object.keys(songData).length === 8 && songData.type !== undefined && songData.mid !== undefined && songData.title !== undefined && songData.title !== undefined && songData.artist !== undefined && songData.duration !== undefined && songData.skipDuration !== undefined && songData.img !== undefined) {
  628. songData.likes = 0;
  629. songData.dislikes = 0
  630. Playlists.update({type: type}, {
  631. $push: {
  632. songs: {
  633. id: songData.id,
  634. mid: songData.mid,
  635. title: songData.title,
  636. artist: songData.artist,
  637. duration: songData.duration,
  638. skipDuration: songData.skipDuration,
  639. img: songData.img,
  640. type: songData.type,
  641. likes: songData.likes,
  642. dislikes: songData.dislikes
  643. }
  644. }
  645. });
  646. Queues.update({type: type}, {$pull: {songs: {mid: songData.mid}}});
  647. return true;
  648. } else {
  649. throw new Meteor.Error(403, "Invalid data.");
  650. }
  651. } else {
  652. throw new Meteor.Error(403, "Invalid genre.");
  653. }
  654. } else {
  655. throw new Meteor.Error(403, "Invalid permissions.");
  656. }
  657. },
  658. createRoom: function(display, tag) {
  659. if (isAdmin()) {
  660. createRoom(display, tag);
  661. } else {
  662. throw new Meteor.Error(403, "Invalid permissions.");
  663. }
  664. },
  665. deleteRoom: function(type){
  666. if (isAdmin()) {
  667. Rooms.remove({type: type});
  668. Playlists.remove({type: type});
  669. Queues.remove({type: type});
  670. return true;
  671. } else {
  672. throw new Meteor.Error(403, "Invalid permissions.");
  673. }
  674. },
  675. getUserNum: function(){
  676. return Object.keys(Meteor.default_server.sessions).length;
  677. }
  678. });
  679. Meteor.setInterval(function() {
  680. checkUsersPR();
  681. }, 10000);