From fbeddf1735d5a840d4cd9cde4ed1392cbb0ac95c Mon Sep 17 00:00:00 2001 From: Bert Owen Date: Sat, 24 May 2014 02:58:59 +0800 Subject: [PATCH] VRFS-1682, VRFS-1679 : upload music notation, download music notation --- ruby/lib/jam_ruby/models/music_notation.rb | 4 ++ ruby/lib/jam_ruby/models/music_session.rb | 23 ++++++++++++ .../api_music_notations_controller.rb | 37 +++++++++++++++++++ web/app/views/api_music_notations/create.rabl | 3 ++ .../api_music_sessions/show_history.rabl | 7 ++++ web/config/routes.rb | 4 ++ 6 files changed, 78 insertions(+) create mode 100644 web/app/controllers/api_music_notations_controller.rb create mode 100644 web/app/views/api_music_notations/create.rabl diff --git a/ruby/lib/jam_ruby/models/music_notation.rb b/ruby/lib/jam_ruby/models/music_notation.rb index 4b5abb4fd..8e926520e 100644 --- a/ruby/lib/jam_ruby/models/music_notation.rb +++ b/ruby/lib/jam_ruby/models/music_notation.rb @@ -30,6 +30,10 @@ module JamRuby s3_manager.sign_url(self[:file_url], {:expires => expiration_time, :secure => false}) end + def filename + File.basename(self.file_url) + end + private def delete_s3_files diff --git a/ruby/lib/jam_ruby/models/music_session.rb b/ruby/lib/jam_ruby/models/music_session.rb index cfa1f4194..717ed7202 100644 --- a/ruby/lib/jam_ruby/models/music_session.rb +++ b/ruby/lib/jam_ruby/models/music_session.rb @@ -80,6 +80,29 @@ module JamRuby tracks end + def can_join? user, as_musician + if as_musician + unless user.musician + return false # "a fan can not join a music session as a musician" + end + + if self.musician_access + if self.approval_required + return self.invited_musicians.exists?(user) + else + return true + end + + else + # the creator can always join, and the invited users can join + return self.creator == user || self.invited_musicians.exists?(user) + end + else + # it's a fan, and the only way a fan can join is if fan_access is true + self.fan_access + end + end + def self.index(current_user, user_id, band_id = nil, genre = nil) hide_private = false if current_user.id != user_id diff --git a/web/app/controllers/api_music_notations_controller.rb b/web/app/controllers/api_music_notations_controller.rb new file mode 100644 index 000000000..121f23da2 --- /dev/null +++ b/web/app/controllers/api_music_notations_controller.rb @@ -0,0 +1,37 @@ +require 'aws-sdk' + +class ApiMusicNotationsController < ApiController + before_filter :api_signed_in_user + + respond_to :json + + def create + client_id = params[:client_id] + + if client_id.nil? + raise JamArgumentError, "client_id must be asdfasfdasdf specified" + end + + @music_notation = MusicNotation.new + @music_notation.client_id = client_id + @music_notation.file_url = params[:file] + @music_notation.user = current_user + @music_notation.save + + if @music_notation.errors.any? + response.status = :unprocessable_entity + respond_with @music_notation + else + respond_with @music_notation, responder: ApiResponder, :statue => 201 + end + end + + def download + @music_notation = MusicNotation.find(params[:id]) + unless @music_notation.music_session.nil? || @music_notation.music_session.can_join?(current_user, true) + raise PermissionError, ValidationMessages::PERMISSION_VALIDATION_ERROR + end + + redirect_to @music_notation.sign_url + end +end \ No newline at end of file diff --git a/web/app/views/api_music_notations/create.rabl b/web/app/views/api_music_notations/create.rabl new file mode 100644 index 000000000..c946120c4 --- /dev/null +++ b/web/app/views/api_music_notations/create.rabl @@ -0,0 +1,3 @@ +object @music_notations + +attribute :id \ No newline at end of file diff --git a/web/app/views/api_music_sessions/show_history.rabl b/web/app/views/api_music_sessions/show_history.rabl index e048dd0d4..c71bcfb61 100644 --- a/web/app/views/api_music_sessions/show_history.rabl +++ b/web/app/views/api_music_sessions/show_history.rabl @@ -49,6 +49,13 @@ else } } + child(:music_notations => :music_notations) { + node do |music_notation| + attributes :id + note(:filename) { |music_notation| music_notation.filename } + end + } + child(:active_music_session => :active_music_session) { attributes :claimed_recording_initiator_id, :track_changes_counter diff --git a/web/config/routes.rb b/web/config/routes.rb index c79c885f8..d894dad3f 100644 --- a/web/config/routes.rb +++ b/web/config/routes.rb @@ -170,6 +170,10 @@ SampleApp::Application.routes.draw do match '/sessions/:id/tracks/:track_id' => 'api_music_sessions#track_show', :via => :get, :as => 'api_session_track_detail' match '/sessions/:id/tracks/:track_id' => 'api_music_sessions#track_destroy', :via => :delete + # Music notations + match '/music_notations' => 'api_music_notations#create', :via => :post + match '/music_notations/:id' => 'api_music_notations#download', :via => :get, :as => :download_music_notation + # RSVP requests match '/rsvp_requests' => 'api_rsvp_requests#index', :via => :get match '/rsvp_requests' => 'api_rsvp_requests#create', :via => :post