From 7b2b94af82d26e9b6fe076d245bd68b7eebff64f Mon Sep 17 00:00:00 2001 From: Hairun Huang Date: Sun, 24 Mar 2024 13:14:35 -0700 Subject: [PATCH 1/3] show history chat --- Hubs/ChatHub.cs | 25 ++++++++-------- Pages/Index.cshtml | 41 +++++++++++++------------- Pages/Shared/_Layout.cshtml | 17 ++++++----- app.db | Bin 36864 -> 36864 bytes app.db-shm | Bin 0 -> 32768 bytes app.db-wal | Bin 0 -> 24752 bytes wwwroot/js/chat.js | 56 +++++++++++++++++++++++++++++------- 7 files changed, 89 insertions(+), 50 deletions(-) create mode 100644 app.db-shm create mode 100644 app.db-wal diff --git a/Hubs/ChatHub.cs b/Hubs/ChatHub.cs index 20deb6d..af8d6d7 100644 --- a/Hubs/ChatHub.cs +++ b/Hubs/ChatHub.cs @@ -179,20 +179,18 @@ public async Task SendMessage(string user, string message) // Get the messages from the Database public async Task ChatHistory() { - // Get the language of requesting user var connectionId = Context.ConnectionId; - var language = _userPreferences[connectionId].Language; - - // Get the last 10 messages from the database that match language - var lastMessages = _context.Messages.Where(m => m.Language == language).OrderByDescending(m => m.SentDate).Take(10).ToList(); + var language = _userPreferences.ContainsKey(connectionId) ? _userPreferences[connectionId].Language : "en"; // Default to English if not set + // Fetch the last N messages from the database that match the language + var lastMessages = _context.Messages + .OrderByDescending(m => m.SentDate) + .Take(20) // Adjust the number of messages as needed + .ToList(); + lastMessages.Reverse(); - // Check if the user wants to receive notifications - if (_userPreferences[connectionId].ReceiveNotifications) - { - // Send the messages to the user - await Clients.Client(connectionId).SendAsync("ReceiveChatHistory", lastMessages, _userPreferences[connectionId].Language); - } + // Send the messages to the user + await Clients.Client(connectionId).SendAsync("ReceiveChatHistory", lastMessages); } @@ -288,11 +286,12 @@ private async Task GenerateSummaryAsync(List messages) } // Override OnConnectedAsync to track connections - public override Task OnConnectedAsync() + public override async Task OnConnectedAsync() { _logger.LogInformation("Connection ID: " + Context.ConnectionId); _userPreferences.Add(Context.ConnectionId, new UserPreferences()); - return base.OnConnectedAsync(); + await base.OnConnectedAsync(); + await ChatHistory(); } // Override OnDisconnectedAsync to clean up when a connection is lost diff --git a/Pages/Index.cshtml b/Pages/Index.cshtml index 6e8dcde..3100b70 100644 --- a/Pages/Index.cshtml +++ b/Pages/Index.cshtml @@ -1,26 +1,27 @@ @page
-
-
- @* messages will be inserted here *@ -
-
- Language: - User: -
-
- Message: - -
-
- -
+
+
+ @* messages will be inserted here *@ +
+
+ Language: + + User:
+
+ Message: + +
+ +
diff --git a/Pages/Shared/_Layout.cshtml b/Pages/Shared/_Layout.cshtml index bd97d11..e1671f2 100644 --- a/Pages/Shared/_Layout.cshtml +++ b/Pages/Shared/_Layout.cshtml @@ -1,5 +1,6 @@  + @@ -8,13 +9,14 @@ +
diff --git a/app.db-shm b/app.db-shm deleted file mode 100644 index fe9ac2845eca6fe6da8a63cd096d9cf9e24ece10..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 32768 zcmeIuAr62r3