server.js 25 KB

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