class UserController < ApplicationController before_filter :authorize def auth @res = {status: 'hello', newbie: false} user = current_user if user.nil? user = User.find_or_create_by_vkid(params[:mid].to_i) if user.lang.nil? user.lang = "ru" user.save end @res[:newbie] = true end session = Session.find_or_create_by_user_id(user.id) if session.key != session_key session.key = session_key session.save @res[:ok_reload] = true return render json: @res end @res[:user] = build_user_info(user) render json: @res end def update allowed_params = [:name, :email, :lang, :show, :lastfm_username, :lastfm_key] user = current_user 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 end end render json: { user: build_user_info(user) } end def fav @res = {status: 'fail'} 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' end end render json: @res end def set_first_favorites return render json: { status: "unauthorized" } unless user_present? 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 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, 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 def similar_names name1, name2 (name2.downcase == name1.downcase or name2.downcase == 'the '+ name1.downcase) end end