Skip to content

Commit

Permalink
feat: adding msg status update to ws event
Browse files Browse the repository at this point in the history
  • Loading branch information
libterty committed Mar 30, 2021
1 parent ba1bfb4 commit 25e9e2f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/aggregates/interfaces/chat-event.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as EChatEvt from '../enums';

import * as IAuth from '../../auth/interfaces';
export interface IResponseWithPk {
id: string;
}
Expand All @@ -10,6 +10,10 @@ export interface IEventAggregateResponse<T, K> 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;
}
4 changes: 3 additions & 1 deletion src/handlers/chat-event.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<EChatEvt.EChatEeventFromSocket, IChatEvt.IUpdateChatStatusEvt> = JSON.parse(socketMessage);
event.data.user = payload;
return this.handler(event);
}

Expand Down
4 changes: 4 additions & 0 deletions src/handlers/chat.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion src/sockets/chat.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
32 changes: 32 additions & 0 deletions src/sockets/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<EChatRoom.EChatRoomSocketEvent, IChatRoom.IChatEntity>(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<EChatRoom.EChatRoomSocketEvent, IChatRoom.IChatEntity>(client, type, msgEvent);
}
});
}
}
2 changes: 2 additions & 0 deletions src/sockets/enums/chat-room.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ export enum EChatRoomSocketEvent {
'DELETEPARTICIPATE' = 'deleteparticipate',
// message
'NEWCHATMESSAGE' = 'newchatmessage',
'UPDATESENDSTATUSMSG' = 'updatesendstatusmsg',
'UPDATEREADSTATUSMSG' = 'updatereadstatusmsg',
}

0 comments on commit 25e9e2f

Please sign in to comment.