Passed
Push — master ( 71f35c...b1a999 )
by Ahmad
06:52 queued 10s
created

Room.unique_bbb_id()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 6
rs 10
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
require 'bbb_api'
20
21
class Room < ApplicationRecord
22
  include Deleteable
23
24
  before_create :setup
25
26
  validates :name, presence: true
27
28
  belongs_to :owner, class_name: 'User', foreign_key: :user_id
29
  has_many :shared_access
30
31
  def self.admins_search(string)
32
    active_database = Rails.configuration.database_configuration[Rails.env]["adapter"]
33
    # Postgres requires created_at to be cast to a string
34
    created_at_query = if active_database == "postgresql"
35
      "created_at::text"
36
    else
37
      "created_at"
38
    end
39
40
    search_query = "rooms.name LIKE :search OR rooms.uid LIKE :search OR users.email LIKE :search" \
41
    " OR users.#{created_at_query} LIKE :search"
42
43
    search_param = "%#{string}%"
44
45
    where(search_query, search: search_param)
46
  end
47
48
  def self.admins_order(column, direction)
49
    # Include the owner of the table
50
    table = joins(:owner)
51
52
    return table.order(Arel.sql("rooms.#{column} #{direction}")) if table.column_names.include?(column)
53
54
    return table.order(Arel.sql("#{column} #{direction}")) if column == "users.name"
55
56
    table
57
  end
58
59
  # Determines if a user owns a room.
60
  def owned_by?(user)
61
    user_id == user&.id
62
  end
63
64
  def shared_users
65
    User.where(id: shared_access.pluck(:user_id))
66
  end
67
68
  def shared_with?(user)
69
    return false if user.nil?
70
    shared_users.include?(user)
71
  end
72
73
  # Determines the invite path for the room.
74
  def invite_path
75
    "#{Rails.configuration.relative_url_root}/#{CGI.escape(uid)}"
76
  end
77
78
  # Notify waiting users that a meeting has started.
79
  def notify_waiting
80
    ActionCable.server.broadcast("#{uid}_waiting_channel", action: "started")
81
  end
82
83
  private
84
85
  # Generates a uid for the room and BigBlueButton.
86
  def setup
87
    self.uid = random_room_uid
88
    self.bbb_id = unique_bbb_id
89
    self.moderator_pw = RandomPassword.generate(length: 12)
90
    self.attendee_pw = RandomPassword.generate(length: 12)
91
  end
92
93
  # Generates a three character uid chunk.
94
  def uid_chunk
95
    charset = ("a".."z").to_a - %w(b i l o s) + ("2".."9").to_a - %w(5 8)
96
    (0...3).map { charset.to_a[rand(charset.size)] }.join
97
  end
98
99
  # Generates a random room uid that uses the users name.
100
  def random_room_uid
101
    [owner.name_chunk, uid_chunk, uid_chunk].join('-').downcase
102
  end
103
104
  # Generates a unique bbb_id based on uuid.
105
  def unique_bbb_id
106
    loop do
107
      bbb_id = SecureRandom.hex(20)
108
      break bbb_id unless Room.exists?(bbb_id: bbb_id)
109
    end
110
  end
111
end
112