technical edits

This commit is contained in:
Artemy
2025-12-12 17:23:01 +07:00
parent bf2aca40d3
commit 7e6f914878
23 changed files with 461 additions and 114 deletions
+91 -28
View File
@@ -1,6 +1,11 @@
using Govor.API.Common.SignalR.Helpers;
using Govor.Application.Interfaces;
using Govor.Application.Interfaces.Friends;
using Govor.Application.Interfaces.Infrastructure.Extensions;
using Govor.Application.Interfaces.Medias;
using Govor.Contracts.Responses.SignalR;
using Govor.Core.Models;
using Govor.Core.Models.Users;
using Govor.Core.Repositories.Groups;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
@@ -11,14 +16,15 @@ namespace Govor.API.Hubs;
public class ProfileHub : Hub
{
private readonly IGroupsRepository _groupsRepository;
private readonly IFriendsService _friendsService;
private readonly IFriendshipService _friendsService;
private readonly IProfileService _profileService;
private readonly IHubUserAccessor _userAccessor;
private readonly ILogger<ProfileHub> _logger;
private readonly IMediaService _mediaService;
public ProfileHub(
IGroupsRepository groupsRepository,
IFriendsService friendsService,
IFriendshipService friendsService,
IProfileService profileService,
IHubUserAccessor userAccessor,
ILogger<ProfileHub> logger)
@@ -33,19 +39,32 @@ public class ProfileHub : Hub
public override async Task OnConnectedAsync()
{
var userId = _userAccessor.GetUserId(Context);
// Добавляем в персональную группу
await Groups.AddToGroupAsync(Context.ConnectionId, $"user-{userId}");
// Friends
var friendships = await _friendsService.GetFriendsAsync(userId);
foreach (var friends in friendships)
await Groups.AddToGroupAsync(Context.ConnectionId, $"friends-{friends.Id}");
try
{
var friendships = await _friendsService.GetFriendsAsync(userId);
foreach (var friends in friendships)
await Groups.AddToGroupAsync(Context.ConnectionId, $"friends-{friends.Id}");
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to get friends for user {userId}", userId);
}
// Groups
var groups = await _groupsRepository.GetByUserIdAsync(userId);
foreach (var group in groups)
await Groups.AddToGroupAsync(Context.ConnectionId, $"group-{group.Id}");
try
{
var groups = await _groupsRepository.GetByUserIdAsync(userId);
foreach (var group in groups)
await Groups.AddToGroupAsync(Context.ConnectionId, $"group-{group.Id}");
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to get groups for user {userId}", userId);
}
_logger.LogInformation("User {UserId} connected with ConnectionId {ConnectionId}", userId, Context.ConnectionId);
@@ -55,24 +74,43 @@ public class ProfileHub : Hub
public override async Task OnDisconnectedAsync(Exception exception)
{
var userId = _userAccessor.GetUserId(Context);
await Groups.RemoveFromGroupAsync(Context.ConnectionId, $"user-{userId}");
// Friends
var friendships = await _friendsService.GetFriendsAsync(userId);
foreach (var friendship in friendships)
await Groups.RemoveFromGroupAsync(Context.ConnectionId, $"friends-{friendship.Id}");
try
{
var friendships = await _friendsService.GetFriendsAsync(userId);
foreach (var friendship in friendships)
await Groups.RemoveFromGroupAsync(Context.ConnectionId, $"friends-{friendship.Id}");
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to remove user {userId} from friend groups", userId);
}
// Groups
var groups = await _groupsRepository.GetByUserIdAsync(userId);
foreach (var group in groups)
await Groups.RemoveFromGroupAsync(Context.ConnectionId, $"group-{group.Id}");
try
{
var groups = await _groupsRepository.GetByUserIdAsync(userId);
foreach (var group in groups)
await Groups.RemoveFromGroupAsync(Context.ConnectionId, $"group-{group.Id}");
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to remove user {userId} from groups", userId);
}
await base.OnDisconnectedAsync(exception);
}
public async Task<HubResult<bool>> SetDescription(string description)
{
if(description.Length > 500)
return HubResult<bool>.Error("The description cannot be longer than 500 characters.");
var userId = _userAccessor.GetUserId(Context);
try
@@ -115,26 +153,51 @@ public class ProfileHub : Hub
return HubResult<bool>.Error("An unaccounted error on the server!");
}
}
private async Task NotifyFriendsAndGroupsAsync(Guid userId, string eventName, object payload)
{
var friendIds = await _friendsService.GetFriendsAsync(userId);
var groups = await _groupsRepository.GetByUserIdAsync(userId);
var friendIds = Enumerable.Empty<User>();
var groups = Enumerable.Empty<ChatGroup>();
try
{
friendIds = await _friendsService.GetFriendsAsync(userId);
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to load friend list for notifications. userId: {userId}", userId);
}
try
{
groups = await _groupsRepository.GetByUserIdAsync(userId);
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to load group list for notifications. userId: {userId}", userId);
}
var groupIds = groups.Select(g => g.Id).ToList();
await Clients.Group($"user-{userId}") // current user
// Current user
await Clients.Group($"user-{userId}")
.SendAsync(eventName, payload);
foreach (var friendId in friendIds)
await Clients.Group($"friends-{friendId}")
// Friends
foreach (var friend in friendIds)
{
await Clients.Group($"friends-{friend.Id}")
.SendAsync(eventName, payload);
}
// Groups
foreach (var groupId in groupIds)
{
await Clients.Group($"group-{groupId}")
.SendAsync(eventName, payload);
}
_logger.LogInformation("Sent {EventName} for {UserId} to {Friends} friends and {Groups} groups",
eventName, userId, friendIds.Count, groupIds.Count);
eventName, userId, friendIds.Count(), groupIds.Count);
}
}