app.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. History = new Mongo.Collection("history");
  2. if (Meteor.isClient) {
  3. Meteor.startup(function() {
  4. reCAPTCHA.config({
  5. publickey: '6LcVxg0TAAAAAE18vBiH00UAyaJggsmLm890SjZl'
  6. });
  7. });
  8. var hpSound = undefined;
  9. Template.register.events({
  10. "submit form": function(e){
  11. e.preventDefault();
  12. var username = e.target.registerUsername.value;
  13. var email = e.target.registerEmail.value;
  14. var password = e.target.registerPassword.value;
  15. var captchaData = grecaptcha.getResponse();
  16. Meteor.call("createUserMethod", {username: username, email: email, password: password}, captchaData, function(err, res) {
  17. grecaptcha.reset();
  18. if (err) {
  19. console.log(err);
  20. } else {
  21. Meteor.loginWithPassword(username, password);
  22. }
  23. });
  24. },
  25. "click #facebook-login": function(){
  26. Meteor.loginWithFacebook()
  27. },
  28. "click #github-login": function(){
  29. Meteor.loginWithGithub()
  30. },
  31. "click #login": function(){
  32. $("#register-view").hide();
  33. $("#login-view").show();
  34. }
  35. });
  36. Template.login.events({
  37. "submit form": function(e){
  38. e.preventDefault();
  39. var username = e.target.loginUsername.value;
  40. var password = e.target.loginPassword.value;
  41. Meteor.loginWithPassword(username, password);
  42. Accounts.onLoginFailure(function(){
  43. $("input").css("background-color","indianred").addClass("animated shake");
  44. $("input").on("click",function(){
  45. $("input").css({
  46. "background-color": "transparent",
  47. "width": "250px"
  48. });
  49. })
  50. });
  51. },
  52. "click #facebook-login": function(){
  53. Meteor.loginWithFacebook()
  54. },
  55. "click #github-login": function(){
  56. Meteor.loginWithGithub()
  57. },
  58. "click #register": function(){
  59. $("#login-view").hide();
  60. $("#register-view").show();
  61. }
  62. });
  63. Template.dashboard.events({
  64. "click .logout": function(e){
  65. e.preventDefault();
  66. Meteor.logout();
  67. if (hpSound !== undefined) {
  68. hpSound.stop();
  69. }
  70. },
  71. "click .button-tunein": function(){
  72. SC.stream("/tracks/172055891/", function(sound){
  73. sound._player._volume = 0.3;
  74. sound.play();
  75. });
  76. },
  77. "click #play": function(){
  78. $("#play").hide();
  79. SC.stream("/tracks/172055891/", function(sound){
  80. hpSound = sound;
  81. sound._player._volume = 0.3;
  82. sound.play();
  83. $("#stop").on("click", function(){
  84. $("#stop").hide();
  85. $("#play").show();
  86. sound._player.pause();
  87. })
  88. });
  89. $("#stop").show();
  90. }
  91. });
  92. Template.room.helpers({
  93. type: function() {
  94. var parts = location.href.split('/');
  95. var id = parts.pop();
  96. return id.toUpperCase();
  97. },
  98. title: function(){
  99. return Session.get("title");
  100. },
  101. artist: function(){
  102. return Session.get("artist");
  103. }
  104. });
  105. Template.room.onCreated(function () {
  106. var currentSong = undefined;
  107. var _sound = undefined;
  108. var size = 0;
  109. function getTimeElapsed() {
  110. if (currentSong !== undefined) {
  111. return Date.now() - currentSong.started;
  112. }
  113. return 0;
  114. }
  115. function startSong() {
  116. if (currentSong !== undefined) {
  117. if (_sound !== undefined)_sound.stop();
  118. if (currentSong.song.type === "soundcloud") {
  119. $("#player").attr("src", "")
  120. SC.stream("/tracks/" + currentSong.song.id + "/", function(sound){
  121. _sound = sound;
  122. sound._player._volume = 0.3;
  123. console.log(sound);
  124. sound.play();
  125. Session.set("title", currentSong.song.title || "Title");
  126. Session.set("artist", currentSong.song.artist || "Artist");
  127. Session.set("albumArt", currentSong.song.albumArt);
  128. Session.set("duration", currentSong.song.duration)
  129. $("#seeker-bar").css("transition", Session.get("duration") + "s")
  130. $("#seeker-bar").width(1400);
  131. setTimeout(function() { // HACK, otherwise seek doesn't work.
  132. sound._player.seek(getTimeElapsed());
  133. }, 500);
  134. });
  135. } else {
  136. console.log("YT!");
  137. $("#player").attr("src", "http://www.youtube.com/embed/" + currentSong.song.id + "?autoplay=1&controls=0&autohide=1");
  138. }
  139. }
  140. }
  141. Meteor.subscribe("history");
  142. Meteor.setInterval(function() {
  143. var data = undefined;
  144. var dataCursor = History.find({type: "edm"});
  145. dataCursor.map(function(doc) {
  146. if (data === undefined) {
  147. data = doc;
  148. }
  149. });
  150. if (data.history.length > size) {
  151. currentSong = data.history[data.history.length-1];
  152. size = data.history.length;
  153. startSong();
  154. }
  155. }, 1000);
  156. });
  157. }
  158. if (Meteor.isServer) {
  159. Meteor.startup(function() {
  160. reCAPTCHA.config({
  161. privatekey: '6LcVxg0TAAAAAI2fgIEEWHFxwNXeVIs8mzq5cfRM'
  162. });
  163. });
  164. Meteor.users.deny({update: function () { return true; }});
  165. Meteor.users.deny({insert: function () { return true; }});
  166. Meteor.users.deny({remove: function () { return true; }});
  167. var startedAt = Date.now();
  168. var songs = [{id: "jofNR_WkoCE", title: "The Fox", artist: "Ylvis", duration: 15, type: "youtube"}, {id: 216112412, title: "How Deep Is Your Love", artist: "Calvin Harris", duration: 193, type: "soundcloud"}];
  169. var currentSong = 0;
  170. addToHistory(songs[currentSong], startedAt);
  171. function addToHistory(song, startedAt) {
  172. History.update({type: "edm"}, {$push: {history: {song: song, started: startedAt}}});
  173. }
  174. function skipSong() {
  175. if (currentSong < (songs.length - 1)) {
  176. currentSong++;
  177. } else currentSong = 0;
  178. songTimer();
  179. addToHistory(songs[currentSong], startedAt);
  180. }
  181. function songTimer() {
  182. startedAt = Date.now();
  183. Meteor.setTimeout(function() {
  184. skipSong();
  185. }, songs[currentSong].duration * 1000);
  186. }
  187. ServiceConfiguration.configurations.remove({
  188. service: "facebook"
  189. });
  190. ServiceConfiguration.configurations.insert({
  191. service: "facebook",
  192. appId: "1496014310695890",
  193. secret: "9a039f254a08a1488c08bb0737dbd2a6"
  194. });
  195. ServiceConfiguration.configurations.remove({
  196. service: "github"
  197. });
  198. ServiceConfiguration.configurations.insert({
  199. service: "github",
  200. clientId: "dcecd720f47c0e4001f7",
  201. secret: "375939d001ef1a0ca67c11dbf8fb9aeaa551e01b"
  202. });
  203. songTimer();
  204. Meteor.publish("history", function() {
  205. return History.find({type: "edm"})
  206. });
  207. Meteor.methods({
  208. createUserMethod: function(formData, captchaData) {
  209. var verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(this.connection.clientAddress, captchaData);
  210. if (!verifyCaptchaResponse.success) {
  211. console.log('reCAPTCHA check failed!', verifyCaptchaResponse);
  212. throw new Meteor.Error(422, 'reCAPTCHA Failed: ' + verifyCaptchaResponse.error);
  213. } else {
  214. console.log('reCAPTCHA verification passed!');
  215. Accounts.createUser({
  216. username: formData.username,
  217. email: formData.email,
  218. password: formData.password
  219. });
  220. }
  221. return true;
  222. }
  223. });
  224. }
  225. Router.route("/", {
  226. template: "home"
  227. });
  228. Router.route("/:type", {
  229. template: "room"
  230. });