app.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. type: "soundcloud"
  115. }
  116. }
  117. }
  118. console.log(id);
  119. Meteor.call("addToPlaylist", songObj, function(err,res){
  120. console.log(res);
  121. });
  122. // if (_sound !== undefined)_sound.stop();
  123. // SC.stream("/tracks/" + id, function(sound){
  124. // _sound = sound;
  125. // sound._player._volume = 0.3;
  126. // sound.play()
  127. // });
  128. })
  129. });
  130. }
  131. });
  132. Template.room.helpers({
  133. type: function() {
  134. var parts = location.href.split('/');
  135. var id = parts.pop();
  136. return id.toUpperCase();
  137. },
  138. title: function(){
  139. return Session.get("title");
  140. },
  141. artist: function(){
  142. return Session.get("artist");
  143. }
  144. });
  145. Template.room.onCreated(function () {
  146. var currentSong = undefined;
  147. var _sound = undefined;
  148. var size = 0;
  149. function getTimeElapsed() {
  150. if (currentSong !== undefined) {
  151. return Date.now() - currentSong.started;
  152. }
  153. return 0;
  154. }
  155. function startSong() {
  156. if (currentSong !== undefined) {
  157. if (_sound !== undefined)_sound.stop();
  158. if (currentSong.song.type === "soundcloud") {
  159. $("#player").attr("src", "")
  160. SC.stream("/tracks/" + currentSong.song.id + "/", function(sound){
  161. _sound = sound;
  162. sound._player._volume = 0.3;
  163. console.log(sound);
  164. sound.play();
  165. Session.set("title", currentSong.song.title || "Title");
  166. Session.set("artist", currentSong.song.artist || "Artist");
  167. Session.set("duration", currentSong.song.duration)
  168. $("#seeker-bar").css("transition", Session.get("duration") + "s")
  169. $("#seeker-bar").width(1400);
  170. setTimeout(function() { // HACK, otherwise seek doesn't work.
  171. sound._player.seek(getTimeElapsed());
  172. }, 500);
  173. });
  174. } else {
  175. console.log("YT!");
  176. $("#player").attr("src", "http://www.youtube.com/embed/" + currentSong.song.id + "?autoplay=1&controls=0&autohide=1");
  177. Session.set("title", currentSong.song.title || "Title");
  178. Session.set("artist", currentSong.song.artist || "Artist");
  179. Session.set("duration", currentSong.song.duration);
  180. $("#seeker-bar").css("transition", Session.get("duration") + "s");
  181. $("#seeker-bar").width(1400);
  182. }
  183. }
  184. }
  185. Meteor.subscribe("history");
  186. Meteor.setInterval(function() {
  187. var data = undefined;
  188. var dataCursor = History.find({type: "edm"});
  189. dataCursor.map(function(doc) {
  190. if (data === undefined) {
  191. data = doc;
  192. }
  193. });
  194. if (data.history.length > size) {
  195. currentSong = data.history[data.history.length-1];
  196. size = data.history.length;
  197. startSong();
  198. }
  199. }, 1000);
  200. });
  201. }
  202. if (Meteor.isServer) {
  203. Meteor.startup(function() {
  204. reCAPTCHA.config({
  205. privatekey: '6LcVxg0TAAAAAI2fgIEEWHFxwNXeVIs8mzq5cfRM'
  206. });
  207. });
  208. Meteor.users.deny({update: function () { return true; }});
  209. Meteor.users.deny({insert: function () { return true; }});
  210. Meteor.users.deny({remove: function () { return true; }});
  211. var startedAt = Date.now();
  212. var songs = [
  213. {id: "eMrh3wYb1mM", title: "Where Are U Now", artist: "Pentatonix", duration: 244, type: "youtube"},
  214. {id: 170202151, title: "Runnaway (U & I)", artist: "Galantis", duration: 193, type: "soundcloud"}
  215. ];
  216. var currentSong = 0;
  217. addToHistory(songs[currentSong], startedAt);
  218. function addToHistory(song, startedAt) {
  219. History.update({type: "edm"}, {$push: {history: {song: song, started: startedAt}}});
  220. }
  221. function skipSong() {
  222. if (currentSong < (songs.length - 1)) {
  223. currentSong++;
  224. } else currentSong = 0;
  225. songTimer();
  226. addToHistory(songs[currentSong], startedAt);
  227. }
  228. function songTimer() {
  229. startedAt = Date.now();
  230. Meteor.setTimeout(function() {
  231. skipSong();
  232. }, songs[currentSong].duration * 1000);
  233. }
  234. ServiceConfiguration.configurations.remove({
  235. service: "facebook"
  236. });
  237. ServiceConfiguration.configurations.insert({
  238. service: "facebook",
  239. appId: "1496014310695890",
  240. secret: "9a039f254a08a1488c08bb0737dbd2a6"
  241. });
  242. ServiceConfiguration.configurations.remove({
  243. service: "github"
  244. });
  245. ServiceConfiguration.configurations.insert({
  246. service: "github",
  247. clientId: "dcecd720f47c0e4001f7",
  248. secret: "375939d001ef1a0ca67c11dbf8fb9aeaa551e01b"
  249. });
  250. songTimer();
  251. Meteor.publish("history", function() {
  252. return History.find({type: "edm"})
  253. });
  254. Meteor.methods({
  255. createUserMethod: function(formData, captchaData) {
  256. var verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(this.connection.clientAddress, captchaData);
  257. if (!verifyCaptchaResponse.success) {
  258. console.log('reCAPTCHA check failed!', verifyCaptchaResponse);
  259. throw new Meteor.Error(422, 'reCAPTCHA Failed: ' + verifyCaptchaResponse.error);
  260. } else {
  261. console.log('reCAPTCHA verification passed!');
  262. Accounts.createUser({
  263. username: formData.username,
  264. email: formData.email,
  265. password: formData.password
  266. });
  267. }
  268. return true;
  269. },
  270. addToPlaylist: function(songObj){
  271. songs.push(songObj);
  272. return songs;
  273. }
  274. });
  275. }
  276. Router.route("/", {
  277. template: "home"
  278. });
  279. Router.route("/:type", {
  280. template: "room"
  281. });