Forráskód Böngészése

SendMessage : empty receiver conn

zinab_elgendy 1 hónapja
szülő
commit
2780903cd5
1 módosított fájl, 14 hozzáadás és 14 törlés
  1. 14 14
      MTWorkHR.Application/Chat/ChatHub.cs

+ 14 - 14
MTWorkHR.Application/Chat/ChatHub.cs

@@ -95,11 +95,20 @@ namespace MTWorkHR.API.Chat
             return CompanyIdLong;
         }
 
-      
 
-      
+        // Get previous messages between two users
+        public async Task GetPreviousMessages(string contactId)
+        {
+            var userId = Context.User.Identities.FirstOrDefault().FindFirst("uid")?.Value;
+            var allmessages = await _unitOfWork.ChatMessage.GetAllWithChildrenAsync(userId, contactId);
+            // Ensure the query is fully materialized before passing it to SignalR
+            var messagesList = await allmessages.Item1.ToListAsync();
+
+            await Clients.Caller.SendAsync("PreviousMessages", messagesList);
+        }
+
         // Send a message from one user to another
-       
+
         public async Task SendMessage(string receiverUserId, string msg)
         {
             try
@@ -113,7 +122,7 @@ namespace MTWorkHR.API.Chat
                 ChatMessage messag = new ChatMessage { 
                     Content = msg, 
                     ReceiverId = receiverUserId, 
-                    ReceiverName = receiverUser.UserName ?? "", 
+                    ReceiverName = receiverUser?.UserName ?? "", 
                     SenderId = userId, 
                     SenderName = userName, 
                     IsSeen = false 
@@ -126,16 +135,7 @@ namespace MTWorkHR.API.Chat
             catch (Exception e) { _logger.LogError(e, e.Message); }
         }
 
-        // Get previous messages between two users
-        public async Task GetPreviousMessages(string contactId)
-        {
-            var userId = Context.User.Identities.FirstOrDefault().FindFirst("uid")?.Value;
-            var allmessages = await _unitOfWork.ChatMessage.GetAllWithChildrenAsync(userId, contactId);
-            // Ensure the query is fully materialized before passing it to SignalR
-            var messagesList = await allmessages.Item1.ToListAsync();
-
-            await Clients.Caller.SendAsync("PreviousMessages", messagesList);
-        }
+      
 
 
         public async Task<Tuple<ChatMessage, int>> GetLastMessage(string contactId)