| Total Complexity | 76 |
| Total Lines | 313 |
| Duplicated Lines | 4.15 % |
| 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 |
||
| 28 | before_action :verify_room_ownership, except: [:create, :show, :join, :logout, :login] |
||
| 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], room_params[:client], |
||
| 42 | room_params[:require_moderator_approval], room_params[:anyone_can_start]) |
||
| 43 | |||
| 44 | if @room.save |
||
| 45 | if room_params[:auto_join] == "1" |
||
| 46 | start |
||
| 47 | else |
||
| 48 | flash[:success] = I18n.t("room.create_room_success") |
||
| 49 | redirect_to @room |
||
| 50 | end |
||
| 51 | else |
||
| 52 | flash[:alert] = I18n.t("room.create_room_error") |
||
| 53 | redirect_to current_user.main_room |
||
| 54 | end |
||
| 55 | end |
||
| 56 | |||
| 57 | # GET /:room_uid |
||
| 58 | def show |
||
| 59 | @is_running = @room.running? |
||
| 60 | @anyone_can_start = JSON.parse(@room[:room_settings])["anyoneCanStart"] |
||
| 61 | |||
| 62 | if current_user && @room.owned_by?(current_user) |
||
| 63 | @search, @order_column, @order_direction, recs = |
||
| 64 | recordings(@room.bbb_id, @user_domain, params.permit(:search, :column, :direction), true) |
||
| 65 | |||
| 66 | @pagy, @recordings = pagy_array(recs) |
||
| 67 | |||
| 68 | else |
||
| 69 | # Get users name |
||
| 70 | @name = if current_user |
||
| 71 | current_user.name |
||
| 72 | elsif cookies.encrypted[:greenlight_name] |
||
| 73 | cookies.encrypted[:greenlight_name] |
||
| 74 | else |
||
| 75 | "" |
||
| 76 | end |
||
| 77 | |||
| 78 | @search, @order_column, @order_direction, pub_recs = |
||
| 79 | public_recordings(@room.bbb_id, @user_domain, params.permit(:search, :column, :direction), true) |
||
| 80 | |||
| 81 | @pagy, @public_recordings = pagy_array(pub_recs) |
||
| 82 | |||
| 83 | render :join |
||
| 84 | end |
||
| 85 | end |
||
| 86 | |||
| 87 | # PATCH /:room_uid |
||
| 88 | def update |
||
| 89 | if params[:setting] == "rename_block" |
||
| 90 | @room = Room.find_by!(uid: params[:room_block_uid]) |
||
| 91 | update_room_attributes("name") |
||
| 92 | elsif params[:setting] == "rename_header" |
||
| 93 | update_room_attributes("name") |
||
| 94 | elsif params[:setting] == "rename_recording" |
||
| 95 | @room.update_recording(params[:record_id], "meta_name" => params[:record_name]) |
||
| 96 | end |
||
| 97 | |||
| 98 | if request.referrer |
||
| 99 | redirect_to request.referrer |
||
| 100 | else |
||
| 101 | redirect_to room_path |
||
| 102 | end |
||
| 103 | end |
||
| 104 | |||
| 105 | # POST /:room_uid |
||
| 106 | def join |
||
| 107 | return redirect_to root_path, |
||
| 108 | flash: { alert: I18n.t("administrator.site_settings.authentication.user-info") } if auth_required |
||
| 109 | |||
| 110 | opts = default_meeting_options |
||
| 111 | unless @room.owned_by?(current_user) |
||
| 112 | # Don't allow users to join unless they have a valid access code or the room doesn't |
||
| 113 | # have an access code |
||
| 114 | if @room.access_code && [email protected]_code.empty? && @room.access_code != session[:access_code] |
||
| 115 | return redirect_to room_path(room_uid: params[:room_uid]), flash: { alert: I18n.t("room.access_code_required") } |
||
| 116 | end |
||
| 117 | |||
| 118 | # Assign join name if passed. |
||
| 119 | if params[@room.invite_path] |
||
| 120 | @join_name = params[@room.invite_path][:join_name] |
||
| 121 | elsif !params[:join_name] |
||
| 122 | # Join name not passed. |
||
| 123 | return |
||
| 124 | end |
||
| 125 | end |
||
| 126 | |||
| 127 | # create or update cookie with join name |
||
| 128 | cookies.encrypted[:greenlight_name] = @join_name unless cookies.encrypted[:greenlight_name] == @join_name |
||
| 129 | |||
| 130 | join_room(opts) |
||
| 131 | end |
||
| 132 | |||
| 133 | # DELETE /:room_uid |
||
| 134 | def destroy |
||
| 135 | # Don't delete the users home room. |
||
| 136 | @room.destroy if @room.owned_by?(current_user) && @room != current_user.main_room |
||
| 137 | |||
| 138 | redirect_to current_user.main_room |
||
| 139 | end |
||
| 140 | |||
| 141 | # POST /:room_uid/start |
||
| 142 | def start |
||
| 143 | # Join the user in and start the meeting. |
||
| 144 | opts = default_meeting_options |
||
| 145 | opts[:user_is_moderator] = true |
||
| 146 | |||
| 147 | # Include the user's choices for the room settings |
||
| 148 | room_settings = JSON.parse(@room[:room_settings]) |
||
| 149 | opts[:mute_on_start] = room_settings["muteOnStart"] if room_settings["muteOnStart"] |
||
| 150 | opts[:join_via_html5] = room_settings["joinViaHtml5"] if room_settings["joinViaHtml5"] |
||
| 151 | opts[:require_moderator_approval] = room_settings["requireModeratorApproval"] |
||
| 152 | |||
| 153 | begin |
||
| 154 | redirect_to @room.join_path(current_user.name, opts, current_user.uid) |
||
| 155 | rescue BigBlueButton::BigBlueButtonException => e |
||
| 156 | redirect_to room_path, alert: I18n.t(e.key.to_s.underscore, default: I18n.t("bigbluebutton_exception")) |
||
| 157 | end |
||
| 158 | |||
| 159 | # Notify users that the room has started. |
||
| 160 | # Delay 5 seconds to allow for server start, although the request will retry until it succeeds. |
||
| 161 | NotifyUserWaitingJob.set(wait: 5.seconds).perform_later(@room) |
||
| 162 | end |
||
| 163 | |||
| 164 | # POST /:room_uid/update_settings |
||
| 165 | def update_settings |
||
| 166 | begin |
||
| 167 | raise "Room name can't be blank" if room_params[:name].empty? |
||
| 168 | |||
| 169 | @room = Room.find_by!(uid: params[:room_uid]) |
||
| 170 | # Update the rooms settings |
||
| 171 | update_room_attributes("settings") |
||
| 172 | # Update the rooms name if it has been changed |
||
| 173 | update_room_attributes("name") if @room.name != room_params[:name] |
||
| 174 | # Update the room's access code if it has changed |
||
| 175 | update_room_attributes("access_code") if @room.access_code != room_params[:access_code] |
||
| 176 | rescue StandardError |
||
| 177 | flash[:alert] = I18n.t("room.update_settings_error") |
||
| 178 | else |
||
| 179 | flash[:success] = I18n.t("room.update_settings_success") |
||
| 180 | end |
||
| 181 | redirect_to room_path |
||
| 182 | end |
||
| 183 | |||
| 184 | # GET /:room_uid/logout |
||
| 185 | def logout |
||
| 186 | # Redirect the correct page. |
||
| 187 | redirect_to @room |
||
| 188 | end |
||
| 189 | |||
| 190 | # POST /:room_uid/login |
||
| 191 | def login |
||
| 192 | session[:access_code] = room_params[:access_code] |
||
| 193 | |||
| 194 | flash[:alert] = I18n.t("room.access_code_required") if session[:access_code] != @room.access_code |
||
| 195 | |||
| 196 | redirect_to room_path(@room.uid) |
||
| 197 | end |
||
| 198 | |||
| 199 | private |
||
| 200 | |||
| 201 | def update_room_attributes(update_type) |
||
| 202 | if @room.owned_by?(current_user) && @room != current_user.main_room |
||
| 203 | if update_type.eql? "name" |
||
| 204 | @room.update_attributes(name: params[:room_name] || room_params[:name]) |
||
| 205 | elsif update_type.eql? "settings" |
||
| 206 | room_settings_string = create_room_settings_string(room_params[:mute_on_join], room_params[:client], |
||
| 207 | room_params[:require_moderator_approval], room_params[:anyone_can_start]) |
||
| 208 | @room.update_attributes(room_settings: room_settings_string) |
||
| 209 | elsif update_type.eql? "access_code" |
||
| 210 | @room.update_attributes(access_code: room_params[:access_code]) |
||
| 211 | end |
||
| 212 | end |
||
| 213 | end |
||
| 214 | |||
| 215 | def create_room_settings_string(mute_res, client_res, require_approval_res, start_res) |
||
| 216 | room_settings = {} |
||
| 217 | room_settings["muteOnStart"] = mute_res == "1" |
||
| 218 | |||
| 219 | room_settings["requireModeratorApproval"] = require_approval_res == "1" |
||
| 220 | |||
| 221 | if client_res.eql? "html5" |
||
| 222 | room_settings["joinViaHtml5"] = true |
||
| 223 | elsif client_res.eql? "flash" |
||
| 224 | room_settings["joinViaHtml5"] = false |
||
| 225 | end |
||
| 226 | |||
| 227 | room_settings["anyoneCanStart"] = start_res == "1" |
||
| 228 | |||
| 229 | room_settings.to_json |
||
| 230 | end |
||
| 231 | |||
| 232 | def room_params |
||
| 233 | params.require(:room).permit(:name, :auto_join, :mute_on_join, :client, :access_code, |
||
| 234 | :require_moderator_approval, :anyone_can_start) |
||
| 235 | end |
||
| 236 | |||
| 237 | # Find the room from the uid. |
||
| 238 | def find_room |
||
| 239 | @room = Room.find_by!(uid: params[:room_uid]) |
||
| 240 | end |
||
| 241 | |||
| 242 | # Ensure the user is logged into the room they are accessing. |
||
| 243 | def verify_room_ownership |
||
| 244 | bring_to_room unless @room.owned_by?(current_user) |
||
| 245 | end |
||
| 246 | |||
| 247 | # Redirects a user to their room. |
||
| 248 | def bring_to_room |
||
| 249 | if current_user |
||
| 250 | # Redirect authenticated users to their room. |
||
| 251 | redirect_to room_path(current_user.main_room) |
||
| 252 | else |
||
| 253 | # Redirect unauthenticated users to root. |
||
| 254 | redirect_to root_path |
||
| 255 | end |
||
| 256 | end |
||
| 257 | |||
| 258 | def validate_accepted_terms |
||
| 259 | if current_user |
||
| 260 | redirect_to terms_path unless current_user.accepted_terms |
||
| 261 | end |
||
| 262 | end |
||
| 263 | |||
| 264 | def validate_verified_email |
||
| 265 | if current_user |
||
| 266 | redirect_to account_activation_path(current_user) unless current_user.activated? |
||
| 267 | end |
||
| 268 | end |
||
| 269 | |||
| 270 | def verify_room_owner_verified |
||
| 271 | unless @room.owner.activated? |
||
| 272 | flash[:alert] = t("room.unavailable") |
||
| 273 | |||
| 274 | if current_user && [email protected]_by?(current_user) |
||
| 275 | redirect_to current_user.main_room |
||
| 276 | else |
||
| 277 | redirect_to root_path |
||
| 278 | end |
||
| 279 | end |
||
| 280 | end |
||
| 281 | |||
| 282 | def verify_user_not_admin |
||
| 283 | redirect_to admins_path if current_user && current_user&.has_cached_role?(:super_admin) |
||
| 284 | end |
||
| 285 | |||
| 286 | View Code Duplication | def auth_required |
|
|
|
|||
| 287 | Setting.find_or_create_by!(provider: user_settings_provider).get_value("Room Authentication") == "true" && |
||
| 288 | current_user.nil? |
||
| 289 | end |
||
| 290 | |||
| 291 | View Code Duplication | def room_limit_exceeded |
|
| 292 | limit = Setting.find_or_create_by!(provider: user_settings_provider).get_value("Room Limit").to_i |
||
| 293 | |||
| 294 | # Does not apply to admin |
||
| 295 | # 15+ option is used as unlimited |
||
| 296 | return false if current_user&.has_cached_role?(:admin) || limit == 15 |
||
| 297 | |||
| 298 | current_user.rooms.count >= limit |
||
| 299 | end |
||
| 300 | |||
| 301 | def join_room(opts) |
||
| 302 | room_settings = JSON.parse(@room[:room_settings]) |
||
| 303 | |||
| 304 | if @room.running? || @room.owned_by?(current_user) || room_settings["anyoneCanStart"] |
||
| 305 | |||
| 306 | # Determine if the user needs to join as a moderator. |
||
| 307 | opts[:user_is_moderator] = @room.owned_by?(current_user) || |
||
| 308 | (room_settings["anyoneCanStart"] && [email protected]?) |
||
| 309 | |||
| 310 | # Check if the user has specified which client to use |
||
| 311 | opts[:join_via_html5] = room_settings["joinViaHtml5"] if room_settings["joinViaHtml5"] |
||
| 312 | opts[:require_moderator_approval] = room_settings["requireModeratorApproval"] |
||
| 313 | |||
| 314 | if current_user |
||
| 315 | redirect_to @room.join_path(current_user.name, opts, current_user.uid) |
||
| 316 | else |
||
| 317 | join_name = params[:join_name] || params[@room.invite_path][:join_name] |
||
| 318 | redirect_to @room.join_path(join_name, opts) |
||
| 319 | end |
||
| 320 | else |
||
| 321 | search_params = params[@room.invite_path] || params |
||
| 322 | @search, @order_column, @order_direction, pub_recs = |
||
| 323 | public_recordings(@room.bbb_id, @user_domain, search_params.permit(:search, :column, :direction), true) |
||
| 324 | |||
| 325 | @pagy, @public_recordings = pagy_array(pub_recs) |
||
| 326 | |||
| 327 | # They need to wait until the meeting begins. |
||
| 328 | render :wait |
||
| 329 | end |
||
| 330 | end |
||
| 331 | end |
||
| 332 |