app.js 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  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. if (Meteor.isClient) {
  7. Meteor.startup(function() {
  8. reCAPTCHA.config({
  9. publickey: '6LcVxg0TAAAAAE18vBiH00UAyaJggsmLm890SjZl'
  10. });
  11. });
  12. Meteor.subscribe("queues");
  13. Meteor.subscribe("playlists");
  14. var minterval;
  15. var hpSound = undefined;
  16. var songsArr = [];
  17. var ytArr = [];
  18. var _sound = undefined;
  19. var parts = location.href.split('/');
  20. var id = parts.pop();
  21. var type = id.toLowerCase();
  22. var resizeSeekerbarInterval;
  23. function getSpotifyInfo(title, cb, artist) {
  24. var q = "";
  25. q = title;
  26. if (artist !== undefined) {
  27. q += " artist:" + artist;
  28. }
  29. $.ajax({
  30. type: "GET",
  31. url: 'https://api.spotify.com/v1/search?q=' + encodeURIComponent(q) + '&type=track',
  32. applicationType: "application/json",
  33. contentType: "json",
  34. success: function (data) {
  35. cb(data);
  36. }
  37. });
  38. }
  39. Template.profile.helpers({
  40. "username": function() {
  41. return Session.get("username");
  42. },
  43. "first_joined": function() {
  44. return moment(Session.get("first_joined")).format("DD/MM/YYYY HH:mm:ss");
  45. },
  46. "rank": function() {
  47. return Session.get("rank");
  48. },
  49. loaded: function() {
  50. return Session.get("loaded");
  51. }
  52. });
  53. Template.profile.onCreated(function() {
  54. var parts = location.href.split('/');
  55. var username = parts.pop();
  56. Session.set("loaded", false);
  57. Meteor.subscribe("userProfiles", function() {
  58. if (Meteor.users.find({"profile.usernameL": username.toLowerCase()}).count() === 0) {
  59. window.location = "/";
  60. } else {
  61. var data = Meteor.users.find({"profile.usernameL": username.toLowerCase()}).fetch()[0];
  62. Session.set("username", data.profile.username);
  63. Session.set("first_joined", data.createdAt);
  64. Session.set("rank", data.profile.rank);
  65. Session.set("loaded", true);
  66. }
  67. });
  68. });
  69. 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}}};
  70. Handlebars.registerHelper('active', function(path) {
  71. return curPath() == path ? 'active' : '';
  72. });
  73. Template.header.helpers({
  74. currentUser: function() {
  75. return Meteor.user();
  76. },
  77. isAdmin: function() {
  78. if (Meteor.user() && Meteor.user().profile) {
  79. return Meteor.user().profile.rank === "admin";
  80. } else {
  81. return false;
  82. }
  83. }
  84. });
  85. Template.header.events({
  86. "click .logout": function(e){
  87. e.preventDefault();
  88. Meteor.logout();
  89. if (hpSound !== undefined) {
  90. hpSound.stop();
  91. }
  92. }
  93. });
  94. Template.register.events({
  95. "submit form": function(e){
  96. e.preventDefault();
  97. var username = e.target.registerUsername.value;
  98. var email = e.target.registerEmail.value;
  99. var password = e.target.registerPassword.value;
  100. var captchaData = grecaptcha.getResponse();
  101. Meteor.call("createUserMethod", {username: username, email: email, password: password}, captchaData, function(err, res) {
  102. grecaptcha.reset();
  103. if (err) {
  104. console.log(err);
  105. var errAlert = $('<div class="alert alert-danger" role="alert"><strong>Oh Snap!</strong> ' + err.reason + '</div>');
  106. $("#login").after(errAlert);
  107. errAlert.fadeOut(20000, function() {
  108. errAlert.remove();
  109. });
  110. } else {
  111. Meteor.loginWithPassword(username, password);
  112. Accounts.onLogin(function(){
  113. window.location.href = "/";
  114. })
  115. }
  116. });
  117. },
  118. "click #github-login": function(){
  119. Meteor.loginWithGithub()
  120. Accounts.onLogin(function(){
  121. window.location.href = "/"
  122. });
  123. }
  124. });
  125. Template.login.events({
  126. "submit form": function(e){
  127. e.preventDefault();
  128. var username = e.target.loginUsername.value;
  129. var password = e.target.loginPassword.value;
  130. Meteor.loginWithPassword(username, password);
  131. Accounts.onLogin(function(){
  132. window.location.href = "/";
  133. })
  134. Accounts.onLoginFailure(function(){
  135. $("input").css("background-color","indianred");
  136. $("input").on("click",function(){
  137. $("input").css({
  138. "-webkit-appearance": "none",
  139. " -moz-appearance": "none",
  140. " appearance": "none",
  141. "outline": "0",
  142. "border": "1px solid rgba(255, 255, 255, 0.4)",
  143. "background-color": "rgba(255, 255, 255, 0.2)",
  144. "width": "304px",
  145. "border-radius": "3px",
  146. "padding": "10px 15px",
  147. "margin": "0 auto 10px auto",
  148. "display": "block",
  149. "text-align": "center",
  150. "font-size": "18px",
  151. "color": "white",
  152. "-webkit-transition-duration": "0.25s",
  153. " transition-duration": "0.25s",
  154. "font-weight": "300"
  155. });
  156. $("input:focus").css({
  157. "width": "354px",
  158. "color": "white"
  159. })
  160. $("input").on("blur", function(){
  161. $(this).css("width", "304px");
  162. })
  163. })
  164. });
  165. },
  166. "click #github-login": function(){
  167. Meteor.loginWithGithub()
  168. Accounts.onLogin(function(){
  169. window.location.href = "/"
  170. });
  171. }
  172. });
  173. Template.dashboard.helpers({
  174. rooms: function() {
  175. return Rooms.find({});
  176. },
  177. currentSong: function() {
  178. var history = History.find({type: this.type}).fetch();
  179. if (history.length < 1) {
  180. return {};
  181. } else {
  182. history = history[0];
  183. return history.history[history.history.length - 1];
  184. }
  185. }
  186. });
  187. Template.dashboard.onCreated(function() {
  188. if (_sound !== undefined) _sound.stop();
  189. if (minterval !== undefined) {
  190. Meteor.clearInterval(minterval);
  191. }
  192. if (resizeSeekerbarInterval !== undefined) {
  193. Meteor.clearInterval(resizeSeekerbarInterval);
  194. resizeSeekerbarInterval = undefined;
  195. }
  196. Meteor.subscribe("history");
  197. });
  198. Template.room.events({
  199. "click #add-song-button": function(e){
  200. e.preventDefault();
  201. parts = location.href.split('/');
  202. id = parts.pop();
  203. var genre = id.toLowerCase();
  204. var type = $("#type").val();
  205. id = $("#id").val();
  206. var title = $("#title").val();
  207. var artist = $("#artist").val();
  208. var img = $("#img").val();
  209. var songData = {type: type, id: id, title: title, artist: artist, img: img};
  210. Meteor.call("addSongToQueue", genre, songData, function(err, res) {
  211. console.log(err, res);
  212. });
  213. $("#close-modal").click();
  214. },
  215. "click #toggle-video": function(e){
  216. e.preventDefault();
  217. if (Session.get("mediaHidden")) {
  218. $("#media-container").removeClass("hidden");
  219. $("#toggle-video").text("Hide video");
  220. Session.set("mediaHidden", false);
  221. } else {
  222. $("#media-container").addClass("hidden");
  223. $("#toggle-video").text("Show video");
  224. Session.set("mediaHidden", true);
  225. }
  226. },
  227. "click #return": function(e){
  228. $("#add-info").hide();
  229. $("#search-info").show();
  230. },
  231. "click #search-song": function(){
  232. $("#song-results").empty();
  233. var search_type = $("#search_type").val();
  234. if (search_type === "YouTube") {
  235. $.ajax({
  236. type: "GET",
  237. url: "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" + $("#song-input").val() + "&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY",
  238. applicationType: "application/json",
  239. contentType: "json",
  240. success: function(data){
  241. for(var i in data.items){
  242. $("#song-results").append("<p>" + data.items[i].snippet.title + "</p>");
  243. ytArr.push({title: data.items[i].snippet.title, id: data.items[i].id.videoId});
  244. }
  245. $("#song-results p").click(function(){
  246. $("#search-info").hide();
  247. $("#add-info").show();
  248. var title = $(this).text();
  249. for(var i in ytArr){
  250. if(ytArr[i].title === title){
  251. var songObj = {
  252. id: ytArr[i].id,
  253. title: ytArr[i].title,
  254. type: "youtube"
  255. };
  256. $("#title").val(songObj.title);
  257. $("#artist").val("");
  258. $("#id").val(songObj.id);
  259. $("#type").val("YouTube");
  260. getSpotifyInfo(songObj.title.replace(/\[.*\]/g, ""), function(data) {
  261. if (data.tracks.items.length > 0) {
  262. $("#title").val(data.tracks.items[0].name);
  263. var artists = [];
  264. $("#img").val(data.tracks.items[0].album.images[1].url);
  265. data.tracks.items[0].artists.forEach(function(artist) {
  266. artists.push(artist.name);
  267. });
  268. $("#artist").val(artists.join(", "));
  269. }
  270. });
  271. }
  272. }
  273. })
  274. }
  275. })
  276. } else if (search_type === "SoundCloud") {
  277. SC.get('/tracks', { q: $("#song-input").val()}, function(tracks) {
  278. for(var i in tracks){
  279. $("#song-results").append("<p>" + tracks[i].title + "</p>")
  280. songsArr.push({title: tracks[i].title, id: tracks[i].id, duration: tracks[i].duration / 1000});
  281. }
  282. $("#song-results p").click(function(){
  283. $("#search-info").hide();
  284. $("#add-info").show();
  285. var title = $(this).text();
  286. for(var i in songsArr){
  287. if(songsArr[i].title === title){
  288. var id = songsArr[i].id;
  289. var duration = songsArr[i].duration;
  290. var songObj = {
  291. title: songsArr[i].title,
  292. id: id,
  293. duration: duration,
  294. type: "soundcloud"
  295. }
  296. $("#title").val(songObj.title);
  297. // Set ID field
  298. $("#id").val(songObj.id);
  299. $("#type").val("SoundCloud");
  300. getSpotifyInfo(songObj.title.replace(/\[.*\]/g, ""), function(data) {
  301. if (data.tracks.items.length > 0) {
  302. $("#title").val(data.tracks.items[0].name);
  303. var artists = [];
  304. data.tracks.items[0].artists.forEach(function(artist) {
  305. artists.push(artist.name);
  306. });
  307. $("#artist").val(artists.join(", "));
  308. }
  309. // Set title field again if possible
  310. // Set artist if possible
  311. });
  312. }
  313. }
  314. })
  315. });
  316. }
  317. },
  318. "click #add-songs": function(){
  319. $("#add-songs-modal").show();
  320. },
  321. "click #close-modal": function(){
  322. $("#search-info").show();
  323. $("#add-info").hide();
  324. },
  325. "click #volume-icon": function(){
  326. var volume = 0;
  327. var slider = $("#volume-slider").slider();
  328. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  329. if (yt_player !== undefined) {
  330. yt_player.setVolume(volume);
  331. localStorage.setItem("volume", volume);
  332. $("#volume-slider").slider("setValue", volume);
  333. } else if (_sound !== undefined) {
  334. _sound.setVolume(volume);
  335. localStorage.setItem("volume", volume);
  336. $("#volume-slider").slider("setValue", volume);
  337. }
  338. },
  339. "click #play": function() {
  340. Meteor.call("resumeRoom", type);
  341. },
  342. "click #pause": function() {
  343. Meteor.call("pauseRoom", type);
  344. }
  345. });
  346. Template.room.onRendered(function() {
  347. $(document).ready(function() {
  348. function makeSlider(){
  349. var slider = $("#volume-slider").slider();
  350. var volume = localStorage.getItem("volume") || 20;
  351. $("#volume-slider").slider("setValue", volume);
  352. if (slider.length === 0) {
  353. Meteor.setTimeout(function() {
  354. makeSlider();
  355. }, 500);
  356. } else {
  357. slider.on("slide", function(val) {
  358. if (val.value === 0) {
  359. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  360. } else {
  361. $("#volume-icon").removeClass("fa-volume-off").addClass("fa-volume-down")
  362. }
  363. if (yt_player !== undefined) {
  364. yt_player.setVolume(val.value);
  365. localStorage.setItem("volume", val.value);
  366. } else if (_sound !== undefined) {
  367. //_sound
  368. var volume = val.value / 100;
  369. _sound.setVolume(volume);
  370. localStorage.setItem("volume", val.value);
  371. }
  372. });
  373. }
  374. }
  375. makeSlider();
  376. });
  377. });
  378. Template.room.helpers({
  379. type: function() {
  380. var parts = location.href.split('/');
  381. var id = parts.pop();
  382. return id.toUpperCase();
  383. },
  384. title: function(){
  385. return Session.get("title");
  386. },
  387. artist: function(){
  388. return Session.get("artist");
  389. },
  390. title_next: function(){
  391. return Session.get("title_next");
  392. },
  393. artist_next: function(){
  394. return Session.get("artist_next");
  395. },
  396. title_after: function(){
  397. return Session.get("title_after");
  398. },
  399. artist_after: function(){
  400. return Session.get("artist_after");
  401. },
  402. loaded: function() {
  403. return Session.get("loaded");
  404. },
  405. isAdmin: function() {
  406. if (Meteor.user() && Meteor.user().profile) {
  407. return Meteor.user().profile.rank === "admin";
  408. } else {
  409. return false;
  410. }
  411. },
  412. paused: function() {
  413. return Session.get("state") === "paused";
  414. }
  415. });
  416. Template.admin.helpers({
  417. queues: function() {
  418. return Queues.find({});
  419. },
  420. playlists: function() {
  421. return Playlists.find({});
  422. }
  423. });
  424. var yt_player = undefined;
  425. var _sound = undefined;
  426. Template.admin.events({
  427. "click .preview-button": function(e){
  428. Session.set("song", this);
  429. },
  430. "click .edit-queue-button": function(e){
  431. Session.set("song", this);
  432. Session.set("genre", $(e.toElement).data("genre"));
  433. Session.set("type", "queue");
  434. $("#type").val(this.type);
  435. $("#artist").val(this.artist);
  436. $("#title").val(this.title);
  437. $("#img").val(this.img);
  438. $("#id").val(this.id);
  439. $("#duration").val(this.duration);
  440. },
  441. "click .edit-playlist-button": function(e){
  442. Session.set("song", this);
  443. Session.set("genre", $(e.toElement).data("genre"));
  444. Session.set("type", "playlist");
  445. $("#type").val(this.type);
  446. $("#artist").val(this.artist);
  447. $("#title").val(this.title);
  448. $("#img").val(this.img);
  449. $("#id").val(this.id);
  450. $("#duration").val(this.duration);
  451. },
  452. "click .add-song-button": function(e){
  453. var genre = $(e.toElement).data("genre") || $(e.toElement).parent().data("genre");
  454. Meteor.call("addSongToPlaylist", genre, this);
  455. },
  456. "click .deny-song-button": function(e){
  457. var genre = $(e.toElement).data("genre") || $(e.toElement).parent().data("genre");
  458. Meteor.call("removeSongFromQueue", genre, this.id);
  459. },
  460. "click .remove-song-button": function(e){
  461. var genre = $(e.toElement).data("genre") || $(e.toElement).parent().data("genre");
  462. Meteor.call("removeSongFromPlaylist", genre, this.id);
  463. },
  464. "click #play": function() {
  465. $("#play").attr("disabled", true);
  466. $("#stop").attr("disabled", false);
  467. var song = Session.get("song");
  468. var id = song.id;
  469. var type = song.type;
  470. var volume = localStorage.getItem("volume") || 20;
  471. if (type === "YouTube") {
  472. if (yt_player === undefined) {
  473. yt_player = new YT.Player("previewPlayer", {
  474. height: 540,
  475. width: 568,
  476. videoId: id,
  477. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0},
  478. events: {
  479. 'onReady': function(event) {
  480. event.target.playVideo();
  481. event.target.setVolume(volume);
  482. },
  483. 'onStateChange': function(event){
  484. if (event.data == YT.PlayerState.PAUSED) {
  485. event.target.playVideo();
  486. }
  487. if (event.data == YT.PlayerState.PLAYING) {
  488. $("#play").attr("disabled", true);
  489. $("#stop").attr("disabled", false);
  490. } else {
  491. $("#play").attr("disabled", false);
  492. $("#stop").attr("disabled", true);
  493. }
  494. }
  495. }
  496. });
  497. } else {
  498. yt_player.loadVideoById(id);
  499. }
  500. $("#previewPlayer").show();
  501. } else if (type === "SoundCloud") {
  502. SC.stream("/tracks/" + song.id, function(sound) {
  503. _sound = sound;
  504. sound.setVolume(volume / 100);
  505. sound.play();
  506. });
  507. }
  508. },
  509. "click #stop": function() {
  510. $("#play").attr("disabled", false);
  511. $("#stop").attr("disabled", true);
  512. if (yt_player !== undefined) {
  513. yt_player.stopVideo();
  514. }
  515. if (_sound !== undefined) {
  516. _sound.stop();
  517. }
  518. },
  519. "click #croom_create": function() {
  520. Meteor.call("createRoom", $("#croom").val(), function (err, res) {
  521. if (err) {
  522. alert("Error " + err.error + ": " + err.reason);
  523. } else {
  524. window.location = "/" + $("#croom").val();
  525. }
  526. });
  527. },
  528. "click #get-spotify-info": function() {
  529. var search = $("#title").val();
  530. var artistName = $("#artist").val();
  531. getSpotifyInfo(search, function(data) {
  532. for(var i in data){
  533. for(var j in data[i].items){
  534. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  535. $("#img").val(data[i].items[j].album.images[1].url);
  536. $("#duration").val(data[i].items[j].duration_ms / 1000);
  537. return;
  538. }
  539. }
  540. }
  541. }, artistName);
  542. },
  543. "click #save-song-button": function() {
  544. var newSong = {};
  545. newSong.title = $("#title").val();
  546. newSong.artist = $("#artist").val();
  547. newSong.img = $("#img").val();
  548. newSong.type = $("#type").val();
  549. newSong.duration = $("#duration").val();
  550. if (Session.get("type") === "playlist") {
  551. Meteor.call("updatePlaylistSong", Session.get("genre"), Session.get("song"), newSong, function() {
  552. $('#editModal').modal('hide');
  553. });
  554. } else {
  555. Meteor.call("updateQueueSong", Session.get("genre"), Session.get("song"), newSong, function() {
  556. $('#editModal').modal('hide');
  557. });
  558. }
  559. }
  560. });
  561. Template.admin.onCreated(function() {
  562. var tag = document.createElement("script");
  563. tag.src = "https://www.youtube.com/iframe_api";
  564. var firstScriptTag = document.getElementsByTagName('script')[0];
  565. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  566. });
  567. Template.admin.onRendered(function() {
  568. $("#previewModal").on("hidden.bs.modal", function() {
  569. if (yt_player !== undefined) {
  570. $("#play").attr("disabled", false);
  571. $("#stop").attr("disabled", true);
  572. $("#previewPlayer").hide();
  573. yt_player.loadVideoById("", 0);
  574. yt_player.seekTo(0);
  575. yt_player.stopVideo();
  576. }
  577. if (_sound !== undefined) {
  578. _sound.stop();
  579. $("#play").attr("disabled", false);
  580. $("#stop").attr("disabled", true);
  581. }
  582. });
  583. $(document).ready(function() {
  584. function makeSlider(){
  585. var slider = $("#volume-slider").slider();
  586. var volume = localStorage.getItem("volume") || 20;
  587. $("#volume-slider").slider("setValue", volume);
  588. if (slider.length === 0) {
  589. Meteor.setTimeout(function() {
  590. makeSlider();
  591. }, 500);
  592. } else {
  593. slider.on("slide", function(val) {
  594. localStorage.setItem("volume", val.value);
  595. if (yt_player !== undefined) {
  596. yt_player.setVolume(val.value);
  597. } else if (_sound !== undefined) {
  598. var volume = val.value / 100;
  599. _sound.setVolume(volume);
  600. }
  601. });
  602. }
  603. }
  604. makeSlider();
  605. });
  606. });
  607. Template.playlist.helpers({
  608. playlist_songs: function() {
  609. parts = location.href.split('/');
  610. id = parts.pop();
  611. type = id.toLowerCase();
  612. var data = Playlists.find({type: type}).fetch();
  613. if (data !== undefined && data.length > 0) {
  614. data[0].songs.map(function(song) {
  615. if (song.title === Session.get("title")) {
  616. song.current = true;
  617. } else {
  618. song.current = false;
  619. }
  620. return song;
  621. });
  622. return data[0].songs;
  623. } else {
  624. return [];
  625. }
  626. }
  627. });
  628. Meteor.subscribe("rooms");
  629. Template.room.onCreated(function () {
  630. if (resizeSeekerbarInterval !== undefined) {
  631. Meteor.clearInterval(resizeSeekerbarInterval);
  632. resizeSeekerbarInterval = undefined;
  633. }
  634. yt_player = undefined;
  635. _sound = undefined;
  636. Session.set("videoHidden", false)
  637. var tag = document.createElement("script");
  638. tag.src = "https://www.youtube.com/iframe_api";
  639. var firstScriptTag = document.getElementsByTagName('script')[0];
  640. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag)
  641. var currentSong = undefined;
  642. var nextSong = undefined;
  643. var afterSong = undefined;
  644. var size = 0;
  645. function getTimeElapsed() {
  646. if (currentSong !== undefined) {
  647. var rooms = Rooms.find({type: type}).fetch();
  648. if (rooms && rooms.length === 1) {
  649. var room = rooms[0];
  650. return Date.now() - currentSong.started + room.timePaused;
  651. }
  652. }
  653. return 0;
  654. }
  655. function getSongInfo(songData){
  656. Session.set("title", songData.title);
  657. Session.set("artist", songData.artist);
  658. $("#song-img").attr("src", songData.img);
  659. Session.set("duration", songData.duration);
  660. }
  661. function resizeSeekerbar() {
  662. if (Session.get("state") === "playing") {
  663. $("#seeker-bar").width(((getTimeElapsed() / 1000) / Session.get("duration") * 100) + "%");
  664. }
  665. }
  666. function startSong() {
  667. if (currentSong !== undefined) {
  668. if (_sound !== undefined) _sound.stop();
  669. if (yt_player !== undefined && yt_player.stopVideo !== undefined) yt_player.stopVideo();
  670. var volume = localStorage.getItem("volume") || 20;
  671. $("#media-container").empty();
  672. yt_player = undefined;
  673. if (currentSong.type === "SoundCloud") {
  674. $("#media-container").append('<img src="/soundcloud-image.png" class="embed-responsive-item" />');
  675. getSongInfo(currentSong);
  676. SC.stream("/tracks/" + currentSong.id, function(sound){
  677. _sound = sound;
  678. sound.setVolume(volume / 100);
  679. sound.play();
  680. var interval = setInterval(function() {
  681. if (sound.getState() === "playing") {
  682. sound.seek(getTimeElapsed());
  683. window.clearInterval(interval);
  684. }
  685. }, 200);
  686. // Session.set("title", currentSong.title || "Title");
  687. // Session.set("artist", currentSong.artist || "Artist");
  688. Session.set("duration", currentSong.duration);
  689. resizeSeekerbar();
  690. });
  691. } else {
  692. $("#media-container").append('<div id="player" class="embed-responsive-item"></div>');
  693. if (yt_player === undefined) {
  694. yt_player = new YT.Player("player", {
  695. height: 540,
  696. width: 960,
  697. videoId: currentSong.id,
  698. playerVars: {controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0},
  699. events: {
  700. 'onReady': function(event) {
  701. event.target.seekTo(getTimeElapsed() / 1000);
  702. event.target.playVideo();
  703. event.target.setVolume(volume);
  704. resizeSeekerbar();
  705. },
  706. 'onStateChange': function(event){
  707. if (event.data == YT.PlayerState.PAUSED && Session.get("state") === "playing") {
  708. event.target.seekTo(getTimeElapsed() / 1000);
  709. event.target.playVideo();
  710. }
  711. if (event.data == YT.PlayerState.PLAYING && Session.get("state") === "paused") {
  712. event.target.seekTo(getTimeElapsed() / 1000);
  713. event.target.pauseVideo();
  714. }
  715. }
  716. }
  717. });
  718. } else {
  719. yt_player.loadVideoById(currentSong.id);
  720. }
  721. // Session.set("title", currentSong.title || "Title");
  722. // Session.set("artist", currentSong.artist || "Artist");
  723. getSongInfo(currentSong);
  724. //Session.set("duration", currentSong.duration);
  725. }
  726. }
  727. }
  728. Meteor.subscribe("history");
  729. Session.set("loaded", false);
  730. Meteor.subscribe("rooms", function() {
  731. var parts = location.href.split('/');
  732. var id = parts.pop();
  733. var type = id.toLowerCase();
  734. if (Rooms.find({type: type}).count() !== 1) {
  735. window.location = "/";
  736. } else {
  737. Session.set("loaded", true);
  738. minterval = Meteor.setInterval(function () {
  739. var rooms = Rooms.find({type: type}).fetch();
  740. if (rooms && rooms.length === 1) {
  741. var room = rooms[0];
  742. if (room.state === "paused") {
  743. Session.set("state", "paused");
  744. if (yt_player !== undefined && yt_player.getPlayerState() === 1) {
  745. yt_player.pauseVideo();
  746. } else if (_sound !== undefined && _sound.getState().indexOf("playing") !== -1) {
  747. _sound.pause();
  748. }
  749. } else {
  750. Session.set("state", "playing");
  751. if (yt_player !== undefined && yt_player.getPlayerState() !== 1) {
  752. yt_player.playVideo();
  753. _sound.play();
  754. }
  755. }
  756. }
  757. var data = undefined;
  758. var dataCursorH = History.find({type: type});
  759. var dataCursorP = Playlists.find({type: type});
  760. dataCursorH.forEach(function (doc) {
  761. if (data === undefined) {
  762. data = doc;
  763. }
  764. });
  765. if (data !== undefined && data.history.length > size) {
  766. //currentSong = data.history[data.history.length - 1];
  767. var songArray = Playlists.find({type: type}).fetch()[0].songs;
  768. var historyObj = data.history[data.history.length - 1];
  769. songArray.forEach(function(song) {
  770. if (song.id === historyObj.song.id) {
  771. currentSong = song;
  772. }
  773. });
  774. currentSong.started = historyObj.started;
  775. var songs = dataCursorP.fetch()[0].songs;
  776. songs.forEach(function(song, index) {
  777. if (currentSong.title === song.title) {
  778. if (index + 1 < songs.length) {
  779. nextSong = songs[index + 1];
  780. } else {
  781. nextSong = songs[0];
  782. }
  783. Session.set("title_next", nextSong.title);
  784. Session.set("artist_next", nextSong.artist);
  785. $("#song-img-next").attr("src", nextSong.img);
  786. if (index + 2 < songs.length) {
  787. afterSong = songs[index + 2];
  788. } else if (songs.length === index + 1 && songs.length > 1 ) {
  789. afterSong = songs[1];
  790. } else {
  791. afterSong = songs[0];
  792. }
  793. Session.set("title_after", afterSong.title);
  794. Session.set("artist_after", afterSong.artist);
  795. $("#song-img-after").attr("src",afterSong.img);
  796. }
  797. });
  798. size = data.history.length;
  799. startSong();
  800. }
  801. if (data !== undefined) {
  802. }
  803. }, 1000);
  804. resizeSeekerbarInterval = Meteor.setInterval(function () {
  805. resizeSeekerbar();
  806. }, 500);
  807. }
  808. });
  809. });
  810. }
  811. if (Meteor.isServer) {
  812. Meteor.startup(function() {
  813. reCAPTCHA.config({
  814. privatekey: '6LcVxg0TAAAAAI2fgIEEWHFxwNXeVIs8mzq5cfRM'
  815. });
  816. var stations = ["edm", "pop"]; //Rooms to be set on server startup
  817. for(var i in stations){
  818. if(Rooms.find({type: stations[i]}).count() === 0){
  819. createRoom(stations[i]);
  820. }
  821. }
  822. });
  823. var stations = [];
  824. function getStation(type, cb) {
  825. stations.forEach(function(station) {
  826. if (station.type === type) {
  827. cb(station);
  828. return;
  829. }
  830. });
  831. }
  832. function createRoom(type) {
  833. if (Rooms.find({type: type}).count() === 0) {
  834. Rooms.insert({type: type}, function(err) {
  835. if (err) {
  836. throw err;
  837. } else {
  838. if (Playlists.find({type: type}).count() === 1) {
  839. if (History.find({type: type}).count() === 0) {
  840. History.insert({type: type, history: []}, function(err3) {
  841. if (err3) {
  842. throw err3;
  843. } else {
  844. stations.push(new Station(type));
  845. return true;
  846. }
  847. });
  848. } else {
  849. stations.push(new Station(type));
  850. return true;
  851. }
  852. } else {
  853. Playlists.insert({type: type, songs: getSongsByType(type)}, function (err2) {
  854. if (err2) {
  855. throw err2;
  856. } else {
  857. if (History.find({type: type}).count() === 0) {
  858. History.insert({type: type, history: []}, function(err3) {
  859. if (err3) {
  860. throw err3;
  861. } else {
  862. stations.push(new Station(type));
  863. return true;
  864. }
  865. });
  866. } else {
  867. stations.push(new Station(type));
  868. return true;
  869. }
  870. }
  871. });
  872. }
  873. }
  874. });
  875. } else {
  876. throw "Room already exists";
  877. }
  878. }
  879. function Station(type) {
  880. var startedAt = Date.now();
  881. var playlist = Playlists.find({type: type}).fetch()[0];
  882. var songs = playlist.songs;
  883. if (playlist.lastSong === undefined) {
  884. Playlists.update({type: type}, {$set: {lastSong: 0}});
  885. playlist = Playlists.find({type: type}).fetch()[0];
  886. songs = playlist.songs;
  887. }
  888. var currentSong = playlist.lastSong;
  889. var currentTitle = songs[currentSong].title;
  890. addToHistory(songs[currentSong], startedAt);
  891. function addToHistory(song, startedAt) {
  892. History.update({type: type}, {$push: {history: {song: song, started: startedAt}}});
  893. }
  894. function skipSong() {
  895. songs = Playlists.find({type: type}).fetch()[0].songs;
  896. songs.forEach(function(song, index) {
  897. if (song.title === currentTitle) {
  898. currentSong = index;
  899. }
  900. });
  901. if (currentSong < (songs.length - 1)) {
  902. currentSong++;
  903. } else currentSong = 0;
  904. if (songs);
  905. if (currentSong === 0) {
  906. Playlists.update({type: type}, {$set: {"songs": []}});
  907. songs = shuffle(songs);
  908. songs.forEach(function(song) {
  909. Playlists.update({type: type}, {$push: {"songs": song}});
  910. });
  911. }
  912. currentTitle = songs[currentSong].title;
  913. Playlists.update({type: type}, {$set: {lastSong: currentSong}});
  914. Rooms.update({type: type}, {$set: {timePaused: 0}});
  915. songTimer();
  916. addToHistory(songs[currentSong], startedAt);
  917. }
  918. Rooms.update({type: type}, {$set: {timePaused: 0}});
  919. var timer;
  920. function songTimer() {
  921. startedAt = Date.now();
  922. timer = new Timer(function() {
  923. skipSong();
  924. }, songs[currentSong].duration * 1000);
  925. }
  926. var state = Rooms.find({type: type}).fetch()[0].state;
  927. this.pauseRoom = function() {
  928. if (state !== "paused") {
  929. timer.pause();
  930. Rooms.update({type: type}, {$set: {state: "paused"}});
  931. state = "paused";
  932. }
  933. };
  934. this.resumeRoom = function() {
  935. if (state !== "playing") {
  936. timer.resume();
  937. Rooms.update({type: type}, {$set: {state: "playing", timePaused: timer.timeWhenPaused()}});
  938. state = "playing";
  939. }
  940. };
  941. this.getState = function() {
  942. return state;
  943. };
  944. this.type = type;
  945. songTimer();
  946. }
  947. function shuffle(array) {
  948. var currentIndex = array.length, temporaryValue, randomIndex ;
  949. // While there remain elements to shuffle...
  950. while (0 !== currentIndex) {
  951. // Pick a remaining element...
  952. randomIndex = Math.floor(Math.random() * currentIndex);
  953. currentIndex -= 1;
  954. // And swap it with the current element.
  955. temporaryValue = array[currentIndex];
  956. array[currentIndex] = array[randomIndex];
  957. array[randomIndex] = temporaryValue;
  958. }
  959. return array;
  960. }
  961. function Timer(callback, delay) {
  962. var timerId, start, remaining = delay;
  963. var timeWhenPaused = 0;
  964. var timePaused = new Date();
  965. this.pause = function() {
  966. Meteor.clearTimeout(timerId);
  967. remaining -= new Date() - start;
  968. timePaused = new Date();
  969. };
  970. this.resume = function() {
  971. start = new Date();
  972. Meteor.clearTimeout(timerId);
  973. timerId = Meteor.setTimeout(callback, remaining);
  974. timeWhenPaused += new Date() - timePaused;
  975. };
  976. this.timeWhenPaused = function() {
  977. return timeWhenPaused;
  978. };
  979. this.resume();
  980. }
  981. Meteor.users.deny({update: function () { return true; }});
  982. Meteor.users.deny({insert: function () { return true; }});
  983. Meteor.users.deny({remove: function () { return true; }});
  984. function getSongDuration(query, artistName){
  985. var duration;
  986. var search = query;
  987. query = query.toLowerCase().split(" ").join("%20");
  988. var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + query + '&type=track');
  989. for(var i in res.data){
  990. for(var j in res.data[i].items){
  991. if(search.indexOf(res.data[i].items[j].name) !== -1 && artistName.indexOf(res.data[i].items[j].artists[0].name) !== -1){
  992. duration = res.data[i].items[j].duration_ms / 1000;
  993. return duration;
  994. }
  995. }
  996. }
  997. }
  998. function getSongAlbumArt(query, artistName){
  999. var albumart;
  1000. var search = query;
  1001. query = query.toLowerCase().split(" ").join("%20");
  1002. var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + query + '&type=track');
  1003. for(var i in res.data){
  1004. for(var j in res.data[i].items){
  1005. if(search.indexOf(res.data[i].items[j].name) !== -1 && artistName.indexOf(res.data[i].items[j].artists[0].name) !== -1){
  1006. albumart = res.data[i].items[j].album.images[1].url
  1007. return albumart;
  1008. }
  1009. }
  1010. }
  1011. }
  1012. //var room_types = ["edm", "nightcore"];
  1013. var songsArr = [];
  1014. function getSongsByType(type) {
  1015. if (type === "edm") {
  1016. return [
  1017. {id: "aE2GCa-_nyU", title: "Radioactive", duration: getSongDuration("Radioactive - Lindsey Stirling and Pentatonix", "Lindsey Stirling, Pentatonix"), artist: "Lindsey Stirling, Pentatonix", type: "YouTube", img: "https://i.scdn.co/image/62167a9007cef2e8ef13ab1d93019312b9b03655"},
  1018. {id: "aHjpOzsQ9YI", title: "Crystallize", artist: "Lindsey Stirling", duration: getSongDuration("Crystallize", "Lindsey Stirling"), type: "YouTube", img: "https://i.scdn.co/image/b0c1ccdd0cd7bcda741ccc1c3e036f4ed2e52312"}
  1019. ];
  1020. } else if (type === "nightcore") {
  1021. return [{id: "f7RKOP87tt4", title: "Monster (DotEXE Remix)", duration: getSongDuration("Monster (DotEXE Remix)", "Meg & Dia"), artist: "Meg & Dia", type: "YouTube", img: "https://i.scdn.co/image/35ecdfba9c31a6c54ee4c73dcf1ad474c560cd00"}];
  1022. } else {
  1023. return [{id: "dQw4w9WgXcQ", title: "Never Gonna Give You Up", duration: getSongDuration("Never Gonna Give You Up", "Rick Astley"), artist: "Rick Astley", type: "YouTube", img: "https://i.scdn.co/image/5246898e19195715e65e261899baba890a2c1ded"}];
  1024. }
  1025. }
  1026. Rooms.find({}).fetch().forEach(function(room) {
  1027. var type = room.type;
  1028. if (Playlists.find({type: type}).count() === 0) {
  1029. if (type === "edm") {
  1030. Playlists.insert({type: type, songs: getSongsByType(type)});
  1031. } else if (type === "nightcore") {
  1032. Playlists.insert({type: type, songs: getSongsByType(type)});
  1033. } else {
  1034. Playlists.insert({type: type, songs: getSongsByType(type)});
  1035. }
  1036. }
  1037. if (History.find({type: type}).count() === 0) {
  1038. History.insert({type: type, history: []});
  1039. }
  1040. if (Playlists.find({type: type}).fetch()[0].songs.length === 0) {
  1041. // Add a global video to Playlist so it can proceed
  1042. } else {
  1043. stations.push(new Station(type));
  1044. }
  1045. });
  1046. Accounts.onCreateUser(function(options, user) {
  1047. var username;
  1048. if (user.services) {
  1049. if (user.services.github) {
  1050. username = user.services.github.username;
  1051. } else if (user.services.facebook) {
  1052. username = user.services.facebook.first_name;
  1053. } else if (user.services.password) {
  1054. username = user.username;
  1055. }
  1056. }
  1057. user.profile = {username: username, usernameL: username.toLowerCase(), rank: "default"};
  1058. return user;
  1059. });
  1060. ServiceConfiguration.configurations.remove({
  1061. service: "facebook"
  1062. });
  1063. ServiceConfiguration.configurations.insert({
  1064. service: "facebook",
  1065. appId: "1496014310695890",
  1066. secret: "9a039f254a08a1488c08bb0737dbd2a6"
  1067. });
  1068. ServiceConfiguration.configurations.remove({
  1069. service: "github"
  1070. });
  1071. ServiceConfiguration.configurations.insert({
  1072. service: "github",
  1073. clientId: "dcecd720f47c0e4001f7",
  1074. secret: "375939d001ef1a0ca67c11dbf8fb9aeaa551e01b"
  1075. });
  1076. Meteor.publish("history", function() {
  1077. return History.find({})
  1078. });
  1079. Meteor.publish("playlists", function() {
  1080. return Playlists.find({})
  1081. });
  1082. Meteor.publish("rooms", function() {
  1083. return Rooms.find({});
  1084. });
  1085. Meteor.publish("queues", function() {
  1086. return Queues.find({});
  1087. });
  1088. Meteor.publish("chat", function() {
  1089. return Chat.find({});
  1090. });
  1091. Meteor.publish("userProfiles", function() {
  1092. //console.log(Meteor.users.find({}, {profile: 1, createdAt: 1, services: 0, username: 0, emails: 0})).fetch();
  1093. return Meteor.users.find({}, {fields: {profile: 1, createdAt: 1}});
  1094. });
  1095. Meteor.publish("isAdmin", function() {
  1096. return Meteor.users.find({_id: this.userId, "profile.rank": "admin"});
  1097. });
  1098. Meteor.methods({
  1099. pauseRoom: function(type) {
  1100. var userData = Meteor.users.find(Meteor.userId());
  1101. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
  1102. getStation(type, function(station) {
  1103. if (station === undefined) {
  1104. throw new Meteor.error(403, "Room doesn't exist.");
  1105. } else {
  1106. station.pauseRoom();
  1107. }
  1108. });
  1109. } else {
  1110. throw new Meteor.error(403, "Invalid permissions.");
  1111. }
  1112. },
  1113. resumeRoom: function(type) {
  1114. var userData = Meteor.users.find(Meteor.userId());
  1115. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
  1116. getStation(type, function(station) {
  1117. if (station === undefined) {
  1118. throw new Meteor.error(403, "Room doesn't exist.");
  1119. } else {
  1120. station.resumeRoom();
  1121. }
  1122. });
  1123. } else {
  1124. throw new Meteor.error(403, "Invalid permissions.");
  1125. }
  1126. },
  1127. createUserMethod: function(formData, captchaData) {
  1128. var verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(this.connection.clientAddress, captchaData);
  1129. if (!verifyCaptchaResponse.success) {
  1130. console.log('reCAPTCHA check failed!', verifyCaptchaResponse);
  1131. throw new Meteor.Error(422, 'reCAPTCHA Failed: ' + verifyCaptchaResponse.error);
  1132. } else {
  1133. console.log('reCAPTCHA verification passed!');
  1134. Accounts.createUser({
  1135. username: formData.username,
  1136. email: formData.email,
  1137. password: formData.password
  1138. });
  1139. }
  1140. return true;
  1141. },
  1142. addSongToQueue: function(type, songData) {
  1143. if (Meteor.userId()) {
  1144. type = type.toLowerCase();
  1145. if (Rooms.find({type: type}).count() === 1) {
  1146. if (Queues.find({type: type}).count() === 0) {
  1147. Queues.insert({type: type, songs: []});
  1148. }
  1149. if (songData !== undefined && Object.keys(songData).length === 5 && songData.type !== undefined && songData.title !== undefined && songData.title !== undefined && songData.artist !== undefined && songData.img !== undefined) {
  1150. songData.duration = getSongDuration(songData.title, songData.artist);
  1151. songData.img = getSongAlbumArt(songData.title, songData.artist);
  1152. Queues.update({type: type}, {
  1153. $push: {
  1154. songs: {
  1155. id: songData.id,
  1156. title: songData.title,
  1157. artist: songData.artist,
  1158. duration: songData.duration,
  1159. img: songData.img,
  1160. type: songData.type
  1161. }
  1162. }
  1163. });
  1164. return true;
  1165. } else {
  1166. throw new Meteor.error(403, "Invalid data.");
  1167. }
  1168. } else {
  1169. throw new Meteor.error(403, "Invalid genre.");
  1170. }
  1171. } else {
  1172. throw new Meteor.error(403, "Invalid permissions.");
  1173. }
  1174. },
  1175. updateQueueSong: function(genre, oldSong, newSong) {
  1176. var userData = Meteor.users.find(Meteor.userId());
  1177. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
  1178. newSong.id = oldSong.id;
  1179. Queues.update({type: genre, "songs": oldSong}, {$set: {"songs.$": newSong}});
  1180. return true;
  1181. } else {
  1182. throw new Meteor.error(403, "Invalid permissions.");
  1183. }
  1184. },
  1185. updatePlaylistSong: function(genre, oldSong, newSong) {
  1186. var userData = Meteor.users.find(Meteor.userId());
  1187. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
  1188. newSong.id = oldSong.id;
  1189. Playlists.update({type: genre, "songs": oldSong}, {$set: {"songs.$": newSong}});
  1190. return true;
  1191. } else {
  1192. throw new Meteor.error(403, "Invalid permissions.");
  1193. }
  1194. },
  1195. removeSongFromQueue: function(type, songId) {
  1196. var userData = Meteor.users.find(Meteor.userId());
  1197. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
  1198. type = type.toLowerCase();
  1199. Queues.update({type: type}, {$pull: {songs: {id: songId}}});
  1200. } else {
  1201. throw new Meteor.error(403, "Invalid permissions.");
  1202. }
  1203. },
  1204. removeSongFromPlaylist: function(type, songId) {
  1205. var userData = Meteor.users.find(Meteor.userId());
  1206. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
  1207. type = type.toLowerCase();
  1208. Playlists.update({type: type}, {$pull: {songs: {id: songId}}});
  1209. } else {
  1210. throw new Meteor.error(403, "Invalid permissions.");
  1211. }
  1212. },
  1213. addSongToPlaylist: function(type, songData) {
  1214. var userData = Meteor.users.find(Meteor.userId());
  1215. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
  1216. type = type.toLowerCase();
  1217. if (Rooms.find({type: type}).count() === 1) {
  1218. if (Playlists.find({type: type}).count() === 0) {
  1219. Playlists.insert({type: type, songs: []});
  1220. }
  1221. 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) {
  1222. Playlists.update({type: type}, {
  1223. $push: {
  1224. songs: {
  1225. id: songData.id,
  1226. title: songData.title,
  1227. artist: songData.artist,
  1228. duration: songData.duration,
  1229. img: songData.img,
  1230. type: songData.type
  1231. }
  1232. }
  1233. });
  1234. Queues.update({type: type}, {$pull: {songs: {id: songData.id}}});
  1235. return true;
  1236. } else {
  1237. throw new Meteor.error(403, "Invalid data.");
  1238. }
  1239. } else {
  1240. throw new Meteor.error(403, "Invalid genre.");
  1241. }
  1242. } else {
  1243. throw new Meteor.error(403, "Invalid permissions.");
  1244. }
  1245. },
  1246. createRoom: function(type) {
  1247. var userData = Meteor.users.find(Meteor.userId());
  1248. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
  1249. createRoom(type);
  1250. } else {
  1251. return false;
  1252. }
  1253. }
  1254. });
  1255. }
  1256. /*Router.waitOn(function() {
  1257. Meteor.subscribe("isAdmin", Meteor.userId());
  1258. });*/
  1259. /*Router.onBeforeAction(function() {
  1260. /*Meteor.autorun(function () {
  1261. if (admin.ready()) {
  1262. this.next();
  1263. }
  1264. });*/
  1265. /*this.next();
  1266. });*/
  1267. Router.route("/", {
  1268. template: "home"
  1269. });
  1270. Router.route("/login", {
  1271. template: "login"
  1272. })
  1273. Router.route("/signup", {
  1274. template: "register"
  1275. })
  1276. Router.route("/terms", {
  1277. template: "terms"
  1278. });
  1279. Router.route("/privacy", {
  1280. template: "privacy"
  1281. });
  1282. Router.route("/about", {
  1283. template: "about"
  1284. });
  1285. Router.route("/admin", {
  1286. waitOn: function() {
  1287. return Meteor.subscribe("isAdmin", Meteor.userId());
  1288. },
  1289. action: function() {
  1290. var user = Meteor.users.find({}).fetch();
  1291. if (user[0] !== undefined && user[0].profile !== undefined && user[0].profile.rank === "admin") {
  1292. this.render("admin");
  1293. } else {
  1294. this.redirect("/");
  1295. }
  1296. }
  1297. });
  1298. Router.route("/vis", {
  1299. template: "visualizer"
  1300. });
  1301. Router.route("/:type", {
  1302. template: "room"
  1303. });
  1304. Router.route("/u/:user", {
  1305. template: "profile"
  1306. });