2
0

app.js 47 KB

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