This repository has been archived on 2021-10-24. You can view files and clone it, but cannot push or open issues or pull requests.
greenlight/app/models/room.rb

103 lines
3.1 KiB
Ruby
Raw Normal View History

2018-06-26 10:29:46 -04:00
# frozen_string_literal: true
2018-05-07 16:06:01 -04:00
2018-08-01 09:45:12 -04:00
# 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'
2018-06-26 10:29:46 -04:00
class Room < ApplicationRecord
include Deleteable
2018-05-31 15:04:18 -04:00
before_create :setup
validates :name, presence: true
2018-05-07 16:06:01 -04:00
2018-05-31 22:15:18 -04:00
belongs_to :owner, class_name: 'User', foreign_key: :user_id
has_many :shared_access
2018-05-07 16:06:01 -04:00
def self.admins_search(string)
active_database = Rails.configuration.database_configuration[Rails.env]["adapter"]
# Postgres requires created_at to be cast to a string
created_at_query = if active_database == "postgresql"
"created_at::text"
else
"created_at"
end
search_query = "rooms.name LIKE :search OR rooms.uid LIKE :search OR users.email LIKE :search" \
" OR users.#{created_at_query} LIKE :search"
search_param = "%#{string}%"
joins(:owner).where(search_query, search: search_param)
end
def self.admins_order(column, direction)
# Include the owner of the table
table = joins(:owner)
2020-01-09 12:35:32 -05:00
return table.order(Arel.sql("#{column} #{direction}")) if table.column_names.include?(column) || column == "users.name"
table
end
2018-05-15 11:43:59 -04:00
# Determines if a user owns a room.
2018-05-07 16:06:01 -04:00
def owned_by?(user)
2018-05-14 14:28:18 -04:00
return false if user.nil?
2018-05-29 15:28:29 -04:00
user.rooms.include?(self)
end
def shared_users
User.where(id: shared_access.pluck(:user_id))
end
def shared_with?(user)
return false if user.nil?
shared_users.include?(user)
end
2018-08-16 13:25:32 -04:00
# Determines the invite path for the room.
2018-05-29 15:28:29 -04:00
def invite_path
2018-08-16 13:25:32 -04:00
"#{Rails.configuration.relative_url_root}/#{CGI.escape(uid)}"
2018-05-29 15:28:29 -04:00
end
2018-06-07 15:52:42 -04:00
# Notify waiting users that a meeting has started.
def notify_waiting
2018-06-26 10:29:46 -04:00
ActionCable.server.broadcast("#{uid}_waiting_channel", action: "started")
2018-06-07 15:52:42 -04:00
end
2018-05-07 16:06:01 -04:00
private
2018-05-29 15:28:29 -04:00
# Generates a uid for the room and BigBlueButton.
2018-05-31 15:04:18 -04:00
def setup
2018-06-21 10:06:10 -04:00
self.uid = random_room_uid
2018-05-29 15:28:29 -04:00
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)
2018-05-29 15:28:29 -04:00
end
2018-06-26 10:29:46 -04:00
# 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
2018-06-21 10:06:10 -04:00
# Generates a random room uid that uses the users name.
def random_room_uid
[owner.name_chunk, uid_chunk, uid_chunk].join('-').downcase
2018-06-21 10:06:10 -04:00
end
2018-05-29 15:28:29 -04:00
end