jam-cloud/app/controllers/api_music_sessions_controll...

176 lines
5.6 KiB
Ruby

class ApiMusicSessionsController < ApiController
# have to be signed in currently to see this screen
before_filter :signed_in_user
respond_to :json
def initialize
@mq_router = MQRouter.new
@message_factory = MessageFactory.new
end
def index
@music_sessions = MusicSession.index(current_user)
end
def create
ConnectionManager.active_record_transaction do |connection_manager|
client_id = params[:client_id]
if client_id.nil?
raise JamArgumentError, "client_id must be specified"
end
@music_session = MusicSession.new()
@music_session.creator = current_user
@music_session.description = params[:description]
@music_session.musician_access = params[:musician_access]
genres = params[:genres]
logger.debug "Genres class: " + genres.class.to_s()
unless genres.nil?
genres.each do |genre_id|
loaded_genre = Genre.find(genre_id)
@music_session.genres << loaded_genre
end
end
@music_session.save
unless @music_session.errors.any?
# auto-join this user into the newly created session
connection_manager.join_music_session(current_user.id, client_id, @music_session.id)
@connection = Connection.find_by_client_id(client_id)
tracks = params[:tracks]
logger.debug "Tracks class: " + tracks.class.to_s()
associate_tracks(@connection, tracks)
@connection.save
if @connection.errors.any?
# rollback the transaction to make sure nothing is disturbed in the database
raise ActiveRecord::Rollback
end
else
# rollback the transaction to make sure nothing is disturbed in the database
raise ActiveRecord::Rollback
end
end
if @music_session.errors.any?
# we have to do this because api_session_detail_url will fail with a bad @music_session
response.status = :unprocessable_entity
respond_with @music_session
elsif @connection.errors.any?
# we have to do this because api_session_detail_url will fail with a bad @music_session
response.status = :unprocessable_entity
respond_with @connection
else
respond_with @music_session, responder: ApiResponder, :location => api_session_detail_url(@music_session)
end
end
def show
@music_session = MusicSession.find(params[:id])
unless @music_session.can_see? current_user
raise ActiveRecord::RecordNotFound
end
end
def delete
@music_session = MusicSession.find(params[:id])
unless @music_session.can_delete? current_user
raise ActiveRecord::RecordNotound
end
@music_session.delete
respond_with @music_session, responder: ApiResponder
end
def participant_show
@connection = Connection.find_by_client_id(params[:id])
end
def participant_create
@music_session = nil
@connection = nil
ConnectionManager.active_record_transaction do |connection_manager|
@music_session = MusicSession.find(params[:id])
client_id = params[:client_id]
connection_manager.join_music_session(current_user.id, client_id, @music_session.id)
@connection = Connection.find_by_client_id(client_id)
tracks = params[:tracks]
associate_tracks(@connection, tracks)
@connection.save
end
# send out notification to queue to the rest of the session
# TODO: also this isn't necessarily a user leaving; it's a client leaving'
user_joined = @message_factory.user_joined_music_session(@music_session.id, current_user.id, current_user.name)
@mq_router.server_publish_to_session(@music_session, user_joined, sender = {:client_id => @connection.client_id})
respond_with @connection, responder: ApiResponder, :location => api_session_participant_detail_url(@connection.client_id)
end
def participant_delete
@music_session = nil
@connection = nil
ConnectionManager.active_record_transaction do |connection_manager|
@connection = Connection.find_by_client_id(params[:id])
unless @connection.nil?
@music_session = MusicSession.find(@connection.music_session_id)
end
if @connection.nil?
raise JamArgumentError, "no client found with specified client_id #{id}"
end
if @connection.user.id != current_user.id
raise PermissionError, "you do not own this connection"
end
connection_manager.leave_music_session(current_user.id, @connection.client_id, @connection.music_session_id)
end
unless @music_session.nil?
# send out notification to queue to the rest of the session
# TODO: we should rename the notification to music_session_participants_change or something
# TODO: also this isn't necessarily a user leaving; it's a client leaving'
user_left = @message_factory.user_left_music_session(@music_session.id, current_user.id, current_user.name)
@mq_router.server_publish_to_session(@music_session, user_left, sender = {:client_id => @connection.client_id})
end
respond_with @connection, responder: ApiResponder
end
private
def associate_tracks(connection, tracks)
logger.debug "Tracks:"
logger.debug tracks
unless tracks.nil?
#tracks.each_pair do |index,track|
tracks.each do |track|
logger.debug "Track"
logger.debug track
instrument = Instrument.find(track["instrument_id"])
connection_track = ConnectionTrack.new
connection_track.instrument = instrument
connection_track.connection = connection
connection_track.sound = track["sound"]
connection_track.save
connection.connection_tracks << connection_track
end
end
end
end