oldhaven/app/controllers/user_controller.rb

136 lines
3.7 KiB
Ruby
Raw Normal View History

2011-06-21 03:28:09 +04:00
class UserController < ApplicationController
2011-09-17 15:38:49 +04:00
before_filter :authorize
2011-06-22 00:44:19 +04:00
2011-06-21 03:28:09 +04:00
def auth
2011-10-20 20:42:38 +04:00
@res = {status: 'hello', newbie: false}
2011-11-30 11:52:16 +04:00
user = current_user
2011-06-22 04:55:09 +04:00
if user.nil?
2011-11-30 11:52:16 +04:00
user = User.find_or_create_by_vkid(params[:mid].to_i)
if user.lang.nil?
user.lang = "ru"
user.save
end
2011-06-26 15:45:51 +04:00
@res[:newbie] = true
2011-06-22 00:44:19 +04:00
end
2011-09-17 14:20:07 +04:00
2011-06-22 11:23:07 +04:00
session = Session.find_or_create_by_user_id(user.id)
2011-09-17 15:38:49 +04:00
if session.key != session_key
session.key = session_key
2011-09-17 14:20:07 +04:00
session.save
@res[:ok_reload] = true
2011-10-20 20:42:38 +04:00
return render json: @res
2011-09-17 14:20:07 +04:00
end
2011-06-22 00:44:19 +04:00
2011-10-20 20:42:38 +04:00
@res[:user] = build_user_info(user)
2011-06-26 15:45:51 +04:00
2011-10-20 20:42:38 +04:00
render json: @res
2011-06-22 00:44:19 +04:00
end
def update
2011-11-29 23:42:21 +04:00
allowed_params = [:name, :email, :lang, :show, :lastfm_username, :lastfm_key]
2011-06-22 03:29:18 +04:00
2011-11-30 11:52:16 +04:00
user = current_user
2011-09-15 14:44:44 +04: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 09:20:50 +04:00
end
2011-06-22 03:29:18 +04:00
end
2011-09-15 09:20:50 +04:00
2011-11-30 03:08:00 +04:00
render json: { user: build_user_info(user) }
2011-06-22 00:44:19 +04:00
end
2011-06-22 04:05:12 +04:00
def fav
@res = {status: 'fail'}
2011-06-22 04:55:09 +04: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 04:05:12 +04:00
end
end
2011-10-20 20:42:38 +04:00
render json: @res
2011-06-22 04:05:12 +04:00
end
2011-11-30 03:08:00 +04:00
def set_first_favorites
2011-11-30 11:52:16 +04:00
return render json: { status: "unauthorized" } unless user_present?
2011-11-30 03:08:00 +04:00
return render json: { status: "already imported" } unless current_user.favorites.empty?
artists = {}
params[:tracks].each do |i, track|
artists[track["artist"]] ||= 0
artists[track["artist"]] += 1
end
top10 = []
artists.sort_by{ |k, v| v }.reverse.each do |info|
artist_name = info[0]
artist = Artist.find_by_name(artist_name)
if artist.nil?
results = MusicBrainz::Artist.search(artist_name)
next if results.empty?
best_match = results[0][:sort_name]
if best_match != artist_name and similar_names(artist_name, best_match)
artist = Artist.find_by_name(best_match)
if artist.nil?
artist = Artist.create( name: best_match, mbid: results[0][:mbid], status: 0 )
Delayed::Job.enqueue(LoadArtistJob.new(best_match))
end
elsif best_match == artist_name
artist = Artist.create( name: best_match, mbid: results[0][:mbid], status: 0 )
Delayed::Job.enqueue(LoadArtistJob.new(best_match))
end
end
top10 << artist unless artist.nil?
break if top10.length == 10
end
top10.each do |artist|
Favorite.create( user: current_user, artist: artist )
end
render json: top10.map(&:name)
end
2011-10-20 20:42:38 +04:00
private
def build_user_info user
{
id: user.id,
name: user.name,
email: user.email,
vkid: user.vkid,
lang: user.lang,
lastfm_username: user.lastfm_username,
2011-11-29 23:42:21 +04:00
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}"
2011-10-20 20:42:38 +04:00
}
end
2011-11-30 03:08:00 +04:00
def similar_names name1, name2
(name2.downcase == name1.downcase or name2.downcase == 'the '+ name1.downcase)
end
2011-06-21 03:28:09 +04:00
end