app.js 30 KB

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