app.js 64 KB

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