app.js 58 KB

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