From acc11d536faf1e1cdd638b0039b05ba3fb56339d Mon Sep 17 00:00:00 2001 From: AlanMoonbase Date: Sat, 28 Jun 2025 10:45:07 -0700 Subject: [PATCH] Fix LINQ Expressions In ``UpdateContactStatus`` and ``DeleteContact`` --- qtc-net-server/Hubs/ChatHub.cs | 21 +++++++++++++++++-- .../Services/ContactService/ContactService.cs | 7 ++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/qtc-net-server/Hubs/ChatHub.cs b/qtc-net-server/Hubs/ChatHub.cs index ae836a7..486dedb 100644 --- a/qtc-net-server/Hubs/ChatHub.cs +++ b/qtc-net-server/Hubs/ChatHub.cs @@ -24,7 +24,7 @@ namespace qtc_api.Hubs if (connection != null) { - var user = OnlineUsers.FirstOrDefault(x => x.Id == connection!.ConnectedUser!.Id); + var user = OnlineUsers.FirstOrDefault(x => x.Id == connection.ConnectedUser!.Id); if (user != null) { @@ -33,10 +33,27 @@ namespace qtc_api.Hubs } } + public async override Task OnConnectedAsync() + { + Log("Client Connected To Hub."); + + var connection = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId); + + if(connection != null) + { + var onlineUser = OnlineUsers.FirstOrDefault(x => x.Id == connection.ConnectedUser!.Id); + + if (onlineUser != null) + { + // if the user is reconnecting, ensure their status is set to Online + await _userService.UpdateStatus(new UserStatusDto { Id = connection.ConnectedUser!.Id, Status = 1 }); + } + } + } + [HubMethodName("LoginHub")] public async Task LoginAsync(User user) { - await Clients.All.SendAsync("rm", $"[SERVER] User {user.Username} Is Now Online"); Log($"User {user.Username} Has Logged In"); var statusDto = new UserStatusDto { Id = user.Id, Status = 1 }; diff --git a/qtc-net-server/Services/ContactService/ContactService.cs b/qtc-net-server/Services/ContactService/ContactService.cs index e109f2e..31aec6b 100644 --- a/qtc-net-server/Services/ContactService/ContactService.cs +++ b/qtc-net-server/Services/ContactService/ContactService.cs @@ -35,9 +35,10 @@ public async Task> UpdateContactStatus(string ownerId, string userId, Contact.ContactStatus ownerStatus, Contact.ContactStatus userStatus) { var serviceResponse = new ServiceResponse(); - var contact = await _dataContext.Contacts.FirstOrDefaultAsync(e => (e.OwnerId == ownerId || e.UserId == userId) || (e.OwnerId == userId || e.UserId == ownerId)); - if(contact != null) + var contact = await _dataContext.Contacts.FirstOrDefaultAsync(e => (e.OwnerId == ownerId && e.UserId == userId) || (e.OwnerId == userId && e.UserId == ownerId)); + + if (contact != null) { contact.OwnerStatus = ownerStatus; contact.UserStatus = userStatus; @@ -53,7 +54,7 @@ public async Task> DeleteContact(string ownerId, string userId) { var serviceResponse = new ServiceResponse(); - var contact = await _dataContext.Contacts.FirstOrDefaultAsync(x => (x.OwnerId == ownerId || x.UserId == userId) || (x.OwnerId == userId || x.UserId == ownerId)); + var contact = await _dataContext.Contacts.FirstOrDefaultAsync(e => (e.OwnerId == ownerId && e.UserId == userId) || (e.OwnerId == userId && e.UserId == ownerId)); if (contact != null) {