diff --git a/src/domain/callService.schema.ts b/src/domain/callService.schema.ts new file mode 100644 index 0000000..a6f84af --- /dev/null +++ b/src/domain/callService.schema.ts @@ -0,0 +1,17 @@ +export interface InviteToCallReq { + userid: string + chatid: string + targetId: string +} + +export interface GetRTCAccessReq { + roomId: string + roomType: string + userid: string + networkId: string +} + +export interface GetRTCAccessResp { + rtcConfig: RTCConfiguration + liveKitToken: string +} \ No newline at end of file diff --git a/src/mocks/handlers/call.http.ts b/src/mocks/handlers/call.http.ts new file mode 100644 index 0000000..6d90750 --- /dev/null +++ b/src/mocks/handlers/call.http.ts @@ -0,0 +1,10 @@ +import {http, HttpResponse} from "msw"; +import {GetRTCAccessResp} from "../../domain/callService.schema"; + +export const callHandlers = [ + http.post('*/v2/chat/getRTCAccess', () => { + return HttpResponse.json({ + liveKitToken: "liveKitToken", + }) + }), +] \ No newline at end of file diff --git a/src/mocks/index.ts b/src/mocks/index.ts index 92b575f..67e34a7 100644 --- a/src/mocks/index.ts +++ b/src/mocks/index.ts @@ -1,9 +1,11 @@ import {networkHandlers} from "./handlers/auth.http"; import {authHandlers} from "./handlers/network.http"; import {pictureHandlers} from "./handlers/picture.http"; +import {callHandlers} from "./handlers/call.http"; export const allHandlers = [ ...authHandlers, ...networkHandlers, - ...pictureHandlers + ...pictureHandlers, + ...callHandlers ] \ No newline at end of file diff --git a/src/services/callService.test.ts b/src/services/callService.test.ts new file mode 100644 index 0000000..7819717 --- /dev/null +++ b/src/services/callService.test.ts @@ -0,0 +1,11 @@ +import {describe, expect, it} from "vitest"; +import {CallService} from "./callService"; + +describe("CallService", () => { + const handler = new CallService("", "") + + it('should get RTC access', async () => { + const access = await handler.getRTCAccess("", "", "", null) + expect(access.liveKitToken).toBe("liveKitToken"); + }); +}) \ No newline at end of file diff --git a/src/services/callService.ts b/src/services/callService.ts new file mode 100644 index 0000000..249d58d --- /dev/null +++ b/src/services/callService.ts @@ -0,0 +1,60 @@ +import {getClient} from "../core/http"; +import {OtpPleCodeSendTestingResp, OtpSendCodeReq} from "../domain/authService.schema"; +import {isAxiosError} from "axios"; +import {GenericErrorBody} from "../domain/http.schema"; +import {GetRTCAccessReq, GetRTCAccessResp, InviteToCallReq} from "../domain/callService.schema"; + +export class CallService { + userid: string; + token: string; + + constructor(token: string, userid: string) { + this.token = token; + this.userid = userid; + } + + /** + * Calls your chat partner (WebSocket + Apple VoIP PUSH) + * @param targetId + * @param chatid + */ + async inviteToCall(targetId: string, chatid: string): Promise { + try { + await getClient(false).post("v2/chat/rtcInvite", { + userid: this.userid, + chatid: chatid, + targetId: targetId + }); + return + } catch (e) { + if (isAxiosError(e)) { + throw e; + } + throw new Error("Unexpected error") + } + } + + /** + * Provides access to liveKit and a WebRTC configuration + * @param targetId + * @param roomId + * @param roomType + * @param networkId + */ + async getRTCAccess(targetId: string, roomId: string, roomType: string, networkId: string|null): Promise { + try { + const resp = await getClient(false).post("v2/chat/getRTCAccess", { + userid: targetId, + networkId: networkId, + roomId: roomId, + roomType: roomType, + }); + return resp.data + } catch (e) { + if (isAxiosError(e)) { + throw e; + } + throw new Error("Unexpected error") + } + } +} \ No newline at end of file