瀏覽代碼

SendMessage : empty receiver conn

zinab_elgendy 2 周之前
父節點
當前提交
2780903cd5
共有 1 個文件被更改,包括 14 次插入14 次删除
  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)