mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
rework and starts for new friends system
This commit is contained in:
@@ -104,6 +104,5 @@ public class FriendshipsController : Controller
|
||||
Status = f.Status,
|
||||
AddresseeId = f.AddresseeId,
|
||||
RequesterId = f.RequesterId,
|
||||
Requester = BuildUserDtos([f.Requester]).First(),
|
||||
}).ToList();
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using Govor.Application.Interfaces;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Govor.API.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
[Route("api/chats")]
|
||||
public class ChatLoadController : Controller
|
||||
{
|
||||
public ChatLoadController(ILogger<ChatLoadController> logger, IMessagesLoader messagesLoader)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -97,6 +97,26 @@ public class FriendsController : Controller
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("responses")]
|
||||
public async Task<IActionResult> GetResponses()
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _friendsService.GetResponsesAsync(_currentUserService.GetCurrentUserId());
|
||||
return Ok(BuildFriendshipDtos(result));
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return Ok(Array.Empty<FriendshipDto>());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(500, new { error = "Internal server error." });
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("accept")]
|
||||
public async Task<IActionResult> AcceptFriend([FromQuery] Guid friendshipId)
|
||||
{
|
||||
@@ -125,6 +145,34 @@ public class FriendsController : Controller
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("reject")]
|
||||
public async Task<IActionResult> RejectFriend([FromQuery] Guid friendshipId)
|
||||
{
|
||||
if (friendshipId == Guid.Empty)
|
||||
return BadRequest("Requester ID is invalid");
|
||||
|
||||
try
|
||||
{
|
||||
await _friendsService.RejectFriendRequestAsync(friendshipId, _currentUserService.GetCurrentUserId());
|
||||
return Ok(new { message = "Friend request rejected." });
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, ex.Message);
|
||||
return NotFound(new { error = ex.Message });
|
||||
}
|
||||
catch (UnauthorizedAccessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, ex.Message);
|
||||
return Forbid();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(500, new { error = "Failed to accept friend request." });
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetFriends()
|
||||
{
|
||||
@@ -160,6 +208,5 @@ public class FriendsController : Controller
|
||||
Status = f.Status,
|
||||
AddresseeId = f.AddresseeId,
|
||||
RequesterId = f.RequesterId,
|
||||
Requester = BuildUserDtos([f.Requester]).First(),
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
@@ -5,9 +5,11 @@ using Govor.Application.Infrastructure.Validators;
|
||||
using Govor.Application.Interfaces;
|
||||
using Govor.Application.Interfaces.AdminsStuff;
|
||||
using Govor.Application.Interfaces.Authentication;
|
||||
using Govor.Application.Interfaces.Friends;
|
||||
using Govor.Application.Interfaces.Infrastructure.Extensions;
|
||||
using Govor.Application.Interfaces.Messages;
|
||||
using Govor.Application.Services;
|
||||
using Govor.Application.Services.Friends;
|
||||
using Govor.Core.Infrastructure.Extensions;
|
||||
using Govor.Core.Infrastructure.Validators;
|
||||
using Govor.Core.Models;
|
||||
@@ -36,7 +38,11 @@ public static class ConfigurationProgramExtensions
|
||||
services.AddScoped<IInvitesService, InvitesService>();
|
||||
services.AddScoped<IInvitationGenerator, InvitationGenerator>();
|
||||
services.AddScoped<IUsernameValidator, UsernameValidator>();
|
||||
services.AddScoped<IFriendsService, FriendsService>();
|
||||
|
||||
// Friends services
|
||||
services.AddScoped<IFriendshipService, FriendshipService>();
|
||||
services.AddScoped<IFriendRequestService, FriendRequestService>();
|
||||
services.AddScoped<IFriendsBlockService, FriendsBlockService>();
|
||||
|
||||
services.AddScoped<IStorageService>(sp =>
|
||||
{
|
||||
@@ -53,6 +59,7 @@ public static class ConfigurationProgramExtensions
|
||||
services.AddScoped<IMessageService, MessageService>();
|
||||
services.AddScoped<IVerifyFriendship, VerifyFriendship>();
|
||||
services.AddScoped<IUserGroupsService, UserGroupsService>();
|
||||
services.AddScoped<IMessagesLoader, MessagesLoader>();
|
||||
}
|
||||
|
||||
public static void AddRepositories(this IServiceCollection services)
|
||||
@@ -65,6 +72,8 @@ public static class ConfigurationProgramExtensions
|
||||
services.AddScoped<IFriendshipsRepository, FriendshipsRepository>();
|
||||
services.AddScoped<IPrivateChatsRepository, PrivateChatsRepository>();
|
||||
services.AddScoped<IGroupsRepository, GroupRepository>();
|
||||
|
||||
// other
|
||||
}
|
||||
|
||||
public static void AddValidators(this IServiceCollection services)
|
||||
|
||||
@@ -23,5 +23,9 @@
|
||||
<ProjectReference Include="..\Govor.Contracts\Govor.Contracts.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Controllers\Friends\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
|
||||
@@ -17,10 +17,11 @@ public class ChatsHub : Hub
|
||||
private readonly IMessageService _messageService;
|
||||
private readonly IUserGroupsService _userService;
|
||||
|
||||
public ChatsHub(ILogger<ChatsHub> logger, IMessageService messageService)
|
||||
public ChatsHub(ILogger<ChatsHub> logger, IMessageService messageService, IUserGroupsService userService)
|
||||
{
|
||||
_logger = logger;
|
||||
_messageService = messageService;
|
||||
_userService = userService;
|
||||
}
|
||||
|
||||
public override async Task OnConnectedAsync()
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
using Govor.Application.Interfaces.Friends;
|
||||
using Govor.Application.Interfaces.Infrastructure.Extensions;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace Govor.API.Hubs;
|
||||
|
||||
public class FriendsHub : Hub
|
||||
{
|
||||
private readonly IFriendRequestService _friendRequestService;
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
|
||||
public FriendsHub(IFriendRequestService friendRequestService, ICurrentUserService currentUserService)
|
||||
{
|
||||
_friendRequestService = friendRequestService;
|
||||
_currentUserService = currentUserService;
|
||||
}
|
||||
|
||||
public async Task SendRequest(Guid targetUserId)
|
||||
{
|
||||
var userId = _currentUserService.GetCurrentUserId();
|
||||
await _friendRequestService.SendFriendRequestAsync(userId, targetUserId);
|
||||
await Clients.User(targetUserId.ToString()).SendAsync("FriendRequestReceived", userId);
|
||||
}
|
||||
|
||||
public async Task AcceptRequest(Guid friendshipId)
|
||||
{
|
||||
var userId = _currentUserService.GetCurrentUserId();
|
||||
await _friendRequestService.AcceptFriendRequestAsync(friendshipId, userId);
|
||||
await Clients.User(userId.ToString()).SendAsync("FriendRequestAccepted", friendshipId);
|
||||
}
|
||||
|
||||
public async Task RejectRequest(Guid friendshipId)
|
||||
{
|
||||
var userId = _currentUserService.GetCurrentUserId();
|
||||
await _friendRequestService.RejectFriendRequestAsync(friendshipId, userId);
|
||||
await Clients.User(userId.ToString()).SendAsync("FriendRequestRejected", friendshipId);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace Govor.API.Hubs;
|
||||
|
||||
public class UsersHub : Hub
|
||||
{
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user