Files
old-holivud2/lib/zoom_gateway.rb

139 lines
3.7 KiB
Ruby
Raw Normal View History

2020-06-24 04:48:12 +02:00
# frozen_string_literal: true
require './lib/zoom_wrapper_monkeypatch'
2020-05-31 22:38:19 +02:00
class ZoomGateway
class AuthenticationError < StandardError; end
class MeetingExpired < StandardError; end
class UserNotFound < StandardError; end
class TooManyHosts < StandardError; end
2020-06-09 06:35:40 +02:00
class UserAlreadyExists < StandardError; end
2020-05-31 22:38:19 +02:00
class << self
def USER_TYPE_NAME
default = 'basic'
env_name = ENV['ZOOM_USER_TYPE'] || default
%w[pro basic].include?(env_name) ? env_name : default
end
def USER_TYPE
self.USER_TYPE_NAME == 'pro' ? 2 : 1
end
def PRO_USERS_LIMIT
(ENV['ZOOM_PRO_USERS_LIMIT'] || 3).to_i
end
def HOST_ROLE
2020-06-11 16:56:29 +02:00
self.host_role_name(self.USER_TYPE_NAME)
2020-05-31 22:38:19 +02:00
end
2020-06-09 06:35:40 +02:00
def ACCOUNT_NUMBER
ENV['ZOOM_ACCOUNT_NUMBER']
end
2020-05-31 22:38:19 +02:00
def enable_recordings?
2020-06-10 11:19:35 +02:00
ENV['ZOOM_ENABLE_RECORDINGS'] == 'true'
2020-05-31 22:38:19 +02:00
end
def apply_limits?
self.USER_TYPE_NAME == 'pro'
end
2020-06-11 16:56:29 +02:00
def host_role_name(user_type_name)
"#{user_type_name}-directme-host"
end
2020-05-31 22:38:19 +02:00
end
def initialize
@client = Zoom.new
end
def create_meeting(host_id, **kwargs)
recording_type = self.class.enable_recordings? ? 'cloud' : 'none'
meeting = @client.meeting_create({ user_id: host_id,
topic: kwargs[:topic],
type: 1, # Instant meeting
settings: {
host_video: true,
participant_video: true,
auto_recording: recording_type,
} })
meeting["id"]
end
def find_meeting(meeting_id)
meeting = @client.meeting_get(meeting_id: meeting_id)
HashWithIndifferentAccess.new(meeting)
rescue Zoom::APIError => e
parse_zoom_error(e)
end
2020-06-24 04:48:12 +02:00
# Update user with custom settings
def update_user_settings(user_id)
custom_defaults = {
id: user_id,
in_meeting: {
auto_saving_chat: true,
co_host: true,
non_verbal_feedback: true,
breakout_room: true,
group_hd: true,
far_end_camera_control: true,
allow_live_streaming: true
}
}
@client.user_settings_update custom_defaults
end
2020-05-31 22:38:19 +02:00
def create_host(host_email)
# Find role
host_role = @client.roles_list["roles"].try(:select) { |r| r["name"] == self.class.HOST_ROLE }.try(:first)
raise StandardError.new("Zoom host role #{self.class.HOST_ROLE} does not exist, try running rails zoom:setup.") unless host_role.present?
if self.class.apply_limits?
hosts_count = @client.roles_members(role_id: host_role["id"]).try(:[], 'total_records').try(:to_i)
raise TooManyHosts, 'The limit of hosts has been reached' if hosts_count >= self.class.PRO_USERS_LIMIT
end
# Create new user
host_user = @client.user_create({
action: "custCreate",
email: host_email,
type: self.class.USER_TYPE
})
2020-06-24 04:48:12 +02:00
update_user_settings(host_user["id"])
2020-05-31 22:38:19 +02:00
# Assign role to user
@client.roles_assign role_id: host_role["id"], members: [{id: host_user["id"]}]
# Return user id
host_user["id"]
end
def delete_host(host_id)
@client.user_delete(id: host_id)
end
def delete_recording(meeting_id, recording_id)
@client.recording_delete(meeting_id: meeting_id, recording_id: recording_id)
end
private
def parse_zoom_error(error)
if error.status_code == 104
raise AuthenticationError, error.message
elsif error.status_code == 1001
raise UserNotFound, error.message
elsif error.status_code == 3001
raise MeetingExpired, error.message
2020-06-09 06:35:40 +02:00
elsif error.status_code == 1005
raise UserAlreadyExists, error.message
2020-05-31 22:38:19 +02:00
else
raise error
end
end
end