2011-06-20 23:28:09 +00:00
|
|
|
class UserController < ApplicationController
|
2011-06-21 20:44:19 +00:00
|
|
|
|
2011-06-20 23:28:09 +00:00
|
|
|
def auth
|
2011-06-22 00:55:09 +00:00
|
|
|
return unless authorized?
|
2011-06-20 23:28:09 +00:00
|
|
|
|
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?
|
|
|
|
user = User.new
|
|
|
|
user.vkid = params[:mid]
|
2011-06-30 05:12:52 +00:00
|
|
|
user.lang = 'ru'
|
2011-06-22 00:55:09 +00:00
|
|
|
user.save
|
2011-06-26 11:45:51 +00:00
|
|
|
@res[:newbie] = true
|
2011-06-21 20:44:19 +00:00
|
|
|
end
|
2011-06-22 07:23:07 +00:00
|
|
|
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
|
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,
|
|
|
|
:lastfm_username => user.lastfm_username
|
2011-06-26 11:45:51 +00:00
|
|
|
}
|
2011-06-30 05:12:52 +00:00
|
|
|
unless user.lastfm_username
|
|
|
|
@res[:lastfm_login_url] = 'http://www.last.fm/api/auth?api_key='+ LastFmController.api_key +
|
|
|
|
'&cb=http://'+ request.host << '/lastfm/connect/?sid='+ user.session.key
|
|
|
|
end
|
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
|
|
|
return unless authorized?
|
|
|
|
@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-06-22 00:55:09 +00:00
|
|
|
return unless authorized?
|
2011-06-21 23:29:18 +00:00
|
|
|
@res = {}
|
|
|
|
|
2011-06-22 00:55:09 +00:00
|
|
|
user = User.find_by_vkid(params[:mid])
|
|
|
|
unless params[:username].nil? or params[:email].nil?
|
|
|
|
user.name = params[:username]
|
|
|
|
user.email = params[:email]
|
2011-06-30 05:12:52 +00:00
|
|
|
user.lang = params[:lang] if ['ru', 'en'].include? params[:lang]
|
2011-06-22 00:55:09 +00:00
|
|
|
user.save
|
2011-06-21 23:29:18 +00:00
|
|
|
end
|
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,
|
|
|
|
:lastfm_username => user.lastfm_username
|
2011-06-26 11:45:51 +00:00
|
|
|
}
|
2011-06-30 05:12:52 +00:00
|
|
|
unless user.lastfm_username
|
|
|
|
@res[:lastfm_login_url] = 'http://www.last.fm/api/auth?api_key='+ LastFmController.api_key +
|
|
|
|
'&cb=http://'+ request.host << '/lastfm/connect/?sid='+ user.session.key
|
|
|
|
end
|
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
|
2011-06-22 00:55:09 +00:00
|
|
|
return unless authorized?
|
2011-06-22 00:05:12 +00:00
|
|
|
@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
|