diff --git a/src/aggregates/interfaces/chat-event.interface.ts b/src/aggregates/interfaces/chat-event.interface.ts index 1c8d061..35a0893 100644 --- a/src/aggregates/interfaces/chat-event.interface.ts +++ b/src/aggregates/interfaces/chat-event.interface.ts @@ -1,5 +1,5 @@ import * as EChatEvt from '../enums'; - +import * as IAuth from '../../auth/interfaces'; export interface IResponseWithPk { id: string; } @@ -10,6 +10,10 @@ export interface IEventAggregateResponse extends IResponseWithPk { } export interface IUpdateChatStatusEvt { - type: EChatEvt.EChatSendStatus | EChatEvt.EChatStatus; - participateId: string; + sendStatus?: EChatEvt.EChatSendStatus; + readStatus?: EChatEvt.EChatStatus; + participateId?: string; + chatId?: string; + requestUserId: string; + user: IAuth.JwtPayload; } diff --git a/src/handlers/chat-event.handler.ts b/src/handlers/chat-event.handler.ts index 6993409..d6d391d 100644 --- a/src/handlers/chat-event.handler.ts +++ b/src/handlers/chat-event.handler.ts @@ -2,6 +2,7 @@ import { Injectable, InternalServerErrorException, Logger } from '@nestjs/common import Kafka from 'node-rdkafka'; import { ChatEventProudcerService } from '../producers/chatevent.producer'; import { ChatEventAggregate } from '../aggregates/chat-event.aggregate'; +import * as IAuth from '../auth/interfaces'; import * as EChatEvt from '../aggregates/enums'; import * as IChatEvt from '../aggregates/interfaces'; import { config } from '../../config'; @@ -18,9 +19,10 @@ export class ChatEventRoutingService { * @param {string} socketMessage * @returns {void} */ - public register(socketMessage: string): void { + public register(socketMessage: string, payload: IAuth.JwtPayload): void { if (!socketMessage) throw new InternalServerErrorException('Non socket message is being proecssed'); const event: IChatEvt.IEventAggregateResponse = JSON.parse(socketMessage); + event.data.user = payload; return this.handler(event); } diff --git a/src/handlers/chat.handler.ts b/src/handlers/chat.handler.ts index 3371b69..795ebd1 100644 --- a/src/handlers/chat.handler.ts +++ b/src/handlers/chat.handler.ts @@ -34,6 +34,10 @@ export class ChatMessageRoutingService { return this.chatSocketService.sendNewChatRoom(event.type, event.data as IChatRoom.IChatRoomEntity); case EChatRoom.EChatRoomSocketEvent.NEWCHATMESSAGE: return this.chatSocketService.sendNewChatMessage(event.type, event.data as IChatRoom.IChatEntity); + case EChatRoom.EChatRoomSocketEvent.UPDATEREADSTATUSMSG: + return this.chatSocketService.sendNewChatReadStatus(event.type, event.data as IChatRoom.IChatEntity); + case EChatRoom.EChatRoomSocketEvent.UPDATESENDSTATUSMSG: + return this.chatSocketService.sendNewChatSendStatus(event.type, event.data as IChatRoom.IChatEntity); } } } diff --git a/src/sockets/chat.gateway.ts b/src/sockets/chat.gateway.ts index edd6955..7dba0c4 100644 --- a/src/sockets/chat.gateway.ts +++ b/src/sockets/chat.gateway.ts @@ -47,7 +47,7 @@ export class ChatSocketGateway { ws['uid'] = payload.id; ws.on('message', (message: string) => { this.logger.log('Messaging is on'); - this.chatEventRoutingService.register(message); + this.chatEventRoutingService.register(message, payload); }); this.logger.log('Connecting ws success'); } diff --git a/src/sockets/chat.service.ts b/src/sockets/chat.service.ts index 74fce13..25bfcc8 100644 --- a/src/sockets/chat.service.ts +++ b/src/sockets/chat.service.ts @@ -73,4 +73,36 @@ export class ChatSocketService { } }); } + + /** + * @description send new chat read status + * @public + * @param {EChatRoom.EChatRoomSocketEvent} type + * @param {IChatRoom.IChatRoomEntity} msgEvent + * @returns {void} + */ + public sendNewChatReadStatus(type: EChatRoom.EChatRoomSocketEvent, msgEvent: IChatRoom.IChatEntity): void { + this.chatSocketGateway.wss.clients.forEach((client: IChatRoom.ISocketWithIdentity) => { + const isClient = this.isRightClient(client, msgEvent); + if (isClient) { + this.sendEvent(client, type, msgEvent); + } + }); + } + + /** + * @description send new chat send status + * @public + * @param {EChatRoom.EChatRoomSocketEvent} type + * @param {IChatRoom.IChatRoomEntity} msgEvent + * @returns {void} + */ + public sendNewChatSendStatus(type: EChatRoom.EChatRoomSocketEvent, msgEvent: IChatRoom.IChatEntity): void { + this.chatSocketGateway.wss.clients.forEach((client: IChatRoom.ISocketWithIdentity) => { + const isClient = this.isRightClient(client, msgEvent); + if (isClient) { + this.sendEvent(client, type, msgEvent); + } + }); + } } diff --git a/src/sockets/enums/chat-room.enum.ts b/src/sockets/enums/chat-room.enum.ts index c6d834b..3f24cb2 100644 --- a/src/sockets/enums/chat-room.enum.ts +++ b/src/sockets/enums/chat-room.enum.ts @@ -14,4 +14,6 @@ export enum EChatRoomSocketEvent { 'DELETEPARTICIPATE' = 'deleteparticipate', // message 'NEWCHATMESSAGE' = 'newchatmessage', + 'UPDATESENDSTATUSMSG' = 'updatesendstatusmsg', + 'UPDATEREADSTATUSMSG' = 'updatereadstatusmsg', }