app.js 39 KB

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