2022-08-11 00:53:05 +00:00
|
|
|
import { z } from "zod";
|
2021-10-26 16:17:24 +00:00
|
|
|
|
2022-03-16 23:36:43 +00:00
|
|
|
import { handleErrorsJson } from "@calcom/lib/errors";
|
2023-03-05 12:59:07 +00:00
|
|
|
import type { GetRecordingsResponseSchema, GetAccessLinkResponseSchema } from "@calcom/prisma/zod-utils";
|
|
|
|
import { getRecordingsResponseSchema, getAccessLinkResponseSchema } from "@calcom/prisma/zod-utils";
|
2022-03-23 22:00:30 +00:00
|
|
|
import type { CalendarEvent } from "@calcom/types/Calendar";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { CredentialPayload } from "@calcom/types/Credential";
|
2022-03-23 22:00:30 +00:00
|
|
|
import type { PartialReference } from "@calcom/types/EventManager";
|
|
|
|
import type { VideoApiAdapter, VideoCallData } from "@calcom/types/VideoApiAdapter";
|
2022-03-16 23:36:43 +00:00
|
|
|
|
2022-08-11 00:53:05 +00:00
|
|
|
import { getDailyAppKeys } from "./getDailyAppKeys";
|
|
|
|
|
2022-03-23 22:00:30 +00:00
|
|
|
/** @link https://docs.daily.co/reference/rest-api/rooms/create-room */
|
2022-08-11 00:53:05 +00:00
|
|
|
const dailyReturnTypeSchema = z.object({
|
2021-10-26 16:17:24 +00:00
|
|
|
/** Long UID string ie: 987b5eb5-d116-4a4e-8e2c-14fcb5710966 */
|
2022-08-11 00:53:05 +00:00
|
|
|
id: z.string(),
|
2021-10-26 16:17:24 +00:00
|
|
|
/** Not a real name, just a random generated string ie: "ePR84NQ1bPigp79dDezz" */
|
2022-08-11 00:53:05 +00:00
|
|
|
name: z.string(),
|
|
|
|
api_created: z.boolean(),
|
|
|
|
privacy: z.union([z.literal("private"), z.literal("public")]),
|
2021-10-26 16:17:24 +00:00
|
|
|
/** https://api-demo.daily.co/ePR84NQ1bPigp79dDezz */
|
2022-08-11 00:53:05 +00:00
|
|
|
url: z.string(),
|
|
|
|
created_at: z.string(),
|
|
|
|
config: z.object({
|
2022-03-23 22:00:30 +00:00
|
|
|
/** Timestamps expressed in seconds, not in milliseconds */
|
2022-08-11 00:53:05 +00:00
|
|
|
nbf: z.number().optional(),
|
2022-03-23 22:00:30 +00:00
|
|
|
/** Timestamps expressed in seconds, not in milliseconds */
|
2022-08-11 00:53:05 +00:00
|
|
|
exp: z.number(),
|
|
|
|
enable_chat: z.boolean(),
|
|
|
|
enable_knocking: z.boolean(),
|
|
|
|
enable_prejoin_ui: z.boolean(),
|
|
|
|
enable_new_call_ui: z.boolean(),
|
|
|
|
}),
|
|
|
|
});
|
2021-10-26 16:17:24 +00:00
|
|
|
|
|
|
|
export interface DailyEventResult {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
api_created: boolean;
|
|
|
|
privacy: string;
|
|
|
|
url: string;
|
|
|
|
created_at: string;
|
|
|
|
config: Record<string, unknown>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface DailyVideoCallData {
|
|
|
|
type: string;
|
|
|
|
id: string;
|
|
|
|
password: string;
|
|
|
|
url: string;
|
|
|
|
}
|
|
|
|
|
2022-08-11 00:53:05 +00:00
|
|
|
const meetingTokenSchema = z.object({
|
|
|
|
token: z.string(),
|
|
|
|
});
|
2021-10-26 16:17:24 +00:00
|
|
|
|
2022-03-23 22:00:30 +00:00
|
|
|
/** @deprecated use metadata on index file */
|
2022-10-31 22:06:03 +00:00
|
|
|
export const FAKE_DAILY_CREDENTIAL: CredentialPayload & { invalid: boolean } = {
|
2021-10-26 16:17:24 +00:00
|
|
|
id: +new Date().getTime(),
|
|
|
|
type: "daily_video",
|
|
|
|
key: { apikey: process.env.DAILY_API_KEY },
|
|
|
|
userId: +new Date().getTime(),
|
2022-05-02 20:39:35 +00:00
|
|
|
appId: "daily-video",
|
2022-10-31 22:06:03 +00:00
|
|
|
invalid: false,
|
2021-10-26 16:17:24 +00:00
|
|
|
};
|
|
|
|
|
2022-12-27 21:03:39 +00:00
|
|
|
export const fetcher = async (endpoint: string, init?: RequestInit | undefined) => {
|
2022-08-11 00:53:05 +00:00
|
|
|
const { api_key } = await getDailyAppKeys();
|
|
|
|
return fetch(`https://api.daily.co/v1${endpoint}`, {
|
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
Authorization: "Bearer " + api_key,
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
...init?.headers,
|
|
|
|
},
|
|
|
|
...init,
|
|
|
|
}).then(handleErrorsJson);
|
|
|
|
};
|
2021-10-26 16:17:24 +00:00
|
|
|
|
2022-08-11 00:53:05 +00:00
|
|
|
function postToDailyAPI(endpoint: string, body: Record<string, any>) {
|
|
|
|
return fetcher(endpoint, {
|
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
});
|
|
|
|
}
|
2021-10-26 16:17:24 +00:00
|
|
|
|
2022-08-11 00:53:05 +00:00
|
|
|
const DailyVideoApiAdapter = (): VideoApiAdapter => {
|
2021-11-26 11:03:43 +00:00
|
|
|
async function createOrUpdateMeeting(endpoint: string, event: CalendarEvent): Promise<VideoCallData> {
|
2021-10-26 16:17:24 +00:00
|
|
|
if (!event.uid) {
|
|
|
|
throw new Error("We need need the booking uid to create the Daily reference in DB");
|
|
|
|
}
|
2022-12-27 21:03:39 +00:00
|
|
|
const body = await translateEvent(event);
|
|
|
|
const dailyEvent = await postToDailyAPI(endpoint, body).then(dailyReturnTypeSchema.parse);
|
2022-08-11 00:53:05 +00:00
|
|
|
const meetingToken = await postToDailyAPI("/meeting-tokens", {
|
2021-10-26 16:17:24 +00:00
|
|
|
properties: { room_name: dailyEvent.name, is_owner: true },
|
2022-08-11 00:53:05 +00:00
|
|
|
}).then(meetingTokenSchema.parse);
|
2021-10-26 16:17:24 +00:00
|
|
|
|
2021-11-26 11:03:43 +00:00
|
|
|
return Promise.resolve({
|
|
|
|
type: "daily_video",
|
|
|
|
id: dailyEvent.name,
|
2022-08-11 00:53:05 +00:00
|
|
|
password: meetingToken.token,
|
|
|
|
url: dailyEvent.url,
|
2021-11-26 11:03:43 +00:00
|
|
|
});
|
2021-10-26 16:17:24 +00:00
|
|
|
}
|
|
|
|
|
2022-12-27 21:03:39 +00:00
|
|
|
const translateEvent = async (event: CalendarEvent) => {
|
2021-10-26 16:17:24 +00:00
|
|
|
// Documentation at: https://docs.daily.co/reference#list-rooms
|
2022-02-19 00:39:10 +00:00
|
|
|
// added a 1 hour buffer for room expiration
|
2021-10-26 16:17:24 +00:00
|
|
|
const exp = Math.round(new Date(event.endTime).getTime() / 1000) + 60 * 60;
|
2022-12-27 21:03:39 +00:00
|
|
|
const { scale_plan: scalePlan } = await getDailyAppKeys();
|
2021-11-16 14:12:10 +00:00
|
|
|
|
|
|
|
if (scalePlan === "true") {
|
|
|
|
return {
|
2022-01-11 10:24:37 +00:00
|
|
|
privacy: "public",
|
2021-11-16 14:12:10 +00:00
|
|
|
properties: {
|
|
|
|
enable_new_call_ui: true,
|
|
|
|
enable_prejoin_ui: true,
|
|
|
|
enable_knocking: true,
|
|
|
|
enable_screenshare: true,
|
|
|
|
enable_chat: true,
|
|
|
|
exp: exp,
|
2022-12-27 21:03:39 +00:00
|
|
|
enable_recording: "cloud",
|
2021-11-16 14:12:10 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2021-10-26 16:17:24 +00:00
|
|
|
return {
|
2022-01-11 10:24:37 +00:00
|
|
|
privacy: "public",
|
2021-10-26 16:17:24 +00:00
|
|
|
properties: {
|
|
|
|
enable_new_call_ui: true,
|
|
|
|
enable_prejoin_ui: true,
|
|
|
|
enable_knocking: true,
|
|
|
|
enable_screenshare: true,
|
|
|
|
enable_chat: true,
|
|
|
|
exp: exp,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
/** Daily doesn't need to return busy times, so we return empty */
|
|
|
|
getAvailability: () => {
|
|
|
|
return Promise.resolve([]);
|
|
|
|
},
|
2021-11-26 11:03:43 +00:00
|
|
|
createMeeting: async (event: CalendarEvent): Promise<VideoCallData> =>
|
|
|
|
createOrUpdateMeeting("/rooms", event),
|
|
|
|
deleteMeeting: async (uid: string): Promise<void> => {
|
2022-08-11 00:53:05 +00:00
|
|
|
await fetcher(`/rooms/${uid}`, { method: "DELETE" });
|
2021-11-26 11:03:43 +00:00
|
|
|
return Promise.resolve();
|
|
|
|
},
|
|
|
|
updateMeeting: (bookingRef: PartialReference, event: CalendarEvent): Promise<VideoCallData> =>
|
2022-08-11 00:53:05 +00:00
|
|
|
createOrUpdateMeeting(`/rooms/${bookingRef.uid}`, event),
|
2022-12-27 21:03:39 +00:00
|
|
|
getRecordings: async (roomName: string): Promise<GetRecordingsResponseSchema> => {
|
|
|
|
try {
|
|
|
|
const res = await fetcher(`/recordings?room_name=${roomName}`).then(
|
|
|
|
getRecordingsResponseSchema.parse
|
|
|
|
);
|
|
|
|
return Promise.resolve(res);
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error("Something went wrong! Unable to get recording");
|
|
|
|
}
|
|
|
|
},
|
2023-03-05 12:59:07 +00:00
|
|
|
getRecordingDownloadLink: async (recordingId: string): Promise<GetAccessLinkResponseSchema> => {
|
|
|
|
try {
|
|
|
|
const res = await fetcher(`/recordings/${recordingId}/access-link`).then(
|
|
|
|
getAccessLinkResponseSchema.parse
|
|
|
|
);
|
|
|
|
return Promise.resolve(res);
|
|
|
|
} catch (err) {
|
|
|
|
console.log("err", err);
|
|
|
|
throw new Error("Something went wrong! Unable to get recording access link");
|
|
|
|
}
|
|
|
|
},
|
2021-10-26 16:17:24 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default DailyVideoApiAdapter;
|