GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#515)
by Jesus
06:56 queued 03:19
created

RoomsController   D

Complexity

Total Complexity 58

Size/Duplication

Total Lines 240
Duplicated Lines 0 %

Importance

Changes 4
Bugs 3 Features 0
Metric Value
dl 0
loc 240
rs 4.5599
c 4
b 3
f 0
wmc 58

5 Methods

Rating   Name   Duplication   Size   Complexity  
B create() 0 19 5
A show() 0 23 4
A room_params() 0 3 1
A destroy() 0 6 3
A update() 0 16 5

How to fix   Complexity   

Complex Class

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
2
3
# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
4
#
5
# Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below).
6
#
7
# This program is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU Lesser General Public License as published by the Free Software
9
# Foundation; either version 3.0 of the License, or (at your option) any later
10
# version.
11
#
12
# BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14
# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
15
#
16
# You should have received a copy of the GNU Lesser General Public License along
17
# with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
18
19
class RoomsController < ApplicationController
20
  include RecordingsHelper
21
22
  before_action :validate_accepted_terms, unless: -> { !Rails.configuration.terms }
23
  before_action :validate_verified_email, except: [:show, :join],
24
                unless: -> { !Rails.configuration.enable_email_verification }
25
  before_action :find_room, except: :create
26
  before_action :verify_room_ownership, except: [:create, :show, :join, :logout]
27
  before_action :verify_room_owner_verified, only: [:show, :join],
28
                unless: -> { !Rails.configuration.enable_email_verification }
29
30
  # POST /
31
  def create
32
    redirect_to(root_path) && return unless current_user
33
34
    @room = Room.new(name: room_params[:name])
35
    @room.owner = current_user
36
    @room.room_settings = create_room_settings_string(room_params[:mute_on_join], room_params[:client])
37
38
    if @room.save
39
      if room_params[:auto_join] == "1"
40
        start
41
      else
42
        flash[:success] = I18n.t("room.create_room_success")
43
        redirect_to @room
44
      end
45
    else
46
      flash[:alert] = I18n.t("room.create_room_error")
47
      redirect_to current_user.main_room
48
    end
49
  end
50
51
  # GET /:room_uid
52
  def show
53
    if current_user && @room.owned_by?(current_user)
54
      if current_user.has_role? :super_admin
55
        redirect_to admins_path
56
      else
57
        recs = @room.recordings
58
59
        @recordings = recs
60
        @is_running = @room.running?
61
      end
62
    else
63
      # Get users name
64
      @name = if current_user
65
        current_user.name
66
      elsif cookies.encrypted[:greenlight_name]
67
        cookies.encrypted[:greenlight_name]
68
      else
69
        ""
70
      end
71
72
      render :join
73
    end
74
  end
75
76
  # PATCH /:room_uid
77
  def update
78
    if params[:setting] == "rename_block"
79
      @room = Room.find_by!(uid: params[:room_block_uid])
80
      update_room_attributes("name")
81
    elsif params[:setting] == "rename_header"
82
      update_room_attributes("name")
83
    elsif params[:setting] == "rename_recording"
84
      @room.update_recording(params[:record_id], "meta_name" => params[:record_name])
85
    end
86
87
    if request.referrer
88
      redirect_to request.referrer
89
    else
90
      redirect_to room_path
91
    end
92
  end
93
94
  # POST /:room_uid
95
  def join
96
    opts = default_meeting_options
97
    unless @room.owned_by?(current_user)
98
      # Assign join name if passed.
99
      if params[@room.invite_path]
100
        @join_name = params[@room.invite_path][:join_name]
101
      elsif !params[:join_name]
102
        # Join name not passed.
103
        return
104
      end
105
    end
106
107
    # create or update cookie with join name
108
    cookies.encrypted[:greenlight_name] = @join_name unless cookies.encrypted[:greenlight_name] == @join_name
109
110
    if @room.running? || @room.owned_by?(current_user)
111
      # Determine if the user needs to join as a moderator.
112
      opts[:user_is_moderator] = @room.owned_by?(current_user)
113
114
      # Check if the user has specified which client to use
115
      room_settings = JSON.parse(@room[:room_settings])
116
      opts[:join_via_html5] = room_settings["joinViaHtml5"] if room_settings["joinViaHtml5"]
117
118
      if current_user
119
        redirect_to @room.join_path(current_user.name, opts, current_user.uid)
120
      else
121
        join_name = params[:join_name] || params[@room.invite_path][:join_name]
