app.js 54 KB

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