-
Notifications
You must be signed in to change notification settings - Fork 3
/
webinars.rb
133 lines (106 loc) · 3.79 KB
/
webinars.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# frozen_string_literal: true
module Zoom
class Webinars
attr_reader :zoom_client
RECURRING_WEBINAR_TYPE = 9
def initialize(zoom_client)
@zoom_client = zoom_client
end
def unmatched(user)
response = zoom_client.get("users/#{user.email}/webinars?page_size=300")
return [] unless response
result =
response
&.body
&.[](:webinars)
&.select do |hash|
hash[:start_time].in_time_zone.utc > Time.now.utc &&
hash[:type] != RECURRING_WEBINAR_TYPE && Webinar.where(zoom_id: hash[:id]).empty?
end
result
end
def find(webinar_id)
webinar_data = zoom_client.webinar(webinar_id)
return false unless webinar_data[:id]
webinar_data[:panelists] = panelists(webinar_id)
webinar_data[:host] = host(webinar_data[:host_id])
existing_topic = Webinar.where(zoom_id: webinar_id).first
webinar_data[:existing_topic] = existing_topic if existing_topic.present?
webinar_data
end
def add_panelist(webinar:, user:)
response =
zoom_client.post(
"webinars/#{webinar.zoom_id}/panelists",
panelists: [{ email: user.email, name: user.name.blank? ? user.username : user.name }],
)
return false if response.status != 201
WebinarUser.where(user: user, webinar: webinar).destroy_all
WebinarUser.create!(user: user, webinar: webinar, type: :panelist)
end
def remove_panelist(webinar:, user:)
panelists = zoom_client.panelists(webinar.zoom_id, true)[:body][:panelists]
matching_panelist = panelists.detect { |panelist| panelist[:email] == user.email }
return false unless matching_panelist
response =
zoom_client.delete("webinars/#{webinar.zoom_id}/panelists/#{matching_panelist[:id]}")
return false if response.status != 204
WebinarUser.where(user: user, webinar: webinar).destroy_all
end
def signature(webinar_id)
return false if !SiteSetting.zoom_sdk_key && !SiteSetting.zoom_sdk_secret
webinar = zoom_client.webinar(webinar_id)
return false unless webinar[:id]
iat = DateTime.now.utc - 30.seconds
exp = iat + 2.hours
header = { alg: "HS256", typ: "JWT" }
role = "0" # regular member role
payload = {
sdkKey: SiteSetting.zoom_sdk_key,
appKey: SiteSetting.zoom_sdk_key,
mn: webinar_id,
role: role,
iat: iat.to_i,
exp: exp.to_i,
tokenExp: exp.to_i,
}
JWT.encode(payload, SiteSetting.zoom_sdk_secret, "HS256", header)
end
private
def host(host_id)
host_data = zoom_client.host(host_id)
user = User.find_by_email(host_data[:email])
return host_data if user.nil?
host_payload(user)
end
def panelists(webinar_id)
panelists_data = zoom_client.panelists(webinar_id)
panelist_emails = panelists_data[:panelists].map { |s| s[:email] }.join(",")
panelists = User.with_email(panelist_emails)
if panelists.empty?
panelists =
panelists_data[:panelists].map { |s| { name: s[:name], avatar_url: s[:avatar_url] } }
return panelists
end
panelists_payload(panelists)
end
def panelists_payload(panelists)
panelists.map do |s|
{ name: s.name || s.username, avatar_url: s.avatar_template_url.gsub("{size}", "25") }
end
end
def host_payload(host)
if SiteSetting.zoom_host_title_override
field_id = UserField.where(name: SiteSetting.zoom_host_title_override).pluck(:id).first
title = host.user_fields[field_id.to_s] || ""
else
title = host.title
end
{
name: host.name || host.username,
title: title,
avatar_url: host.avatar_template_url.gsub("{size}", "120"),
}
end
end
end