2
0

app.js 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622
  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. $("input").css("background-color","indianred");
  135. $("input").on("click",function(){
  136. $("input").css({
  137. "-webkit-appearance": "none",
  138. " -moz-appearance": "none",
  139. " appearance": "none",
  140. "outline": "0",
  141. "border": "1px solid rgba(255, 255, 255, 0.4)",
  142. "background-color": "rgba(255, 255, 255, 0.2)",
  143. "width": "304px",
  144. "border-radius": "3px",
  145. "padding": "10px 15px",
  146. "margin": "0 auto 10px auto",
  147. "display": "block",
  148. "text-align": "center",
  149. "font-size": "18px",
  150. "color": "white",
  151. "-webkit-transition-duration": "0.25s",
  152. " transition-duration": "0.25s",
  153. "font-weight": "300"
  154. });
  155. $("input:focus").css({
  156. "width": "354px",
  157. "color": "white"
  158. })
  159. $("input").on("blur", function(){
  160. $(this).css("width", "304px");
  161. })
  162. })
  163. });
  164. },
  165. "click #github-login": function(){
  166. Meteor.loginWithGithub()
  167. Accounts.onLogin(function(){
  168. window.location.href = "/"
  169. });
  170. }
  171. });
  172. Template.dashboard.helpers({
  173. rooms: function() {
  174. return Rooms.find({});
  175. },
  176. currentSong: function() {
  177. var type = this.type;
  178. var room = Rooms.findOne({type: type});
  179. if (room !== undefined) {
  180. return room.currentSong;
  181. } else {
  182. return {};
  183. }
  184. }
  185. });
  186. Template.dashboard.onCreated(function() {
  187. if (_sound !== undefined) _sound.stop();
  188. if (minterval !== undefined) {
  189. Meteor.clearInterval(minterval);
  190. }
  191. if (resizeSeekerbarInterval !== undefined) {
  192. Meteor.clearInterval(resizeSeekerbarInterval);
  193. resizeSeekerbarInterval = undefined;
  194. }
  195. 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. songDuration: function() {
  504. var duration = Session.get("duration");
  505. var d = moment.duration(duration, 'seconds');
  506. return d.minutes() + ":" + ("0" + d.seconds()).slice(-2);
  507. },
  508. type: function() {
  509. var parts = location.href.split('/');
  510. var id = parts.pop().toLowerCase();
  511. return Rooms.findOne({type: id}).display;
  512. },
  513. title: function(){
  514. return Session.get("title");
  515. },
  516. artist: function(){
  517. return Session.get("artist");
  518. },
  519. loaded: function() {
  520. return Session.get("loaded");
  521. },
  522. isAdmin: function() {
  523. if (Meteor.user() && Meteor.user().profile) {
  524. return Meteor.user().profile.rank === "admin";
  525. } else {
  526. return false;
  527. }
  528. },
  529. paused: function() {
  530. return Session.get("state") === "paused";
  531. },
  532. report: function() {
  533. return Session.get("reportObj");
  534. },
  535. reportSong: function() {
  536. return Session.get("reportSong");
  537. },
  538. reportTitle: function() {
  539. return Session.get("reportTitle");
  540. },
  541. reportAuthor: function() {
  542. return Session.get("reportAuthor");
  543. },
  544. reportDuration: function() {
  545. return Session.get("reportDuration");
  546. },
  547. reportAudio: function() {
  548. return Session.get("reportAudio");
  549. },
  550. reportAlbumart: function() {
  551. return Session.get("reportAlbumart");
  552. },
  553. reportOther: function() {
  554. return Session.get("reportOther");
  555. },
  556. currentSong: function() {
  557. return Session.get("currentSong");
  558. },
  559. previousSong: function() {
  560. return Session.get("previousSong");
  561. },
  562. currentSongR: function() {
  563. return Session.get("currentSongR");
  564. },
  565. previousSongR: function() {
  566. return Session.get("previousSongR");
  567. },
  568. reportingSong: function() {
  569. if (Session.get("reportPrevious")) {
  570. return Session.get("previousSongR");
  571. } else {
  572. return Session.get("currentSongR");
  573. }
  574. }
  575. });
  576. Template.admin.helpers({
  577. queues: function() {
  578. var queues = Queues.find({}).fetch();
  579. queues.map(function(queue) {
  580. if (Rooms.find({type: queue.type}).count() !== 1) {
  581. return;
  582. } else {
  583. queue.display = Rooms.findOne({type: queue.type}).display;
  584. return queue;
  585. }
  586. });
  587. return queues;
  588. },
  589. playlists: function() {
  590. var playlists = Playlists.find({}).fetch();
  591. playlists.map(function(playlist) {
  592. if (Rooms.find({type: playlist.type}).count() !== 1) {
  593. return;
  594. } else {
  595. playlist.display = Rooms.findOne({type: playlist.type}).display;
  596. return playlist;
  597. }
  598. });
  599. return playlists;
  600. }
  601. });
  602. var yt_player = undefined;
  603. var _sound = undefined;
  604. Template.admin.events({
  605. "click .preview-button": function(e){
  606. Session.set("song", this);
  607. },
  608. "click .edit-queue-button": function(e){
  609. Session.set("song", this);
  610. Session.set("genre", $(e.toElement).data("genre"));
  611. Session.set("type", "queue");
  612. $("#type").val(this.type);
  613. $("#artist").val(this.artist);
  614. $("#title").val(this.title);
  615. $("#img").val(this.img);
  616. $("#id").val(this.id);
  617. $("#duration").val(this.duration);
  618. },
  619. "click .edit-playlist-button": function(e){
  620. Session.set("song", this);
  621. Session.set("genre", $(e.toElement).data("genre"));
  622. Session.set("type", "playlist");
  623. $("#type").val(this.type);
  624. $("#artist").val(this.artist);
  625. $("#title").val(this.title);
  626. $("#img").val(this.img);
  627. $("#id").val(this.id);
  628. $("#duration").val(this.duration);
  629. },
  630. "click .add-song-button": function(e){
  631. var genre = $(e.toElement).data("genre") || $(e.toElement).parent().data("genre");
  632. Meteor.call("addSongToPlaylist", genre, this);
  633. },
  634. "click .deny-song-button": function(e){
  635. var genre = $(e.toElement).data("genre") || $(e.toElement).parent().data("genre");
  636. Meteor.call("removeSongFromQueue", genre, this.mid);
  637. },
  638. "click .remove-song-button": function(e){
  639. var genre = $(e.toElement).data("genre") || $(e.toElement).parent().data("genre");
  640. Meteor.call("removeSongFromPlaylist", genre, this.mid);
  641. },
  642. "click #play": function() {
  643. $("#play").attr("disabled", true);
  644. $("#stop").attr("disabled", false);
  645. var song = Session.get("song");
  646. var id = song.id;
  647. var type = song.type;
  648. var volume = localStorage.getItem("volume") || 20;
  649. if (type === "YouTube") {
  650. if (yt_player === undefined) {
  651. yt_player = new YT.Player("previewPlayer", {
  652. height: 540,
  653. width: 568,
  654. videoId: id,
  655. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0},
  656. events: {
  657. 'onReady': function(event) {
  658. event.target.playVideo();
  659. event.target.setVolume(volume);
  660. },
  661. 'onStateChange': function(event){
  662. if (event.data == YT.PlayerState.PAUSED) {
  663. event.target.playVideo();
  664. }
  665. if (event.data == YT.PlayerState.PLAYING) {
  666. $("#play").attr("disabled", true);
  667. $("#stop").attr("disabled", false);
  668. } else {
  669. $("#play").attr("disabled", false);
  670. $("#stop").attr("disabled", true);
  671. }
  672. }
  673. }
  674. });
  675. } else {
  676. yt_player.loadVideoById(id);
  677. }
  678. $("#previewPlayer").show();
  679. } else if (type === "SoundCloud") {
  680. SC.stream("/tracks/" + song.id, function(sound) {
  681. _sound = sound;
  682. sound.setVolume(volume / 100);
  683. sound.play();
  684. });
  685. }
  686. },
  687. "click #stop": function() {
  688. $("#play").attr("disabled", false);
  689. $("#stop").attr("disabled", true);
  690. if (yt_player !== undefined) {
  691. yt_player.stopVideo();
  692. }
  693. if (_sound !== undefined) {
  694. _sound.stop();
  695. }
  696. },
  697. "click #croom_create": function() {
  698. Meteor.call("createRoom", $("#croom_display").val(), $("#croom_tag").val(), function (err, res) {
  699. if (err) {
  700. alert("Error " + err.error + ": " + err.reason);
  701. } else {
  702. window.location = "/" + $("#croom_tag").val();
  703. }
  704. });
  705. },
  706. "click #get-spotify-info": function() {
  707. var search = $("#title").val();
  708. var artistName = $("#artist").val();
  709. getSpotifyInfo(search, function(data) {
  710. for(var i in data){
  711. for(var j in data[i].items){
  712. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  713. $("#img").val(data[i].items[j].album.images[1].url);
  714. $("#duration").val(data[i].items[j].duration_ms / 1000);
  715. return;
  716. }
  717. }
  718. }
  719. }, artistName);
  720. },
  721. "click #save-song-button": function() {
  722. var newSong = {};
  723. newSong.id = $("#id").val();
  724. newSong.title = $("#title").val();
  725. newSong.artist = $("#artist").val();
  726. newSong.img = $("#img").val();
  727. newSong.type = $("#type").val();
  728. newSong.duration = $("#duration").val();
  729. if (Session.get("type") === "playlist") {
  730. Meteor.call("updatePlaylistSong", Session.get("genre"), Session.get("song"), newSong, function() {
  731. $('#editModal').modal('hide');
  732. });
  733. } else {
  734. Meteor.call("updateQueueSong", Session.get("genre"), Session.get("song"), newSong, function() {
  735. $('#editModal').modal('hide');
  736. });
  737. }
  738. },
  739. "click .delete-room": function(){
  740. var typeDel = $(this)[0].type;
  741. Meteor.call("deleteRoom", typeDel);
  742. }
  743. });
  744. Template.admin.onCreated(function() {
  745. var tag = document.createElement("script");
  746. tag.src = "https://www.youtube.com/iframe_api";
  747. var firstScriptTag = document.getElementsByTagName('script')[0];
  748. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  749. });
  750. Template.admin.onRendered(function() {
  751. $("#previewModal").on("hidden.bs.modal", function() {
  752. if (yt_player !== undefined) {
  753. $("#play").attr("disabled", false);
  754. $("#stop").attr("disabled", true);
  755. $("#previewPlayer").hide();
  756. yt_player.loadVideoById("", 0);
  757. yt_player.seekTo(0);
  758. yt_player.stopVideo();
  759. }
  760. if (_sound !== undefined) {
  761. _sound.stop();
  762. $("#play").attr("disabled", false);
  763. $("#stop").attr("disabled", true);
  764. }
  765. });
  766. $(document).ready(function() {
  767. function makeSlider(){
  768. var slider = $("#volume-slider").slider();
  769. var volume = localStorage.getItem("volume") || 20;
  770. $("#volume-slider").slider("setValue", volume);
  771. if (slider.length === 0) {
  772. Meteor.setTimeout(function() {
  773. makeSlider();
  774. }, 500);
  775. } else {
  776. slider.on("slide", function(val) {
  777. localStorage.setItem("volume", val.value);
  778. if (yt_player !== undefined) {
  779. yt_player.setVolume(val.value);
  780. } else if (_sound !== undefined) {
  781. var volume = val.value / 100;
  782. _sound.setVolume(volume);
  783. }
  784. });
  785. }
  786. }
  787. makeSlider();
  788. });
  789. });
  790. Template.playlist.helpers({
  791. playlist_songs: function() {
  792. parts = location.href.split('/');
  793. id = parts.pop();
  794. type = id.toLowerCase();
  795. var data = Playlists.findOne({type: type});
  796. if (data !== undefined) {
  797. data.songs.map(function(song) {
  798. if (Session.get("currentSong") !== undefined && song.mid === Session.get("currentSong").mid) {
  799. song.current = true;
  800. } else {
  801. song.current = false;
  802. }
  803. return song;
  804. });
  805. return data.songs;
  806. } else {
  807. return [];
  808. }
  809. }
  810. });
  811. Meteor.subscribe("rooms");
  812. Template.room.onCreated(function () {
  813. Session.set("reportSong", false);
  814. Session.set("reportTitle", false);
  815. Session.set("reportAuthor", false);
  816. Session.set("reportDuration", false);
  817. Session.set("reportAudio", false);
  818. Session.set("reportAlbumart", false);
  819. Session.set("reportOther", false);
  820. if (resizeSeekerbarInterval !== undefined) {
  821. Meteor.clearInterval(resizeSeekerbarInterval);
  822. resizeSeekerbarInterval = undefined;
  823. }
  824. yt_player = undefined;
  825. _sound = undefined;
  826. Session.set("videoHidden", false);
  827. var tag = document.createElement("script");
  828. tag.src = "https://www.youtube.com/iframe_api";
  829. var firstScriptTag = document.getElementsByTagName('script')[0];
  830. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  831. var currentSong = undefined;
  832. var currentSongR = undefined;
  833. function getTimeElapsed() {
  834. if (currentSong !== undefined) {
  835. var room = Rooms.findOne({type: type});
  836. if (room !== undefined) {
  837. return Date.now() - currentSong.started - room.timePaused;
  838. }
  839. }
  840. return 0;
  841. }
  842. function getSongInfo(songData){
  843. Session.set("title", songData.title);
  844. Session.set("artist", songData.artist);
  845. Session.set("id", songData.id);
  846. $("#song-img").attr("src", songData.img);
  847. Session.set("duration", songData.duration);
  848. }
  849. function resizeSeekerbar() {
  850. if (Session.get("state") === "playing") {
  851. $("#seeker-bar").width(((getTimeElapsed() / 1000) / Session.get("duration") * 100) + "%");
  852. }
  853. }
  854. function startSong() {
  855. $("#time-elapsed").text("0:00");
  856. if (currentSong !== undefined) {
  857. if (_sound !== undefined) _sound.stop();
  858. if (yt_player !== undefined && yt_player.stopVideo !== undefined) yt_player.stopVideo();
  859. var volume = localStorage.getItem("volume") || 20;
  860. $("#media-container").empty();
  861. yt_player = undefined;
  862. if (currentSong.type === "SoundCloud") {
  863. $("#media-container").append('<img src="/soundcloud-image.png" class="embed-responsive-item" />');
  864. getSongInfo(currentSong);
  865. SC.stream("/tracks/" + currentSong.id, function(sound){
  866. _sound = sound;
  867. sound.setVolume(volume / 100);
  868. sound.play();
  869. var interval = setInterval(function() {
  870. if (sound.getState() === "playing") {
  871. sound.seek(getTimeElapsed());
  872. window.clearInterval(interval);
  873. }
  874. }, 200);
  875. Session.set("duration", currentSong.duration);
  876. resizeSeekerbar();
  877. });
  878. } else {
  879. $("#media-container").append('<div id="player" class="embed-responsive-item"></div>');
  880. if (yt_player === undefined) {
  881. yt_player = new YT.Player("player", {
  882. height: 540,
  883. width: 960,
  884. videoId: currentSong.id,
  885. playerVars: {controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0},
  886. events: {
  887. 'onReady': function(event) {
  888. event.target.seekTo(getTimeElapsed() / 1000);
  889. event.target.playVideo();
  890. event.target.setVolume(volume);
  891. resizeSeekerbar();
  892. },
  893. 'onStateChange': function(event){
  894. if (event.data == YT.PlayerState.PAUSED && Session.get("state") === "playing") {
  895. event.target.seekTo(getTimeElapsed() / 1000);
  896. event.target.playVideo();
  897. }
  898. if (event.data == YT.PlayerState.PLAYING && Session.get("state") === "paused") {
  899. event.target.seekTo(getTimeElapsed() / 1000);
  900. event.target.pauseVideo();
  901. }
  902. }
  903. }
  904. });
  905. } else {
  906. yt_player.loadVideoById(currentSong.id);
  907. }
  908. getSongInfo(currentSong);
  909. }
  910. }
  911. }
  912. Session.set("loaded", false);
  913. Meteor.subscribe("rooms", function() {
  914. var parts = location.href.split('/');
  915. var id = parts.pop();
  916. var type = id.toLowerCase();
  917. Session.set("type", type);
  918. if (Rooms.find({type: type}).count() !== 1) {
  919. window.location = "/";
  920. } else {
  921. Session.set("loaded", true);
  922. minterval = Meteor.setInterval(function () {
  923. var room = Rooms.findOne({type: type});
  924. if (room !== undefined) {
  925. if (room.state === "paused") {
  926. Session.set("state", "paused");
  927. if (yt_player !== undefined && yt_player.getPlayerState !== undefined && yt_player.getPlayerState() === 1) {
  928. yt_player.pauseVideo();
  929. } else if (_sound !== undefined && _sound.getState().indexOf("playing") !== -1) {
  930. _sound.pause();
  931. }
  932. } else {
  933. Session.set("state", "playing");
  934. if (yt_player !== undefined && yt_player.getPlayerState !== undefined && yt_player.getPlayerState() !== 1) {
  935. yt_player.playVideo();
  936. } else if (_sound !== undefined && _sound.getState().indexOf("paused") !== -1) {
  937. _sound.play();
  938. }
  939. }
  940. }
  941. if (currentSongR === undefined || room.currentSong.started !== currentSongR.started) {
  942. Session.set("previousSong", currentSong);
  943. currentSongR = room.currentSong;
  944. currentSong = room.currentSong.song;
  945. currentSong.started = room.currentSong.started;
  946. Session.set("currentSong", currentSong);
  947. startSong();
  948. }
  949. if (currentSong !== undefined) {
  950. if (room !== undefined) {
  951. var duration = (Date.now() - currentSong.started - room.timePaused) / 1000;
  952. var d = moment.duration(duration, 'seconds');
  953. if (Session.get("state") === "playing") {
  954. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  955. }
  956. }
  957. }
  958. }, 1000);
  959. resizeSeekerbarInterval = Meteor.setInterval(function () {
  960. resizeSeekerbar();
  961. }, 500);
  962. }
  963. });
  964. });
  965. }
  966. if (Meteor.isServer) {
  967. Meteor.startup(function() {
  968. reCAPTCHA.config({
  969. privatekey: '6LcVxg0TAAAAAI2fgIEEWHFxwNXeVIs8mzq5cfRM'
  970. });
  971. var stations = [{tag: "edm", display: "EDM"}, {tag: "pop", display: "Pop"}]; //Rooms to be set on server startup
  972. for(var i in stations){
  973. if(Rooms.find({type: stations[i]}).count() === 0){
  974. createRoom(stations[i].display, stations[i].tag);
  975. }
  976. }
  977. });
  978. var stations = [];
  979. var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_";
  980. function createUniqueSongId() {
  981. var code = "";
  982. for (var i = 0; i < 6; i++) {
  983. code += chars[Math.floor(Math.random() * chars.length)];
  984. }
  985. if (Playlists.find({"songs.mid": code}).count() > 0) {
  986. return createUniqueSongId();
  987. } else {
  988. return code;
  989. }
  990. }
  991. function getStation(type, cb) {
  992. stations.forEach(function(station) {
  993. if (station.type === type) {
  994. cb(station);
  995. return;
  996. }
  997. });
  998. }
  999. function createRoom(display, tag) {
  1000. var type = tag;
  1001. if (Rooms.find({type: type}).count() === 0) {
  1002. Rooms.insert({display: display, type: type}, function(err) {
  1003. if (err) {
  1004. throw err;
  1005. } else {
  1006. if (Playlists.find({type: type}).count() === 1) {
  1007. stations.push(new Station(type));
  1008. } else {
  1009. Playlists.insert({type: type, songs: getSongsByType(type)}, function (err2) {
  1010. if (err2) {
  1011. throw err2;
  1012. } else {
  1013. stations.push(new Station(type));
  1014. }
  1015. });
  1016. }
  1017. }
  1018. });
  1019. } else {
  1020. return "Room already exists";
  1021. }
  1022. }
  1023. function Station(type) {
  1024. var _this = this;
  1025. var startedAt = Date.now();
  1026. var playlist = Playlists.findOne({type: type});
  1027. var songs = playlist.songs;
  1028. if (playlist.lastSong === undefined) {
  1029. Playlists.update({type: type}, {$set: {lastSong: 0}});
  1030. playlist = Playlists.findOne({type: type});
  1031. songs = playlist.songs;
  1032. }
  1033. var currentSong = playlist.lastSong;
  1034. var currentTitle = songs[currentSong].title;
  1035. Rooms.update({type: type}, {$set: {currentSong: {song: songs[currentSong], started: startedAt}}});
  1036. this.skipSong = function() {
  1037. songs = Playlists.findOne({type: type}).songs;
  1038. songs.forEach(function(song, index) {
  1039. if (song.title === currentTitle) {
  1040. currentSong = index;
  1041. }
  1042. });
  1043. if (currentSong < (songs.length - 1)) {
  1044. currentSong++;
  1045. } else currentSong = 0;
  1046. if (songs);
  1047. if (currentSong === 0) {
  1048. this.shufflePlaylist();
  1049. } else {
  1050. if (songs[currentSong].mid === undefined) {
  1051. var newSong = songs[currentSong];
  1052. newSong.mid = createUniqueSongId();
  1053. songs[currentSong].mid = newSong.mid;
  1054. Playlists.update({type: type, "songs": songs[currentSong]}, {$set: {"songs.$": newSong}});
  1055. }
  1056. currentTitle = songs[currentSong].title;
  1057. Playlists.update({type: type}, {$set: {lastSong: currentSong}});
  1058. Rooms.update({type: type}, {$set: {timePaused: 0}});
  1059. this.songTimer();
  1060. Rooms.update({type: type}, {$set: {currentSong: {song: songs[currentSong], started: startedAt}}});
  1061. }
  1062. };
  1063. this.shufflePlaylist = function() {
  1064. songs = Playlists.findOne({type: type}).songs;
  1065. currentSong = 0;
  1066. Playlists.update({type: type}, {$set: {"songs": []}});
  1067. songs = shuffle(songs);
  1068. songs.forEach(function(song) {
  1069. if (song.mid === undefined) {
  1070. song.mid = createUniqueSongId();
  1071. }
  1072. Playlists.update({type: type}, {$push: {"songs": song}});
  1073. });
  1074. currentTitle = songs[currentSong].title;
  1075. Playlists.update({type: type}, {$set: {lastSong: currentSong}});
  1076. Rooms.update({type: type}, {$set: {timePaused: 0}});
  1077. this.songTimer();
  1078. Rooms.update({type: type}, {$set: {currentSong: {song: songs[currentSong], started: startedAt}}});
  1079. };
  1080. Rooms.update({type: type}, {$set: {timePaused: 0}});
  1081. var timer;
  1082. this.songTimer = function() {
  1083. startedAt = Date.now();
  1084. if (timer !== undefined) {
  1085. timer.pause();
  1086. }
  1087. timer = new Timer(function() {
  1088. _this.skipSong();
  1089. }, songs[currentSong].duration * 1000);
  1090. };
  1091. var state = Rooms.findOne({type: type}).state;
  1092. this.pauseRoom = function() {
  1093. if (state !== "paused") {
  1094. timer.pause();
  1095. Rooms.update({type: type}, {$set: {state: "paused"}});
  1096. state = "paused";
  1097. }
  1098. };
  1099. this.resumeRoom = function() {
  1100. if (state !== "playing") {
  1101. timer.resume();
  1102. Rooms.update({type: type}, {$set: {state: "playing", timePaused: timer.timeWhenPaused()}});
  1103. state = "playing";
  1104. }
  1105. };
  1106. this.cancelTimer = function() {
  1107. timer.pause();
  1108. };
  1109. this.getState = function() {
  1110. return state;
  1111. };
  1112. this.type = type;
  1113. this.songTimer();
  1114. }
  1115. function shuffle(array) {
  1116. var currentIndex = array.length, temporaryValue, randomIndex ;
  1117. // While there remain elements to shuffle...
  1118. while (0 !== currentIndex) {
  1119. // Pick a remaining element...
  1120. randomIndex = Math.floor(Math.random() * currentIndex);
  1121. currentIndex -= 1;
  1122. // And swap it with the current element.
  1123. temporaryValue = array[currentIndex];
  1124. array[currentIndex] = array[randomIndex];
  1125. array[randomIndex] = temporaryValue;
  1126. }
  1127. return array;
  1128. }
  1129. function Timer(callback, delay) {
  1130. var timerId, start, remaining = delay;
  1131. var timeWhenPaused = 0;
  1132. var timePaused = new Date();
  1133. this.pause = function() {
  1134. Meteor.clearTimeout(timerId);
  1135. remaining -= new Date() - start;
  1136. timePaused = new Date();
  1137. };
  1138. this.resume = function() {
  1139. start = new Date();
  1140. Meteor.clearTimeout(timerId);
  1141. timerId = Meteor.setTimeout(callback, remaining);
  1142. timeWhenPaused += new Date() - timePaused;
  1143. };
  1144. this.timeWhenPaused = function() {
  1145. return timeWhenPaused;
  1146. };
  1147. this.resume();
  1148. }
  1149. Meteor.users.deny({update: function () { return true; }});
  1150. Meteor.users.deny({insert: function () { return true; }});
  1151. Meteor.users.deny({remove: function () { return true; }});
  1152. function getSongDuration(query, artistName){
  1153. var duration;
  1154. var search = query;
  1155. query = query.toLowerCase().split(" ").join("%20");
  1156. var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + query + '&type=track');
  1157. for(var i in res.data){
  1158. for(var j in res.data[i].items){
  1159. if(search.indexOf(res.data[i].items[j].name) !== -1 && artistName.indexOf(res.data[i].items[j].artists[0].name) !== -1){
  1160. duration = res.data[i].items[j].duration_ms / 1000;
  1161. return duration;
  1162. }
  1163. }
  1164. }
  1165. }
  1166. function getSongAlbumArt(query, artistName){
  1167. var albumart;
  1168. var search = query;
  1169. query = query.toLowerCase().split(" ").join("%20");
  1170. var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + query + '&type=track');
  1171. for(var i in res.data){
  1172. for(var j in res.data[i].items){
  1173. if(search.indexOf(res.data[i].items[j].name) !== -1 && artistName.indexOf(res.data[i].items[j].artists[0].name) !== -1){
  1174. albumart = res.data[i].items[j].album.images[1].url
  1175. return albumart;
  1176. }
  1177. }
  1178. }
  1179. }
  1180. //var room_types = ["edm", "nightcore"];
  1181. var songsArr = [];
  1182. function getSongsByType(type) {
  1183. if (type === "edm") {
  1184. return [
  1185. {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"},
  1186. {id: "aHjpOzsQ9YI", mid: "goG88g", title: "Crystallize", artist: "Lindsey Stirling", duration: getSongDuration("Crystallize", "Lindsey Stirling"), type: "YouTube", img: "https://i.scdn.co/image/b0c1ccdd0cd7bcda741ccc1c3e036f4ed2e52312"}
  1187. ];
  1188. } else if (type === "nightcore") {
  1189. 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"}];
  1190. } else {
  1191. 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"}];
  1192. }
  1193. }
  1194. Rooms.find({}).fetch().forEach(function(room) {
  1195. var type = room.type;
  1196. if (Playlists.find({type: type}).count() === 0) {
  1197. if (type === "edm") {
  1198. Playlists.insert({type: type, songs: getSongsByType(type)});
  1199. } else if (type === "nightcore") {
  1200. Playlists.insert({type: type, songs: getSongsByType(type)});
  1201. } else {
  1202. Playlists.insert({type: type, songs: getSongsByType(type)});
  1203. }
  1204. }
  1205. if (Playlists.findOne({type: type}).songs.length === 0) {
  1206. // Add a global video to Playlist so it can proceed
  1207. } else {
  1208. stations.push(new Station(type));
  1209. }
  1210. });
  1211. Accounts.onCreateUser(function(options, user) {
  1212. var username;
  1213. if (user.services) {
  1214. if (user.services.github) {
  1215. username = user.services.github.username;
  1216. } else if (user.services.facebook) {
  1217. username = user.services.facebook.first_name;
  1218. } else if (user.services.password) {
  1219. username = user.username;
  1220. }
  1221. }
  1222. user.profile = {username: username, usernameL: username.toLowerCase(), rank: "default"};
  1223. return user;
  1224. });
  1225. ServiceConfiguration.configurations.remove({
  1226. service: "facebook"
  1227. });
  1228. ServiceConfiguration.configurations.insert({
  1229. service: "facebook",
  1230. appId: "1496014310695890",
  1231. secret: "9a039f254a08a1488c08bb0737dbd2a6"
  1232. });
  1233. ServiceConfiguration.configurations.remove({
  1234. service: "github"
  1235. });
  1236. ServiceConfiguration.configurations.insert({
  1237. service: "github",
  1238. clientId: "dcecd720f47c0e4001f7",
  1239. secret: "375939d001ef1a0ca67c11dbf8fb9aeaa551e01b"
  1240. });
  1241. Meteor.publish("playlists", function() {
  1242. return Playlists.find({})
  1243. });
  1244. Meteor.publish("rooms", function() {
  1245. return Rooms.find({});
  1246. });
  1247. Meteor.publish("queues", function() {
  1248. return Queues.find({});
  1249. });
  1250. Meteor.publish("chat", function() {
  1251. return Chat.find({});
  1252. });
  1253. Meteor.publish("userProfiles", function() {
  1254. return Meteor.users.find({}, {fields: {profile: 1, createdAt: 1}});
  1255. });
  1256. Meteor.publish("isAdmin", function() {
  1257. return Meteor.users.find({_id: this.userId, "profile.rank": "admin"});
  1258. });
  1259. function isAdmin() {
  1260. var userData = Meteor.users.find(Meteor.userId());
  1261. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
  1262. return true;
  1263. } else {
  1264. return false;
  1265. }
  1266. }
  1267. Meteor.methods({
  1268. submitReport: function(report, id) {
  1269. var obj = report;
  1270. obj.id = id;
  1271. Reports.insert(obj);
  1272. },
  1273. shufflePlaylist: function(type) {
  1274. if (isAdmin()) {
  1275. getStation(type, function(station) {
  1276. if (station === undefined) {
  1277. throw new Meteor.Error(404, "Station not found.");
  1278. } else {
  1279. station.cancelTimer();
  1280. station.shufflePlaylist();
  1281. }
  1282. });
  1283. }
  1284. },
  1285. skipSong: function(type) {
  1286. if (isAdmin()) {
  1287. getStation(type, function(station) {
  1288. if (station === undefined) {
  1289. throw new Meteor.Error(404, "Station not found.");
  1290. } else {
  1291. station.skipSong();
  1292. }
  1293. });
  1294. }
  1295. },
  1296. pauseRoom: function(type) {
  1297. if (isAdmin()) {
  1298. getStation(type, function(station) {
  1299. if (station === undefined) {
  1300. throw new Meteor.Error(403, "Room doesn't exist.");
  1301. } else {
  1302. station.pauseRoom();
  1303. }
  1304. });
  1305. } else {
  1306. throw new Meteor.Error(403, "Invalid permissions.");
  1307. }
  1308. },
  1309. resumeRoom: function(type) {
  1310. if (isAdmin()) {
  1311. getStation(type, function(station) {
  1312. if (station === undefined) {
  1313. throw new Meteor.Error(403, "Room doesn't exist.");
  1314. } else {
  1315. station.resumeRoom();
  1316. }
  1317. });
  1318. } else {
  1319. throw new Meteor.Error(403, "Invalid permissions.");
  1320. }
  1321. },
  1322. createUserMethod: function(formData, captchaData) {
  1323. var verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(this.connection.clientAddress, captchaData);
  1324. if (!verifyCaptchaResponse.success) {
  1325. console.log('reCAPTCHA check failed!', verifyCaptchaResponse);
  1326. throw new Meteor.Error(422, 'reCAPTCHA Failed: ' + verifyCaptchaResponse.error);
  1327. } else {
  1328. console.log('reCAPTCHA verification passed!');
  1329. Accounts.createUser({
  1330. username: formData.username,
  1331. email: formData.email,
  1332. password: formData.password
  1333. });
  1334. }
  1335. return true;
  1336. },
  1337. addSongToQueue: function(type, songData) {
  1338. if (Meteor.userId()) {
  1339. type = type.toLowerCase();
  1340. if (Rooms.find({type: type}).count() === 1) {
  1341. if (Queues.find({type: type}).count() === 0) {
  1342. Queues.insert({type: type, songs: []});
  1343. }
  1344. if (songData !== undefined && Object.keys(songData).length === 5 && songData.type !== undefined && songData.title !== undefined && songData.title !== undefined && songData.artist !== undefined && songData.img !== undefined) {
  1345. songData.duration = getSongDuration(songData.title, songData.artist);
  1346. songData.img = getSongAlbumArt(songData.title, songData.artist);
  1347. var mid = createUniqueSongId();
  1348. if (mid !== undefined) {
  1349. songData.mid = mid;
  1350. Queues.update({type: type}, {
  1351. $push: {
  1352. songs: {
  1353. id: songData.id,
  1354. mid: songData.mid,
  1355. title: songData.title,
  1356. artist: songData.artist,
  1357. duration: songData.duration,
  1358. img: songData.img,
  1359. type: songData.type
  1360. }
  1361. }
  1362. });
  1363. return true;
  1364. } else {
  1365. throw new Meteor.Error(500, "Am error occured.");
  1366. }
  1367. } else {
  1368. throw new Meteor.Error(403, "Invalid data.");
  1369. }
  1370. } else {
  1371. throw new Meteor.Error(403, "Invalid genre.");
  1372. }
  1373. } else {
  1374. throw new Meteor.Error(403, "Invalid permissions.");
  1375. }
  1376. },
  1377. updateQueueSong: function(genre, oldSong, newSong) {
  1378. if (isAdmin()) {
  1379. newSong.mid = oldSong.mid;
  1380. Queues.update({type: genre, "songs": oldSong}, {$set: {"songs.$": newSong}});
  1381. return true;
  1382. } else {
  1383. throw new Meteor.Error(403, "Invalid permissions.");
  1384. }
  1385. },
  1386. updatePlaylistSong: function(genre, oldSong, newSong) {
  1387. if (isAdmin()) {
  1388. newSong.mid = oldSong.mid;
  1389. Playlists.update({type: genre, "songs": oldSong}, {$set: {"songs.$": newSong}});
  1390. return true;
  1391. } else {
  1392. throw new Meteor.Error(403, "Invalid permissions.");
  1393. }
  1394. },
  1395. removeSongFromQueue: function(type, mid) {
  1396. if (isAdmin()) {
  1397. type = type.toLowerCase();
  1398. Queues.update({type: type}, {$pull: {songs: {mid: mid}}});
  1399. } else {
  1400. throw new Meteor.Error(403, "Invalid permissions.");
  1401. }
  1402. },
  1403. removeSongFromPlaylist: function(type, mid) {
  1404. if (isAdmin()) {
  1405. type = type.toLowerCase();
  1406. Playlists.update({type: type}, {$pull: {songs: {mid: mid}}});
  1407. } else {
  1408. throw new Meteor.Error(403, "Invalid permissions.");
  1409. }
  1410. },
  1411. addSongToPlaylist: function(type, songData) {
  1412. if (isAdmin()) {
  1413. type = type.toLowerCase();
  1414. if (Rooms.find({type: type}).count() === 1) {
  1415. if (Playlists.find({type: type}).count() === 0) {
  1416. Playlists.insert({type: type, songs: []});
  1417. }
  1418. 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) {
  1419. Playlists.update({type: type}, {
  1420. $push: {
  1421. songs: {
  1422. id: songData.id,
  1423. mid: songData.mid,
  1424. title: songData.title,
  1425. artist: songData.artist,
  1426. duration: songData.duration,
  1427. img: songData.img,
  1428. type: songData.type
  1429. }
  1430. }
  1431. });
  1432. Queues.update({type: type}, {$pull: {songs: {mid: songData.mid}}});
  1433. return true;
  1434. } else {
  1435. throw new Meteor.Error(403, "Invalid data.");
  1436. }
  1437. } else {
  1438. throw new Meteor.Error(403, "Invalid genre.");
  1439. }
  1440. } else {
  1441. throw new Meteor.Error(403, "Invalid permissions.");
  1442. }
  1443. },
  1444. createRoom: function(display, tag) {
  1445. if (isAdmin()) {
  1446. createRoom(display, tag);
  1447. } else {
  1448. throw new Meteor.Error(403, "Invalid permissions.");
  1449. }
  1450. },
  1451. deleteRoom: function(type){
  1452. if (isAdmin()) {
  1453. Rooms.remove({type: type});
  1454. Playlists.remove({type: type});
  1455. Queues.remove({type: type});
  1456. return true;
  1457. } else {
  1458. throw new Meteor.Error(403, "Invalid permissions.");
  1459. }
  1460. }
  1461. });
  1462. }
  1463. /*Router.waitOn(function() {
  1464. Meteor.subscribe("isAdmin", Meteor.userId());
  1465. });*/
  1466. /*Router.onBeforeAction(function() {
  1467. /*Meteor.autorun(function () {
  1468. if (admin.ready()) {
  1469. this.next();
  1470. }
  1471. });*/
  1472. /*this.next();
  1473. });*/
  1474. Router.route("/", {
  1475. template: "home"
  1476. });
  1477. Router.route("/login", {
  1478. template: "login"
  1479. });
  1480. Router.route("/signup", {
  1481. template: "register"
  1482. });
  1483. Router.route("/terms", {
  1484. template: "terms"
  1485. });
  1486. Router.route("/privacy", {
  1487. template: "privacy"
  1488. });
  1489. Router.route("/about", {
  1490. template: "about"
  1491. });
  1492. Router.route("/admin", {
  1493. waitOn: function() {
  1494. return Meteor.subscribe("isAdmin", Meteor.userId());
  1495. },
  1496. action: function() {
  1497. var user = Meteor.users.findOne({});
  1498. if (user !== undefined && user.profile !== undefined && user.profile.rank === "admin") {
  1499. this.render("admin");
  1500. } else {
  1501. this.redirect("/");
  1502. }
  1503. }
  1504. });
  1505. Router.route("/vis", {
  1506. template: "visualizer"
  1507. });
  1508. Router.route("/:type", {
  1509. template: "room"
  1510. });
  1511. Router.route("/u/:user", {
  1512. template: "profile"
  1513. });