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