From 14dd3273f9e9204aa88bca97185983defd83715c Mon Sep 17 00:00:00 2001 From: AlanMoonbase Date: Sun, 6 Jul 2025 10:22:16 -0700 Subject: [PATCH] Rework Contacts List Refreshing --- .../Controllers/ContactController.cs | 19 ++++++++++++++++++- qtc-net-server/Hubs/ChatHub.cs | 14 -------------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/qtc-net-server/Controllers/ContactController.cs b/qtc-net-server/Controllers/ContactController.cs index 931a40f..fc4811e 100644 --- a/qtc-net-server/Controllers/ContactController.cs +++ b/qtc-net-server/Controllers/ContactController.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc; using qtc_api.Models; using qtc_api.Services.ContactService; +using System.Runtime.CompilerServices; using System.Security.Claims; namespace qtc_api.Controllers @@ -11,10 +12,12 @@ namespace qtc_api.Controllers public class ContactController : ControllerBase { private IContactService _contactService; + private IHubContext _chatGwContext; - public ContactController(IContactService contactService) + public ContactController(IContactService contactService, IHubContext chatGwContext) { _contactService = contactService; + _chatGwContext = chatGwContext; } [HttpPost("add-contact")] @@ -30,6 +33,10 @@ namespace qtc_api.Controllers var result = await _contactService.CreateContact(ownerId, userId); + // refresh contacts list for both users + await _chatGwContext.Clients.User(userId).SendAsync("RefreshContactsList"); + await _chatGwContext.Clients.User(ownerId).SendAsync("RefreshContactsList"); + return Ok(result); } @@ -48,6 +55,11 @@ namespace qtc_api.Controllers var ownerId = claims.First().Value; var result = await _contactService.UpdateContactStatus(ownerId, userId, Contact.ContactStatus.Accepted, Contact.ContactStatus.Accepted); + + // refresh contacts list for both users + await _chatGwContext.Clients.User(userId).SendAsync("RefreshContactsList"); + await _chatGwContext.Clients.User(ownerId).SendAsync("RefreshContactsList"); + return Ok(result); } @@ -66,6 +78,11 @@ namespace qtc_api.Controllers var ownerId = claims.First().Value; var response = await _contactService.DeleteContact(ownerId, userId); + + // refresh contacts list for both users + await _chatGwContext.Clients.User(userId).SendAsync("RefreshContactsList"); + await _chatGwContext.Clients.User(ownerId).SendAsync("RefreshContactsList"); + return Ok(response); } diff --git a/qtc-net-server/Hubs/ChatHub.cs b/qtc-net-server/Hubs/ChatHub.cs index 4cb1dff..123d56c 100644 --- a/qtc-net-server/Hubs/ChatHub.cs +++ b/qtc-net-server/Hubs/ChatHub.cs @@ -152,20 +152,6 @@ namespace qtc_api.Hubs await Clients.All.SendAsync("RefreshRoomList"); } - [HubMethodName("RefreshContactsListOnUser")] - [Authorize] - public async Task RefreshContactsListForUser(UserInformationDto user, User execUser) - { - var connection = ConnectedUsers.FirstOrDefault(e => e.ConnectedUser.Id == user.Id); - var connection2 = ConnectedUsers.FirstOrDefault(e => e.ConnectedUser.Id == execUser.Id); - if (connection != null && connection2 != null) - { - await Clients.Client(connection.ConnectionId).SendAsync("RefreshContactsList"); - await Clients.Client(connection2.ConnectionId).SendAsync("RefreshContactsList"); - return; - } - } - [HubMethodName("SendMessage")] [Authorize] public async Task SendMessageAsync(User user, Message message, bool IsLobbyMsg, Room room = null!)