1
0
Fork 0
oldhaven/app/controllers/user_controller.rb

100 lines
2.6 KiB
Ruby
Raw Normal View History

2011-06-20 23:28:09 +00:00
class UserController < ApplicationController
2011-09-17 11:38:49 +00:00
before_filter :authorize
2011-06-21 20:44:19 +00:00
2011-06-20 23:28:09 +00:00
def auth
2011-06-26 11:45:51 +00:00
@res = {:status => 'hello', :newbie => false}
2011-06-22 00:55:09 +00:00
user = User.find_by_vkid(params[:mid])
if user.nil?
2011-10-20 11:01:57 +00:00
user = User.new(:vkid => params[:mid], :lang => 'ru').save
2011-06-26 11:45:51 +00:00
@res[:newbie] = true
2011-06-21 20:44:19 +00:00
end
2011-09-17 10:20:07 +00:00
2011-06-22 07:23:07 +00:00
session = Session.find_or_create_by_user_id(user.id)
2011-09-17 11:38:49 +00:00
if session.key != session_key
session.key = session_key
2011-09-17 10:20:07 +00:00
session.save
@res[:ok_reload] = true
2011-10-20 11:01:57 +00:00
return render :json => @res
2011-09-17 10:20:07 +00:00
end
2011-06-21 20:44:19 +00:00
2011-06-26 11:45:51 +00:00
@res[:user] = {
:id => user.id,
:name => user.name,
:email => user.email,
:vkid => user.vkid,
2011-06-30 05:12:52 +00:00
:lang => user.lang,
2011-09-15 10:44:44 +00:00
:lastfm_username => user.lastfm_username,
2011-09-17 19:00:08 +00:00
:lastfm_login_url => 'http://www.last.fm/api/auth?api_key=' << LastFM.api_key <<
2011-09-15 10:44:44 +00:00
'&cb=http://' << request.host << ':' << request.port.to_s << '/lastfm/connect/?sid=' << user.session.key
2011-06-26 11:45:51 +00:00
}
2011-06-21 20:44:19 +00:00
render :json => @res
end
2011-09-09 23:30:03 +00:00
def settings
2011-09-12 16:16:13 +00:00
@user = User.find_by_vkid(params[:mid])
2011-09-09 23:30:03 +00:00
render :partial => 'settings'
end
2011-06-21 20:44:19 +00:00
def update
2011-09-15 05:20:50 +00:00
allowed_params = [:name, :email, :lang, :show]
2011-06-21 23:29:18 +00:00
@res = {}
2011-06-22 00:55:09 +00:00
user = User.find_by_vkid(params[:mid])
2011-09-15 10:44:44 +00:00
unless params[:params].nil?
update_params = {}
params[:params] = params[:params].each{ |k, v| update_params[k.to_sym] = v }
if (update_params.keys - allowed_params).empty?
if update_params.include? :show
update_params[:show] = update_params[:show].map{ |k, v| k.to_sym }
end
params[:params].each do |k, v|
user[k] = v
end
user.save
2011-09-15 05:20:50 +00:00
end
2011-06-21 23:29:18 +00:00
end
2011-09-15 05:20:50 +00:00
2011-06-26 11:45:51 +00:00
@res[:user] = {
:id => user.id,
:name => user.name,
:email => user.email,
:vkid => user.vkid,
2011-06-30 05:12:52 +00:00
:lang => user.lang,
2011-09-15 10:44:44 +00:00
:lastfm_username => user.lastfm_username,
2011-09-17 19:00:08 +00:00
:lastfm_login_url => 'http://www.last.fm/api/auth?api_key=' << LastFM.api_key <<
2011-09-15 10:44:44 +00:00
'&cb=http://' << request.host << ':' << request.port.to_s << '/lastfm/connect/?sid=' << user.session.key
2011-06-26 11:45:51 +00:00
}
2011-06-21 20:44:19 +00:00
2011-06-21 23:29:18 +00:00
render :json => @res
2011-06-21 20:44:19 +00:00
end
2011-06-22 00:05:12 +00:00
def fav
@res = {status: 'fail'}
2011-06-22 00:55:09 +00:00
fav = Favorite.new
if not params[:artist].nil?
artist = Artist.find(params[:artist]);
unless artist.nil?
fav.artist_id = artist.id
res[:status] = 'added'
end
elsif not params[:album].nil?
album = Album.find(params[:album]);
unless album.nil?
fav.album_id = album.id
res[:status] = 'added'
end
elsif not params[:track].nil?
track = Track.find(params[:track]);
unless track.nil?
fav.track_id = track.id
res[:status] = 'added'
2011-06-22 00:05:12 +00:00
end
end
render :json => @res
end
2011-06-20 23:28:09 +00:00
end