2021-09-22 19:52:38 +00:00
|
|
|
import { Credential } from "@prisma/client";
|
2021-07-15 01:19:30 +00:00
|
|
|
import short from "short-uuid";
|
2021-09-22 19:52:38 +00:00
|
|
|
import { v5 as uuidv5 } from "uuid";
|
|
|
|
|
2022-03-23 22:00:30 +00:00
|
|
|
import appStore from "@calcom/app-store";
|
2022-10-14 02:36:43 +00:00
|
|
|
import { getDailyAppKeys } from "@calcom/app-store/dailyvideo/lib/getDailyAppKeys";
|
2022-10-19 16:11:50 +00:00
|
|
|
import type { ExtendedCredential } from "@calcom/core/EventManager";
|
2022-06-25 05:16:20 +00:00
|
|
|
import { sendBrokenIntegrationEmail } from "@calcom/emails";
|
2022-03-23 22:00:30 +00:00
|
|
|
import { getUid } from "@calcom/lib/CalEventParser";
|
|
|
|
import logger from "@calcom/lib/logger";
|
2022-07-22 11:52:17 +00:00
|
|
|
import type { CalendarEvent, EventBusyDate } from "@calcom/types/Calendar";
|
2022-03-23 22:00:30 +00:00
|
|
|
import type { EventResult, PartialReference } from "@calcom/types/EventManager";
|
2022-06-17 18:34:41 +00:00
|
|
|
import type { VideoApiAdapter, VideoApiAdapterFactory, VideoCallData } from "@calcom/types/VideoApiAdapter";
|
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();
|
|
|
|
|
2021-06-13 13:22:17 +00:00
|
|
|
// factory
|
2021-10-26 16:17:24 +00:00
|
|
|
const getVideoAdapters = (withCredentials: Credential[]): VideoApiAdapter[] =>
|
2021-09-22 18:36:13 +00:00
|
|
|
withCredentials.reduce<VideoApiAdapter[]>((acc, cred) => {
|
2022-03-23 22:00:30 +00:00
|
|
|
const appName = cred.type.split("_").join(""); // Transform `zoom_video` to `zoomvideo`;
|
|
|
|
const app = appStore[appName as keyof typeof appStore];
|
2022-03-24 03:29:27 +00:00
|
|
|
if (app && "lib" in app && "VideoApiAdapter" in app.lib) {
|
2022-03-23 22:00:30 +00:00
|
|
|
const makeVideoApiAdapter = app.lib.VideoApiAdapter as VideoApiAdapterFactory;
|
|
|
|
const videoAdapter = makeVideoApiAdapter(cred);
|
|
|
|
acc.push(videoAdapter);
|
|
|
|
return acc;
|
2021-09-22 18:36:13 +00:00
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
|
2021-12-09 15:51:37 +00:00
|
|
|
const getBusyVideoTimes = (withCredentials: Credential[]) =>
|
2022-07-22 11:52:17 +00:00
|
|
|
Promise.all(getVideoAdapters(withCredentials).map((c) => c?.getAvailability())).then((results) =>
|
|
|
|
results.reduce((acc, availability) => acc.concat(availability), [] as (EventBusyDate | undefined)[])
|
2021-07-15 01:19:30 +00:00
|
|
|
);
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2022-10-19 16:11:50 +00:00
|
|
|
const createMeeting = async (credential: ExtendedCredential, calEvent: CalendarEvent) => {
|
2021-11-26 11:03:43 +00:00
|
|
|
const uid: string = getUid(calEvent);
|
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-10-26 16:17:24 +00:00
|
|
|
const videoAdapters = getVideoAdapters([credential]);
|
|
|
|
const [firstVideoAdapter] = videoAdapters;
|
2022-10-14 02:36:43 +00:00
|
|
|
let createdMeeting;
|
|
|
|
try {
|
|
|
|
createdMeeting = await firstVideoAdapter?.createMeeting(calEvent);
|
|
|
|
|
|
|
|
if (!createdMeeting) {
|
|
|
|
return {
|
2022-10-19 16:11:50 +00:00
|
|
|
appName: credential.appName,
|
2022-10-14 02:36:43 +00:00
|
|
|
type: credential.type,
|
|
|
|
success: false,
|
|
|
|
uid,
|
|
|
|
originalEvent: calEvent,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2022-06-25 05:16:20 +00:00
|
|
|
await sendBrokenIntegrationEmail(calEvent, "video");
|
2022-10-14 02:36:43 +00:00
|
|
|
console.error("createMeeting failed", err, calEvent);
|
2021-10-26 16:17:24 +00:00
|
|
|
|
2022-10-14 02:36:43 +00:00
|
|
|
// Default to calVideo
|
|
|
|
const defaultMeeting = await createMeetingWithCalVideo(calEvent);
|
|
|
|
if (defaultMeeting) {
|
|
|
|
createdMeeting = defaultMeeting;
|
|
|
|
calEvent.location = "integrations:dailyvideo";
|
|
|
|
}
|
2021-10-26 16:17:24 +00:00
|
|
|
}
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2021-06-16 22:56:02 +00:00
|
|
|
return {
|
2022-10-19 16:11:50 +00:00
|
|
|
appName: credential.appName,
|
2021-07-15 01:19:30 +00:00
|
|
|
type: credential.type,
|
2021-12-22 20:38:24 +00:00
|
|
|
success: true,
|
2021-06-16 22:56:02 +00:00
|
|
|
uid,
|
2021-10-26 16:17:24 +00:00
|
|
|
createdEvent: createdMeeting,
|
2021-07-15 01:19:30 +00:00
|
|
|
originalEvent: calEvent,
|
2021-06-16 22:56:02 +00:00
|
|
|
};
|
2021-06-13 13:22:17 +00:00
|
|
|
};
|
|
|
|
|
2021-11-09 16:27:33 +00:00
|
|
|
const updateMeeting = async (
|
2022-10-19 16:11:50 +00:00
|
|
|
credential: ExtendedCredential,
|
2021-11-09 16:27:33 +00:00
|
|
|
calEvent: CalendarEvent,
|
2021-11-26 11:03:43 +00:00
|
|
|
bookingRef: PartialReference | null
|
2022-06-17 18:34:41 +00:00
|
|
|
): Promise<EventResult<VideoCallData>> => {
|
2021-11-26 11:03:43 +00:00
|
|
|
const uid = translator.fromUUID(uuidv5(JSON.stringify(calEvent), uuidv5.URL));
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2021-07-15 01:19:30 +00:00
|
|
|
let success = true;
|
|
|
|
|
2021-10-26 16:17:24 +00:00
|
|
|
const [firstVideoAdapter] = getVideoAdapters([credential]);
|
2021-11-09 16:27:33 +00:00
|
|
|
const updatedMeeting =
|
2021-11-26 11:03:43 +00:00
|
|
|
credential && bookingRef
|
2022-07-22 11:52:17 +00:00
|
|
|
? await firstVideoAdapter?.updateMeeting(bookingRef, calEvent).catch(async (e) => {
|
2022-06-25 05:16:20 +00:00
|
|
|
await sendBrokenIntegrationEmail(calEvent, "video");
|
2021-11-09 16:27:33 +00:00
|
|
|
log.error("updateMeeting failed", e, calEvent);
|
|
|
|
success = false;
|
|
|
|
return undefined;
|
|
|
|
})
|
|
|
|
: undefined;
|
2021-10-26 16:17:24 +00:00
|
|
|
|
2021-11-26 11:03:43 +00:00
|
|
|
if (!updatedMeeting) {
|
|
|
|
return {
|
2022-10-19 16:11:50 +00:00
|
|
|
appName: credential.appName,
|
2021-11-26 11:03:43 +00:00
|
|
|
type: credential.type,
|
|
|
|
success,
|
|
|
|
uid,
|
|
|
|
originalEvent: calEvent,
|
|
|
|
};
|
2021-06-17 00:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2022-10-19 16:11:50 +00:00
|
|
|
appName: credential.appName,
|
2021-07-15 01:19:30 +00:00
|
|
|
type: credential.type,
|
|
|
|
success,
|
2021-11-09 16:27:33 +00:00
|
|
|
uid,
|
2021-10-26 16:17:24 +00:00
|
|
|
updatedEvent: updatedMeeting,
|
2021-07-15 01:19:30 +00:00
|
|
|
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) {
|
2022-07-22 11:52:17 +00:00
|
|
|
const videoAdapter = getVideoAdapters([credential])[0];
|
|
|
|
// There are certain video apps with no video adapter defined. e.g. riverby,whereby
|
|
|
|
if (videoAdapter) {
|
|
|
|
return videoAdapter.deleteMeeting(uid);
|
|
|
|
}
|
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 Promise.resolve({});
|
2021-06-13 13:22:17 +00:00
|
|
|
};
|
|
|
|
|
2022-10-14 02:36:43 +00:00
|
|
|
// @TODO: This is a temporary solution to create a meeting with cal.com video as fallback url
|
|
|
|
const createMeetingWithCalVideo = async (calEvent: CalendarEvent) => {
|
|
|
|
const [videoAdapter] = getVideoAdapters([
|
|
|
|
{
|
|
|
|
id: 0,
|
|
|
|
appId: "daily-video",
|
|
|
|
type: "daily_video",
|
|
|
|
userId: null,
|
|
|
|
key: await getDailyAppKeys(),
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
return videoAdapter?.createMeeting(calEvent);
|
|
|
|
};
|
|
|
|
|
2021-07-15 01:19:30 +00:00
|
|
|
export { getBusyVideoTimes, createMeeting, updateMeeting, deleteMeeting };
|