2
0

app.js 31 KB

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