app.js 11 KB

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