app.js 39 KB

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