From 1811a8e3016c3a8a38fcbfaaf2c8c3a25dc2d33a Mon Sep 17 00:00:00 2001 From: Jesus Federico Date: Thu, 11 Apr 2024 16:28:49 -0400 Subject: [PATCH] LTI-321: Pull Default Room Setting Values From Broker (feat) (#299) * LTI-321: Pull Default Room Setting Values From Broker * LTI-321: Fixed errors * LTI-321: Updated initialize_setting_defaults to make the code cleaner * LTI-321: Fixed errors presented by rubocop * LTI-321: Fixed Rspec errors * LTI-321: Fixed default settings not appearing if room_setting_defaults is not applied * LTI-321: Fixed Rubocop offense --------- Co-authored-by: ahmed-abdou1 --- app/controllers/concerns/broker_helper.rb | 4 ++ app/models/room.rb | 54 +++++++++++++++++------ 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/app/controllers/concerns/broker_helper.rb b/app/controllers/concerns/broker_helper.rb index 89833b89..207215d0 100644 --- a/app/controllers/concerns/broker_helper.rb +++ b/app/controllers/concerns/broker_helper.rb @@ -49,4 +49,8 @@ def hide_build_tag(tenant) def bbb_moderator_roles_params(tenant) tenant_settings(tenant: tenant)&.[]('settings')&.[]('bigbluebutton_moderator_roles')&.split(',') end + + def room_setting_defaults(tenant) + tenant_settings(tenant: tenant)&.dig('settings', 'room_setting_defaults') + end end diff --git a/app/models/room.rb b/app/models/room.rb index a22d8056..17ea05f8 100644 --- a/app/models/room.rb +++ b/app/models/room.rb @@ -28,6 +28,8 @@ class Room < ApplicationRecord attr_accessor :can_grade + include BrokerHelper + RECORDING_SETTINGS = [:record, :autoStartRecording, :allowStartStopRecording].freeze CODE_LENGTH = 10 @@ -84,20 +86,44 @@ def delete_settings end def initialize_setting_defaults - self.lockSettingsDisableCam = '0' unless lockSettingsDisableCam_changed? - self.lockSettingsDisableMic = '0' unless lockSettingsDisableMic_changed? - self.lockSettingsDisablePrivateChat = '0' unless lockSettingsDisablePrivateChat_changed? - self.lockSettingsDisablePublicChat = '0' unless lockSettingsDisablePublicChat_changed? - self.lockSettingsDisableNote = '0' unless lockSettingsDisableNote_changed? - self.autoStartRecording = '0' unless autoStartRecording_changed? - self.allowStartStopRecording = '1' unless allowStartStopRecording_changed? - - # these settings existed as their own column in the db - # therefore we take the value in that column if it already exists - # this is done to ensure previous values are not overwritten. - self.waitForModerator = wait_moderator.nil? ? '1' : bool_to_binary(wait_moderator) unless waitForModerator_changed? - self.allModerators = all_moderators.nil? ? '0' : bool_to_binary(all_moderators) unless allModerators_changed? - self.record = record.nil? ? '1' : bool_to_binary(record) unless record_changed? + # get the key value pair from the broker using the room_setting_defaults function + room_settings = room_setting_defaults(tenant) + + # Define default values + defaults = { + lockSettingsDisableCam: '0', + lockSettingsDisableMic: '0', + lockSettingsDisablePrivateChat: '0', + lockSettingsDisablePublicChat: '0', + lockSettingsDisableNote: '0', + autoStartRecording: '0', + allowStartStopRecording: '1', + waitForModerator: '1', + allModerators: '0', + record: '1', + } + + if room_settings.blank? + # If room_settings is not present or null, assign defaults directly + defaults.each do |key, value| + send("#{key}=", value) unless send("#{key}_changed?") + end + else + # Parse the values using the parse_defaults function + parsed_defaults = parse_defaults(room_settings) + + # Iterate over default values and set them using send method + defaults.each do |key, value| + send("#{key}=", parsed_defaults.fetch(key, value)) unless send("#{key}_changed?") + end + end + end + + def parse_defaults(defaults_str) + defaults_str.gsub(/[{}]/, '').split(',').map do |pair| + key, value = pair.split(':') + [key.strip.to_sym, value.strip] + end.to_h end def bool_to_binary(value)