Total Complexity | 72 |
Total Lines | 298 |
Duplicated Lines | 4.36 % |
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], |
||
42 | 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 | |||
151 | begin |
||
152 | redirect_to @room.join_path(current_user.name, opts, current_user.uid) |
||
153 | rescue BigBlueButton::BigBlueButtonException => e |
||
154 | redirect_to room_path, alert: I18n.t(e.key.to_s.underscore, default: I18n.t("bigbluebutton_exception")) |
||
155 | end |
||
156 | |||
157 | # Notify users that the room has started. |
||
158 | # Delay 5 seconds to allow for server start, although the request will retry until it succeeds. |
||
159 | NotifyUserWaitingJob.set(wait: 5.seconds).perform_later(@room) |
||
160 | end |
||
161 | |||
162 | # POST /:room_uid/update_settings |
||
163 | def update_settings |
||
164 | begin |
||
165 | raise "Room name can't be blank" if room_params[:name].empty? |
||
166 | |||
167 | @room = Room.find_by!(uid: params[:room_uid]) |
||
168 | # Update the rooms settings |
||
169 | update_room_attributes("settings") |
||
170 | # Update the rooms name if it has been changed |
||
171 | update_room_attributes("name") if @room.name != room_params[:name] |
||
172 | # Update the room's access code if it has changed |
||
173 | update_room_attributes("access_code") if @room.access_code != room_params[:access_code] |
||
174 | rescue StandardError |
||
175 | flash[:alert] = I18n.t("room.update_settings_error") |
||
176 | else |
||
177 | flash[:success] = I18n.t("room.update_settings_success") |
||
178 | end |
||
179 | redirect_to room_path |
||
180 | end |
||
181 | |||
182 | # GET /:room_uid/logout |
||
183 | def logout |
||
184 | # Redirect the correct page. |
||
185 | redirect_to @room |
||
186 | end |
||
187 | |||
188 | # POST /:room_uid/login |
||
189 | def login |
||
190 | session[:access_code] = room_params[:access_code] |
||
191 | |||
192 | flash[:alert] = I18n.t("room.access_code_required") if session[:access_code] != @room.access_code |
||
193 | |||
194 | redirect_to room_path(@room.uid) |
||
195 | end |
||
196 | |||
197 | private |
||
198 | |||
199 | def update_room_attributes(update_type) |
||
200 | if @room.owned_by?(current_user) && @room != current_user.main_room |
||
201 | if update_type.eql? "name" |
||
202 | @room.update_attributes(name: params[:room_name] || room_params[:name]) |
||
203 | elsif update_type.eql? "settings" |
||
204 | room_settings_string = create_room_settings_string(room_params[:mute_on_join], |
||
205 | room_params[:anyone_can_start]) |
||
206 | @room.update_attributes(room_settings: room_settings_string) |
||
207 | elsif update_type.eql? "access_code" |
||
208 | @room.update_attributes(access_code: room_params[:access_code]) |
||
209 | end |
||
210 | end |
||
211 | end |
||
212 | |||
213 | def create_room_settings_string(mute_res, start_res) |
||
214 | room_settings = {} |
||
215 | room_settings["muteOnStart"] = mute_res == "1" |
||
216 | |||
217 | room_settings["anyoneCanStart"] = start_res == "1" |
||
218 | |||
219 | room_settings.to_json |
||
220 | end |
||
221 | |||
222 | def room_params |
||
223 | params.require(:room).permit(:name, :auto_join, :mute_on_join, :access_code, :anyone_can_start) |
||
224 | end |
||
225 | |||
226 | # Find the room from the uid. |
||
227 | def find_room |
||
228 | @room = Room.find_by!(uid: params[:room_uid]) |
||
229 | end |
||
230 | |||
231 | # Ensure the user is logged into the room they are accessing. |
||
232 | def verify_room_ownership |
||
233 | bring_to_room unless @room.owned_by?(current_user) |
||
234 | end |
||
235 | |||
236 | # Redirects a user to their room. |
||
237 | def bring_to_room |
||
238 | if current_user |
||
239 | # Redirect authenticated users to their room. |
||
240 | redirect_to room_path(current_user.main_room) |
||
241 | else |
||
242 | # Redirect unauthenticated users to root. |
||
243 | redirect_to root_path |
||
244 | end |
||
245 | end |
||
246 | |||
247 | def validate_accepted_terms |
||
248 | if current_user |
||
249 | redirect_to terms_path unless current_user.accepted_terms |
||
250 | end |
||
251 | end |
||
252 | |||
253 | def validate_verified_email |
||
254 | if current_user |
||
255 | redirect_to account_activation_path(current_user) unless current_user.activated? |
||
256 | end |
||
257 | end |
||
258 | |||
259 | def verify_room_owner_verified |
||
260 | unless @room.owner.activated? |
||
261 | flash[:alert] = t("room.unavailable") |
||
262 | |||
263 | if current_user && [email protected]_by?(current_user) |
||
264 | redirect_to current_user.main_room |
||
265 | else |
||
266 | redirect_to root_path |
||
267 | end |
||
268 | end |
||
269 | end |
||
270 | |||
271 | def verify_user_not_admin |
||
272 | redirect_to admins_path if current_user && current_user&.has_cached_role?(:super_admin) |
||
273 | end |
||
274 | |||
275 | View Code Duplication | def auth_required |
|
|
|||
276 | Setting.find_or_create_by!(provider: user_settings_provider).get_value("Room Authentication") == "true" && |
||
277 | current_user.nil? |
||
278 | end |
||
279 | |||
280 | View Code Duplication | def room_limit_exceeded |
|
281 | limit = Setting.find_or_create_by!(provider: user_settings_provider).get_value("Room Limit").to_i |
||
282 | |||
283 | # Does not apply to admin |
||
284 | # 15+ option is used as unlimited |
||
285 | return false if current_user&.has_cached_role?(:admin) || limit == 15 |
||
286 | |||
287 | current_user.rooms.count >= limit |
||
288 | end |
||
289 | |||
290 | def join_room(opts) |
||
291 | room_settings = JSON.parse(@room[:room_settings]) |
||
292 | |||
293 | if @room.running? || @room.owned_by?(current_user) || room_settings["anyoneCanStart"] |
||
294 | |||
295 | # Determine if the user needs to join as a moderator. |
||
296 | opts[:user_is_moderator] = @room.owned_by?(current_user) || |
||
297 | (room_settings["anyoneCanStart"] && [email protected]?) |
||
298 | |||
299 | if current_user |
||
300 | redirect_to @room.join_path(current_user.name, opts, current_user.uid) |
||
301 | else |
||
302 | join_name = params[:join_name] || params[@room.invite_path][:join_name] |
||
303 | redirect_to @room.join_path(join_name, opts) |
||
304 | end |
||
305 | else |
||
306 | search_params = params[@room.invite_path] || params |
||
307 | @search, @order_column, @order_direction, pub_recs = |
||
308 | public_recordings(@room.bbb_id, @user_domain, search_params.permit(:search, :column, :direction), true) |
||
309 | |||
310 | @pagy, @public_recordings = pagy_array(pub_recs) |
||
311 | |||
312 | # They need to wait until the meeting begins. |
||
313 | render :wait |
||
314 | end |
||
315 | end |
||
316 | end |
||
317 |