122
        redirect_to @room.join_path(join_name, opts)
123
      end
124
    else
125
      # They need to wait until the meeting begins.
126
      render :wait
127
    end
128
  end
129
130
  # DELETE /:room_uid
131
  def destroy
132
    # Don't delete the users home room.
133
    @room.destroy if @room.owned_by?(current_user) && @room != current_user.main_room
134
135
    redirect_to current_user.main_room
136
  end
137
138
  # POST /:room_uid/start
139
  def start
140
    # Join the user in and start the meeting.
141
    opts = default_meeting_options
142
    opts[:user_is_moderator] = true
143
144
    # Include the user's choices for the room settings
145
    room_settings = JSON.parse(@room[:room_settings])
146
    opts[:mute_on_start] = room_settings["muteOnStart"] if room_settings["muteOnStart"]
147
    opts[:join_via_html5] = room_settings["joinViaHtml5"] if room_settings["joinViaHtml5"]
148
149
    begin
150
      redirect_to @room.join_path(current_user.name, opts, current_user.uid)
151
    rescue BigBlueButton::BigBlueButtonException => e
152
      redirect_to room_path, alert: I18n.t(e.key.to_s.underscore, default: I18n.t("bigbluebutton_exception"))
153
    end
154
155
    # Notify users that the room has started.
156
    # Delay 5 seconds to allow for server start, although the request will retry until it succeeds.
157
    NotifyUserWaitingJob.set(wait: 5.seconds).perform_later(@room)
158
  end
159
160
  # POST /:room_uid/update_settings
161
  def update_settings
162
    begin
163
      raise "Room name can't be blank" if room_params[:name].empty?
164
165
      @room = Room.find_by!(uid: params[:room_uid])
166
      # Update the rooms settings
167
      update_room_attributes("settings")
168
      # Update the rooms name if it has been changed
169
      update_room_attributes("name") if @room.name != room_params[:name]
170
    rescue StandardError
171
      flash[:alert] = I18n.t("room.update_settings_error")
172
    else
173
      flash[:success] = I18n.t("room.update_settings_success")
174
    end
175
    redirect_to room_path
176
  end
177
178
  # GET /:room_uid/logout
179
  def logout
180
    # Redirect the correct page.
181
    redirect_to @room
182
  end
183
184
  private
185
186
  def update_room_attributes(update_type)
187
    if @room.owned_by?(current_user) && @room != current_user.main_room
188
      if update_type.eql? "name"
189
        @room.update_attributes(name: params[:room_name] || room_params[:name])
190
      elsif update_type.eql? "settings"
191
        room_settings_string = create_room_settings_string(room_params[:mute_on_join], room_params[:client])
192
        @room.update_attributes(room_settings: room_settings_string)
193
      end
194
    end
195
  end
196
197
  def create_room_settings_string(mute_res, client_res)
198
    room_settings = {}
199
    room_settings["muteOnStart"] = mute_res == "1"
200
201
    if client_res.eql? "html5"
202
      room_settings["joinViaHtml5"] = true
203
    elsif client_res.eql? "flash"
204
      room_settings["joinViaHtml5"] = false
205
    end
206
207
    room_settings.to_json
208
  end
209
210
  def room_params
211
    params.require(:room).permit(:name, :auto_join, :mute_on_join, :client)
212
  end
213
214
  # Find the room from the uid.
215
  def find_room
216
    @room = Room.find_by!(uid: params[:room_uid])
217
  end
218
219
  # Ensure the user is logged into the room they are accessing.
220
  def verify_room_ownership
221
    bring_to_room unless @room.owned_by?(current_user)
222
  end
223
224
  # Redirects a user to their room.
225
  def bring_to_room
226
    if current_user
227
      # Redirect authenticated users to their room.
228
      redirect_to room_path(current_user.main_room)
229
    else
230
      # Redirect unauthenticated users to root.
231
      redirect_to root_path
232
    end
233
  end
234
235
  def validate_accepted_terms
236
    if current_user
237
      redirect_to terms_path unless current_user.accepted_terms
238
    end
239
  end
240
241
  def validate_verified_email
242
    if current_user
243
      redirect_to account_activation_path(current_user) unless current_user.activated?
244
    end
245
  end
246
247
  def verify_room_owner_verified
248
    unless @room.owner.activated?
249
      flash[:alert] = t("room.unavailable")
250
251
      if current_user
252
        redirect_to current_user.main_room
253
      else
254
        redirect_to root_path
255
      end
256
    end
257
  end
258
end
259