2021-06-14 16:13:54 +00:00
|
|
|
import prisma from "./prisma";
|
2021-07-15 01:19:30 +00:00
|
|
|
import { CalendarEvent } from "./calendarClient";
|
2021-06-18 00:44:41 +00:00
|
|
|
import VideoEventOrganizerMail from "./emails/VideoEventOrganizerMail";
|
2021-06-16 22:56:02 +00:00
|
|
|
import VideoEventAttendeeMail from "./emails/VideoEventAttendeeMail";
|
2021-07-15 01:19:30 +00:00
|
|
|
import { v5 as uuidv5 } from "uuid";
|
|
|
|
import short from "short-uuid";
|
2021-06-17 00:44:13 +00:00
|
|
|
import EventAttendeeRescheduledMail from "./emails/EventAttendeeRescheduledMail";
|
2021-06-18 00:44:41 +00:00
|
|
|
import EventOrganizerRescheduledMail from "./emails/EventOrganizerRescheduledMail";
|
2021-07-15 01:19:30 +00:00
|
|
|
import { EventResult } from "@lib/events/EventManager";
|
|
|
|
import logger from "@lib/logger";
|
2021-07-20 18:40:41 +00:00
|
|
|
import { AdditionInformation, EntryPoint } from "@lib/emails/EventMail";
|
|
|
|
import { getIntegrationName } from "@lib/emails/helpers";
|
2021-07-25 17:15:31 +00:00
|
|
|
import CalEventParser from "@lib/CalEventParser";
|
2021-07-28 20:05:37 +00:00
|
|
|
import { Credential } from "@prisma/client";
|
2021-07-15 01:19:30 +00:00
|
|
|
|
|
|
|
const log = logger.getChildLogger({ prefix: ["[lib] videoClient"] });
|
2021-06-16 22:56:02 +00:00
|
|
|
|
|
|
|
const translator = short();
|
|
|
|
|
|
|
|
export interface VideoCallData {
|
|
|
|
type: string;
|
|
|
|
id: string;
|
|
|
|
password: string;
|
|
|
|
url: string;
|
|
|
|
}
|
2021-06-14 16:13:54 +00:00
|
|
|
|
2021-06-13 13:22:17 +00:00
|
|
|
function handleErrorsJson(response) {
|
2021-06-16 22:56:02 +00:00
|
|
|
if (!response.ok) {
|
|
|
|
response.json().then(console.log);
|
|
|
|
throw Error(response.statusText);
|
|
|
|
}
|
|
|
|
return response.json();
|
2021-06-13 13:22:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleErrorsRaw(response) {
|
2021-06-16 22:56:02 +00:00
|
|
|
if (!response.ok) {
|
|
|
|
response.text().then(console.log);
|
|
|
|
throw Error(response.statusText);
|
|
|
|
}
|
|
|
|
return response.text();
|
2021-06-13 13:22:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const zoomAuth = (credential) => {
|
2021-07-15 01:19:30 +00:00
|
|
|
const isExpired = (expiryDate) => expiryDate < +new Date();
|
|
|
|
const authHeader =
|
|
|
|
"Basic " +
|
|
|
|
Buffer.from(process.env.ZOOM_CLIENT_ID + ":" + process.env.ZOOM_CLIENT_SECRET).toString("base64");
|
|
|
|
|
|
|
|
const refreshAccessToken = (refreshToken) =>
|
|
|
|
fetch("https://zoom.us/oauth/token", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
Authorization: authHeader,
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
|
|
},
|
|
|
|
body: new URLSearchParams({
|
|
|
|
refresh_token: refreshToken,
|
|
|
|
grant_type: "refresh_token",
|
|
|
|
}),
|
2021-06-16 22:56:02 +00:00
|
|
|
})
|
2021-07-15 01:19:30 +00:00
|
|
|
.then(handleErrorsJson)
|
|
|
|
.then(async (responseBody) => {
|
|
|
|
// Store new tokens in database.
|
|
|
|
await prisma.credential.update({
|
|
|
|
where: {
|
|
|
|
id: credential.id,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
key: responseBody,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
credential.key.access_token = responseBody.access_token;
|
|
|
|
credential.key.expires_in = Math.round(+new Date() / 1000 + responseBody.expires_in);
|
|
|
|
return credential.key.access_token;
|
2021-06-16 22:56:02 +00:00
|
|
|
});
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2021-06-16 22:56:02 +00:00
|
|
|
return {
|
2021-07-15 01:19:30 +00:00
|
|
|
getToken: () =>
|
|
|
|
!isExpired(credential.key.expires_in)
|
|
|
|
? Promise.resolve(credential.key.access_token)
|
|
|
|
: refreshAccessToken(credential.key.refresh_token),
|
2021-06-16 22:56:02 +00:00
|
|
|
};
|
2021-06-13 13:22:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
interface VideoApiAdapter {
|
2021-06-16 22:56:02 +00:00
|
|
|
createMeeting(event: CalendarEvent): Promise<any>;
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2021-07-15 01:19:30 +00:00
|
|
|
updateMeeting(uid: string, event: CalendarEvent);
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2021-07-28 20:05:37 +00:00
|
|
|
deleteMeeting(uid: string): Promise<unknown>;
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2021-06-16 22:56:02 +00:00
|
|
|
getAvailability(dateFrom, dateTo): Promise<any>;
|
2021-06-13 13:22:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const ZoomVideo = (credential): VideoApiAdapter => {
|
2021-06-16 22:56:02 +00:00
|
|
|
const auth = zoomAuth(credential);
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2021-06-16 22:56:02 +00:00
|
|
|
const translateEvent = (event: CalendarEvent) => {
|
|
|
|
// Documentation at: https://marketplace.zoom.us/docs/api-reference/zoom-api/meetings/meetingcreate
|
2021-06-13 13:22:17 +00:00
|
|
|
return {
|
2021-06-16 22:56:02 +00:00
|
|
|
topic: event.title,
|
2021-07-15 01:19:30 +00:00
|
|
|
type: 2, // Means that this is a scheduled meeting
|
2021-06-16 22:56:02 +00:00
|
|
|
start_time: event.startTime,
|
2021-07-15 01:19:30 +00:00
|
|
|
duration: (new Date(event.endTime).getTime() - new Date(event.startTime).getTime()) / 60000,
|
2021-06-16 22:56:02 +00:00
|
|
|
//schedule_for: "string", TODO: Used when scheduling the meeting for someone else (needed?)
|
|
|
|
timezone: event.attendees[0].timeZone,
|
|
|
|
//password: "string", TODO: Should we use a password? Maybe generate a random one?
|
|
|
|
agenda: event.description,
|
|
|
|
settings: {
|
|
|
|
host_video: true,
|
|
|
|
participant_video: true,
|
2021-07-15 01:19:30 +00:00
|
|
|
cn_meeting: false, // TODO: true if host meeting in China
|
|
|
|
in_meeting: false, // TODO: true if host meeting in India
|
2021-06-16 22:56:02 +00:00
|
|
|
join_before_host: true,
|
|
|
|
mute_upon_entry: false,
|
|
|
|
watermark: false,
|
|
|
|
use_pmi: false,
|
|
|
|
approval_type: 2,
|
|
|
|
audio: "both",
|
|
|
|
auto_recording: "none",
|
|
|
|
enforce_login: false,
|
2021-07-15 01:19:30 +00:00
|
|
|
registrants_email_notification: true,
|
|
|
|
},
|
2021-06-16 22:56:02 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
2021-07-15 01:19:30 +00:00
|
|
|
getAvailability: () => {
|
|
|
|
return auth
|
|
|
|
.getToken()
|
|
|
|
.then(
|
|
|
|
// TODO Possibly implement pagination for cases when there are more than 300 meetings already scheduled.
|
|
|
|
(accessToken) =>
|
|
|
|
fetch("https://api.zoom.us/v2/users/me/meetings?type=scheduled&page_size=300", {
|
|
|
|
method: "get",
|
|
|
|
headers: {
|
|
|
|
Authorization: "Bearer " + accessToken,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(handleErrorsJson)
|
|
|
|
.then((responseBody) => {
|
|
|
|
return responseBody.meetings.map((meeting) => ({
|
|
|
|
start: meeting.start_time,
|
|
|
|
end: new Date(
|
|
|
|
new Date(meeting.start_time).getTime() + meeting.duration * 60000
|
|
|
|
).toISOString(),
|
|
|
|
}));
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
});
|
2021-06-16 22:56:02 +00:00
|
|
|
},
|
2021-07-15 01:19:30 +00:00
|
|
|
createMeeting: (event: CalendarEvent) =>
|
|
|
|
auth.getToken().then((accessToken) =>
|
|
|
|
fetch("https://api.zoom.us/v2/users/me/meetings", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
Authorization: "Bearer " + accessToken,
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(translateEvent(event)),
|
|
|
|
}).then(handleErrorsJson)
|
|
|
|
),
|
|
|
|
deleteMeeting: (uid: string) =>
|
|
|
|
auth.getToken().then((accessToken) =>
|
|
|
|
fetch("https://api.zoom.us/v2/meetings/" + uid, {
|
|
|
|
method: "DELETE",
|
|
|
|
headers: {
|
|
|
|
Authorization: "Bearer " + accessToken,
|
|
|
|
},
|
|
|
|
}).then(handleErrorsRaw)
|
|
|
|
),
|
|
|
|
updateMeeting: (uid: string, event: CalendarEvent) =>
|
|
|
|
auth.getToken().then((accessToken) =>
|
|
|
|
fetch("https://api.zoom.us/v2/meetings/" + uid, {
|
|
|
|
method: "PATCH",
|
|
|
|
headers: {
|
|
|
|
Authorization: "Bearer " + accessToken,
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(translateEvent(event)),
|
|
|
|
}).then(handleErrorsRaw)
|
|
|
|
),
|
|
|
|
};
|
2021-06-13 13:22:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// factory
|
2021-07-15 01:19:30 +00:00
|
|
|
const videoIntegrations = (withCredentials): VideoApiAdapter[] =>
|
|
|
|
withCredentials
|
|
|
|
.map((cred) => {
|
|
|
|
switch (cred.type) {
|
|
|
|
case "zoom_video":
|
|
|
|
return ZoomVideo(cred);
|
|
|
|
default:
|
|
|
|
return; // unknown credential, could be legacy? In any case, ignore
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.filter(Boolean);
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2021-07-28 20:05:37 +00:00
|
|
|
const getBusyVideoTimes: (withCredentials) => Promise<unknown[]> = (withCredentials) =>
|
2021-07-15 01:19:30 +00:00
|
|
|
Promise.all(videoIntegrations(withCredentials).map((c) => c.getAvailability())).then((results) =>
|
|
|
|
results.reduce((acc, availability) => acc.concat(availability), [])
|
|
|
|
);
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2021-07-25 15:05:18 +00:00
|
|
|
const createMeeting = async (
|
2021-07-28 20:05:37 +00:00
|
|
|
credential: Credential,
|
2021-07-25 15:05:18 +00:00
|
|
|
calEvent: CalendarEvent,
|
|
|
|
maybeUid: string = null
|
|
|
|
): Promise<EventResult> => {
|
2021-07-25 17:15:31 +00:00
|
|
|
const parser: CalEventParser = new CalEventParser(calEvent, maybeUid);
|
|
|
|
const uid: string = parser.getUid();
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2021-06-16 22:56:02 +00:00
|
|
|
if (!credential) {
|
2021-07-15 01:19:30 +00:00
|
|
|
throw new Error(
|
|
|
|
"Credentials must be set! Video platforms are optional, so this method shouldn't even be called when no video credentials are set."
|
|
|
|
);
|
2021-06-16 22:56:02 +00:00
|
|
|
}
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2021-07-15 01:19:30 +00:00
|
|
|
let success = true;
|
|
|
|
|
|
|
|
const creationResult = await videoIntegrations([credential])[0]
|
|
|
|
.createMeeting(calEvent)
|
|
|
|
.catch((e) => {
|
|
|
|
log.error("createMeeting failed", e, calEvent);
|
|
|
|
success = false;
|
|
|
|
});
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2021-06-16 22:56:02 +00:00
|
|
|
const videoCallData: VideoCallData = {
|
|
|
|
type: credential.type,
|
|
|
|
id: creationResult.id,
|
|
|
|
password: creationResult.password,
|
|
|
|
url: creationResult.join_url,
|
|
|
|
};
|
2021-06-16 21:40:13 +00:00
|
|
|
|
2021-07-20 18:40:41 +00:00
|
|
|
const entryPoint: EntryPoint = {
|
|
|
|
entryPointType: getIntegrationName(videoCallData),
|
|
|
|
uri: videoCallData.url,
|
|
|
|
label: "Enter Meeting",
|
|
|
|
pin: videoCallData.password,
|
|
|
|
};
|
|
|
|
|
|
|
|
const additionInformation: AdditionInformation = {
|
|
|
|
entryPoints: [entryPoint],
|
|
|
|
};
|
|
|
|
|
|
|
|
const organizerMail = new VideoEventOrganizerMail(calEvent, uid, videoCallData, additionInformation);
|
|
|
|
const attendeeMail = new VideoEventAttendeeMail(calEvent, uid, videoCallData, additionInformation);
|
2021-06-21 16:15:05 +00:00
|
|
|
try {
|
|
|
|
await organizerMail.sendEmail();
|
|
|
|
} catch (e) {
|
2021-07-15 01:19:30 +00:00
|
|
|
console.error("organizerMail.sendEmail failed", e);
|
2021-06-21 16:15:05 +00:00
|
|
|
}
|
2021-06-16 22:56:02 +00:00
|
|
|
|
2021-06-16 23:41:56 +00:00
|
|
|
if (!creationResult || !creationResult.disableConfirmationEmail) {
|
2021-06-21 16:15:05 +00:00
|
|
|
try {
|
|
|
|
await attendeeMail.sendEmail();
|
|
|
|
} catch (e) {
|
2021-07-15 01:19:30 +00:00
|
|
|
console.error("attendeeMail.sendEmail failed", e);
|
2021-06-21 16:15:05 +00:00
|
|
|
}
|
2021-06-16 22:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2021-07-15 01:19:30 +00:00
|
|
|
type: credential.type,
|
|
|
|
success,
|
2021-06-16 22:56:02 +00:00
|
|
|
uid,
|
2021-07-15 01:19:30 +00:00
|
|
|
createdEvent: creationResult,
|
|
|
|
originalEvent: calEvent,
|
2021-06-16 22:56:02 +00:00
|
|
|
};
|
2021-06-13 13:22:17 +00:00
|
|
|
};
|
|
|
|
|
2021-07-15 01:19:30 +00:00
|
|
|
const updateMeeting = async (
|
2021-07-28 20:44:52 +00:00
|
|
|
credential: Credential,
|
2021-07-15 01:19:30 +00:00
|
|
|
uidToUpdate: string,
|
|
|
|
calEvent: CalendarEvent
|
|
|
|
): Promise<EventResult> => {
|
2021-06-17 00:44:13 +00:00
|
|
|
const newUid: string = translator.fromUUID(uuidv5(JSON.stringify(calEvent), uuidv5.URL));
|
|
|
|
|
|
|
|
if (!credential) {
|
2021-07-15 01:19:30 +00:00
|
|
|
throw new Error(
|
|
|
|
"Credentials must be set! Video platforms are optional, so this method shouldn't even be called when no video credentials are set."
|
|
|
|
);
|
2021-06-16 22:56:02 +00:00
|
|
|
}
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2021-07-15 01:19:30 +00:00
|
|
|
let success = true;
|
|
|
|
|
|
|
|
const updateResult = credential
|
|
|
|
? await videoIntegrations([credential])[0]
|
|
|
|
.updateMeeting(uidToUpdate, calEvent)
|
|
|
|
.catch((e) => {
|
|
|
|
log.error("updateMeeting failed", e, calEvent);
|
|
|
|
success = false;
|
|
|
|
})
|
|
|
|
: null;
|
2021-06-17 00:44:13 +00:00
|
|
|
|
2021-06-18 00:44:41 +00:00
|
|
|
const organizerMail = new EventOrganizerRescheduledMail(calEvent, newUid);
|
2021-06-17 00:44:13 +00:00
|
|
|
const attendeeMail = new EventAttendeeRescheduledMail(calEvent, newUid);
|
2021-06-21 16:15:05 +00:00
|
|
|
try {
|
|
|
|
await organizerMail.sendEmail();
|
|
|
|
} catch (e) {
|
2021-07-15 01:19:30 +00:00
|
|
|
console.error("organizerMail.sendEmail failed", e);
|
2021-06-21 16:15:05 +00:00
|
|
|
}
|
2021-06-17 00:44:13 +00:00
|
|
|
|
|
|
|
if (!updateResult || !updateResult.disableConfirmationEmail) {
|
2021-06-21 16:15:05 +00:00
|
|
|
try {
|
|
|
|
await attendeeMail.sendEmail();
|
|
|
|
} catch (e) {
|
2021-07-15 01:19:30 +00:00
|
|
|
console.error("attendeeMail.sendEmail failed", e);
|
2021-06-21 16:15:05 +00:00
|
|
|
}
|
2021-06-17 00:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2021-07-15 01:19:30 +00:00
|
|
|
type: credential.type,
|
|
|
|
success,
|
2021-06-17 00:44:13 +00:00
|
|
|
uid: newUid,
|
2021-07-15 01:19:30 +00:00
|
|
|
updatedEvent: updateResult,
|
|
|
|
originalEvent: calEvent,
|
2021-06-17 00:44:13 +00:00
|
|
|
};
|
2021-06-13 13:22:17 +00:00
|
|
|
};
|
|
|
|
|
2021-07-28 20:05:37 +00:00
|
|
|
const deleteMeeting = (credential: Credential, uid: string): Promise<unknown> => {
|
2021-06-16 22:56:02 +00:00
|
|
|
if (credential) {
|
|
|
|
return videoIntegrations([credential])[0].deleteMeeting(uid);
|
|
|
|
}
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2021-06-16 22:56:02 +00:00
|
|
|
return Promise.resolve({});
|
2021-06-13 13:22:17 +00:00
|
|
|
};
|
|
|
|
|
2021-07-15 01:19:30 +00:00
|
|
|
export { getBusyVideoTimes, createMeeting, updateMeeting, deleteMeeting };
|