app.js 54 KB

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