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";
|
2023-10-02 10:51:04 +00:00
|
|
|
import { getPiiFreeCalendarEvent, getPiiFreeCredential } from "@calcom/lib/piiFreeData";
|
2023-09-30 13:28:52 +00:00
|
|
|
import { safeStringify } from "@calcom/lib/safeStringify";
|
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-09-14 16:53:58 +00:00
|
|
|
import type { CredentialPayload } 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
|
|
|
|
2023-10-17 19:00:48 +00:00
|
|
|
const log = logger.getSubLogger({ 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-10-02 10:51:04 +00:00
|
|
|
log.silly("Getting video adapter for", safeStringify({ appName, cred: getPiiFreeCredential(cred) }));
|
2023-06-23 17:04:34 +00:00
|
|
|
const appImportFn = appStore[appName as keyof typeof appStore];
|
2023-04-05 14:55:57 +00:00
|
|
|
|
2023-06-23 17:04:34 +00:00
|
|
|
// Static Link Video Apps don't exist in packages/app-store/index.ts(it's manually maintained at the moment) and they aren't needed there anyway.
|
|
|
|
const app = appImportFn ? await appImportFn() : null;
|
|
|
|
|
|
|
|
if (!app) {
|
|
|
|
log.error(`Couldn't get adapter for ${appName}`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ("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);
|
2023-09-30 04:52:32 +00:00
|
|
|
} else {
|
|
|
|
log.error(`App ${appName} doesn't have 'lib.VideoApiAdapter' defined`);
|
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
|
|
|
|
2023-09-14 16:53:58 +00:00
|
|
|
const createMeeting = async (credential: CredentialPayload, calEvent: CalendarEvent) => {
|
2021-11-26 11:03:43 +00:00
|
|
|
const uid: string = getUid(calEvent);
|
2023-10-10 04:16:04 +00:00
|
|
|
log.debug(
|
2023-10-02 10:51:04 +00:00
|
|
|
"createMeeting",
|
|
|
|
safeStringify({
|
|
|
|
credential: getPiiFreeCredential(credential),
|
|
|
|
uid,
|
|
|
|
calEvent: getPiiFreeCalendarEvent(calEvent),
|
|
|
|
})
|
|
|
|
);
|
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;
|
2023-07-06 21:39:33 +00:00
|
|
|
credentialId: number;
|
2022-12-07 21:47:02 +00:00
|
|
|
} = {
|
2023-09-14 16:53:58 +00:00
|
|
|
appName: credential.appId || "",
|
2022-12-07 21:47:02 +00:00
|
|
|
type: credential.type,
|
|
|
|
uid,
|
|
|
|
originalEvent: calEvent,
|
|
|
|
success: false,
|
|
|
|
createdEvent: undefined,
|
2023-07-06 21:39:33 +00:00
|
|
|
credentialId: credential.id,
|
2022-12-07 21:47:02 +00:00
|
|
|
};
|
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,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-10-10 04:16:04 +00:00
|
|
|
if (!enabledApp?.enabled)
|
|
|
|
throw `Location app ${credential.appId} is either disabled or not seeded at all`;
|
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 };
|
2023-10-10 04:16:04 +00:00
|
|
|
log.debug("created Meeting", safeStringify(returnObject));
|
2022-10-14 02:36:43 +00:00
|
|
|
} catch (err) {
|
2022-06-25 05:16:20 +00:00
|
|
|
await sendBrokenIntegrationEmail(calEvent, "video");
|
2023-09-30 13:28:52 +00:00
|
|
|
log.error("createMeeting failed", safeStringify({ err, calEvent: getPiiFreeCalendarEvent(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 (
|
2023-09-14 16:53:58 +00:00
|
|
|
credential: CredentialPayload,
|
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-07-15 01:19:30 +00:00
|
|
|
let success = true;
|
2023-04-05 14:55:57 +00:00
|
|
|
const [firstVideoAdapter] = await getVideoAdapters([credential]);
|
2023-09-30 04:52:32 +00:00
|
|
|
const canCallUpdateMeeting = !!(credential && bookingRef);
|
|
|
|
const updatedMeeting = canCallUpdateMeeting
|
|
|
|
? await firstVideoAdapter?.updateMeeting(bookingRef, calEvent).catch(async (e) => {
|
|
|
|
await sendBrokenIntegrationEmail(calEvent, "video");
|
|
|
|
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) {
|
2023-09-30 04:52:32 +00:00
|
|
|
log.error(
|
|
|
|
"updateMeeting failed",
|
|
|
|
JSON.stringify({ bookingRef, canCallUpdateMeeting, calEvent, credential })
|
|
|
|
);
|
2021-11-26 11:03:43 +00:00
|
|
|
return {
|
2023-09-14 16:53:58 +00:00
|
|
|
appName: credential.appId || "",
|
2021-11-26 11:03:43 +00:00
|
|
|
type: credential.type,
|
|
|
|
success,
|
|
|
|
uid,
|
|
|
|
originalEvent: calEvent,
|
|
|
|
};
|
2021-06-17 00:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2023-09-14 16:53:58 +00:00
|
|
|
appName: credential.appId || "",
|
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-09-22 23:13:17 +00:00
|
|
|
const deleteMeeting = async (credential: CredentialPayload | null, 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];
|
2023-10-02 10:51:04 +00:00
|
|
|
log.debug(
|
|
|
|
"Calling deleteMeeting for",
|
|
|
|
safeStringify({ credential: getPiiFreeCredential(credential), uid })
|
|
|
|
);
|
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,
|
2023-09-14 16:53:58 +00:00
|
|
|
user: { email: "" },
|
2023-08-01 11:10:52 +00:00
|
|
|
teamId: 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,
|
2023-09-14 16:53:58 +00:00
|
|
|
user: { email: "" },
|
2023-08-01 11:10:52 +00:00
|
|
|
teamId: null,
|
2022-12-27 21:03:39 +00:00
|
|
|
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,
|
2023-09-14 16:53:58 +00:00
|
|
|
user: { email: "" },
|
2023-08-01 11:10:52 +00:00
|
|
|
teamId: null,
|
2023-03-05 12:59:07 +00:00
|
|
|
key: dailyAppKeys,
|
|
|
|
invalid: false,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
return videoAdapter?.getRecordingDownloadLink?.(recordingId);
|
|
|
|
};
|
|
|
|
|
|
|
|
export {
|
|
|
|
getBusyVideoTimes,
|
|
|
|
createMeeting,
|
|
|
|
updateMeeting,
|
|
|
|
deleteMeeting,
|
|
|
|
getRecordingsOfCalVideoByRoomName,
|
|
|
|
getDownloadLinkOfCalVideoByRecordingId,
|
|
|
|
};
|