app.js 39 KB

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