From 295811c6dea0343b2118b20b19f2e3727f03ab5c Mon Sep 17 00:00:00 2001 From: magnolia-fan Date: Sun, 26 Jun 2011 15:45:51 +0400 Subject: [PATCH] Updated auth controll --- app/controllers/last_fm_controller.rb | 4 ++++ app/controllers/user_controller.rb | 27 +++++++++++++++++++++------ public/css/player.css | 12 ++++++------ public/js/beathaven/ajax.js | 2 +- public/js/beathaven/player.js | 8 ++++++-- public/js/beathaven/settings.js | 23 ++++++++--------------- public/js/beathaven/vkontakte.js | 10 ++++++---- public/templates/settings.html | 2 +- 8 files changed, 53 insertions(+), 35 deletions(-) diff --git a/app/controllers/last_fm_controller.rb b/app/controllers/last_fm_controller.rb index 8953c90..58fb6cc 100644 --- a/app/controllers/last_fm_controller.rb +++ b/app/controllers/last_fm_controller.rb @@ -89,4 +89,8 @@ class LastFmController < ApplicationController return end + def self.api_key + @@api_key + end + end diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index ac2daa2..74d3126 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -2,21 +2,29 @@ class UserController < ApplicationController def auth return unless authorized? - @res = {} - @res[:status] = 'hello' + @res = {:status => 'hello', :newbie => false} user = User.find_by_vkid(params[:mid]) if user.nil? user = User.new user.vkid = params[:mid] user.save + @res[:newbie] = true end - @res[:id] = user.id - @res[:username] = user.name session = Session.find_or_create_by_user_id(user.id) session.key = Digest::SHA256.hexdigest(rand(99999999).to_s + user.id.to_s + rand(99999999).to_s) session.save + @res[:user] = { + :id => user.id, + :name => user.name, + :email => user.email, + :vkid => user.vkid, + :lastfm_username => user.lastfm_username, + :lastfm_login_url => 'http://www.last.fm/api/auth?api_key='+ LastFmController.api_key + + '&cb=http://'+ request.host << '/lastfm/connect/?sid='+ user.session.key + } + render :json => @res end @@ -30,8 +38,15 @@ class UserController < ApplicationController user.email = params[:email] user.save end - @res[:username] = user.name - @res[:email] = user.email + @res[:user] = { + :id => user.id, + :name => user.name, + :email => user.email, + :vkid => user.vkid, + :lastfm_username => user.lastfm_username, + :lastfm_login_url => 'http://www.last.fm/api/auth?api_key='+ LastFmController.api_key + + '&cb=http://'+ request.host << '/lastfm/connect/?sid='+ user.session.key + } render :json => @res end diff --git a/public/css/player.css b/public/css/player.css index 8b44eca..29f67d6 100644 --- a/public/css/player.css +++ b/public/css/player.css @@ -13,13 +13,13 @@ width: 80px; height: 30px; z-index: 10; - background: -moz-linear-gradient(left, rgba(34,34,34,0) 0%, rgba(34,34,34,1) 50%, rgba(34,34,34,1) 100%); /* FF3.6+ */ - background: -webkit-gradient(linear, left top, right top, color-stop(0%,rgba(34,34,34,0)), color-stop(50%,rgba(34,34,34,1)), color-stop(100%,rgba(34,34,34,1))); /* Chrome,Safari4+ */ - background: -webkit-linear-gradient(left, rgba(34,34,34,0) 0%,rgba(34,34,34,1) 50%,rgba(34,34,34,1) 100%); /* Chrome10+,Safari5.1+ */ - background: -o-linear-gradient(left, rgba(34,34,34,0) 0%,rgba(34,34,34,1) 50%,rgba(34,34,34,1) 100%); /* Opera11.10+ */ - background: -ms-linear-gradient(left, rgba(34,34,34,0) 0%,rgba(34,34,34,1) 50%,rgba(34,34,34,1) 100%); /* IE10+ */ + background: -moz-linear-gradient(left, rgba(34,34,34,0) 0%, rgba(34,34,34,1) 100%); /* FF3.6+ */ + background: -webkit-gradient(linear, left top, right top, color-stop(0%,rgba(34,34,34,0)), color-stop(100%,rgba(34,34,34,1))); /* Chrome,Safari4+ */ + background: -webkit-linear-gradient(left, rgba(34,34,34,0) 0%,rgba(34,34,34,1) 100%); /* Chrome10+,Safari5.1+ */ + background: -o-linear-gradient(left, rgba(34,34,34,0) 0%,rgba(34,34,34,1) 100%); /* Opera11.10+ */ + background: -ms-linear-gradient(left, rgba(34,34,34,0) 0%,rgba(34,34,34,1) 100%); /* IE10+ */ filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00222222', endColorstr='#222222',GradientType=1 ); /* IE6-9 */ - background: linear-gradient(left, rgba(34,34,34,0) 0%,rgba(34,34,34,1) 50%,rgba(34,34,34,1) 100%); /* W3C */ + background: linear-gradient(left, rgba(34,34,34,0) 0%,rgba(34,34,34,1) 100%); /* W3C */ } #player .progress { width: 330px; diff --git a/public/js/beathaven/ajax.js b/public/js/beathaven/ajax.js index 3244891..7426b06 100644 --- a/public/js/beathaven/ajax.js +++ b/public/js/beathaven/ajax.js @@ -70,7 +70,7 @@ var Ajax = { } else if (Ajax.getAnchor() === '' || Ajax.getAnchor().match(/\/search\//)) { Ajax.loadSearchPage(); } else if (Ajax.getAnchor().match(/\/settings\//)) { - Ajax.loadSettingsPage(); + Ajax.loadSearchPage(); } else { Ajax.load404Page(); } diff --git a/public/js/beathaven/player.js b/public/js/beathaven/player.js index b13f804..7feb442 100644 --- a/public/js/beathaven/player.js +++ b/public/js/beathaven/player.js @@ -158,11 +158,15 @@ var Player = { }, updateNowListening: function(artist, album, track) { - Session.query('/lastfm/listening?r='+ Math.random(), {'artist': artist, 'album': album, 'track': track}) + if (Settings.user.lastfm_username) { + Session.query('/lastfm/listening?r='+ Math.random(), {'artist': artist, 'album': album, 'track': track}); + } }, scrobble: function(artist, album, track) { - Session.query('/lastfm/scrobble?r='+ Math.random(), {'artist': artist, 'album': album, 'track': track}) + if (Settings.user.lastfm_username) { + Session.query('/lastfm/scrobble?r='+ Math.random(), {'artist': artist, 'album': album, 'track': track}); + } } } diff --git a/public/js/beathaven/settings.js b/public/js/beathaven/settings.js index ac22a93..892a159 100644 --- a/public/js/beathaven/settings.js +++ b/public/js/beathaven/settings.js @@ -1,6 +1,6 @@ var Settings = { - lastfm_login_url: null, + user: null, getAccountInfo: function(callback) { Session.query('/user/update', {}, callback); @@ -12,10 +12,12 @@ var Settings = { loadFormData: function(form) { if (form == 'account') { - this.getAccountInfo(function(data){ - $('.settings-container .form input[name$="username"]').val(data.username); - $('.settings-container .form input[name$="email"]').val(data.email); - }) + $('.settings-container .form input[name$="username"]').val(Settings.user.name); + $('.settings-container .form input[name$="email"]').val(Settings.user.email); + } else if (form == 'lastfm') { + if (Settings.user.lastfm_username) { + $('.form-container input[name$="username"]').first().val(Settings.user.lastfm_username); + } } } } @@ -31,19 +33,10 @@ $('.settings-container .tabs .tab').live('click', function(){ $('.form-container').html($('.forms .'+ $(this).attr('data-fieldset')).html()); Settings.loadFormData($(this).attr('data-fieldset')); } - if ($(this).attr('data-fieldset') == 'lastfm') { - Session.query('/lastfm/getinfo', {}, function(data){ - if (data.connected) { - $('.form-container input[name$="username"]').first().val(data.username); - } else { - Settings.lastfm_login_url = data.lastfm_login_url; - } - }) - } }); $('.lastfm-connect').live('click', function(){ - window.open(Settings.lastfm_login_url); + window.open(Settings.user.lastfm_login_url); }); $('.settings-container .form input').live('blur', function(){ diff --git a/public/js/beathaven/vkontakte.js b/public/js/beathaven/vkontakte.js index 61d5bb2..828df35 100644 --- a/public/js/beathaven/vkontakte.js +++ b/public/js/beathaven/vkontakte.js @@ -7,15 +7,17 @@ function authInfo(response) { $('#search_field').focus(); } Session.query('/user/auth', {}, function(ar){ - if (!ar.username) { + if (ar.newbie) { VK.Api.call('getVariable', {key: 1281}, function(r) { Session.query('/user/update', {'name': r.response}, function(ar2) { - $('#header-container .hello .greating').text('Hi there, '+ (ar2.username ? ar2.username : '%username%') +'!'); + Settings.user = ar2.user; + $('#header-container .hello .greating').text('Hi there, '+ (Settings.user.name ? Settings.user.name : '%username%') +'!'); }); }); + } else { + Settings.user = ar.user; } - $('#header-container .hello .greating').text('Hi there, '+ (ar.username ? ar.username : '%username%') +'!'); - Session.user_id = ar.id; + $('#header-container .hello .greating').text('Hi there, '+ (Settings.user.name ? Settings.user.name : '%username%') +'!'); }); } else { $('#vk_login, .auth_notice').css('display', 'block'); diff --git a/public/templates/settings.html b/public/templates/settings.html index dee94f3..d858f24 100644 --- a/public/templates/settings.html +++ b/public/templates/settings.html @@ -23,7 +23,7 @@
Username:
-
+