app.js 42 KB

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