|
| 1 | +import { |
| 2 | + WebSocketGateway, |
| 3 | + WebSocketServer, |
| 4 | + SubscribeMessage, |
| 5 | + MessageBody, |
| 6 | + ConnectedSocket, |
| 7 | + OnGatewayInit, |
| 8 | + OnGatewayConnection, |
| 9 | + OnGatewayDisconnect, |
| 10 | +} from "@nestjs/websockets"; |
| 11 | +import { Server, Socket } from "socket.io"; |
| 12 | +import { Logger } from "@nestjs/common"; |
| 13 | +import { GroupChatService } from "./group-chat.service"; |
| 14 | + |
| 15 | +@WebSocketGateway({ cors: { origin: "*" } }) |
| 16 | +export class GroupChatGateway |
| 17 | + implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect |
| 18 | +{ |
| 19 | + @WebSocketServer() |
| 20 | + server: Server; |
| 21 | + |
| 22 | + private readonly logger = new Logger(GroupChatGateway.name); |
| 23 | + |
| 24 | + constructor(private readonly chatService: GroupChatService) {} |
| 25 | + |
| 26 | + afterInit() { |
| 27 | + this.logger.log("GroupChatGateway initialised"); |
| 28 | + } |
| 29 | + |
| 30 | + handleConnection(client: Socket) { |
| 31 | + this.logger.debug(`Client connected: ${client.id}`); |
| 32 | + } |
| 33 | + |
| 34 | + handleDisconnect(client: Socket) { |
| 35 | + this.logger.debug(`Client disconnected: ${client.id}`); |
| 36 | + } |
| 37 | + |
| 38 | + /** Client joins a workspace room */ |
| 39 | + @SubscribeMessage("group-chat:join") |
| 40 | + handleJoin( |
| 41 | + @MessageBody() data: { workspaceId: string }, |
| 42 | + @ConnectedSocket() client: Socket, |
| 43 | + ) { |
| 44 | + if (!data?.workspaceId) return; |
| 45 | + client.join(`ws:${data.workspaceId}`); |
| 46 | + this.logger.debug(`${client.id} joined room ws:${data.workspaceId}`); |
| 47 | + } |
| 48 | + |
| 49 | + /** Client leaves a workspace room */ |
| 50 | + @SubscribeMessage("group-chat:leave") |
| 51 | + handleLeave( |
| 52 | + @MessageBody() data: { workspaceId: string }, |
| 53 | + @ConnectedSocket() client: Socket, |
| 54 | + ) { |
| 55 | + if (!data?.workspaceId) return; |
| 56 | + client.leave(`ws:${data.workspaceId}`); |
| 57 | + } |
| 58 | + |
| 59 | + /** Client sends a message (content and/or attachments) */ |
| 60 | + @SubscribeMessage("group-chat:send") |
| 61 | + async handleSend( |
| 62 | + @MessageBody() |
| 63 | + data: { |
| 64 | + workspaceId: string; |
| 65 | + memberId: string; |
| 66 | + memberName: string; |
| 67 | + memberPhoto?: string; |
| 68 | + content: string; |
| 69 | + attachments?: Array<{ |
| 70 | + url: string; |
| 71 | + name: string; |
| 72 | + type: string; |
| 73 | + size: number; |
| 74 | + }>; |
| 75 | + replyTo?: { |
| 76 | + id: string; |
| 77 | + memberName: string; |
| 78 | + content: string; |
| 79 | + attachments?: Array<{ |
| 80 | + url: string; |
| 81 | + name: string; |
| 82 | + type: string; |
| 83 | + size: number; |
| 84 | + }> | null; |
| 85 | + }; |
| 86 | + }, |
| 87 | + @ConnectedSocket() client: Socket, |
| 88 | + ) { |
| 89 | + const hasContent = data?.content?.trim(); |
| 90 | + const hasAttachments = |
| 91 | + Array.isArray(data?.attachments) && data.attachments.length > 0; |
| 92 | + if (!data?.workspaceId || (!hasContent && !hasAttachments)) return; |
| 93 | + |
| 94 | + const saved = await this.chatService.saveMessage({ |
| 95 | + workspaceId: data.workspaceId, |
| 96 | + memberId: data.memberId, |
| 97 | + memberName: data.memberName, |
| 98 | + memberPhoto: data.memberPhoto ?? null, |
| 99 | + content: data.content?.trim() ?? "", |
| 100 | + attachments: hasAttachments ? data.attachments : undefined, |
| 101 | + replyTo: data.replyTo ?? undefined, |
| 102 | + }); |
| 103 | + |
| 104 | + // broadcast to everyone in the room (including sender) |
| 105 | + this.server.to(`ws:${data.workspaceId}`).emit("group-chat:message", saved); |
| 106 | + } |
| 107 | +} |
0 commit comments