app.js 53 KB

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