greenlight/app/models/room.rb

65 lines
2.1 KiB
Ruby

# frozen_string_literal: true
# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
#
# Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below).
#
# This program is free software; you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free Software
# Foundation; either version 3.0 of the License, or (at your option) any later
# version.
#
# BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
require 'bbb_api'
class Room < ApplicationRecord
before_create :setup
validates :name, presence: true
belongs_to :owner, class_name: 'User', foreign_key: :user_id
# Determines if a user owns a room.
def owned_by?(user)
return false if user.nil?
user.rooms.include?(self)
end
# Determines the invite path for the room.
def invite_path
"#{Rails.configuration.relative_url_root}/#{CGI.escape(uid)}"
end
# Notify waiting users that a meeting has started.
def notify_waiting
ActionCable.server.broadcast("#{uid}_waiting_channel", action: "started")
end
private
# Generates a uid for the room and BigBlueButton.
def setup
self.uid = random_room_uid
self.bbb_id = Digest::SHA1.hexdigest(Rails.application.secrets[:secret_key_base] + Time.now.to_i.to_s).to_s
self.moderator_pw = RandomPassword.generate(length: 12)
self.attendee_pw = RandomPassword.generate(length: 12)
end
# Generates a three character uid chunk.
def uid_chunk
charset = ("a".."z").to_a - %w(b i l o s) + ("2".."9").to_a - %w(5 8)
(0...3).map { charset.to_a[rand(charset.size)] }.join
end
# Generates a random room uid that uses the users name.
def random_room_uid
[owner.name_chunk, uid_chunk, uid_chunk].join('-').downcase
end
end