app.js 68 KB

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