app.js 56 KB

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