jam-cloud/lib/managers/music_session_manager.rb

120 lines
4.3 KiB
Ruby

require 'recaptcha'
class MusicSessionManager < BaseManager
include Recaptcha::Verify
def initialize(options={})
super(options)
@log = Logging.logger[self]
@mq_router = MQRouter.new
@message_factory = MessageFactory.new
end
def create(user, client_id, description, musician_access, approval_required, fan_chat, fan_access, band, genres, tracks, legal_terms)
return_value = nil
ActiveRecord::Base.transaction do
music_session = MusicSession.new()
music_session.creator = user
music_session.description = description
music_session.musician_access = musician_access
music_session.approval_required = approval_required
music_session.fan_chat = fan_chat
music_session.fan_access = fan_access
music_session.band = band
music_session.legal_terms = legal_terms
genres = genres
@log.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?
User.save_session_settings(user, music_session)
# auto-join this user into the newly created session
connection = ConnectionManager.new.join_music_session(user.id, client_id, music_session.id, true, tracks)
unless connection.errors.any?
return_value = music_session
else
return_value = connection
# rollback the transaction to make sure nothing is disturbed in the database
raise ActiveRecord::Rollback
end
else
return_value = music_session
# rollback the transaction to make sure nothing is disturbed in the database
raise ActiveRecord::Rollback
end
end
return return_value
end
def update(music_session, description, genres, musician_access, approval_required, fan_chat, fan_access)
ActiveRecord::Base.transaction do
music_session.description = description
music_session.musician_access = musician_access
music_session.approval_required = approval_required
music_session.fan_chat = fan_chat
music_session.fan_access = fan_access
# Do I have to do this the way he did above? Not sure. Probably yes.
genre_array = []
unless genres.nil?
genres.each do |genre_id|
loaded_genre = Genre.find(genre_id)
genre_array << loaded_genre
end
end
music_session.genres = genre_array
end
end
def participant_create(user, music_session_id, client_id, as_musician, tracks)
connection = nil
ActiveRecord::Base.transaction do
music_session = MusicSession.find(music_session_id)
as_musician = as_musician
connection = ConnectionManager.new.join_music_session(user.id, client_id, music_session.id, as_musician, tracks)
if connection.errors.any?
# rollback the transaction to make sure nothing is disturbed in the database
raise ActiveRecord::Rollback
else
# 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(connection.music_session.id, user.id, user.name)
@mq_router.server_publish_to_session(connection.music_session, user_joined, sender = {:client_id => connection.client_id})
end
end
return connection
end
def participant_delete(user, connection, music_session)
if connection.user.id != user.id
raise PermissionError, "you do not own this connection"
end
ConnectionManager.new.leave_music_session(user.id, connection.client_id, connection.music_session.id)
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, user.id, user.first_name + " " + user.last_name)
@mq_router.server_publish_to_session(music_session, user_left, sender = {:client_id => connection.client_id})
end
end
end