server.js 24 KB

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