-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Expand file tree
/
Copy pathVideoApiAdapter.ts
More file actions
156 lines (136 loc) · 4.42 KB
/
Copy pathVideoApiAdapter.ts
File metadata and controls
156 lines (136 loc) · 4.42 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { createHash, randomUUID } from "node:crypto";
import type { CalendarEvent, EventBusyDate } from "@calcom/types/Calendar";
import type { PartialReference } from "@calcom/types/EventManager";
import type {
VideoApiAdapter,
VideoCallData,
} from "@calcom/types/VideoApiAdapter";
import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug";
import { metadata } from "../_metadata";
type BigBlueButtonKeys = {
bigBlueButtonServerUrl?: string;
bigBlueButtonSharedSecret?: string;
};
const normalizeApiUrl = (serverUrl: string) => {
const url = serverUrl.trim().replace(/\/+$/, "");
return url.endsWith("/bigbluebutton/api") ? url : `${url}/bigbluebutton/api`;
};
const checksum = (method: string, query: string, sharedSecret: string) =>
createHash("sha1").update(`${method}${query}${sharedSecret}`).digest("hex");
const deriveMeetingPassword = (
role: "attendee" | "moderator",
meetingID: string,
sharedSecret: string,
) =>
createHash("sha256")
.update(`${role}:${meetingID}:${sharedSecret}`)
.digest("hex")
.slice(0, 24);
const buildApiUrl = (
apiUrl: string,
method: string,
params: URLSearchParams,
sharedSecret: string,
) => {
const query = params.toString();
const signedQuery = new URLSearchParams(params);
signedQuery.set("checksum", checksum(method, query, sharedSecret));
return `${apiUrl}/${method}?${signedQuery.toString()}`;
};
const assertSuccessResponse = async (response: Response) => {
const body = await response.text();
if (!response.ok || !body.includes("<returncode>SUCCESS</returncode>")) {
throw new Error(`BigBlueButton API request failed: ${body}`);
}
};
const getBigBlueButtonConfig = async () => {
const appKeys = (await getAppKeysFromSlug(
metadata.slug,
)) as BigBlueButtonKeys;
const serverUrl = appKeys.bigBlueButtonServerUrl?.trim();
const sharedSecret = appKeys.bigBlueButtonSharedSecret?.trim();
if (!serverUrl || !sharedSecret) {
throw new Error("BigBlueButton server URL and shared secret are required");
}
return {
apiUrl: normalizeApiUrl(serverUrl),
sharedSecret,
};
};
const BigBlueButtonVideoApiAdapter = (): VideoApiAdapter => {
return {
getAvailability: (): Promise<EventBusyDate[]> => {
return Promise.resolve([]);
},
createMeeting: async (eventData: CalendarEvent): Promise<VideoCallData> => {
const { apiUrl, sharedSecret } = await getBigBlueButtonConfig();
const meetingID = eventData.uid || randomUUID();
const attendeePassword = deriveMeetingPassword(
"attendee",
meetingID,
sharedSecret,
);
const moderatorPassword = deriveMeetingPassword(
"moderator",
meetingID,
sharedSecret,
);
const createParams = new URLSearchParams({
name: eventData.title,
meetingID,
attendeePW: attendeePassword,
moderatorPW: moderatorPassword,
record: "false",
});
const createUrl = buildApiUrl(
apiUrl,
"create",
createParams,
sharedSecret,
);
await assertSuccessResponse(await fetch(createUrl));
const joinParams = new URLSearchParams({
fullName: "Guest",
meetingID,
password: attendeePassword,
redirect: "true",
});
return {
type: metadata.type,
id: meetingID,
password: moderatorPassword,
url: buildApiUrl(apiUrl, "join", joinParams, sharedSecret),
};
},
deleteMeeting: async (uid: string): Promise<void> => {
const { apiUrl, sharedSecret } = await getBigBlueButtonConfig();
const moderatorPassword = deriveMeetingPassword(
"moderator",
uid,
sharedSecret,
);
const endParams = new URLSearchParams({
meetingID: uid,
password: moderatorPassword,
});
await assertSuccessResponse(
await fetch(buildApiUrl(apiUrl, "end", endParams, sharedSecret)),
);
},
updateMeeting: (bookingRef: PartialReference): Promise<VideoCallData> => {
const { meetingId, meetingPassword, meetingUrl } = bookingRef;
if (!meetingId || !meetingPassword || !meetingUrl) {
throw new Error(
"BigBlueButton booking reference is missing meeting data",
);
}
return Promise.resolve({
type: metadata.type,
id: meetingId,
password: meetingPassword,
url: meetingUrl,
});
},
};
};
export default BigBlueButtonVideoApiAdapter;