From 46074c89b00680bf087147dc755ceae00937a204 Mon Sep 17 00:00:00 2001 From: Ly Tuan Kiet Date: Fri, 6 Dec 2024 17:03:45 +0700 Subject: [PATCH] feat: enhance WorkshopMeetingRoomSchema with join info and LiveKit integration - Added a new method `workshopMeetingRoomJoinInfo` to provide details for joining a workshop meeting room, including ID, token, and server URL. - Integrated LiveKitService to generate tokens and retrieve server URLs, improving the functionality of the workshop meeting room. - Updated the `init` method to include the new join info field, ensuring proper resolution and access control for users. - Enhanced error handling to ensure unauthorized access is properly managed during the join process. --- .../workshopmeetingroom.schema.ts | 51 ++++++++++++++++--- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/src/WorkshopMeetingRoom/workshopmeetingroom.schema.ts b/src/WorkshopMeetingRoom/workshopmeetingroom.schema.ts index 5acb47f..3e78e16 100644 --- a/src/WorkshopMeetingRoom/workshopmeetingroom.schema.ts +++ b/src/WorkshopMeetingRoom/workshopmeetingroom.schema.ts @@ -1,18 +1,14 @@ import { Inject, Injectable } from '@nestjs/common' -import { - Pothos, - PothosRef, - PothosSchema, - SchemaBuilderToken, -} from '@smatch-corp/nestjs-pothos' +import { Pothos, PothosRef, PothosSchema, SchemaBuilderToken } from '@smatch-corp/nestjs-pothos' import { Builder } from '../Graphql/graphql.builder' import { PrismaService } from '../Prisma/prisma.service' - +import { LiveKitService } from '../LiveKit/livekit.service' @Injectable() export class WorkshopMeetingRoomSchema extends PothosSchema { constructor( @Inject(SchemaBuilderToken) private readonly builder: Builder, private readonly prisma: PrismaService, + private readonly livekitService: LiveKitService, ) { super() } @@ -33,6 +29,22 @@ export class WorkshopMeetingRoomSchema extends PothosSchema { }), }) } + @PothosRef() + workshopMeetingRoomJoinInfo() { + return this.builder.simpleObject('WorkshopMeetingRoomJoinInfo', { + fields: (t) => ({ + id: t.string({ + description: 'The ID of the workshop meeting room.', + }), + token: t.string({ + description: 'The token to join the workshop meeting room.', + }), + serverUrl: t.string({ + description: 'The URL of the server.', + }), + }), + }) + } @Pothos() init(): void { @@ -47,6 +59,31 @@ export class WorkshopMeetingRoomSchema extends PothosSchema { }) }, }), + workshopMeetingRoomJoinInfo: t.field({ + type: this.workshopMeetingRoomJoinInfo(), + args: { + workshopId: t.arg({ + type: 'String', + required: true, + }), + }, + resolve: async (_, args, ctx) => { + if (ctx.isSubscription) throw new Error('Not allowed') + if (!ctx.http?.me) throw new Error('Unauthorized') + const meetingRoom = await this.prisma.workshopMeetingRoom.findUnique({ + where: { + workshopId: args.workshopId, + }, + }) + if (!meetingRoom) throw new Error('Meeting room not found') + const serverUrl = this.livekitService.getServerUrl() + return { + id: meetingRoom.id, + token: await this.livekitService.createToken(ctx.http?.me, meetingRoom.id), + serverUrl, + } + }, + }), workshopMeetingRooms: t.prismaField({ type: [this.workshopMeetingRoom()], args: this.builder.generator.findManyArgs('WorkshopMeetingRoom'),