diff --git a/src/chat/chat.gateway.ts b/src/chat/chat.gateway.ts index fc16f96..9445558 100644 --- a/src/chat/chat.gateway.ts +++ b/src/chat/chat.gateway.ts @@ -158,6 +158,8 @@ export class ChatGateway //verify memebership, store chat and return event's data const data = await this.chatService.handleRoomMessage(body, sender); + if(!data) return; + // send everone in the room(including the sender) this.server.to(body.roomId).emit('roomMessage', data); } @@ -169,6 +171,6 @@ export class ChatGateway @ConnectedSocket() client: Socket, ) { - return this.chatService.handleStartP2P(body.targetUserId, client); + return this.chatService.handleStartP2P(body.targetUserId, client, this.server); } } diff --git a/src/chat/chat.service.ts b/src/chat/chat.service.ts index ad76712..4d2878b 100644 --- a/src/chat/chat.service.ts +++ b/src/chat/chat.service.ts @@ -2,7 +2,7 @@ import { ConversationsService } from './../conversations/conversations.service'; import { MessagesService } from './../messages/messages.service'; import { Injectable } from '@nestjs/common'; import { RoomMessageDto } from './dto/room-message.dto'; -import { Socket } from 'socket.io'; +import { Socket, Server } from 'socket.io'; import { ConversationType } from 'src/conversations/schemas/conversation.schema'; @Injectable() @@ -38,7 +38,7 @@ export class ChatService { } } - async handleStartP2P(targetUserId: string, client: Socket) { + async handleStartP2P(targetUserId: string, client: Socket, server:Server) { const senderId = client.data.user.id; if (senderId === targetUserId) { @@ -67,8 +67,19 @@ export class ChatService { message: `P2P room ready`, }); + // find target user's socket and notify him/her + const targetSockets=await server.in('/chat').fetchSockets(); + const targetSocket=targetSockets.find(s=>s.data.user?.id===targetUserId) + if(targetSocket){ + targetSocket.join(roomId); + targetSocket.emit("p2pInvite",{ + roomId, + fromUserId:senderId, + message: 'You have been added to a P2P conversation', + }) + } + return { roomId }; } - } diff --git a/src/conversations/conversations.controller.ts b/src/conversations/conversations.controller.ts index 52c5226..89ad9e7 100644 --- a/src/conversations/conversations.controller.ts +++ b/src/conversations/conversations.controller.ts @@ -2,16 +2,19 @@ import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/commo import { ConversationsService } from './conversations.service'; import { CreateConversationDto } from './dto/create-conversation.dto'; import { UpdateConversationDto } from './dto/update-conversation.dto'; +import { ApiOperation } from '@nestjs/swagger'; @Controller('conversations') export class ConversationsController { constructor(private readonly conversationsService: ConversationsService) {} + @ApiOperation({ summary: 'Create a conversation'}) @Post() async create(@Body() createConversationDto: CreateConversationDto) { return await this.conversationsService.create(createConversationDto); } + @ApiOperation({ summary: 'Get all conversations' }) @Get() async findAll() { return await this.conversationsService.findAll(); @@ -24,7 +27,7 @@ export class ConversationsController { @Patch(':id') update(@Param('id') id: string, @Body() updateConversationDto: UpdateConversationDto) { - return this.conversationsService.update(+id, updateConversationDto); + return this.conversationsService.update(id, updateConversationDto); } @Delete(':id') diff --git a/src/conversations/conversations.service.ts b/src/conversations/conversations.service.ts index 4b07d3d..ebf1637 100644 --- a/src/conversations/conversations.service.ts +++ b/src/conversations/conversations.service.ts @@ -31,8 +31,12 @@ export class ConversationsService { // return `This action returns a #${id} conversation`; // } - update(id: number, updateConversationDto: UpdateConversationDto) { - return `This action updates a #${id} conversation`; + async update(id: string, updateConversationDto: UpdateConversationDto) { + await this.conversationModel.findByIdAndUpdate( + {_id:id}, + updateConversationDto, + {new:true} + ) } remove(id: number) {