add recordings api actions

This commit is contained in:
Zachary Chai 2016-10-28 16:21:44 -04:00
parent fdf6f0e2c4
commit 3871e0129c
9 changed files with 129 additions and 19 deletions

View File

@ -1,17 +1,18 @@
class BbbController < ApplicationController
before_action :authorize_owner_recording, only: [:update_recordings, :delete_recordings]
# GET /:resource/:id/join
def join
if ( params[:id].blank? )
render_response("missing_parameter", "meeting token was not included", :bad_request)
elsif ( params[:name].blank? )
render_response("missing_parameter", "user name was not included", :bad_request)
if params[:name].blank?
render_bbb_response("missing_parameter", "user name was not included", :unprocessable_entity)
else
user = User.find_by username: params[:id]
options = if user
{
wait_for_moderator: true,
meeting_recorded: true,
user_is_moderator: current_user == user
}
else
@ -25,22 +26,63 @@ class BbbController < ApplicationController
options
)
if bbb_res[:returncode] && current_user && current_user == user
ActionCable.server.broadcast "moderator_#{user.username}_join_channel",
moderator: "joined"
end
render_response bbb_res[:messageKey], bbb_res[:message], bbb_res[:status], bbb_res[:response]
render_bbb_response bbb_res, bbb_res[:response]
end
end
# GET /rooms/:id/recordings
def recordings
user = User.find_by username: params[:id]
if !user
render head(:not_found) && return
end
bbb_res = helpers.bbb_get_recordings user.username
render_bbb_response bbb_res, bbb_res[:recordings]
end
# PATCH /rooms/:id/recordings/:record_id
def update_recordings
bbb_res = helpers.bbb_update_recordings(params[:record_id], params[:published] == 'true')
render_bbb_response bbb_res, bbb_res[:recordings]
end
# DELETE /rooms/:id/recordings/:record_id
def delete_recordings
byebug
bbb_res = helpers.bbb_delete_recordings(params[:record_id])
render_bbb_response bbb_res, bbb_res[:recordings]
end
private
def render_response(messageKey, message, status, response={})
@messageKey = messageKey
@message = message
@status = status
def authorize_owner_recording
user = User.find_by username: params[:id]
if !user
render head(:not_found) && return
elsif !current_user || current_user != user
render head(:unauthorized) && return
end
recordings = helpers.bbb_get_recordings(params[:record_id])[:recordings]
recordings.each do |recording|
if recording[:recordID] == params[:record_id]
return true
end
end
render head(:not_found) && return
end
def render_bbb_response(bbb_res, response)
@messageKey = bbb_res[:messageKey]
@message = bbb_res[:message]
@status = bbb_res[:status]
@response = response
render status: @status
render status: @status && return
end
end

View File

@ -7,6 +7,10 @@ module BbbHelper
Rails.application.secrets[:bbb_secret]
end
def bbb
@bbb ||= BigBlueButton::BigBlueButtonApi.new(bbb_endpoint + "api", bbb_secret, "0.8", true)
end
def random_password(length)
o = [('a'..'z'), ('A'..'Z')].map { |i| i.to_a }.flatten
password = (0...length).map { o[rand(o.length)] }.join
@ -19,7 +23,6 @@ module BbbHelper
options[:wait_for_moderator] ||= false
options[:meeting_logout_url] ||= nil
bbb ||= BigBlueButton::BigBlueButtonApi.new(bbb_endpoint + "api", bbb_secret, "0.8", true)
if !bbb
return call_invalid_res
else
@ -41,8 +44,7 @@ module BbbHelper
logout_url = options[:meeting_logout_url] || "#{request.base_url}"
moderator_password = random_password(12)
viewer_password = random_password(12)
meeting_options = {:record => options[:meeting_recorded].to_s, :logoutURL => logout_url, :moderatorPW => moderator_password, :attendeePW => viewer_password }
meeting_options = {record: options[:meeting_recorded].to_s, logoutURL: logout_url, moderatorPW: moderator_password, attendeePW: viewer_password}
# Create the meeting
bbb.create_meeting(meeting_token, meeting_id, meeting_options)
@ -65,6 +67,34 @@ module BbbHelper
end
end
def bbb_get_recordings(meeting_token)
meeting_id = (Digest::SHA1.hexdigest(Rails.application.secrets[:secret_key_base]+meeting_token)).to_s
bbb_safe_execute :get_recordings, meetingID: meeting_id
end
def bbb_update_recordings(id, published)
bbb_safe_execute :publish_recordings, id, published
end
def bbb_delete_recordings(id)
bbb_safe_execute :delete_recordings, id
end
# method must be a symbol of the method's name
def bbb_safe_execute(method, *args)
if !bbb
return call_invalid_res
else
begin
response_data = bbb.send(method, *args)
response_data[:status] = :ok
rescue BigBlueButton::BigBlueButtonException => exc
response_data = bbb_exception_res exc
end
end
response_data
end
def success_res(join_url)
{
returncode: true,
@ -94,4 +124,13 @@ module BbbHelper
status: :internal_server_error
}
end
def bbb_exception_res(exc)
{
returncode: false,
messageKey: 'BBB'+exc.key.capitalize.underscore,
message: exc.message,
status: :internal_server_error
}
end
end

View File

@ -0,0 +1,3 @@
json.messageKey messageKey
json.message message
json.status status

View File

@ -0,0 +1 @@
json.partial! 'bbb', messageKey: @messageKey, message: @message, status: @status

View File

@ -1,7 +1,5 @@
json.messageKey @messageKey
json.message @message
json.status @status
if @response
json.partial! 'bbb', messageKey: @messageKey, message: @message, status: @status
unless @response.blank?
json.response do
json.join_url(@response[:join_url]) if @response[:join_url]
end

View File

@ -0,0 +1,21 @@
json.partial! 'bbb', messageKey: @messageKey, message: @message, status: @status
unless @response.blank?
json.recordings do
unless @response.is_a? Array
@response = [@response]
end
json.array!(@response) do |recording|
json.id recording[:recordID]
json.name recording[:name]
json.start_time recording[:startTime]
json.end_time recording[:endTime]
json.published recording[:published]
json.playbacks do
json.array!(recording[:playback][:format]) do |playback|
json.type playback[:type]
json.url playback[:url]
end
end
end
end
end

View File

@ -0,0 +1 @@
json.partial! 'bbb', messageKey: @messageKey, message: @message, status: @status

View File

@ -34,5 +34,7 @@
<% end %>
</div>
<table id="recordings" class="table table-striped table-bordered dt-responsive" width="100%"></table>
</div>
</div>

View File

@ -11,8 +11,11 @@ Rails.application.routes.draw do
# meetings offer a landing page for NON authenticated users to create and join session in BigBlueButton
# rooms offer a customized landing page for authenticated users to create and join session in BigBlueButton
get '/:resource/:id', to: 'landing#index', as: :resource
get '/:resource/:id/join', to: 'bbb#join', as: :bbb_join, defaults: { :format => 'json' }
get '/:resource/:id/join', to: 'bbb#join', as: :bbb_join, defaults: {format: 'json'}
get '/:resource/:id/wait', to: 'landing#wait_for_moderator'
get '/rooms/:id/recordings', to: 'bbb#recordings', defaults: {format: 'json'}
patch '/rooms/:id/recordings/:record_id', to: 'bbb#update_recordings', defaults: {format: 'json'}
delete '/rooms/:id/recordings/:record_id', to: 'bbb#delete_recordings', defaults: {format: 'json'}
root to: 'landing#index', :resource => "meetings"
end