diff --git a/app/assets/javascripts/player.coffee b/app/assets/javascripts/player.coffee index f660f71..be7bfe0 100644 --- a/app/assets/javascripts/player.coffee +++ b/app/assets/javascripts/player.coffee @@ -183,8 +183,7 @@ class window.Player false scrobble: (track) -> - if _session.getUser().lastfm_username - _session.query '/lastfm/scrobble?r=' +Math.random(), track + _session.query '/lastfm/scrobble?r=' +Math.random(), track false emptyPlaylist: -> diff --git a/app/assets/javascripts/settings.coffee b/app/assets/javascripts/settings.coffee index fc35a14..4071f28 100644 --- a/app/assets/javascripts/settings.coffee +++ b/app/assets/javascripts/settings.coffee @@ -9,8 +9,8 @@ class window.Settings false updateLastfmLogin: -> - log "Updating Last.fm info ..." if window.lastfm_popup.closed + log "Updating Last.fm info ..." _session.query '/user/auth', {}, (ar) -> _session.setUser ar.user if _session.user.lastfm_username not in ["", null] @@ -68,7 +68,7 @@ $('.lastfm-disconnect').live 'click', -> _settings.saveAccountInfo { params: lastfm_username: "" - lastfm_token: "" + lastfm_key: "" }, (ar) -> _session.setUser ar.user $('#settings .lastfm-on').hide() diff --git a/app/assets/stylesheets/authorized.css.scss b/app/assets/stylesheets/authorized.css.scss index 32de6ed..0e2b8a5 100644 --- a/app/assets/stylesheets/authorized.css.scss +++ b/app/assets/stylesheets/authorized.css.scss @@ -1,10 +1,9 @@ #login, .ad_here { display: block; } -#authorized, .player, .set-playlist, .dynamic-playlist { +#authorized, .player, .playlist, .set-playlist, .dynamic-playlist { display: none; } - .album { .art .button-container { display: none; @@ -20,6 +19,12 @@ } body.authorized { + #login, .ad_here { + display: none; + } + #authorized, .player, .playlist, .set-playlist, .dynamic-playlist { + display: block; + } .album { .art .button-container { display: block; @@ -33,13 +38,4 @@ body.authorized { } } } - .set-playlist, .dynamic-playlist { - display: block; - } - #login, .ad_here { - display: none; - } - #authorized, .player { - display: block; - } } diff --git a/app/assets/stylesheets/misc.scss b/app/assets/stylesheets/misc.scss index 32de56d..ad02cc3 100644 --- a/app/assets/stylesheets/misc.scss +++ b/app/assets/stylesheets/misc.scss @@ -14,10 +14,6 @@ height: 200px; } -.player, .playlist { - display: none; -} - .ad_here { display: block; border: #e0e0e0 1px solid; diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c93e5d8..c3280ac 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -50,8 +50,22 @@ class ApplicationController < ActionController::Base protected - def cache_key_for object - "#{object.class.to_s.underscore}_#{object.id}" + def session + Session.find_by_key(session_key) + end + + def current_user + session.user if current_user? + end + + def current_user? + !session.nil? and !session.user.nil? + end + + def set_locale + unless session.nil? + I18n.locale = session.user.lang + end end def authorize @@ -60,13 +74,6 @@ protected end end - def set_locale - session = Session.find_by_key(session_key) - unless session.nil? - I18n.locale = session.user.lang - end - end - def session_key if cookies[:beathaven_sid].nil? cookies[:beathaven_sid] = { @@ -77,6 +84,10 @@ protected cookies[:beathaven_sid] end + def cache_key_for object + "#{object.class.to_s.underscore}_#{object.id}" + end + def render_compact_partial partial_name (render_to_string :partial => partial_name.to_s).gsub(/\n(\s+)?/, '') end diff --git a/app/controllers/last_fm_controller.rb b/app/controllers/last_fm_controller.rb index 5ff9528..749d5f3 100644 --- a/app/controllers/last_fm_controller.rb +++ b/app/controllers/last_fm_controller.rb @@ -2,22 +2,22 @@ require 'open-uri' require 'net/http' class LastFmController < ApplicationController - before_filter :authorize, :except => [:connect, :autocomplete] + before_filter :authorize, except: [:connect, :autocomplete] def connect unless params[:sid].nil? or params[:token].nil? - session = Session.find_by_key(params[:sid]) - unless session.nil? - lastfm_api_session = LastFM::Auth.get_session(:token => params[:token], :api_sig => true) - session.user.lastfm_key = lastfm_api_session["session"]['key'] - session.user.lastfm_username = lastfm_api_session["session"]['name'] - session.user.save - render :text => '' + if current_user? + lastfm_api_session = LastFM::Auth.get_session(token: params[:token], api_sig: true) + user = current_user + user.lastfm_key = lastfm_api_session["session"]['key'] + user.lastfm_username = lastfm_api_session["session"]['name'] + user.save + return render text: "" else - return render :text => 'You Don\'t Fool Me', status: 403 + return render text: "You Don't Fool Me", status: 403 end else - return render :text => 'So Much Trouble In The World', status: 403 + return render text: "So Much Trouble In The World", status: 403 end end @@ -25,15 +25,15 @@ class LastFmController < ApplicationController @res = {} user = User.find_by_vkid(params[:mid]) unless user.lastfm_key.nil? - render :json => { - :connected => true, - :username => user.lastfm_username + render json: { + connected: true, + username: user.lastfm_username } else - render :json => { - :connected => false, - :lastfm_login_url => 'http://www.last.fm/api/auth?api_key='+ LastFM.api_key + - '&cb=http://'+ request.host << '/lastfm/connect/?sid='+ user.session.key + render json: { + connected: false, + lastfm_login_url: "http://www.last.fm/api/auth?api_key=#{LastFM.api_key}"+ + "&cb=http://#{request.host}:#{request.port.to_s}/lastfm/connect/?sid=#{user.session.key}" } end end @@ -41,42 +41,43 @@ class LastFmController < ApplicationController def listening @res = {} if params[:artist].nil? or params[:album].nil? or params[:name].nil? - return render :json => { :status => 'bad params' }, status: 403 + return render json: { status: 'bad params' }, status: 403 end - user = User.find_by_vkid(params[:mid]) - if user.lastfm_key.nil? - return render :json => {:status => 'lastfm account is not connected', :user => user} + if !current_user? or (current_user.lastfm_key.nil? or current_user.lastfm_key == "") + return render json: {status: 'lastfm account is not connected' } end r = LastFM::Track.update_now_playing( - :track => params[:name], - :artist => params[:artist], - :album => params[:album], - :trackNumber => params[:position].gsub(/[a-z]/, "").to_i, - :duration => params[:length].to_i, - :sk => user.lastfm_key # Auth session key + track: params[:name], + artist: params[:artist], + album: params[:album], + trackNumber: params[:position].gsub(/[a-z]/, "").to_i, + duration: params[:length].to_i, + sk: current_user.lastfm_key # Auth session key ) - render :json => { :status => r['error'].nil? ? 'success' : r }, :status => r['error'].nil? ? 200 : 403 + render json: { status: r['error'].nil? ? 'success' : r }, status: r['error'].nil? ? 200 : 403 end def scrobble @res = {} if params[:artist].nil? or params[:album].nil? or params[:name].nil? - return render :json => { :status => 'bad params' }, status: 403 + return render json: { status: 'bad params' }, status: 403 end - user = User.find_by_vkid(params[:mid]) - if user.lastfm_key.nil? - return render :json => { :status => 'lastfm account is not connected', :user => user } + if current_user? and track = Track.find_by_id(params[:id]) + Scrobble.create(user: current_user, track: track, scrobbled_at: DateTime.now) + end + if !current_user? or (current_user.lastfm_key.nil? or current_user.lastfm_key == "") + return render json: { status: 'lastfm account is not connected' } end r = LastFM::Track.scrobble( - :track => params[:name], - :timestamp => Time.now.utc.to_i, - :artist => params[:artist], - :album => params[:album], - :trackNumber => params[:position].gsub(/[a-z]/, "").to_i, - :duration => params[:length].to_i, - :sk => user.lastfm_key # Auth session key + track: params[:name], + timestamp: Time.now.utc.to_i, + artist: params[:artist], + album: params[:album], + trackNumber: params[:position].gsub(/[a-z]/, "").to_i, + duration: params[:length].to_i, + sk: current_user.lastfm_key # Auth session key ) - render :json => { :status => r['error'].nil? ? 'success' : r }, :status => r['error'].nil? ? 200 : 403 + render json: { status: r['error'].nil? ? 'success' : r }, status: r['error'].nil? ? 200 : 403 end def self.top_playlist artist @@ -89,16 +90,16 @@ class LastFmController < ApplicationController def autocomplete autocomplete = getSuggestions(params[:query]) - return render :nothing => true if autocomplete.nil? + return render nothing: true if autocomplete.nil? suggestions = [] autocomplete["response"]["docs"].each do |doc| suggestions << doc["artist"] unless suggestions.include?(doc["artist"]) or doc["artist"].nil? or doc['restype'] != 6 end response.headers['Cache-Control'] = 'public, max-age='+1.week.seconds.to_s - render :json => { - :query => params[:query], - :suggestions => suggestions.take(5) + render json: { + query: params[:query], + suggestions: suggestions.take(5) } end diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 50cc054..bbf3951 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -28,7 +28,7 @@ class UserController < ApplicationController end def update - allowed_params = [:name, :email, :lang, :show, :lastfm_username, :lastfm_token] + allowed_params = [:name, :email, :lang, :show, :lastfm_username, :lastfm_key] user = User.find_by_vkid(params[:mid]) unless params[:params].nil? @@ -85,9 +85,8 @@ private vkid: user.vkid, lang: user.lang, lastfm_username: user.lastfm_username, - lastfm_login_url: ('http://www.last.fm/api/auth?api_key=' << LastFM.api_key << - '&cb=http://' << request.host << ':' << request.port.to_s << '/lastfm/connect/?sid=' << - user.session.key if user.lastfm_username.nil? or user.lastfm_username == "") + lastfm_login_url: "http://www.last.fm/api/auth?api_key=#{LastFM.api_key}"+ + "&cb=http://#{request.host}:#{request.port.to_s}/lastfm/connect/?sid=#{user.session.key}" } end end diff --git a/app/models/beathaven/scrobble.rb b/app/models/beathaven/scrobble.rb new file mode 100644 index 0000000..346537d --- /dev/null +++ b/app/models/beathaven/scrobble.rb @@ -0,0 +1,4 @@ +class Scrobble < ActiveRecord::Base + belongs_to :user + belongs_to :track +end diff --git a/db/migrate/20111129193103_create_scrobbles.rb b/db/migrate/20111129193103_create_scrobbles.rb new file mode 100644 index 0000000..4544812 --- /dev/null +++ b/db/migrate/20111129193103_create_scrobbles.rb @@ -0,0 +1,9 @@ +class CreateScrobbles < ActiveRecord::Migration + def change + create_table :scrobbles do |t| + t.integer :user_id + t.integer :track_id + t.datetime :scrobbled_at + end + end +end diff --git a/db/schema.rb b/db/schema.rb index cce0cb8..94eb23d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20111127102836) do +ActiveRecord::Schema.define(:version => 20111129193103) do create_table "album_formats", :force => true do |t| t.integer "album_id" @@ -166,6 +166,12 @@ ActiveRecord::Schema.define(:version => 20111127102836) do add_index "release_formats", ["hash"], :name => "index_release_formats_on_hash" + create_table "scrobbles", :force => true do |t| + t.integer "user_id" + t.integer "track_id" + t.datetime "scrobbled_at" + end + create_table "sessions", :force => true do |t| t.integer "user_id" t.string "key" diff --git a/test/fixtures/scrobbles.yml b/test/fixtures/scrobbles.yml new file mode 100644 index 0000000..dd068c8 --- /dev/null +++ b/test/fixtures/scrobbles.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html + +one: + user_id: 1 + track_id: 1 + scrobbled_at: 2011-11-29 23:31:03 + +two: + user_id: 1 + track_id: 1 + scrobbled_at: 2011-11-29 23:31:03 diff --git a/test/unit/scrobble_test.rb b/test/unit/scrobble_test.rb new file mode 100644 index 0000000..3734f0e --- /dev/null +++ b/test/unit/scrobble_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ScrobbleTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end