app.js 46 KB

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