mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
Refactor friend request and media services, remove console commands
Refactored the friend request command service and SignalR hub to return and broadcast FriendshipDto objects, improving real-time updates. Enhanced media upload and download logic to support file storage and retrieval, including database integration. Removed all command classes and related infrastructure from the Govor.Console project, streamlining the console client. Updated dependency injection and interfaces to reflect these changes.
This commit is contained in:
@@ -1,157 +1,70 @@
|
||||
using Microsoft.AspNetCore.SignalR.Client;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.AspNetCore.SignalR.Client;
|
||||
using Govor.Contracts.Responses.SignalR;
|
||||
|
||||
class FriendsHubClient
|
||||
namespace Govor.ConsoleClient;
|
||||
|
||||
|
||||
public class FriendsHubClient
|
||||
{
|
||||
private readonly HubConnection _connection;
|
||||
private readonly string _hubUrl;
|
||||
private readonly string _jwtToken;
|
||||
private HubConnection _connection;
|
||||
|
||||
public FriendsHubClient(string hubUrl, string jwtToken)
|
||||
{
|
||||
_hubUrl = hubUrl;
|
||||
_jwtToken = jwtToken;
|
||||
}
|
||||
|
||||
public async Task ConnectAsync()
|
||||
{
|
||||
_connection = new HubConnectionBuilder()
|
||||
.WithUrl(hubUrl, options =>
|
||||
.WithUrl($"{_hubUrl}/friends", options =>
|
||||
{
|
||||
options.AccessTokenProvider = () => Task.FromResult(jwtToken);
|
||||
options.AccessTokenProvider = () => Task.FromResult(_jwtToken);
|
||||
})
|
||||
.WithAutomaticReconnect()
|
||||
.Build();
|
||||
|
||||
// Подписка на события от сервера
|
||||
_connection.On<Guid>("FriendRequestReceived", OnFriendRequestReceived);
|
||||
_connection.On<Guid>("FriendRequestAccepted", OnFriendRequestAccepted);
|
||||
_connection.On<Guid>("FriendRequestRejected", OnFriendRequestRejected);
|
||||
}
|
||||
// Событие: получен входящий запрос в друзья
|
||||
_connection.On<string>("FriendRequestReceived", userId =>
|
||||
{
|
||||
Console.WriteLine($"📨 Friend request received from: {userId}");
|
||||
});
|
||||
|
||||
private void OnFriendRequestReceived(Guid userId)
|
||||
{
|
||||
Console.WriteLine($"[Event] Получен запрос в друзья от пользователя: {userId}");
|
||||
}
|
||||
// Событие: запрос принят
|
||||
_connection.On<string>("FriendRequestAccepted", friendshipId =>
|
||||
{
|
||||
Console.WriteLine($"✅ Friend request accepted! Friendship ID: {friendshipId}");
|
||||
});
|
||||
|
||||
private void OnFriendRequestAccepted(Guid friendshipId)
|
||||
{
|
||||
Console.WriteLine($"[Event] Запрос в друзья принят. ID дружбы: {friendshipId}");
|
||||
}
|
||||
// Событие: запрос отклонён
|
||||
_connection.On<string>("FriendRequestRejected", friendshipId =>
|
||||
{
|
||||
Console.WriteLine($"❌ Friend request rejected! Friendship ID: {friendshipId}");
|
||||
});
|
||||
|
||||
private void OnFriendRequestRejected(Guid friendshipId)
|
||||
{
|
||||
Console.WriteLine($"[Event] Запрос в друзья отклонён. ID дружбы: {friendshipId}");
|
||||
}
|
||||
|
||||
public async Task StartAsync()
|
||||
{
|
||||
await _connection.StartAsync();
|
||||
Console.WriteLine("Подключено к FriendsHub");
|
||||
Console.WriteLine("✅ Connected to FriendsHub");
|
||||
}
|
||||
|
||||
public async Task StopAsync()
|
||||
public async Task SendFriendRequestAsync(Guid targetUserId)
|
||||
{
|
||||
await _connection.StopAsync();
|
||||
Console.WriteLine("Отключено от FriendsHub");
|
||||
var result = await _connection.InvokeAsync<HubResult<object>>("SendRequest", targetUserId);
|
||||
Console.WriteLine($"📤 Friend request sent. Status: {result.Status}");
|
||||
}
|
||||
|
||||
public async Task SendRequest(Guid targetUserId)
|
||||
public async Task AcceptFriendRequestAsync(Guid friendshipId)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _connection.InvokeAsync("SendRequest", targetUserId);
|
||||
Console.WriteLine($"Запрос на добавление в друзья отправлен пользователю {targetUserId}");
|
||||
}
|
||||
catch (HubException ex)
|
||||
{
|
||||
Console.WriteLine($"Ошибка при отправке запроса: {ex.Message}");
|
||||
}
|
||||
var result = await _connection.InvokeAsync<HubResult<object>>("AcceptRequest", friendshipId);
|
||||
Console.WriteLine($"👍 Accept result: {result.Status}");
|
||||
}
|
||||
|
||||
public async Task AcceptRequest(Guid friendshipId)
|
||||
public async Task RejectFriendRequestAsync(Guid friendshipId)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _connection.InvokeAsync("AcceptRequest", friendshipId);
|
||||
Console.WriteLine($"Запрос в друзья с ID {friendshipId} принят");
|
||||
}
|
||||
catch (HubException ex)
|
||||
{
|
||||
Console.WriteLine($"Ошибка при принятии запроса: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RejectRequest(Guid friendshipId)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _connection.InvokeAsync("RejectRequest", friendshipId);
|
||||
Console.WriteLine($"Запрос в друзья с ID {friendshipId} отклонён");
|
||||
}
|
||||
catch (HubException ex)
|
||||
{
|
||||
Console.WriteLine($"Ошибка при отклонении запроса: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task TestMain(string[] args)
|
||||
{
|
||||
// Адрес хаба SignalR
|
||||
string hubUrl = "https://yourserver.com/api/friends";
|
||||
|
||||
// JWT токен для аутентификации (замени на настоящий)
|
||||
string jwtToken = "your_jwt_token_here";
|
||||
|
||||
var client = new FriendsHubClient(hubUrl, jwtToken);
|
||||
|
||||
await client.StartAsync();
|
||||
|
||||
Console.WriteLine("Команды:");
|
||||
Console.WriteLine("send <userId> - Отправить запрос в друзья");
|
||||
Console.WriteLine("accept <friendshipId> - Принять запрос");
|
||||
Console.WriteLine("reject <friendshipId> - Отклонить запрос");
|
||||
Console.WriteLine("exit - Выйти");
|
||||
|
||||
while (true)
|
||||
{
|
||||
Console.Write("> ");
|
||||
var input = Console.ReadLine();
|
||||
if (string.IsNullOrWhiteSpace(input))
|
||||
continue;
|
||||
|
||||
var parts = input.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
|
||||
var command = parts[0].ToLowerInvariant();
|
||||
|
||||
if (command == "exit")
|
||||
break;
|
||||
|
||||
if (parts.Length < 2)
|
||||
{
|
||||
Console.WriteLine("Ошибка: не хватает аргументов");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!Guid.TryParse(parts[1], out var id))
|
||||
{
|
||||
Console.WriteLine("Ошибка: некорректный GUID");
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (command)
|
||||
{
|
||||
case "send":
|
||||
await client.SendRequest(id);
|
||||
break;
|
||||
case "accept":
|
||||
await client.AcceptRequest(id);
|
||||
break;
|
||||
case "reject":
|
||||
await client.RejectRequest(id);
|
||||
break;
|
||||
default:
|
||||
Console.WriteLine("Неизвестная команда");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
await client.StopAsync();
|
||||
var result = await _connection.InvokeAsync<HubResult<object>>("RejectRequest", friendshipId);
|
||||
Console.WriteLine($"👎 Reject result: {result.Status}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user