app.js 38 KB

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