diff --git a/app/assets/javascripts/player.coffee b/app/assets/javascripts/player.coffee index 05c1dc4..143a592 100644 --- a/app/assets/javascripts/player.coffee +++ b/app/assets/javascripts/player.coffee @@ -53,9 +53,13 @@ class window.Player autoplay = false initial_count = $('.playlist li').length for item in tracks + len = parseInt(item.length, 10) + m = Math.floor(len / 60) + s = len - Math.floor(len / 60) * 60 + duration = m + ':' + (if s < 10 then '0' else '') + s $('.playlist').append '
  • -
    ' +item.duration+ '
    +
    ' +duration+ '
    remove
    ' +item.artist+ '
    ' +item.name+ '
    diff --git a/app/assets/javascripts/search.coffee b/app/assets/javascripts/search.coffee index c43e1a1..fefbfa1 100644 --- a/app/assets/javascripts/search.coffee +++ b/app/assets/javascripts/search.coffee @@ -20,8 +20,8 @@ class window.Search _ajax.setArchor '/artist/' +name+ '/' _page.print data.html if _session.getUser().id - if data.albums? - for album in data.albums + if data.artist.albums? + for album in data.artist.albums _player.albums[album.id] = album $('.button-container').show() _search.hideSpinner() diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d55b3fd..84c8101 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -15,12 +15,11 @@ class ApplicationController < ActionController::Base render :partial => 'about' end - protected +protected def authorize unless Vkontakte.check(params) render :json => { :status => 'login failed' } - false end end @@ -40,4 +39,8 @@ class ApplicationController < ActionController::Base end cookies[:beathaven_sid] end + + def render_compact_partial partial_name + (render_to_string :partial => partial_name.to_s).gsub(/\n(\s+)?/, '') + end end diff --git a/app/controllers/artist_controller.rb b/app/controllers/artist_controller.rb index 214110c..debaf23 100644 --- a/app/controllers/artist_controller.rb +++ b/app/controllers/artist_controller.rb @@ -2,121 +2,52 @@ require 'open-uri' require 'musicbrainz' class ArtistController < ApplicationController - @@default_album_types = [:album, :soundtrack] def data - @data = { - :status => '', - :correct_name => '', - :html => '' - } - @loading = false - # Bad params - if params[:name].nil? or params[:name].length == 0 - @data[:status] = 'loading_failed'; - render :json => @data - return + if params[:name].nil? or params[:name].empty? + return render :json => { :status => 'loading_failed' } end # Searching for artist - name = params[:name].gsub('%20', ' ').gsub('+', ' ').gsub('.html', '') - @artist = Artist.find_by_name(name) + artist_name = get_artist_name_from_query + @artist = Artist.find_by_name(artist_name, include: {artist_links: {}, albums: {tracks: {}}}) - # Artist not found + # Artist not found in DB unless @artist - results = MusicBrainz::Artist.search(name) + results = MusicBrainz::Artist.search(artist_name) if results.empty? - @data[:status] = 'not_found' - render :json => @data - elsif results[0][:name] != name and (results[0][:name].downcase == name.downcase or results[0][:name].downcase == 'the '+ name.downcase) - @data[:status] = 'corrected' - @data[:correct_name] = results[0][:name] - render :json => @data - elsif results[0][:name] == name - # Saving artist and queueing job - @artist = Artist.new - @artist.name = name - @artist.mbid = results[0][:mbid] - @artist.status = 0 - @artist.save - Delayed::Job.enqueue LoadArtistJob.new(name) - # Rendering loading info + return render :json => { :status => 'not_found' } + end + best_match = results[0][:name] + if best_match != artist_name and (best_match.downcase == artist_name.downcase or best_match.downcase == 'the '+ artist_name.downcase) + return render :json => { :status => 'corrected', :correct_name => best_match } + elsif results[0][:name] == artist_name + queue_loading(artist_name, results[0][:mbid]) @artist = { :artist => @artist, :albums => [] } @loading = true - @data[:status] = 'loading' - @data[:html] = (render_to_string :partial => 'page').gsub(/\n\s+/, '').gsub(/\n/, '') - render :json => @data + return render :json => { :status => 'loading', :html => render_compact_partial(:page) } else @suggestions = results.take(5) - @data[:status] = 'suggestions' - @data[:html] = (render_to_string :partial => 'suggestions').gsub(/\n\s+/, '').gsub(/\n/, '') - render :json => @data - end - return - end - - if @artist and @artist.status == 2 - @data[:status] = 'fail' - @data[:html] = (render_to_string :partial => 'fail').gsub(/\n\s+/, '').gsub(/\n/, '') - render :json => @data - return - end - - if @artist.status == 1 - @data[:status] = 'ok' - else - @data[:status] = 'loading' - @loading = true - end - - @data[:albums], @data[:html] = buildArtistHTML(@artist) - render :json => @data - end - - def buildArtistHTML artist - @artist[:artist] = { - id: artist.id, - name: artist.name, - desc: ActionController::Base.helpers.strip_tags(artist.desc), - pic: artist.pic_url, - urls: artist.artist_links - } - - media_types = @@default_album_types - session = Session.find_by_key(session_key) - unless session.nil? - media_types = session.user.music - end - - @artist[:albums] = [] - artist.albums.each do |album| - if true # media_types.include? album.album_type.downcase.to_sym and album.status == 1 - tmp_album = {:id => album.id, :name => album.name, :year => album.year, :pic => album.pic_url, :tracks => []} - album.tracks.each do |track| - tmp_track = {id: track.id, name: track.name, live: track.live, acoustic: track.acoustic} - tmp_track[:length] = track.length unless track.length.nil? - tmp_track[:duration] = formatTrackDuration(track.length) - tmp_track[:position] = track.position.to_s 36 - # tmp_track[:mbid] = track.mbid - # (track.bonus == 0 ? album_tracks : bonus_tracks) << tmp_track - tmp_album[:tracks] << tmp_track - end - # tmp_album[:tracks] = {album: album_tracks, bonus: bonus_tracks} - @artist[:albums] << tmp_album unless tmp_album[:tracks].empty? + return render :json => { :status => 'suggestions', :html => render_compact_partial(:suggestions) } end end - return @artist[:albums], (render_to_string :partial => 'page').gsub(/\n\s+/, '').gsub(/\n/, '') + # Artist loading failed + if @artist.status == 2 + return render :json => { :status => 'fail', :html => render_compact_partial(:fail) } + end + + render json: { status: @artist.status_str, artist: @artist, html: render_compact_partial(:page) }, include: {albums: {include: {tracks: {}}}} end - def formatTrackDuration length - if length - time = length # (length / 1000).round - time_m = (time / 60).floor - time_s = time - time_m * 60 - time_m.to_s + ':' + (time_s < 10 ? '0' : '') + time_s.to_s - else - '0:00' - end +private + + def get_artist_name_from_query + params[:name].gsub('%20', ' ').gsub('+', ' ').gsub('.html', '') + end + + def queue_loading artist_name, mbid + @artist = Artist.create( :name => artist_name, :mbid => mbid, :status => 0 ) + Delayed::Job.enqueue(LoadArtistJob.new(artist_name)) end end diff --git a/app/controllers/track_controller.rb b/app/controllers/track_controller.rb index 3938234..9467963 100644 --- a/app/controllers/track_controller.rb +++ b/app/controllers/track_controller.rb @@ -8,8 +8,7 @@ class TrackController < ApplicationController if track.length == nil or track.length.to_i == 0 and params[:length].to_i > 0 track.length = params[:length].to_i track.save - render :json => { :result => :success } - return + return render :json => { :result => :success } end end end @@ -21,8 +20,7 @@ class TrackController < ApplicationController unless track.nil? or track.available track.available = true track.save - render :json => { :status => :success } - return + return render :json => { :status => :success } end render :json => { :status => :failed } end @@ -32,8 +30,7 @@ class TrackController < ApplicationController unless track.nil? # Saving track file vote(params[:track_id], params[:owner_id], params[:audio_id], -1) - render :json => { :status => :success } - return + return render :json => { :status => :success } end render :json => { :status => :failed } end diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 1b77e8f..b118687 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -5,10 +5,7 @@ class UserController < ApplicationController @res = {:status => 'hello', :newbie => false} user = User.find_by_vkid(params[:mid]) if user.nil? - user = User.new - user.vkid = params[:mid] - user.lang = 'ru' - user.save + user = User.new(:vkid => params[:mid], :lang => 'ru').save @res[:newbie] = true end @@ -17,8 +14,7 @@ class UserController < ApplicationController session.key = session_key session.save @res[:ok_reload] = true - render :json => @res - return + return render :json => @res end @res[:user] = { @@ -52,7 +48,6 @@ class UserController < ApplicationController 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 diff --git a/app/models/music/artist.rb b/app/models/music/artist.rb index 7365c24..6667162 100644 --- a/app/models/music/artist.rb +++ b/app/models/music/artist.rb @@ -3,4 +3,14 @@ class Artist < ActiveRecord::Base has_many :artist_links, :dependent => :destroy has_many :artist_aliases, :dependent => :destroy has_many :tracks, :through => :track_artists + + after_initialize :prepare_description + + def status_str + %w(loading ok fail)[self.status] + end + + def prepare_description + self.desc.gsub! /[\[<].*?[\]>]/, '' + end end diff --git a/app/models/music/track.rb b/app/models/music/track.rb index c13468d..ee71386 100644 --- a/app/models/music/track.rb +++ b/app/models/music/track.rb @@ -2,4 +2,16 @@ class Track < ActiveRecord::Base belongs_to :album has_many :track_artists has_many :artists, :through => :track_artists + @duration + + def duration + if self.length + time = self.length + time_m = (time / 60).floor + time_s = time - time_m * 60 + time_m.to_s + ':' + (time_s < 10 ? '0' : '') + time_s.to_s + else + '0:00' + end + end end diff --git a/app/views/artist/_page.html.haml b/app/views/artist/_page.html.haml index 79f3823..36186b0 100644 --- a/app/views/artist/_page.html.haml +++ b/app/views/artist/_page.html.haml @@ -1,32 +1,32 @@ -- if @loading +- if @artist.status == 0 .alert-message.warning %p= I18n.t 'search.loading' .row.artist-info .span4.columns.pic - = image_tag @artist[:artist][:pic] unless @artist[:artist][:pic].nil? + = image_tag @artist.pic_url unless @artist.pic_url.nil? .span7.columns.desc - %h2= @artist[:artist][:name] - = @artist[:artist][:desc] unless @artist[:artist][:desc].nil? - - unless @artist[:artist][:urls].nil? + %h2= @artist.name + = @artist.desc unless @artist.desc.nil? + - unless @artist.artist_links.empty? .service-icons - - @artist[:artist][:urls].each do |service| + - @artist.artist_links.each do |service| - if ['wikipedia', 'microblog', 'official_homepage', 'social_network'].include?service.service %a.foreign-link{ :href => service.url, :target => '_blank' } = image_tag 'services/'+service.service+(service.service == 'official_homepage' ? '.png' : '.ico') -- @artist[:albums].each do |album| +- @artist.albums.each do |album| .row.album .span4.columns.art - %img{ :src => album[:pic] } + %img{ :src => album.pic_url } .button-container - %a.btn.add-album{ :'data-album-id' => album[:id] }= I18n.t 'player.add' + %a.btn.add-album{ :'data-album-id' => album.id }= I18n.t 'player.add' .span7.columns.tracks - %h3{ :'data-album-id' => album[:id] }= album[:name] + " (" + album[:year].to_s + ")" + %h3{ :'data-album-id' => album.id }= album.name + " (" + album.year.to_s + ")" %table.zebra-striped.tracklist - - album[:tracks].each do |track| + - album.tracks.each do |track| %tr - %td.song-title= track[:name] + %td.song-title= track.name %td.song-duration - .s-duration= (track[:duration] != '0:00' ? track[:duration] : ' '.html_safe) - .s-add{ :'data-album-id' => album[:id], :'data-id' => track[:id] }= I18n.t 'player.add_one' + .s-duration= (track.duration != '0:00' ? track.duration : ' '.html_safe) + .s-add{ :'data-album-id' => album.id, :'data-id' => track.id }= I18n.t 'player.add_one'