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

105 lines
2.7 KiB
Ruby
Raw Normal View History

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-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 05:20:50 +00:00
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
2011-09-15 05:42:41 +00:00
update_params[:show] = update_params[:show].map{ |k, v| k.to_sym }
2011-09-15 05:20:50 +00:00
end
params[:params].each do |k, v|
user[k] = v
end
2011-06-22 00:55:09 +00:00
user.save
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,
: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