app.js 10 KB

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