app.js 48 KB

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