app.js 47 KB

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