11package com .connect .service ;
22
3+ import com .connect .buffer .MessageBuffer ;
34import com .connect .dto .MessageDTO ;
45import com .connect .enums .UserStatus ;
56import com .connect .kafka .KafkaPublisherService ;
@@ -37,6 +38,9 @@ public class ChatUserService {
3738 @ Autowired
3839 private KafkaPublisherService kafkaPublisherService ;
3940
41+ @ Autowired
42+ private MessageBuffer messageBuffer ;
43+
4044 private Map <String , User > users = new ConcurrentHashMap <>();
4145
4246 public void greetingHandler (String token ) {
@@ -61,16 +65,14 @@ public void joiningRequestHandler(String username, String roomId) {
6165 return ;
6266 }
6367
64- ObjectId roomID = new ObjectId (roomId );
65-
6668 // Checking the Room exists from the RoomId
67- Optional <Room > requestedRoom = roomRepository .findRoomByID (roomID );
69+ Optional <Room > requestedRoom = roomRepository .findRoomByID (roomId );
6870 if (requestedRoom .isEmpty ()) {
6971 log .error ("No room exists" );
7072 return ;
7173 }
7274 // If Room exists and the User is valid we have to update the room data.
73- roomService .addUserToRoom (requiredUser , roomID );
75+ roomService .addUserToRoom (requiredUser , roomId );
7476 }
7577
7678 public void publishMessageToKafka (MessageDTO messageDTO , String roomId ) {
@@ -80,39 +82,41 @@ public void publishMessageToKafka(MessageDTO messageDTO, String roomId) {
8082 } else if (messageDTO .getMessage ().isEmpty () || roomId .isEmpty ()) {
8183 log .error ("Empty Data occurred" );
8284 return ;
85+ } else if (!ObjectId .isValid (roomId )) {
86+ log .error ("Room ID is not valid" );
87+ return ;
8388 }
84- ObjectId roomID = new ObjectId (roomId );
85- // We are assuming that the roomID is not null so.
89+ // Room ID is valid and not null.
8690 Message message = Message .builder ()
87- .roomId (roomID )
91+ .roomId (roomId )
8892 .sender (messageDTO .getSender ())
8993 .message (messageDTO .getMessage ())
94+ .timeStamp (messageDTO .getTimeStamp ())
9095 .build ();
9196
92- kafkaPublisherService .sendEvent (message );
93- }
94-
95- public Optional <Message > addMessagetoRoom (MessageDTO messageDTO , String roomId ) {
96- if (messageDTO == null ) {
97- log .error ("Message DTO is null" );
98- return Optional .empty ();
99- } else if (messageDTO .getMessage ().isEmpty () || roomId .isEmpty ()) {
100- log .error ("Empty Data occurred" );
101- return Optional .empty ();
102- }
103-
104- ObjectId roomID = new ObjectId (roomId );
97+ System .out .println (message .toString ());
10598
106- return roomService . addMessage ( messageDTO , roomID );
99+ kafkaPublisherService . sendEvent ( message );
107100 }
108101
109102 public Optional <List <Message >> chatHistoryHandler (String roomId ) {
110103 if (roomId .isEmpty ()) {
111104 log .error ("Room ID is null in the chat history handler" );
112105 return Optional .empty ();
113106 }
114- ObjectId roomID = new ObjectId (roomId );
115- return roomRepository .getRoomSpecificMessages (roomID );
107+
108+ // Here we have to check the roomSpecific message is in the buffer or not.
109+ if (messageBuffer .size () != 0 ) {
110+ // Here we need to filter out the room specific messages if present in the buffer.
111+ List <Message > messageList = messageBuffer .getMessages ().stream ()
112+ .filter (message -> message .getRoomId ().equals (roomId ))
113+ .toList ();
114+ if (!messageList .isEmpty ()) {
115+ return Optional .of (messageList );
116+ }
117+ }
118+ // If the buffer is empty we have to fetch the messages from the db.
119+ return roomRepository .getRoomSpecificMessages (roomId );
116120 }
117121
118122 public Optional <List <User >> fetchUser () {
0 commit comments