app.js 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  1. History = new Mongo.Collection("history");
  2. Playlists = new Mongo.Collection("playlists");
  3. Rooms = new Mongo.Collection("rooms");
  4. Queues = new Mongo.Collection("queues");
  5. Chat = new Mongo.Collection("chat");
  6. if (Meteor.isClient) {
  7. Meteor.startup(function() {
  8. reCAPTCHA.config({
  9. publickey: '6LcVxg0TAAAAAE18vBiH00UAyaJggsmLm890SjZl'
  10. });
  11. });
  12. Meteor.subscribe("queues");
  13. Meteor.subscribe("playlists");
  14. var minterval;
  15. var hpSound = undefined;
  16. var songsArr = [];
  17. var ytArr = [];
  18. var _sound = undefined;
  19. var parts = location.href.split('/');
  20. var id = parts.pop();
  21. var type = id.toLowerCase();
  22. var resizeSeekerbarInterval;
  23. function getSpotifyInfo(title, cb, artist) {
  24. var q = "";
  25. q = title;
  26. if (artist !== undefined) {
  27. q += " artist:" + artist;
  28. }
  29. $.ajax({
  30. type: "GET",
  31. url: 'https://api.spotify.com/v1/search?q=' + encodeURIComponent(q) + '&type=track',
  32. applicationType: "application/json",
  33. contentType: "json",
  34. success: function (data) {
  35. cb(data);
  36. }
  37. });
  38. }
  39. Template.profile.helpers({
  40. "username": function() {
  41. return Session.get("username");
  42. },
  43. "first_joined": function() {
  44. return moment(Session.get("first_joined")).format("DD/MM/YYYY HH:mm:ss");
  45. },
  46. "rank": function() {
  47. return Session.get("rank");
  48. },
  49. loaded: function() {
  50. return Session.get("loaded");
  51. }
  52. });
  53. Template.profile.onCreated(function() {
  54. var parts = location.href.split('/');
  55. var username = parts.pop();
  56. Session.set("loaded", false);
  57. Meteor.subscribe("userProfiles", function() {
  58. if (Meteor.users.find({"profile.usernameL": username.toLowerCase()}).count() === 0) {
  59. window.location = "/";
  60. } else {
  61. var data = Meteor.users.find({"profile.usernameL": username.toLowerCase()}).fetch()[0];
  62. Session.set("username", data.profile.username);
  63. Session.set("first_joined", data.createdAt);
  64. Session.set("rank", data.profile.rank);
  65. Session.set("loaded", true);
  66. }
  67. });
  68. });
  69. curPath=function(){var c=window.location.pathname;var b=c.slice(0,-1);var a=c.slice(-1);if(b==""){return"/"}else{if(a=="/"){return b}else{return c}}};
  70. Handlebars.registerHelper('active', function(path) {
  71. return curPath() == path ? 'active' : '';
  72. });
  73. Template.header.helpers({
  74. currentUser: function() {
  75. return Meteor.user();
  76. },
  77. isAdmin: function() {
  78. if (Meteor.user() && Meteor.user().profile) {
  79. return Meteor.user().profile.rank === "admin";
  80. } else {
  81. return false;
  82. }
  83. }
  84. });
  85. Template.header.events({
  86. "click .logout": function(e){
  87. e.preventDefault();
  88. Meteor.logout();
  89. if (hpSound !== undefined) {
  90. hpSound.stop();
  91. }
  92. }
  93. });
  94. Template.register.events({
  95. "submit form": function(e){
  96. e.preventDefault();
  97. var username = e.target.registerUsername.value;
  98. var email = e.target.registerEmail.value;
  99. var password = e.target.registerPassword.value;
  100. var captchaData = grecaptcha.getResponse();
  101. Meteor.call("createUserMethod", {username: username, email: email, password: password}, captchaData, function(err, res) {
  102. grecaptcha.reset();
  103. if (err) {
  104. console.log(err);
  105. var errAlert = $('<div class="alert alert-danger" role="alert"><strong>Oh Snap!</strong> ' + err.reason + '</div>');
  106. $("#login").after(errAlert);
  107. errAlert.fadeOut(20000, function() {
  108. errAlert.remove();
  109. });
  110. } else {
  111. Meteor.loginWithPassword(username, password);
  112. Accounts.onLogin(function(){
  113. window.location.href = "/";
  114. })
  115. }
  116. });
  117. },
  118. "click #github-login": function(){
  119. Meteor.loginWithGithub()
  120. Accounts.onLogin(function(){
  121. window.location.href = "/"
  122. });
  123. }
  124. });
  125. Template.login.events({
  126. "submit form": function(e){
  127. e.preventDefault();
  128. var username = e.target.loginUsername.value;
  129. var password = e.target.loginPassword.value;
  130. Meteor.loginWithPassword(username, password);
  131. Accounts.onLogin(function(){
  132. window.location.href = "/";
  133. })
  134. Accounts.onLoginFailure(function(){
  135. $("input").css("background-color","indianred");
  136. $("input").on("click",function(){
  137. $("input").css({
  138. "-webkit-appearance": "none",
  139. " -moz-appearance": "none",
  140. " appearance": "none",
  141. "outline": "0",
  142. "border": "1px solid rgba(255, 255, 255, 0.4)",
  143. "background-color": "rgba(255, 255, 255, 0.2)",
  144. "width": "304px",
  145. "border-radius": "3px",
  146. "padding": "10px 15px",
  147. "margin": "0 auto 10px auto",
  148. "display": "block",
  149. "text-align": "center",
  150. "font-size": "18px",
  151. "color": "white",
  152. "-webkit-transition-duration": "0.25s",
  153. " transition-duration": "0.25s",
  154. "font-weight": "300"
  155. });
  156. $("input:focus").css({
  157. "width": "354px",
  158. "color": "white"
  159. })
  160. $("input").on("blur", function(){
  161. $(this).css("width", "304px");
  162. })
  163. })
  164. });
  165. },
  166. "click #github-login": function(){
  167. Meteor.loginWithGithub()
  168. Accounts.onLogin(function(){
  169. window.location.href = "/"
  170. });
  171. }
  172. });
  173. Template.dashboard.helpers({
  174. rooms: function() {
  175. return Rooms.find({});
  176. },
  177. currentSong: function() {
  178. var history = History.find({type: this.type}).fetch();
  179. if (history.length < 1) {
  180. return {};
  181. } else {
  182. history = history[0];
  183. return history.history[history.history.length - 1];
  184. }
  185. }
  186. });
  187. Template.dashboard.onCreated(function() {
  188. if (_sound !== undefined) _sound.stop();
  189. if (minterval !== undefined) {
  190. Meteor.clearInterval(minterval);
  191. }
  192. if (resizeSeekerbarInterval !== undefined) {
  193. Meteor.clearInterval(resizeSeekerbarInterval);
  194. resizeSeekerbarInterval = undefined;
  195. }
  196. Meteor.subscribe("history");
  197. });
  198. Template.room.events({
  199. "click #add-song-button": function(e){
  200. e.preventDefault();
  201. parts = location.href.split('/');
  202. id = parts.pop();
  203. var genre = id.toLowerCase();
  204. var type = $("#type").val();
  205. id = $("#id").val();
  206. var title = $("#title").val();
  207. var artist = $("#artist").val();
  208. var img = $("#img").val();
  209. var songData = {type: type, id: id, title: title, artist: artist, img: img};
  210. Meteor.call("addSongToQueue", genre, songData, function(err, res) {
  211. console.log(err, res);
  212. });
  213. $("#close-modal").click();
  214. },
  215. "click #toggle-video": function(e){
  216. e.preventDefault();
  217. if (Session.get("mediaHidden")) {
  218. $("#media-container").removeClass("hidden");
  219. $("#toggle-video").text("Hide video");
  220. Session.set("mediaHidden", false);
  221. } else {
  222. $("#media-container").addClass("hidden");
  223. $("#toggle-video").text("Show video");
  224. Session.set("mediaHidden", true);
  225. }
  226. },
  227. "click #return": function(e){
  228. $("#add-info").hide();
  229. $("#search-info").show();
  230. },
  231. "click #search-song": function(){
  232. $("#song-results").empty();
  233. var search_type = $("#search_type").val();
  234. if (search_type === "YouTube") {
  235. $.ajax({
  236. type: "GET",
  237. url: "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" + $("#song-input").val() + "&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY",
  238. applicationType: "application/json",
  239. contentType: "json",
  240. success: function(data){
  241. for(var i in data.items){
  242. $("#song-results").append("<p>" + data.items[i].snippet.title + "</p>");
  243. ytArr.push({title: data.items[i].snippet.title, id: data.items[i].id.videoId});
  244. }
  245. $("#song-results p").click(function(){
  246. $("#search-info").hide();
  247. $("#add-info").show();
  248. var title = $(this).text();
  249. for(var i in ytArr){
  250. if(ytArr[i].title === title){
  251. var songObj = {
  252. id: ytArr[i].id,
  253. title: ytArr[i].title,
  254. type: "youtube"
  255. };
  256. $("#title").val(songObj.title);
  257. $("#artist").val("");
  258. $("#id").val(songObj.id);
  259. $("#type").val("YouTube");
  260. getSpotifyInfo(songObj.title.replace(/\[.*\]/g, ""), function(data) {
  261. if (data.tracks.items.length > 0) {
  262. $("#title").val(data.tracks.items[0].name);
  263. var artists = [];
  264. $("#img").val(data.tracks.items[0].album.images[1].url);
  265. data.tracks.items[0].artists.forEach(function(artist) {
  266. artists.push(artist.name);
  267. });
  268. $("#artist").val(artists.join(", "));
  269. }
  270. });
  271. }
  272. }
  273. })
  274. }
  275. })
  276. } else if (search_type === "SoundCloud") {
  277. SC.get('/tracks', { q: $("#song-input").val()}, function(tracks) {
  278. for(var i in tracks){
  279. $("#song-results").append("<p>" + tracks[i].title + "</p>")
  280. songsArr.push({title: tracks[i].title, id: tracks[i].id, duration: tracks[i].duration / 1000});
  281. }
  282. $("#song-results p").click(function(){
  283. $("#search-info").hide();
  284. $("#add-info").show();
  285. var title = $(this).text();
  286. for(var i in songsArr){
  287. if(songsArr[i].title === title){
  288. var id = songsArr[i].id;
  289. var duration = songsArr[i].duration;
  290. var songObj = {
  291. title: songsArr[i].title,
  292. id: id,
  293. duration: duration,
  294. type: "soundcloud"
  295. }
  296. $("#title").val(songObj.title);
  297. // Set ID field
  298. $("#id").val(songObj.id);
  299. $("#type").val("SoundCloud");
  300. getSpotifyInfo(songObj.title.replace(/\[.*\]/g, ""), function(data) {
  301. if (data.tracks.items.length > 0) {
  302. $("#title").val(data.tracks.items[0].name);
  303. var artists = [];
  304. data.tracks.items[0].artists.forEach(function(artist) {
  305. artists.push(artist.name);
  306. });
  307. $("#artist").val(artists.join(", "));
  308. }
  309. // Set title field again if possible
  310. // Set artist if possible
  311. });
  312. }
  313. }
  314. })
  315. });
  316. }
  317. },
  318. "click #add-songs": function(){
  319. $("#add-songs-modal").show();
  320. },
  321. "click #close-modal": function(){
  322. $("#search-info").show();
  323. $("#add-info").hide();
  324. },
  325. "click #volume-icon": function(){
  326. var volume = 0;
  327. var slider = $("#volume-slider").slider();
  328. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  329. if (yt_player !== undefined) {
  330. yt_player.setVolume(volume);
  331. localStorage.setItem("volume", volume);
  332. $("#volume-slider").slider("setValue", volume);
  333. } else if (_sound !== undefined) {
  334. _sound.setVolume(volume);
  335. localStorage.setItem("volume", volume);
  336. $("#volume-slider").slider("setValue", volume);
  337. }
  338. },
  339. "click #play": function() {
  340. Meteor.call("resumeRoom", type);
  341. },
  342. "click #pause": function() {
  343. Meteor.call("pauseRoom", type);
  344. }
  345. });
  346. Template.room.onRendered(function() {
  347. $(document).ready(function() {
  348. function makeSlider(){
  349. var slider = $("#volume-slider").slider();
  350. var volume = localStorage.getItem("volume") || 20;
  351. $("#volume-slider").slider("setValue", volume);
  352. if (slider.length === 0) {
  353. Meteor.setTimeout(function() {
  354. makeSlider();
  355. }, 500);
  356. } else {
  357. slider.on("slide", function(val) {
  358. if (val.value === 0) {
  359. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  360. } else {
  361. $("#volume-icon").removeClass("fa-volume-off").addClass("fa-volume-down")
  362. }
  363. if (yt_player !== undefined) {
  364. yt_player.setVolume(val.value);
  365. localStorage.setItem("volume", val.value);
  366. } else if (_sound !== undefined) {
  367. //_sound
  368. var volume = val.value / 100;
  369. _sound.setVolume(volume);
  370. localStorage.setItem("volume", val.value);
  371. }
  372. });
  373. }
  374. }
  375. makeSlider();
  376. });
  377. });
  378. Template.room.helpers({
  379. type: function() {
  380. var parts = location.href.split('/');
  381. var id = parts.pop();
  382. return id.toUpperCase();
  383. },
  384. title: function(){
  385. return Session.get("title");
  386. },
  387. artist: function(){
  388. return Session.get("artist");
  389. },
  390. title_next: function(){
  391. return Session.get("title_next");
  392. },
  393. artist_next: function(){
  394. return Session.get("artist_next");
  395. },
  396. title_after: function(){
  397. return Session.get("title_after");
  398. },
  399. artist_after: function(){
  400. return Session.get("artist_after");
  401. },
  402. loaded: function() {
  403. return Session.get("loaded");
  404. },
  405. isAdmin: function() {
  406. if (Meteor.user() && Meteor.user().profile) {
  407. return Meteor.user().profile.rank === "admin";
  408. } else {
  409. return false;
  410. }
  411. },
  412. paused: function() {
  413. return Session.get("state") === "paused";
  414. }
  415. });
  416. Template.admin.helpers({
  417. queues: function() {
  418. return Queues.find({});
  419. },
  420. playlists: function() {
  421. return Playlists.find({});
  422. }
  423. });
  424. var yt_player = undefined;
  425. var _sound = undefined;
  426. Template.admin.events({
  427. "click .preview-button": function(e){
  428. Session.set("song", this);
  429. },
  430. "click .edit-queue-button": function(e){
  431. Session.set("song", this);
  432. Session.set("genre", $(e.toElement).data("genre"));
  433. Session.set("type", "queue");
  434. $("#type").val(this.type);
  435. $("#artist").val(this.artist);
  436. $("#title").val(this.title);
  437. $("#img").val(this.img);
  438. $("#id").val(this.id);
  439. $("#duration").val(this.duration);
  440. },
  441. "click .edit-playlist-button": function(e){
  442. Session.set("song", this);
  443. Session.set("genre", $(e.toElement).data("genre"));
  444. Session.set("type", "playlist");
  445. $("#type").val(this.type);
  446. $("#artist").val(this.artist);
  447. $("#title").val(this.title);
  448. $("#img").val(this.img);
  449. $("#id").val(this.id);
  450. $("#duration").val(this.duration);
  451. },
  452. "click .add-song-button": function(e){
  453. var genre = $(e.toElement).data("genre") || $(e.toElement).parent().data("genre");
  454. Meteor.call("addSongToPlaylist", genre, this);
  455. },
  456. "click .deny-song-button": function(e){
  457. var genre = $(e.toElement).data("genre") || $(e.toElement).parent().data("genre");
  458. Meteor.call("removeSongFromQueue", genre, this.id);
  459. },
  460. "click .remove-song-button": function(e){
  461. var genre = $(e.toElement).data("genre") || $(e.toElement).parent().data("genre");
  462. Meteor.call("removeSongFromPlaylist", genre, this.id);
  463. },
  464. "click #play": function() {
  465. $("#play").attr("disabled", true);
  466. $("#stop").attr("disabled", false);
  467. var song = Session.get("song");
  468. var id = song.id;
  469. var type = song.type;
  470. var volume = localStorage.getItem("volume") || 20;
  471. if (type === "YouTube") {
  472. if (yt_player === undefined) {
  473. yt_player = new YT.Player("previewPlayer", {
  474. height: 540,
  475. width: 568,
  476. videoId: id,
  477. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0},
  478. events: {
  479. 'onReady': function(event) {
  480. event.target.playVideo();
  481. event.target.setVolume(volume);
  482. },
  483. 'onStateChange': function(event){
  484. if (event.data == YT.PlayerState.PAUSED) {
  485. event.target.playVideo();
  486. }
  487. if (event.data == YT.PlayerState.PLAYING) {
  488. $("#play").attr("disabled", true);
  489. $("#stop").attr("disabled", false);
  490. } else {
  491. $("#play").attr("disabled", false);
  492. $("#stop").attr("disabled", true);
  493. }
  494. }
  495. }
  496. });
  497. } else {
  498. yt_player.loadVideoById(id);
  499. }
  500. $("#previewPlayer").show();
  501. } else if (type === "SoundCloud") {
  502. SC.stream("/tracks/" + song.id, function(sound) {
  503. _sound = sound;
  504. sound.setVolume(volume / 100);
  505. sound.play();
  506. });
  507. }
  508. },
  509. "click #stop": function() {
  510. $("#play").attr("disabled", false);
  511. $("#stop").attr("disabled", true);
  512. if (yt_player !== undefined) {
  513. yt_player.stopVideo();
  514. }
  515. if (_sound !== undefined) {
  516. _sound.stop();
  517. }
  518. },
  519. "click #croom_create": function() {
  520. Meteor.call("createRoom", $("#croom").val(), function (err, res) {
  521. if (err) {
  522. alert("Error " + err.error + ": " + err.reason);
  523. } else {
  524. window.location = "/" + $("#croom").val();
  525. }
  526. });
  527. },
  528. "click #get-spotify-info": function() {
  529. var search = $("#title").val();
  530. var artistName = $("#artist").val();
  531. getSpotifyInfo(search, function(data) {
  532. for(var i in data){
  533. for(var j in data[i].items){
  534. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  535. $("#img").val(data[i].items[j].album.images[1].url);
  536. $("#duration").val(data[i].items[j].duration_ms / 1000);
  537. return;
  538. }
  539. }
  540. }
  541. }, artistName);
  542. },
  543. "click #save-song-button": function() {
  544. var newSong = {};
  545. newSong.title = $("#title").val();
  546. newSong.artist = $("#artist").val();
  547. newSong.img = $("#img").val();
  548. newSong.type = $("#type").val();
  549. newSong.duration = $("#duration").val();
  550. if (Session.get("type") === "playlist") {
  551. Meteor.call("updatePlaylistSong", Session.get("genre"), Session.get("song"), newSong, function() {
  552. $('#editModal').modal('hide');
  553. });
  554. } else {
  555. Meteor.call("updateQueueSong", Session.get("genre"), Session.get("song"), newSong, function() {
  556. $('#editModal').modal('hide');
  557. });
  558. }
  559. }
  560. });
  561. Template.admin.onCreated(function() {
  562. var tag = document.createElement("script");
  563. tag.src = "https://www.youtube.com/iframe_api";
  564. var firstScriptTag = document.getElementsByTagName('script')[0];
  565. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  566. });
  567. Template.admin.onRendered(function() {
  568. $("#previewModal").on("hidden.bs.modal", function() {
  569. if (yt_player !== undefined) {
  570. $("#play").attr("disabled", false);
  571. $("#stop").attr("disabled", true);
  572. $("#previewPlayer").hide();
  573. yt_player.loadVideoById("", 0);
  574. yt_player.seekTo(0);
  575. yt_player.stopVideo();
  576. }
  577. if (_sound !== undefined) {
  578. _sound.stop();
  579. $("#play").attr("disabled", false);
  580. $("#stop").attr("disabled", true);
  581. }
  582. });
  583. $(document).ready(function() {
  584. function makeSlider(){
  585. var slider = $("#volume-slider").slider();
  586. var volume = localStorage.getItem("volume") || 20;
  587. $("#volume-slider").slider("setValue", volume);
  588. if (slider.length === 0) {
  589. Meteor.setTimeout(function() {
  590. makeSlider();
  591. }, 500);
  592. } else {
  593. slider.on("slide", function(val) {
  594. localStorage.setItem("volume", val.value);
  595. if (yt_player !== undefined) {
  596. yt_player.setVolume(val.value);
  597. } else if (_sound !== undefined) {
  598. var volume = val.value / 100;
  599. _sound.setVolume(volume);
  600. }
  601. });
  602. }
  603. }
  604. makeSlider();
  605. });
  606. });
  607. Template.playlist.helpers({
  608. playlist_songs: function() {
  609. parts = location.href.split('/');
  610. id = parts.pop();
  611. type = id.toLowerCase();
  612. var data = Playlists.find({type: type}).fetch();
  613. if (data !== undefined && data.length > 0) {
  614. data[0].songs.map(function(song) {
  615. if (song.title === Session.get("title")) {
  616. song.current = true;
  617. } else {
  618. song.current = false;
  619. }
  620. return song;
  621. });
  622. return data[0].songs;
  623. } else {
  624. return [];
  625. }
  626. }
  627. });
  628. Meteor.subscribe("rooms");
  629. Template.room.onCreated(function () {
  630. if (resizeSeekerbarInterval !== undefined) {
  631. Meteor.clearInterval(resizeSeekerbarInterval);
  632. resizeSeekerbarInterval = undefined;
  633. }
  634. yt_player = undefined;
  635. _sound = undefined;
  636. Session.set("videoHidden", false)
  637. var tag = document.createElement("script");
  638. tag.src = "https://www.youtube.com/iframe_api";
  639. var firstScriptTag = document.getElementsByTagName('script')[0];
  640. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag)
  641. var currentSong = undefined;
  642. var nextSong = undefined;
  643. var afterSong = undefined;
  644. var size = 0;
  645. function getTimeElapsed() {
  646. if (currentSong !== undefined) {
  647. var rooms = Rooms.find({type: type}).fetch();
  648. if (rooms && rooms.length === 1) {
  649. var room = rooms[0];
  650. return Date.now() - currentSong.started + room.timePaused;
  651. }
  652. }
  653. return 0;
  654. }
  655. function getSongInfo(songData){
  656. Session.set("title", songData.title);
  657. Session.set("artist", songData.artist);
  658. $("#song-img").attr("src", songData.img);
  659. Session.set("duration", songData.duration);
  660. }
  661. function resizeSeekerbar() {
  662. if (Session.get("state") === "playing") {
  663. $("#seeker-bar").width(((getTimeElapsed() / 1000) / Session.get("duration") * 100) + "%");
  664. }
  665. }
  666. function startSong() {
  667. if (currentSong !== undefined) {
  668. if (_sound !== undefined) _sound.stop();
  669. if (yt_player !== undefined && yt_player.stopVideo !== undefined) yt_player.stopVideo();
  670. var volume = localStorage.getItem("volume") || 20;
  671. $("#media-container").empty();
  672. yt_player = undefined;
  673. if (currentSong.type === "SoundCloud") {
  674. $("#media-container").append('<img src="/soundcloud-image.png" class="embed-responsive-item" />');
  675. getSongInfo(currentSong);
  676. SC.stream("/tracks/" + currentSong.id, function(sound){
  677. _sound = sound;
  678. sound.setVolume(volume / 100);
  679. sound.play();
  680. var interval = setInterval(function() {
  681. if (sound.getState() === "playing") {
  682. sound.seek(getTimeElapsed());
  683. window.clearInterval(interval);
  684. }
  685. }, 200);
  686. // Session.set("title", currentSong.title || "Title");
  687. // Session.set("artist", currentSong.artist || "Artist");
  688. Session.set("duration", currentSong.duration);
  689. resizeSeekerbar();
  690. });
  691. } else {
  692. $("#media-container").append('<div id="player" class="embed-responsive-item"></div>');
  693. if (yt_player === undefined) {
  694. yt_player = new YT.Player("player", {
  695. height: 540,
  696. width: 960,
  697. videoId: currentSong.id,
  698. playerVars: {controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0},
  699. events: {
  700. 'onReady': function(event) {
  701. event.target.seekTo(getTimeElapsed() / 1000);
  702. event.target.playVideo();
  703. event.target.setVolume(volume);
  704. resizeSeekerbar();
  705. },
  706. 'onStateChange': function(event){
  707. if (event.data == YT.PlayerState.PAUSED && Session.get("state") === "playing") {
  708. event.target.seekTo(getTimeElapsed() / 1000);
  709. event.target.playVideo();
  710. }
  711. if (event.data == YT.PlayerState.PLAYING && Session.get("state") === "paused") {
  712. event.target.seekTo(getTimeElapsed() / 1000);
  713. event.target.pauseVideo();
  714. }
  715. }
  716. }
  717. });
  718. } else {
  719. yt_player.loadVideoById(currentSong.id);
  720. }
  721. // Session.set("title", currentSong.title || "Title");
  722. // Session.set("artist", currentSong.artist || "Artist");
  723. getSongInfo(currentSong);
  724. //Session.set("duration", currentSong.duration);
  725. }
  726. }
  727. }
  728. Meteor.subscribe("history");
  729. Session.set("loaded", false);
  730. Meteor.subscribe("rooms", function() {
  731. var parts = location.href.split('/');
  732. var id = parts.pop();
  733. var type = id.toLowerCase();
  734. if (Rooms.find({type: type}).count() !== 1) {
  735. window.location = "/";
  736. } else {
  737. Session.set("loaded", true);
  738. minterval = Meteor.setInterval(function () {
  739. var rooms = Rooms.find({type: type}).fetch();
  740. if (rooms && rooms.length === 1) {
  741. var room = rooms[0];
  742. if (room.state === "paused") {
  743. Session.set("state", "paused");
  744. if (yt_player !== undefined && yt_player.getPlayerState() === 1) {
  745. yt_player.pauseVideo();
  746. } else if (_sound !== undefined && _sound.getState().indexOf("playing") !== -1) {
  747. _sound.pause();
  748. }
  749. } else {
  750. Session.set("state", "playing");
  751. if (yt_player !== undefined && yt_player.getPlayerState() !== 1) {
  752. yt_player.playVideo();
  753. } else if (_sound !== undefined && _sound.getState().indexOf("paused") !== -1) {
  754. _sound.play();
  755. }
  756. }
  757. }
  758. var data = undefined;
  759. var dataCursorH = History.find({type: type});
  760. var dataCursorP = Playlists.find({type: type});
  761. dataCursorH.forEach(function (doc) {
  762. if (data === undefined) {
  763. data = doc;
  764. }
  765. });
  766. if (data !== undefined && data.history.length > size) {
  767. //currentSong = data.history[data.history.length - 1];
  768. var songArray = Playlists.find({type: type}).fetch()[0].songs;
  769. var historyObj = data.history[data.history.length - 1];
  770. songArray.forEach(function(song) {
  771. if (song.id === historyObj.song.id) {
  772. currentSong = song;
  773. }
  774. });
  775. currentSong.started = historyObj.started;
  776. var songs = dataCursorP.fetch()[0].songs;
  777. songs.forEach(function(song, index) {
  778. if (currentSong.title === song.title) {
  779. if (index + 1 < songs.length) {
  780. nextSong = songs[index + 1];
  781. } else {
  782. nextSong = songs[0];
  783. }
  784. Session.set("title_next", nextSong.title);
  785. Session.set("artist_next", nextSong.artist);
  786. $("#song-img-next").attr("src", nextSong.img);
  787. if (index + 2 < songs.length) {
  788. afterSong = songs[index + 2];
  789. } else if (songs.length === index + 1 && songs.length > 1 ) {
  790. afterSong = songs[1];
  791. } else {
  792. afterSong = songs[0];
  793. }
  794. Session.set("title_after", afterSong.title);
  795. Session.set("artist_after", afterSong.artist);
  796. $("#song-img-after").attr("src",afterSong.img);
  797. }
  798. });
  799. size = data.history.length;
  800. startSong();
  801. }
  802. if (data !== undefined) {
  803. }
  804. }, 1000);
  805. resizeSeekerbarInterval = Meteor.setInterval(function () {
  806. resizeSeekerbar();
  807. }, 500);
  808. }
  809. });
  810. });
  811. }
  812. if (Meteor.isServer) {
  813. Meteor.startup(function() {
  814. reCAPTCHA.config({
  815. privatekey: '6LcVxg0TAAAAAI2fgIEEWHFxwNXeVIs8mzq5cfRM'
  816. });
  817. var stations = ["edm", "pop"]; //Rooms to be set on server startup
  818. for(var i in stations){
  819. if(Rooms.find({type: stations[i]}).count() === 0){
  820. createRoom(stations[i]);
  821. }
  822. }
  823. });
  824. var stations = [];
  825. function getStation(type, cb) {
  826. stations.forEach(function(station) {
  827. if (station.type === type) {
  828. cb(station);
  829. return;
  830. }
  831. });
  832. }
  833. function createRoom(type) {
  834. if (Rooms.find({type: type}).count() === 0) {
  835. Rooms.insert({type: type}, function(err) {
  836. if (err) {
  837. throw err;
  838. } else {
  839. if (Playlists.find({type: type}).count() === 1) {
  840. if (History.find({type: type}).count() === 0) {
  841. History.insert({type: type, history: []}, function(err3) {
  842. if (err3) {
  843. throw err3;
  844. } else {
  845. stations.push(new Station(type));
  846. return true;
  847. }
  848. });
  849. } else {
  850. stations.push(new Station(type));
  851. return true;
  852. }
  853. } else {
  854. Playlists.insert({type: type, songs: getSongsByType(type)}, function (err2) {
  855. if (err2) {
  856. throw err2;
  857. } else {
  858. if (History.find({type: type}).count() === 0) {
  859. History.insert({type: type, history: []}, function(err3) {
  860. if (err3) {
  861. throw err3;
  862. } else {
  863. stations.push(new Station(type));
  864. return true;
  865. }
  866. });
  867. } else {
  868. stations.push(new Station(type));
  869. return true;
  870. }
  871. }
  872. });
  873. }
  874. }
  875. });
  876. } else {
  877. throw "Room already exists";
  878. }
  879. }
  880. function Station(type) {
  881. var startedAt = Date.now();
  882. var playlist = Playlists.find({type: type}).fetch()[0];
  883. var songs = playlist.songs;
  884. if (playlist.lastSong === undefined) {
  885. Playlists.update({type: type}, {$set: {lastSong: 0}});
  886. playlist = Playlists.find({type: type}).fetch()[0];
  887. songs = playlist.songs;
  888. }
  889. var currentSong = playlist.lastSong;
  890. var currentTitle = songs[currentSong].title;
  891. addToHistory(songs[currentSong], startedAt);
  892. function addToHistory(song, startedAt) {
  893. History.update({type: type}, {$push: {history: {song: song, started: startedAt}}});
  894. }
  895. function skipSong() {
  896. songs = Playlists.find({type: type}).fetch()[0].songs;
  897. songs.forEach(function(song, index) {
  898. if (song.title === currentTitle) {
  899. currentSong = index;
  900. }
  901. });
  902. if (currentSong < (songs.length - 1)) {
  903. currentSong++;
  904. } else currentSong = 0;
  905. if (songs);
  906. if (currentSong === 0) {
  907. Playlists.update({type: type}, {$set: {"songs": []}});
  908. songs = shuffle(songs);
  909. songs.forEach(function(song) {
  910. Playlists.update({type: type}, {$push: {"songs": song}});
  911. });
  912. }
  913. currentTitle = songs[currentSong].title;
  914. Playlists.update({type: type}, {$set: {lastSong: currentSong}});
  915. Rooms.update({type: type}, {$set: {timePaused: 0}});
  916. songTimer();
  917. addToHistory(songs[currentSong], startedAt);
  918. }
  919. Rooms.update({type: type}, {$set: {timePaused: 0}});
  920. var timer;
  921. function songTimer() {
  922. startedAt = Date.now();
  923. timer = new Timer(function() {
  924. skipSong();
  925. }, songs[currentSong].duration * 1000);
  926. }
  927. var state = Rooms.find({type: type}).fetch()[0].state;
  928. this.pauseRoom = function() {
  929. if (state !== "paused") {
  930. timer.pause();
  931. Rooms.update({type: type}, {$set: {state: "paused"}});
  932. state = "paused";
  933. }
  934. };
  935. this.resumeRoom = function() {
  936. if (state !== "playing") {
  937. timer.resume();
  938. Rooms.update({type: type}, {$set: {state: "playing", timePaused: timer.timeWhenPaused()}});
  939. state = "playing";
  940. }
  941. };
  942. this.getState = function() {
  943. return state;
  944. };
  945. this.type = type;
  946. songTimer();
  947. }
  948. function shuffle(array) {
  949. var currentIndex = array.length, temporaryValue, randomIndex ;
  950. // While there remain elements to shuffle...
  951. while (0 !== currentIndex) {
  952. // Pick a remaining element...
  953. randomIndex = Math.floor(Math.random() * currentIndex);
  954. currentIndex -= 1;
  955. // And swap it with the current element.
  956. temporaryValue = array[currentIndex];
  957. array[currentIndex] = array[randomIndex];
  958. array[randomIndex] = temporaryValue;
  959. }
  960. return array;
  961. }
  962. function Timer(callback, delay) {
  963. var timerId, start, remaining = delay;
  964. var timeWhenPaused = 0;
  965. var timePaused = new Date();
  966. this.pause = function() {
  967. Meteor.clearTimeout(timerId);
  968. remaining -= new Date() - start;
  969. timePaused = new Date();
  970. };
  971. this.resume = function() {
  972. start = new Date();
  973. Meteor.clearTimeout(timerId);
  974. timerId = Meteor.setTimeout(callback, remaining);
  975. timeWhenPaused += new Date() - timePaused;
  976. };
  977. this.timeWhenPaused = function() {
  978. return timeWhenPaused;
  979. };
  980. this.resume();
  981. }
  982. Meteor.users.deny({update: function () { return true; }});
  983. Meteor.users.deny({insert: function () { return true; }});
  984. Meteor.users.deny({remove: function () { return true; }});
  985. function getSongDuration(query, artistName){
  986. var duration;
  987. var search = query;
  988. query = query.toLowerCase().split(" ").join("%20");
  989. var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + query + '&type=track');
  990. for(var i in res.data){
  991. for(var j in res.data[i].items){
  992. if(search.indexOf(res.data[i].items[j].name) !== -1 && artistName.indexOf(res.data[i].items[j].artists[0].name) !== -1){
  993. duration = res.data[i].items[j].duration_ms / 1000;
  994. return duration;
  995. }
  996. }
  997. }
  998. }
  999. function getSongAlbumArt(query, artistName){
  1000. var albumart;
  1001. var search = query;
  1002. query = query.toLowerCase().split(" ").join("%20");
  1003. var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + query + '&type=track');
  1004. for(var i in res.data){
  1005. for(var j in res.data[i].items){
  1006. if(search.indexOf(res.data[i].items[j].name) !== -1 && artistName.indexOf(res.data[i].items[j].artists[0].name) !== -1){
  1007. albumart = res.data[i].items[j].album.images[1].url
  1008. return albumart;
  1009. }
  1010. }
  1011. }
  1012. }
  1013. //var room_types = ["edm", "nightcore"];
  1014. var songsArr = [];
  1015. function getSongsByType(type) {
  1016. if (type === "edm") {
  1017. return [
  1018. {id: "aE2GCa-_nyU", 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"},
  1019. {id: "aHjpOzsQ9YI", title: "Crystallize", artist: "Lindsey Stirling", duration: getSongDuration("Crystallize", "Lindsey Stirling"), type: "YouTube", img: "https://i.scdn.co/image/b0c1ccdd0cd7bcda741ccc1c3e036f4ed2e52312"}
  1020. ];
  1021. } else if (type === "nightcore") {
  1022. return [{id: "f7RKOP87tt4", title: "Monster (DotEXE Remix)", duration: getSongDuration("Monster (DotEXE Remix)", "Meg & Dia"), artist: "Meg & Dia", type: "YouTube", img: "https://i.scdn.co/image/35ecdfba9c31a6c54ee4c73dcf1ad474c560cd00"}];
  1023. } else {
  1024. return [{id: "dQw4w9WgXcQ", 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"}];
  1025. }
  1026. }
  1027. Rooms.find({}).fetch().forEach(function(room) {
  1028. var type = room.type;
  1029. if (Playlists.find({type: type}).count() === 0) {
  1030. if (type === "edm") {
  1031. Playlists.insert({type: type, songs: getSongsByType(type)});
  1032. } else if (type === "nightcore") {
  1033. Playlists.insert({type: type, songs: getSongsByType(type)});
  1034. } else {
  1035. Playlists.insert({type: type, songs: getSongsByType(type)});
  1036. }
  1037. }
  1038. if (History.find({type: type}).count() === 0) {
  1039. History.insert({type: type, history: []});
  1040. }
  1041. if (Playlists.find({type: type}).fetch()[0].songs.length === 0) {
  1042. // Add a global video to Playlist so it can proceed
  1043. } else {
  1044. stations.push(new Station(type));
  1045. }
  1046. });
  1047. Accounts.onCreateUser(function(options, user) {
  1048. var username;
  1049. if (user.services) {
  1050. if (user.services.github) {
  1051. username = user.services.github.username;
  1052. } else if (user.services.facebook) {
  1053. username = user.services.facebook.first_name;
  1054. } else if (user.services.password) {
  1055. username = user.username;
  1056. }
  1057. }
  1058. user.profile = {username: username, usernameL: username.toLowerCase(), rank: "default"};
  1059. return user;
  1060. });
  1061. ServiceConfiguration.configurations.remove({
  1062. service: "facebook"
  1063. });
  1064. ServiceConfiguration.configurations.insert({
  1065. service: "facebook",
  1066. appId: "1496014310695890",
  1067. secret: "9a039f254a08a1488c08bb0737dbd2a6"
  1068. });
  1069. ServiceConfiguration.configurations.remove({
  1070. service: "github"
  1071. });
  1072. ServiceConfiguration.configurations.insert({
  1073. service: "github",
  1074. clientId: "dcecd720f47c0e4001f7",
  1075. secret: "375939d001ef1a0ca67c11dbf8fb9aeaa551e01b"
  1076. });
  1077. Meteor.publish("history", function() {
  1078. return History.find({})
  1079. });
  1080. Meteor.publish("playlists", function() {
  1081. return Playlists.find({})
  1082. });
  1083. Meteor.publish("rooms", function() {
  1084. return Rooms.find({});
  1085. });
  1086. Meteor.publish("queues", function() {
  1087. return Queues.find({});
  1088. });
  1089. Meteor.publish("chat", function() {
  1090. return Chat.find({});
  1091. });
  1092. Meteor.publish("userProfiles", function() {
  1093. //console.log(Meteor.users.find({}, {profile: 1, createdAt: 1, services: 0, username: 0, emails: 0})).fetch();
  1094. return Meteor.users.find({}, {fields: {profile: 1, createdAt: 1}});
  1095. });
  1096. Meteor.publish("isAdmin", function() {
  1097. return Meteor.users.find({_id: this.userId, "profile.rank": "admin"});
  1098. });
  1099. Meteor.methods({
  1100. pauseRoom: function(type) {
  1101. var userData = Meteor.users.find(Meteor.userId());
  1102. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
  1103. getStation(type, function(station) {
  1104. if (station === undefined) {
  1105. throw new Meteor.error(403, "Room doesn't exist.");
  1106. } else {
  1107. station.pauseRoom();
  1108. }
  1109. });
  1110. } else {
  1111. throw new Meteor.error(403, "Invalid permissions.");
  1112. }
  1113. },
  1114. resumeRoom: function(type) {
  1115. var userData = Meteor.users.find(Meteor.userId());
  1116. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
  1117. getStation(type, function(station) {
  1118. if (station === undefined) {
  1119. throw new Meteor.error(403, "Room doesn't exist.");
  1120. } else {
  1121. station.resumeRoom();
  1122. }
  1123. });
  1124. } else {
  1125. throw new Meteor.error(403, "Invalid permissions.");
  1126. }
  1127. },
  1128. createUserMethod: function(formData, captchaData) {
  1129. var verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(this.connection.clientAddress, captchaData);
  1130. if (!verifyCaptchaResponse.success) {
  1131. console.log('reCAPTCHA check failed!', verifyCaptchaResponse);
  1132. throw new Meteor.Error(422, 'reCAPTCHA Failed: ' + verifyCaptchaResponse.error);
  1133. } else {
  1134. console.log('reCAPTCHA verification passed!');
  1135. Accounts.createUser({
  1136. username: formData.username,
  1137. email: formData.email,
  1138. password: formData.password
  1139. });
  1140. }
  1141. return true;
  1142. },
  1143. addSongToQueue: function(type, songData) {
  1144. if (Meteor.userId()) {
  1145. type = type.toLowerCase();
  1146. if (Rooms.find({type: type}).count() === 1) {
  1147. if (Queues.find({type: type}).count() === 0) {
  1148. Queues.insert({type: type, songs: []});
  1149. }
  1150. if (songData !== undefined && Object.keys(songData).length === 5 && songData.type !== undefined && songData.title !== undefined && songData.title !== undefined && songData.artist !== undefined && songData.img !== undefined) {
  1151. songData.duration = getSongDuration(songData.title, songData.artist);
  1152. songData.img = getSongAlbumArt(songData.title, songData.artist);
  1153. Queues.update({type: type}, {
  1154. $push: {
  1155. songs: {
  1156. id: songData.id,
  1157. title: songData.title,
  1158. artist: songData.artist,
  1159. duration: songData.duration,
  1160. img: songData.img,
  1161. type: songData.type
  1162. }
  1163. }
  1164. });
  1165. return true;
  1166. } else {
  1167. throw new Meteor.error(403, "Invalid data.");
  1168. }
  1169. } else {
  1170. throw new Meteor.error(403, "Invalid genre.");
  1171. }
  1172. } else {
  1173. throw new Meteor.error(403, "Invalid permissions.");
  1174. }
  1175. },
  1176. updateQueueSong: function(genre, oldSong, newSong) {
  1177. var userData = Meteor.users.find(Meteor.userId());
  1178. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
  1179. newSong.id = oldSong.id;
  1180. Queues.update({type: genre, "songs": oldSong}, {$set: {"songs.$": newSong}});
  1181. return true;
  1182. } else {
  1183. throw new Meteor.error(403, "Invalid permissions.");
  1184. }
  1185. },
  1186. updatePlaylistSong: function(genre, oldSong, newSong) {
  1187. var userData = Meteor.users.find(Meteor.userId());
  1188. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
  1189. newSong.id = oldSong.id;
  1190. Playlists.update({type: genre, "songs": oldSong}, {$set: {"songs.$": newSong}});
  1191. return true;
  1192. } else {
  1193. throw new Meteor.error(403, "Invalid permissions.");
  1194. }
  1195. },
  1196. removeSongFromQueue: function(type, songId) {
  1197. var userData = Meteor.users.find(Meteor.userId());
  1198. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
  1199. type = type.toLowerCase();
  1200. Queues.update({type: type}, {$pull: {songs: {id: songId}}});
  1201. } else {
  1202. throw new Meteor.error(403, "Invalid permissions.");
  1203. }
  1204. },
  1205. removeSongFromPlaylist: function(type, songId) {
  1206. var userData = Meteor.users.find(Meteor.userId());
  1207. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
  1208. type = type.toLowerCase();
  1209. Playlists.update({type: type}, {$pull: {songs: {id: songId}}});
  1210. } else {
  1211. throw new Meteor.error(403, "Invalid permissions.");
  1212. }
  1213. },
  1214. addSongToPlaylist: function(type, songData) {
  1215. var userData = Meteor.users.find(Meteor.userId());
  1216. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
  1217. type = type.toLowerCase();
  1218. if (Rooms.find({type: type}).count() === 1) {
  1219. if (Playlists.find({type: type}).count() === 0) {
  1220. Playlists.insert({type: type, songs: []});
  1221. }
  1222. if (songData !== undefined && Object.keys(songData).length === 6 && songData.type !== undefined && songData.title !== undefined && songData.title !== undefined && songData.artist !== undefined && songData.duration !== undefined && songData.img !== undefined) {
  1223. Playlists.update({type: type}, {
  1224. $push: {
  1225. songs: {
  1226. id: songData.id,
  1227. title: songData.title,
  1228. artist: songData.artist,
  1229. duration: songData.duration,
  1230. img: songData.img,
  1231. type: songData.type
  1232. }
  1233. }
  1234. });
  1235. Queues.update({type: type}, {$pull: {songs: {id: songData.id}}});
  1236. return true;
  1237. } else {
  1238. throw new Meteor.error(403, "Invalid data.");
  1239. }
  1240. } else {
  1241. throw new Meteor.error(403, "Invalid genre.");
  1242. }
  1243. } else {
  1244. throw new Meteor.error(403, "Invalid permissions.");
  1245. }
  1246. },
  1247. createRoom: function(type) {
  1248. var userData = Meteor.users.find(Meteor.userId());
  1249. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
  1250. createRoom(type);
  1251. } else {
  1252. return false;
  1253. }
  1254. }
  1255. });
  1256. }
  1257. /*Router.waitOn(function() {
  1258. Meteor.subscribe("isAdmin", Meteor.userId());
  1259. });*/
  1260. /*Router.onBeforeAction(function() {
  1261. /*Meteor.autorun(function () {
  1262. if (admin.ready()) {
  1263. this.next();
  1264. }
  1265. });*/
  1266. /*this.next();
  1267. });*/
  1268. Router.route("/", {
  1269. template: "home"
  1270. });
  1271. Router.route("/login", {
  1272. template: "login"
  1273. })
  1274. Router.route("/signup", {
  1275. template: "register"
  1276. })
  1277. Router.route("/terms", {
  1278. template: "terms"
  1279. });
  1280. Router.route("/privacy", {
  1281. template: "privacy"
  1282. });
  1283. Router.route("/about", {
  1284. template: "about"
  1285. });
  1286. Router.route("/admin", {
  1287. waitOn: function() {
  1288. return Meteor.subscribe("isAdmin", Meteor.userId());
  1289. },
  1290. action: function() {
  1291. var user = Meteor.users.find({}).fetch();
  1292. if (user[0] !== undefined && user[0].profile !== undefined && user[0].profile.rank === "admin") {
  1293. this.render("admin");
  1294. } else {
  1295. this.redirect("/");
  1296. }
  1297. }
  1298. });
  1299. Router.route("/vis", {
  1300. template: "visualizer"
  1301. });
  1302. Router.route("/:type", {
  1303. template: "room"
  1304. });
  1305. Router.route("/u/:user", {
  1306. template: "profile"
  1307. });