From 4fdbd9feec2b2db1bc617827c2e7d334ce1fa2b4 Mon Sep 17 00:00:00 2001 From: Jesus Federico Date: Thu, 16 Feb 2023 22:37:27 +0100 Subject: [PATCH] LTI-208: fetch record settings for legacy rooms from external app (#196) --- app/controllers/rooms_controller.rb | 68 +++++++++++++++++------------ config/application.rb | 4 ++ dotenv | 8 +++- 3 files changed, 51 insertions(+), 29 deletions(-) diff --git a/app/controllers/rooms_controller.rb b/app/controllers/rooms_controller.rb index b10ea629..5c39bc45 100644 --- a/app/controllers/rooms_controller.rb +++ b/app/controllers/rooms_controller.rb @@ -16,8 +16,10 @@ # You should have received a copy of the GNU Lesser General Public License along # with BigBlueButton; if not, see . -require 'bbb_app_rooms/user' require 'json' +require 'uri' +require 'bbb_app_rooms/user' +require 'net/http' class RoomsController < ApplicationController # Include libraries. @@ -257,13 +259,15 @@ def set_launch # Continue through happy path. @tenant = session_params['tenant'] - resource_handler = Digest::SHA1.hexdigest("rooms#{@tenant}#{launch_params['resource_link_id']}") - to_new_room_params = launch_params_to_new_room_params(launch_params) - @room = Room.find_by(handler: resource_handler, tenant: @tenant) + handler = Digest::SHA1.hexdigest("rooms#{@tenant}#{launch_params['resource_link_id']}") + new_room_params = launch_params_to_new_room_params(launch_params) + @room = Room.find_by(handler: handler, tenant: @tenant) if @room - @room.update(to_new_room_params) + @room.update(new_room_params) else - @room = Room.create(to_new_room_params) + # Overrides with fetched parameters if legacy api is enabled + new_room_params = fetch_new_room_params(new_room_params) if Rails.configuration.handler_legacy_api_enabled + @room = Room.create(new_room_params) end user_params = launch_params_to_new_user_params(launch_params) @@ -286,32 +290,42 @@ def room_params ) end - def new_room_params(name, description, recording, wait_moderator, all_moderators, hide_name, hide_description, handler_legacy, settings) + def launch_params_to_new_room_params(launch_params) params.permit.merge( - name: name, - description: description, + name: launch_params['resource_link_title'] || t('default.room.room'), + description: launch_params['resource_link_description'] || '', welcome: '', - recording: recording || true, - wait_moderator: wait_moderator || false, - all_moderators: all_moderators || false, - hide_name: hide_name || false, - hide_description: hide_description || false, - handler_legacy: handler_legacy, - settings: settings || {} + recording: launch_params['custom_params'].key?('custom_record') ? launch_params['custom_params']['custom_record'] : true, + wait_moderator: message_has_custom?(launch_params, 'wait_moderator') || false, + all_moderators: message_has_custom?(launch_params, 'all_moderators') || false, + hide_name: message_has_custom?(launch_params, 'hide_name') || false, + hide_description: message_has_custom?(launch_params, 'hide_description') || false, + handler_legacy: launch_params['custom_params'].key?('custom_handler_legacy') ? launch_params['custom_params']['custom_handler_legacy'] : nil, + settings: message_has_custom?(launch_params, 'settings') || {} ) end - def launch_params_to_new_room_params(launch_params) - name = launch_params['resource_link_title'] || t('default.room.room') - description = launch_params['resource_link_description'] || '' - record = launch_params['custom_params'].key?('custom_record') ? launch_params['custom_params']['custom_record'] : true - wait_moderator = message_has_custom?(launch_params, 'wait_moderator') - all_moderators = message_has_custom?(launch_params, 'all_moderators') - hide_name = message_has_custom?(launch_params, 'hide_name') - hide_description = message_has_custom?(launch_params, 'hide_description') - handler_legacy = launch_params['custom_params'].key?('custom_handler_legacy') ? launch_params['custom_params']['custom_handler_legacy'] : nil - settings = message_has_custom?(launch_params, 'settings') - new_room_params(name, description, record, wait_moderator, all_moderators, hide_name, hide_description, handler_legacy, settings) + def fetch_new_room_params(new_room_params) + handler_legacy = new_room_params.key?('handler_legacy') ? new_room_params['handler_legacy'] : nil + handler_legacy_api_url = "#{Rails.configuration.handler_legacy_api_endpoint}rooms/#{handler_legacy}" + checksum = Digest::SHA256.hexdigest(handler_legacy_api_url + Rails.configuration.handler_legacy_api_secret) + uri = URI("#{handler_legacy_api_url}?checksum=#{checksum}") + res = Net::HTTP.get_response(uri) + return new_room_params unless res.is_a?(Net::HTTPSuccess) + + room = JSON.parse(res.body) + params.permit.merge( + name: room['name'], + description: room['description'], + welcome: room['welcome'], + recording: room['recording'], + wait_moderator: room['wait_moderator'], + handler_legacy: handler_legacy, + settings: { + waitForModerator: room['wait_moderator'], + record: room['recording'], + } + ) end def launch_params_to_new_user_params(launch_params) diff --git a/config/application.rb b/config/application.rb index 31c11121..abb18455 100644 --- a/config/application.rb +++ b/config/application.rb @@ -71,5 +71,9 @@ class Application < Rails::Application checksum_env_var = ENV['BIGBLUEBUTTON_CHECKSUM_ALGORITHM'] null_or_invalid = checksum_env_var.nil? || !valid_sha_versions.include?(checksum_env_var.downcase) config.checksum_algorithm = null_or_invalid ? 'sha1' : checksum_env_var.downcase + + config.handler_legacy_api_endpoint = ENV['HANDLER_LEGACY_API_ENDPOINT'] + config.handler_legacy_api_secret = ENV['HANDLER_LEGACY_API_SECRET'] + config.handler_legacy_api_enabled = (config.handler_legacy_api_endpoint && config.handler_legacy_api_secret) end end diff --git a/dotenv b/dotenv index 3f63ee30..09cb670e 100644 --- a/dotenv +++ b/dotenv @@ -76,5 +76,9 @@ RAILS_SERVE_STATIC_FILES=true # LOG_LEVEL=warn ## SHA algorithm to use for checksum. sha1 is default -## BIGBLUEBUTTON_CHECKSUM_ALGORITHM: "SHA1|SHA256|SHA512" -# BIGBLUEBUTTON_CHECKSUM_ALGORITHM='sha256' +## BIGBLUEBUTTON_CHECKSUM_ALGORITHM: "SHA1|SHA256|SHA512" +# BIGBLUEBUTTON_CHECKSUM_ALGORITHM='sha256' + +## Define endpoint for fetching data for rooms based on legacy_handler +# HANDLER_LEGACY_API_ENDPOINT= +# HANDLER_LEGACY_API_SECRET=