mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
new app server and database + added hub and controller of user profile
This commit is contained in:
@@ -97,6 +97,8 @@ public static class ConfigurationProgramExtensions
|
||||
services.AddScoped<ISessionKeyAttacher, SessionKeyAttacher>();
|
||||
services.AddScoped<ISessionKeysReader, SessionKeysReader>();
|
||||
services.AddScoped<IOneTimePreKeysRotator, OneTimePreKeysRotator>();
|
||||
|
||||
services.AddScoped<IProfileService, ProfileService>();
|
||||
}
|
||||
|
||||
public static void AddRepositories(this IServiceCollection services)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using AutoMapper;
|
||||
using Govor.API.Extensions.Mapping;
|
||||
using Govor.Application.Profiles;
|
||||
using Govor.Contracts.DTOs;
|
||||
using Govor.Contracts.Responses;
|
||||
using Govor.Core.Models;
|
||||
@@ -23,5 +24,7 @@ public class MappingProfile : Profile
|
||||
CreateMap<Friendship, FriendshipDto>();
|
||||
|
||||
CreateMap<UserSession, SessionDto>();
|
||||
|
||||
CreateMap<UserProfile, UserProfileDto>();
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ namespace Govor.API.Controllers.AdminStuff;
|
||||
|
||||
[Route("api/admin/[controller]")]
|
||||
[ApiController]
|
||||
[Authorize(Roles = "Admin")]
|
||||
//[Authorize(Roles = "Admin")]
|
||||
public class InviteUserController : Controller
|
||||
{
|
||||
private readonly IInvitesRepository _repository;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using Govor.Application.Interfaces.Infrastructure.Extensions;
|
||||
using Govor.Application.Interfaces.Medias;
|
||||
using Govor.Contracts.Requests;
|
||||
using Govor.Core.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
@@ -47,7 +48,7 @@ public class MediaController : Controller
|
||||
try
|
||||
{
|
||||
byte[] fileBytes = await ReadFileAsync(request.FromFile);
|
||||
|
||||
|
||||
var media = new Media(
|
||||
_currentUserService.GetCurrentUserId(),
|
||||
DateTime.UtcNow,
|
||||
@@ -55,8 +56,15 @@ public class MediaController : Controller
|
||||
fileBytes,
|
||||
request.Type,
|
||||
request.MimeType,
|
||||
request.EncryptedKey
|
||||
);
|
||||
request.EncryptedKey,
|
||||
request.OwnerType,
|
||||
null)
|
||||
{
|
||||
OwnerType = request.OwnerType,
|
||||
OwnerId = request.OwnerType == MediaOwnerType.Avatar
|
||||
? _currentUserService.GetCurrentUserId()
|
||||
: null,
|
||||
};
|
||||
|
||||
var result = await _mediaService.UploadMediaAsync(media);
|
||||
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
using AutoMapper;
|
||||
using Govor.Application.Interfaces;
|
||||
using Govor.Application.Interfaces.Infrastructure.Extensions;
|
||||
using Govor.Application.Interfaces.Medias;
|
||||
using Govor.Contracts.DTOs;
|
||||
using Govor.Data.Repositories.Exceptions;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Govor.API.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/profile")]
|
||||
[Authorize(Roles = "Admin,User")]
|
||||
public class ProfileController : ControllerBase
|
||||
{
|
||||
private readonly ILogger<ProfileController> _logger;
|
||||
private readonly IMediaService _mediaService;
|
||||
private readonly IProfileService _profileService;
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public ProfileController(
|
||||
ILogger<ProfileController> logger,
|
||||
IMediaService mediaService,
|
||||
IProfileService profileService,
|
||||
ICurrentUserService currentUserService,
|
||||
IMapper mapper)
|
||||
{
|
||||
_logger = logger;
|
||||
_mediaService = mediaService;
|
||||
_profileService = profileService;
|
||||
_currentUserService = currentUserService;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
[HttpGet("dowload")]
|
||||
public async Task<IActionResult> DowloadProfile()
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = _currentUserService.GetCurrentUserId();
|
||||
var user = await _profileService.GetUserProfileAsync(userId);
|
||||
|
||||
if (user is null)
|
||||
return NotFound("Profile not found");
|
||||
|
||||
var dto = _mapper.Map<UserProfileDto>(user);
|
||||
return Ok(dto);
|
||||
}
|
||||
catch (UnauthorizedAccessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex.Message);
|
||||
return Forbid(ex.Message);
|
||||
}
|
||||
catch (NotFoundException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, ex.Message);
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(500, "Unexpected Error! Please try again later.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ public class SessionController : Controller
|
||||
{
|
||||
private readonly ILogger<SessionController> _logger;
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
private readonly ICurrentUserSessionService _currentUserSessionService;
|
||||
private readonly IUserSessionReader _userSessionReader;
|
||||
private readonly IUserSessionRevoker _userSessionRevoker;
|
||||
private readonly IMapper _mapper;
|
||||
@@ -22,12 +23,14 @@ public class SessionController : Controller
|
||||
public SessionController(
|
||||
ILogger<SessionController> logger,
|
||||
ICurrentUserService currentUserService,
|
||||
ICurrentUserSessionService currentUserSessionService,
|
||||
IUserSessionReader userSessionReader,
|
||||
IUserSessionRevoker userSessionRevoker,
|
||||
IMapper mapper)
|
||||
{
|
||||
_logger = logger;
|
||||
_currentUserService = currentUserService;
|
||||
_currentUserSessionService = currentUserSessionService;
|
||||
_userSessionReader = userSessionReader;
|
||||
_userSessionRevoker = userSessionRevoker;
|
||||
_mapper = mapper;
|
||||
@@ -86,7 +89,40 @@ public class SessionController : Controller
|
||||
return StatusCode(500, "Unexpected Error! Please try again later.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[HttpDelete("close")]
|
||||
public async Task<IActionResult> CloseCurrent()
|
||||
{
|
||||
try
|
||||
{
|
||||
await _userSessionRevoker.CloseSessionByIdAsync(
|
||||
_currentUserSessionService.GetUserSessionId(),
|
||||
_currentUserService.GetCurrentUserId());
|
||||
|
||||
return Ok();
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
catch (UnauthorizedAccessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, ex.Message);
|
||||
return Forbid(ex.Message);
|
||||
}
|
||||
catch (NotFoundException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, ex.Message);
|
||||
return NotFound(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(500, "Unexpected Error! Please try again later.");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpDelete("close/all")]
|
||||
public async Task<IActionResult> CloseAllSessions()
|
||||
{
|
||||
|
||||
@@ -51,7 +51,7 @@ public class ChatsHub : Hub
|
||||
await base.OnConnectedAsync();
|
||||
}
|
||||
|
||||
public override async Task OnDisconnectedAsync(Exception? exception)
|
||||
public override async Task OnDisconnectedAsync(Exception exception)
|
||||
{
|
||||
var userId = _userAccessor.GetUserId(Context, true);
|
||||
if (userId != Guid.Empty)
|
||||
@@ -87,7 +87,7 @@ public class ChatsHub : Hub
|
||||
|
||||
public async Task<HubResult<UserMessageResponse>> Send(MessageRequest request)
|
||||
{
|
||||
var senderId= _userAccessor.GetUserId(Context);
|
||||
var senderId = _userAccessor.GetUserId(Context);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(request.EncryptedContent) &&
|
||||
(request.MediaAttachments == null || !request.MediaAttachments.Any()))
|
||||
@@ -96,6 +96,12 @@ public class ChatsHub : Hub
|
||||
return HubResult<UserMessageResponse>.BadRequest("Message must contain content or media.");
|
||||
}
|
||||
|
||||
if (request.EncryptedContent.Length > 50_000)
|
||||
{
|
||||
_logger.LogWarning("User {SenderId} tried to send a too long message (length: {Length})", senderId, request.EncryptedContent.Length);
|
||||
return HubResult<UserMessageResponse>.BadRequest("Message cannot exceed 50,000 characters.");
|
||||
}
|
||||
|
||||
_logger.LogInformation("Sending message from {SenderId} to {RecipientId} ({RecipientType})",
|
||||
senderId, request.RecipientId, request.RecipientType);
|
||||
|
||||
@@ -143,7 +149,6 @@ public class ChatsHub : Hub
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task<HubResult<MessageRemovedResponse>> Remove(RemoveMessageRequest request)
|
||||
{
|
||||
var removerId = _userAccessor.GetUserId(Context);
|
||||
@@ -183,7 +188,6 @@ public class ChatsHub : Hub
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task<HubResult<MessageEditResponse>> Edit(EditMessageRequest request)
|
||||
{
|
||||
var editor = _userAccessor.GetUserId(Context);
|
||||
@@ -232,7 +236,7 @@ public class ChatsHub : Hub
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#region common
|
||||
private UserMessageResponse BuildUserMessageResponse(Message message, Guid? replyToId)
|
||||
{
|
||||
return new UserMessageResponse
|
||||
@@ -305,4 +309,5 @@ public class ChatsHub : Hub
|
||||
_logger.LogWarning(ex, "{Msg}: {UserId} -> {TargetId}", msg, userId, targetId);
|
||||
return HubResult<T>.NotFound("Message not found.");
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
using Govor.API.Common.SignalR.Helpers;
|
||||
using Govor.Application.Interfaces;
|
||||
using Govor.Contracts.Responses.SignalR;
|
||||
using Govor.Core.Repositories.Groups;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace Govor.API.Hubs;
|
||||
|
||||
[Authorize]
|
||||
public class ProfileHub : Hub
|
||||
{
|
||||
private readonly IGroupsRepository _groupsRepository;
|
||||
private readonly IFriendsService _friendsService;
|
||||
private readonly IProfileService _profileService;
|
||||
private readonly IHubUserAccessor _userAccessor;
|
||||
private readonly ILogger<ProfileHub> _logger;
|
||||
|
||||
public ProfileHub(
|
||||
IGroupsRepository groupsRepository,
|
||||
IFriendsService friendsService,
|
||||
IProfileService profileService,
|
||||
IHubUserAccessor userAccessor,
|
||||
ILogger<ProfileHub> logger)
|
||||
{
|
||||
_groupsRepository = groupsRepository;
|
||||
_friendsService = friendsService;
|
||||
_profileService = profileService;
|
||||
_userAccessor = userAccessor;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
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}");
|
||||
|
||||
// Groups
|
||||
var groups = await _groupsRepository.GetByUserIdAsync(userId);
|
||||
foreach (var group in groups)
|
||||
await Groups.AddToGroupAsync(Context.ConnectionId, $"group-{group.Id}");
|
||||
|
||||
_logger.LogInformation("User {UserId} connected with ConnectionId {ConnectionId}", userId, Context.ConnectionId);
|
||||
|
||||
await base.OnConnectedAsync();
|
||||
}
|
||||
|
||||
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}");
|
||||
|
||||
// Groups
|
||||
var groups = await _groupsRepository.GetByUserIdAsync(userId);
|
||||
foreach (var group in groups)
|
||||
await Groups.RemoveFromGroupAsync(Context.ConnectionId, $"group-{group.Id}");
|
||||
|
||||
await base.OnDisconnectedAsync(exception);
|
||||
}
|
||||
|
||||
|
||||
public async Task<HubResult<bool>> SetDescription(string description)
|
||||
{
|
||||
var userId = _userAccessor.GetUserId(Context);
|
||||
|
||||
try
|
||||
{
|
||||
await _profileService.SetDescription(description, userId);
|
||||
|
||||
var payload = new { userId, description };
|
||||
|
||||
await NotifyFriendsAndGroupsAsync(userId, "DescriptionUpdated", payload);
|
||||
|
||||
return HubResult<bool>.Ok(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "An error occurred while updating the user's {userId} descripton!", userId);
|
||||
return HubResult<bool>.Error("An unaccounted error on the server!");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<HubResult<bool>> SetAvatar(Guid iconId)
|
||||
{
|
||||
var userId = _userAccessor.GetUserId(Context);
|
||||
|
||||
try
|
||||
{
|
||||
if (iconId == Guid.Empty)
|
||||
return HubResult<bool>.BadRequest("IconId can't be empty!");
|
||||
|
||||
await _profileService.SetNewIcon(userId, iconId);
|
||||
|
||||
var payload = new { userId, iconId };
|
||||
|
||||
await NotifyFriendsAndGroupsAsync(userId, "AvatarUpdated", payload);
|
||||
|
||||
return HubResult<bool>.Ok(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "An error occurred while updating the user's {userId} avatar {iconId}", userId, iconId);
|
||||
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 groupIds = groups.Select(g => g.Id).ToList();
|
||||
|
||||
await Clients.Group($"user-{userId}") // current user
|
||||
.SendAsync(eventName, payload);
|
||||
|
||||
foreach (var friendId in friendIds)
|
||||
await Clients.Group($"friends-{friendId}")
|
||||
.SendAsync(eventName, payload);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@ builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy("AllowFrontend", policy =>
|
||||
{
|
||||
policy.WithOrigins("http://localhost:5000", "https://5.129.212.144:5000")
|
||||
policy.WithOrigins("https://localhost:7155", "http://localhost:7155")
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod()
|
||||
.AllowCredentials();
|
||||
@@ -126,6 +126,7 @@ app.MapControllers();
|
||||
|
||||
app.MapHub<ChatsHub>("/hubs/chats");
|
||||
app.MapHub<FriendsHub>("/hubs/friends");
|
||||
app.MapHub<ProfileHub>("/hubs/profiles");
|
||||
|
||||
app.MapSwagger().RequireAuthorization();
|
||||
|
||||
|
||||
@@ -6,13 +6,13 @@
|
||||
}
|
||||
},
|
||||
"ConnectionStrings": {
|
||||
"GovorDbContext": "Server=147.45.255.215;Port=3306;Database=artemy_DB;User=artemy;Password=LoxHuy))228Goy;"
|
||||
"GovorDbContext": "Host=77.233.213.226;Port=5432;Database=GovorDb;Username=gen_user;Password=co|HPq}JI6D@,t;"
|
||||
},
|
||||
"UseMySql": true,
|
||||
"AllowedHosts": "govor-team-govor-88b3.twc1.net;localhost;localhost:7155",
|
||||
"UseMySql": false,
|
||||
"AllowedHosts": "govor-team-govor-8ce1.twc1.net;localhost;localhost:7155",
|
||||
"JwtAccessOption": {
|
||||
"SecretKey": "Q89eY7zP7C4+TqLmHF4kw9xkF1E8Ru4Zpg+up9wFt9g=",
|
||||
"Minutes": 5
|
||||
"Minutes": 10
|
||||
},
|
||||
"JwtRefreshOption": {
|
||||
"RefreshTokenLifetimeDays": 30
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 691 KiB |
BIN
Binary file not shown.
|
After Width: | Height: | Size: 138 KiB |
BIN
Binary file not shown.
|
After Width: | Height: | Size: 314 KiB |
BIN
Binary file not shown.
|
After Width: | Height: | Size: 138 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 480 KiB |
Reference in New Issue
Block a user