| Total Complexity | 81 |
| Total Lines | 347 |
| Duplicated Lines | 3.75 % |
| Changes | 6 | ||
| Bugs | 3 | Features | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like RoomsController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # frozen_string_literal: true |
||
| 19 | class RoomsController < ApplicationController |
||
| 20 | include RecordingsHelper |
||
| 21 | include Pagy::Backend |
||
| 22 | include Recorder |
||
| 23 | |||
| 24 | before_action :validate_accepted_terms, unless: -> { !Rails.configuration.terms } |
||
| 25 | before_action :validate_verified_email, except: [:show, :join], |
||
| 26 | unless: -> { !Rails.configuration.enable_email_verification } |
||
| 27 | before_action :find_room, except: [:create, :join_specific_room] |
||
| 28 | before_action :verify_room_ownership, except: [:create, :show, :join, :logout, :login, :join_specific_room] |
||
| 29 | before_action :verify_room_owner_verified, only: [:show, :join], |
||
| 30 | unless: -> { !Rails.configuration.enable_email_verification } |
||
| 31 | before_action :verify_user_not_admin, only: [:show] |
||
| 32 | |||
| 33 | # POST / |
||
| 34 | def create |
||
| 35 | redirect_to(root_path) && return unless current_user |
||
| 36 | |||
| 37 | return redirect_to current_user.main_room, flash: { alert: I18n.t("room.room_limit") } if room_limit_exceeded |
||
| 38 | |||
| 39 | @room = Room.new(name: room_params[:name], access_code: room_params[:access_code]) |
||
| 40 | @room.owner = current_user |
||
| 41 | @room.room_settings = create_room_settings_string(room_params[:mute_on_join], |
||
| 42 | room_params[:require_moderator_approval], room_params[:anyone_can_start], room_params[:all_join_moderator]) |
||
| 43 | |||
| 44 | if @room.save |
||
| 45 | logger.info("Support: #{current_user.email} has created a new room #{@room.uid}.") |
||
| 46 | |||
| 47 | if room_params[:auto_join] == "1" |
||
| 48 | start |
||
| 49 | else |
||
| 50 | flash[:success] = I18n.t("room.create_room_success") |
||
| 51 | redirect_to @room |
||
| 52 | end |
||
| 53 | else |
||
| 54 | flash[:alert] = I18n.t("room.create_room_error") |
||
| 55 | redirect_to current_user.main_room |
||
| 56 | end |
||
| 57 | end |
||
| 58 | |||
| 59 | # GET /:room_uid |
||
| 60 | def show |
||
| 61 | @is_running = @room.running? |
||
| 62 | @anyone_can_start = JSON.parse(@room[:room_settings])["anyoneCanStart"] |
||
| 63 | |||
| 64 | if current_user && @room.owned_by?(current_user) |
||
| 65 | if current_user.highest_priority_role.can_create_rooms |
||
| 66 | @search, @order_column, @order_direction, recs = |
||
| 67 | recordings(@room.bbb_id, @user_domain, params.permit(:search, :column, :direction), true) |
||
| 68 | |||
| 69 | @pagy, @recordings = pagy_array(recs) |
||
| 70 | else |
||
| 71 | @recent_rooms = Room.where(id: cookies.encrypted["#{current_user.uid}_recently_joined_rooms"]) |
||
| 72 | render :cant_create_rooms |
||
| 73 | end |
||
| 74 | else |
||
| 75 | return redirect_to root_path, flash: { alert: I18n.t("room.invalid_provider") } if incorrect_user_domain |
||
| 76 | |||
| 77 | # Get users name |
||
| 78 | @name = if current_user |
||
| 79 | current_user.name |
||
| 80 | elsif cookies.encrypted[:greenlight_name] |
||
| 81 | cookies.encrypted[:greenlight_name] |
||
| 82 | else |
||
| 83 | "" |
||
| 84 | end |
||
| 85 | |||
| 86 | @search, @order_column, @order_direction, pub_recs = |
||
| 87 | public_recordings(@room.bbb_id, @user_domain, params.permit(:search, :column, :direction), true) |
||
| 88 | |||
| 89 | @pagy, @public_recordings = pagy_array(pub_recs) |
||
| 90 | |||
| 91 | render :join |
||
| 92 | end |
||
| 93 | end |
||
| 94 | |||
| 95 | # PATCH /:room_uid |
||
| 96 | def update |
||
| 97 | if params[:setting] == "rename_block" |
||
| 98 | @room = Room.find_by!(uid: params[:room_block_uid]) |
||
| 99 | update_room_attributes("name") |
||
| 100 | elsif params[:setting] == "rename_header" |
||
| 101 | update_room_attributes("name") |
||
| 102 | elsif params[:setting] == "rename_recording" |
||
| 103 | @room.update_recording(params[:record_id], "meta_name" => params[:record_name]) |
||
| 104 | end |
||
| 105 | |||
| 106 | if request.referrer |
||
| 107 | redirect_to request.referrer |
||
| 108 | else |
||
| 109 | redirect_to room_path |
||
| 110 | end |
||
| 111 | end |
||
| 112 | |||
| 113 | # POST /:room_uid |
||
| 114 | def join |
||
| 115 | return redirect_to root_path, |
||
| 116 | flash: { alert: I18n.t("administrator.site_settings.authentication.user-info") } if auth_required |
||
| 117 | |||
| 118 | opts = default_meeting_options |
||
| 119 | unless @room.owned_by?(current_user) |
||
| 120 | # Don't allow users to join unless they have a valid access code or the room doesn't |
||
| 121 | # have an access code |
||
| 122 | if @room.access_code && [email protected]_code.empty? && @room.access_code != session[:access_code] |
||
| 123 | return redirect_to room_path(room_uid: params[:room_uid]), flash: { alert: I18n.t("room.access_code_required") } |
||
| 124 | end |
||
| 125 | |||
| 126 | # Assign join name if passed. |
||
| 127 | if params[@room.invite_path] |
||
| 128 | @join_name = params[@room.invite_path][:join_name] |
||
| 129 | elsif !params[:join_name] |
||
| 130 | # Join name not passed. |
||
| 131 | return |
||
| 132 | end |
||
| 133 | end |
||
| 134 | |||
| 135 | # create or update cookie with join name |
||
| 136 | cookies.encrypted[:greenlight_name] = @join_name unless cookies.encrypted[:greenlight_name] == @join_name |
||
| 137 | |||
| 138 | if current_user |
||
| 139 | # create or update cookie to track the three most recent rooms a user joined |
||
| 140 | recently_joined_rooms = cookies.encrypted["#{current_user.uid}_recently_joined_rooms"].to_a |
||
| 141 | cookies.encrypted["#{current_user.uid}_recently_joined_rooms"] = recently_joined_rooms.prepend(@room.id) |
||
| 142 | .uniq[0..2] |
||
| 143 | end |
||
| 144 | |||
| 145 | logger.info("Support: #{current_user.present? ? current_user.email : @join_name} is joining room #{@room.uid}") |
||
| 146 | |||
| 147 | join_room(opts) |
||
| 148 | end |
||
| 149 | |||
| 150 | # DELETE /:room_uid |
||
| 151 | def destroy |
||
| 152 | # Don't delete the users home room. |
||
| 153 | @room.destroy if @room.owned_by?(current_user) && @room != current_user.main_room |
||
| 154 | |||
| 155 | redirect_to current_user.main_room |
||
| 156 | end |
||
| 157 | |||
| 158 | # POST room/join |
||
| 159 | def join_specific_room |
||
| 160 | room_uid = params[:join_room][:url].split('/').last |
||
| 161 | |||
| 162 | begin |
||
| 163 | @room = Room.find_by(uid: room_uid) |
||
| 164 | rescue ActiveRecord::RecordNotFound |
||
| 165 | return redirect_to current_user.main_room, alert: I18n.t("room.no_room.invalid_room_uid") |
||
| 166 | end |
||
| 167 | |||
| 168 | return redirect_to current_user.main_room, alert: I18n.t("room.no_room.invalid_room_uid") if @room.nil? |
||
| 169 | |||
| 170 | redirect_to room_path(@room) |
||
| 171 | end |
||
| 172 | |||
| 173 | # POST /:room_uid/start |
||
| 174 | def start |
||
| 175 | logger.info("Support: #{current_user.email} is starting room #{@room.uid}") |
||
| 176 | |||
| 177 | # Join the user in and start the meeting. |
||
| 178 | opts = default_meeting_options |
||
| 179 | opts[:user_is_moderator] = true |
||
| 180 | |||
| 181 | # Include the user's choices for the room settings |
||
| 182 | room_settings = JSON.parse(@room[:room_settings]) |
||
| 183 | opts[:mute_on_start] = room_settings["muteOnStart"] if room_settings["muteOnStart"] |
||
| 184 | opts[:require_moderator_approval] = room_settings["requireModeratorApproval"] |
||
| 185 | |||
| 186 | begin |
||
| 187 | redirect_to @room.join_path(current_user.name, opts, current_user.uid) |
||
| 188 | rescue BigBlueButton::BigBlueButtonException => e |
||
| 189 | logger.error("Support: #{@room.uid} start failed: #{e}") |
||
| 190 | |||
| 191 | redirect_to room_path, alert: I18n.t(e.key.to_s.underscore, default: I18n.t("bigbluebutton_exception")) |
||
| 192 | end |
||
| 193 | |||
| 194 | # Notify users that the room has started. |
||
| 195 | # Delay 5 seconds to allow for server start, although the request will retry until it succeeds. |
||
| 196 | NotifyUserWaitingJob.set(wait: 5.seconds).perform_later(@room) |
||
| 197 | end |
||
| 198 | |||
| 199 | # POST /:room_uid/update_settings |
||
| 200 | def update_settings |
||
| 201 | begin |
||
| 202 | raise "Room name can't be blank" if room_params[:name].empty? |
||
| 203 | |||
| 204 | @room = Room.find_by!(uid: params[:room_uid]) |
||
| 205 | # Update the rooms settings |
||
| 206 | update_room_attributes("settings") |
||
| 207 | # Update the rooms name if it has been changed |
||
| 208 | update_room_attributes("name") if @room.name != room_params[:name] |
||
| 209 | # Update the room's access code if it has changed |
||
| 210 | update_room_attributes("access_code") if @room.access_code != room_params[:access_code] |
||
| 211 | rescue StandardError |
||
| 212 | flash[:alert] = I18n.t("room.update_settings_error") |
||
| 213 | else |
||
| 214 | flash[:success] = I18n.t("room.update_settings_success") |
||
| 215 | end |
||
| 216 | redirect_to room_path |
||
| 217 | end |
||
| 218 | |||
| 219 | # GET /:room_uid/logout |
||
| 220 | def logout |
||
| 221 | logger.info("Support: #{current_user.present? ? current_user.email : 'Guest'} has left room #{@room.uid}") |
||
| 222 | |||
| 223 | # Redirect the correct page. |
||
| 224 | redirect_to @room |
||
| 225 | end |
||
| 226 | |||
| 227 | # POST /:room_uid/login |
||
| 228 | def login |
||
| 229 | session[:access_code] = room_params[:access_code] |
||
| 230 | |||
| 231 | flash[:alert] = I18n.t("room.access_code_required") if session[:access_code] != @room.access_code |
||
| 232 | |||
| 233 | redirect_to room_path(@room.uid) |
||
| 234 | end |
||
| 235 | |||
| 236 | private |
||
| 237 | |||
| 238 | def update_room_attributes(update_type) |
||
| 239 | if @room.owned_by?(current_user) && @room != current_user.main_room |
||
| 240 | if update_type.eql? "name" |
||
| 241 | @room.update_attributes(name: params[:room_name] || room_params[:name]) |
||
| 242 | elsif update_type.eql? "settings" |
||
| 243 | room_settings_string = create_room_settings_string(room_params[:mute_on_join], |
||
| 244 | room_params[:require_moderator_approval], room_params[:anyone_can_start], room_params[:all_join_moderator]) |
||
| 245 | @room.update_attributes(room_settings: room_settings_string) |
||
| 246 | elsif update_type.eql? "access_code" |
||
| 247 | @room.update_attributes(access_code: room_params[:access_code]) |
||
| 248 | end |
||
| 249 | end |
||
| 250 | end |
||
| 251 | |||
| 252 | def create_room_settings_string(mute_res, require_approval_res, start_res, join_mod) |
||
| 253 | room_settings = {} |
||
| 254 | room_settings["muteOnStart"] = mute_res == "1" |
||
| 255 | |||
| 256 | room_settings["requireModeratorApproval"] = require_approval_res == "1" |
||
| 257 | |||
| 258 | room_settings["anyoneCanStart"] = start_res == "1" |
||
| 259 | |||
| 260 | room_settings["joinModerator"] = join_mod == "1" |
||
| 261 | |||
| 262 | room_settings.to_json |
||
| 263 | end |
||
| 264 | |||
| 265 | def room_params |
||
| 266 | params.require(:room).permit(:name, :auto_join, :mute_on_join, :access_code, |
||
| 267 | :require_moderator_approval, :anyone_can_start, :all_join_moderator) |
||
| 268 | end |
||
| 269 | |||
| 270 | # Find the room from the uid. |
||
| 271 | def find_room |
||
| 272 | @room = Room.find_by!(uid: params[:room_uid]) |
||
| 273 | end |
||
| 274 | |||
| 275 | # Ensure the user is logged into the room they are accessing. |
||
| 276 | def verify_room_ownership |
||
| 277 | bring_to_room unless @room.owned_by?(current_user) |
||
| 278 | end |
||
| 279 | |||
| 280 | # Redirects a user to their room. |
||
| 281 | def bring_to_room |
||
| 282 | if current_user |
||
| 283 | # Redirect authenticated users to their room. |
||
| 284 | redirect_to room_path(current_user.main_room) |
||
| 285 | else |
||
| 286 | # Redirect unauthenticated users to root. |
||
| 287 | redirect_to root_path |
||
| 288 | end |
||
| 289 | end |
||
| 290 | |||
| 291 | def validate_accepted_terms |
||
| 292 | if current_user |
||
| 293 | redirect_to terms_path unless current_user.accepted_terms |
||
| 294 | end |
||
| 295 | end |
||
| 296 | |||
| 297 | def validate_verified_email |
||
| 298 | if current_user |
||
| 299 | redirect_to account_activation_path(current_user) unless current_user.activated? |
||
| 300 | end |
||
| 301 | end |
||
| 302 | |||
| 303 | def verify_room_owner_verified |
||
| 304 | unless @room.owner.activated? |
||
| 305 | flash[:alert] = t("room.unavailable") |
||
| 306 | |||
| 307 | if current_user && [email protected]_by?(current_user) |
||
| 308 | redirect_to current_user.main_room |
||
| 309 | else |
||
| 310 | redirect_to root_path |
||
| 311 | end |
||
| 312 | end |
||
| 313 | end |
||
| 314 | |||
| 315 | def verify_user_not_admin |
||
| 316 | redirect_to admins_path if current_user && current_user&.has_role?(:super_admin) |
||
| 317 | end |
||
| 318 | |||
| 319 | View Code Duplication | def auth_required |
|
|
|
|||
| 320 | Setting.find_or_create_by!(provider: user_settings_provider).get_value("Room Authentication") == "true" && |
||
| 321 | current_user.nil? |
||
| 322 | end |
||
| 323 | |||
| 324 | View Code Duplication | def room_limit_exceeded |
|
| 325 | limit = Setting.find_or_create_by!(provider: user_settings_provider).get_value("Room Limit").to_i |
||
| 326 | |||
| 327 | # Does not apply to admin |
||
| 328 | # 15+ option is used as unlimited |
||
| 329 | return false if current_user&.has_role?(:admin) || limit == 15 |
||
| 330 | |||
| 331 | current_user.rooms.count >= limit |
||
| 332 | end |
||
| 333 | |||
| 334 | def join_room(opts) |
||
| 335 | room_settings = JSON.parse(@room[:room_settings]) |
||
| 336 | |||
| 337 | if @room.running? || @room.owned_by?(current_user) || room_settings["anyoneCanStart"] |
||
| 338 | |||
| 339 | # Determine if the user needs to join as a moderator. |
||
| 340 | opts[:user_is_moderator] = @room.owned_by?(current_user) || room_settings["joinModerator"] |
||
| 341 | |||
| 342 | opts[:require_moderator_approval] = room_settings["requireModeratorApproval"] |
||
| 343 | |||
| 344 | if current_user |
||
| 345 | redirect_to @room.join_path(current_user.name, opts, current_user.uid) |
||
| 346 | else |
||
| 347 | join_name = params[:join_name] || params[@room.invite_path][:join_name] |
||
| 348 | redirect_to @room.join_path(join_name, opts) |
||
| 349 | end |
||
| 350 | else |
||
| 351 | search_params = params[@room.invite_path] || params |
||
| 352 | @search, @order_column, @order_direction, pub_recs = |
||
| 353 | public_recordings(@room.bbb_id, @user_domain, search_params.permit(:search, :column, :direction), true) |
||
| 354 | |||
| 355 | @pagy, @public_recordings = pagy_array(pub_recs) |
||
| 356 | |||
| 357 | # They need to wait until the meeting begins. |
||
| 358 | render :wait |
||
| 359 | end |
||
| 360 | end |
||
| 361 | |||
| 362 | def incorrect_user_domain |
||
| 363 | Rails.configuration.loadbalanced_configuration && @room.owner.provider != @user_domain |
||
| 364 | end |
||
| 365 | end |
||
| 366 |