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-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-12-07 21:47:02 +00:00
|
|
|
import { prisma } from "@calcom/prisma";
|
2023-03-05 12:59:07 +00:00
|
|
|
import type { GetRecordingsResponseSchema } from "@calcom/prisma/zod-utils";
|
2022-07-22 11:52:17 +00:00
|
|
|
import type { CalendarEvent, EventBusyDate } from "@calcom/types/Calendar";
|
2023-03-05 12:59:07 +00:00
|
|
|
import type { CredentialPayload, CredentialWithAppName } from "@calcom/types/Credential";
|
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
|
2023-04-05 14:55:57 +00:00
|
|
|
const getVideoAdapters = async (withCredentials: CredentialPayload[]): Promise<VideoApiAdapter[]> => {
|
|
|
|
const videoAdapters: VideoApiAdapter[] = [];
|
|
|
|
|
|
|
|
for (const cred of withCredentials) {
|
2022-03-23 22:00:30 +00:00
|
|
|
const appName = cred.type.split("_").join(""); // Transform `zoom_video` to `zoomvideo`;
|
2023-04-05 14:55:57 +00:00
|
|
|
const app = await 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);
|
2023-04-05 14:55:57 +00:00
|
|
|
videoAdapters.push(videoAdapter);
|
2021-09-22 18:36:13 +00:00
|
|
|
}
|
2023-04-05 14:55:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return videoAdapters;
|
|
|
|
};
|
2021-09-22 18:36:13 +00:00
|
|
|
|
2023-04-05 14:55:57 +00:00
|
|
|
const getBusyVideoTimes = async (withCredentials: CredentialPayload[]) =>
|
|
|
|
Promise.all((await getVideoAdapters(withCredentials)).map((c) => c?.getAvailability())).then((results) =>
|
2022-07-22 11:52:17 +00:00
|
|
|
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-31 22:06:03 +00:00
|
|
|
const createMeeting = async (credential: CredentialWithAppName, calEvent: CalendarEvent) => {
|
2021-11-26 11:03:43 +00:00
|
|
|
const uid: string = getUid(calEvent);
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2022-12-07 21:47:02 +00:00
|
|
|
if (!credential || !credential.appId) {
|
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
|
|
|
|
2023-04-05 14:55:57 +00:00
|
|
|
const videoAdapters = await getVideoAdapters([credential]);
|
2021-10-26 16:17:24 +00:00
|
|
|
const [firstVideoAdapter] = videoAdapters;
|
2022-10-14 02:36:43 +00:00
|
|
|
let createdMeeting;
|
2022-12-07 21:47:02 +00:00
|
|
|
let returnObject: {
|
|
|
|
appName: string;
|
|
|
|
type: string;
|
|
|
|
uid: string;
|
|
|
|
originalEvent: CalendarEvent;
|
|
|
|
success: boolean;
|
|
|
|
createdEvent: VideoCallData | undefined;
|
|
|
|
} = {
|
|
|
|
appName: credential.appName,
|
|
|
|
type: credential.type,
|
|
|
|
uid,
|
|
|
|
originalEvent: calEvent,
|
|
|
|
success: false,
|
|
|
|
createdEvent: undefined,
|
|
|
|
};
|
2022-10-14 02:36:43 +00:00
|
|
|
try {
|
2022-12-07 21:47:02 +00:00
|
|
|
// Check to see if video app is enabled
|
|
|
|
const enabledApp = await prisma.app.findFirst({
|
|
|
|
where: {
|
|
|
|
slug: credential.appId,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
enabled: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!enabledApp?.enabled) throw "Current location app is not enabled";
|
2022-12-01 15:20:01 +00:00
|
|
|
|
2022-10-14 02:36:43 +00:00
|
|
|
createdMeeting = await firstVideoAdapter?.createMeeting(calEvent);
|
|
|
|
|
2022-12-07 21:47:02 +00:00
|
|
|
returnObject = { ...returnObject, createdEvent: createdMeeting, success: true };
|
2022-10-14 02:36:43 +00:00
|
|
|
} 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) {
|
|
|
|
calEvent.location = "integrations:dailyvideo";
|
|
|
|
}
|
2022-12-07 21:47:02 +00:00
|
|
|
|
|
|
|
returnObject = { ...returnObject, createdEvent: defaultMeeting };
|
2021-10-26 16:17:24 +00:00
|
|
|
}
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2022-12-07 21:47:02 +00:00
|
|
|
return returnObject;
|
2021-06-13 13:22:17 +00:00
|
|
|
};
|
|
|
|
|
2021-11-09 16:27:33 +00:00
|
|
|
const updateMeeting = async (
|
2022-10-31 22:06:03 +00:00
|
|
|
credential: CredentialWithAppName,
|
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;
|
|
|
|
|
2023-04-05 14:55:57 +00:00
|
|
|
const [firstVideoAdapter] = await 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
|
|
|
};
|
|
|
|
|
2023-04-05 14:55:57 +00:00
|
|
|
const deleteMeeting = async (credential: CredentialPayload, uid: string): Promise<unknown> => {
|
2021-06-16 22:56:02 +00:00
|
|
|
if (credential) {
|
2023-04-05 14:55:57 +00:00
|
|
|
const videoAdapter = (await getVideoAdapters([credential]))[0];
|
2022-07-22 11:52:17 +00:00
|
|
|
// 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) => {
|
2022-11-30 16:44:20 +00:00
|
|
|
let dailyAppKeys: Awaited<ReturnType<typeof getDailyAppKeys>>;
|
|
|
|
try {
|
|
|
|
dailyAppKeys = await getDailyAppKeys();
|
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
2023-04-05 14:55:57 +00:00
|
|
|
const [videoAdapter] = await getVideoAdapters([
|
2022-10-14 02:36:43 +00:00
|
|
|
{
|
|
|
|
id: 0,
|
|
|
|
appId: "daily-video",
|
|
|
|
type: "daily_video",
|
|
|
|
userId: null,
|
2022-11-30 16:44:20 +00:00
|
|
|
key: dailyAppKeys,
|
2022-12-07 21:47:02 +00:00
|
|
|
invalid: false,
|
2022-10-14 02:36:43 +00:00
|
|
|
},
|
|
|
|
]);
|
|
|
|
return videoAdapter?.createMeeting(calEvent);
|
|
|
|
};
|
|
|
|
|
2022-12-27 21:03:39 +00:00
|
|
|
const getRecordingsOfCalVideoByRoomName = async (
|
|
|
|
roomName: string
|
|
|
|
): Promise<GetRecordingsResponseSchema | undefined> => {
|
|
|
|
let dailyAppKeys: Awaited<ReturnType<typeof getDailyAppKeys>>;
|
|
|
|
try {
|
|
|
|
dailyAppKeys = await getDailyAppKeys();
|
|
|
|
} catch (e) {
|
2023-03-05 12:59:07 +00:00
|
|
|
console.error("Error: Cal video provider is not installed.");
|
2022-12-27 21:03:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-04-05 14:55:57 +00:00
|
|
|
const [videoAdapter] = await getVideoAdapters([
|
2022-12-27 21:03:39 +00:00
|
|
|
{
|
|
|
|
id: 0,
|
|
|
|
appId: "daily-video",
|
|
|
|
type: "daily_video",
|
|
|
|
userId: null,
|
|
|
|
key: dailyAppKeys,
|
|
|
|
invalid: false,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
return videoAdapter?.getRecordings?.(roomName);
|
|
|
|
};
|
|
|
|
|
2023-03-05 12:59:07 +00:00
|
|
|
const getDownloadLinkOfCalVideoByRecordingId = async (recordingId: string) => {
|
|
|
|
let dailyAppKeys: Awaited<ReturnType<typeof getDailyAppKeys>>;
|
|
|
|
try {
|
|
|
|
dailyAppKeys = await getDailyAppKeys();
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Error: Cal video provider is not installed.");
|
|
|
|
return;
|
|
|
|
}
|
2023-04-05 14:55:57 +00:00
|
|
|
const [videoAdapter] = await getVideoAdapters([
|
2023-03-05 12:59:07 +00:00
|
|
|
{
|
|
|
|
id: 0,
|
|
|
|
appId: "daily-video",
|
|
|
|
type: "daily_video",
|
|
|
|
userId: null,
|
|
|
|
key: dailyAppKeys,
|
|
|
|
invalid: false,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
return videoAdapter?.getRecordingDownloadLink?.(recordingId);
|
|
|
|
};
|
|
|
|
|
|
|
|
export {
|
|
|
|
getBusyVideoTimes,
|
|
|
|
createMeeting,
|
|
|
|
updateMeeting,
|
|
|
|
deleteMeeting,
|
|
|
|
getRecordingsOfCalVideoByRoomName,
|
|
|
|
getDownloadLinkOfCalVideoByRecordingId,
|
|
|
|
};
|