app.js 56 KB

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