app.js 34 KB

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