using Govor.Core.Models.Users;

This commit is contained in:
Artemy
2025-07-11 21:52:37 +07:00
parent 9d06984e8c
commit c7de2318fc
84 changed files with 317 additions and 180 deletions
@@ -1,6 +1,7 @@
using AutoFixture; using AutoFixture;
using Govor.Core.Infrastructure.Validators; using Govor.Core.Infrastructure.Validators;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Messages;
namespace Govor.API.Tests.UnitTests.Infrastructure.Validators; namespace Govor.API.Tests.UnitTests.Infrastructure.Validators;
@@ -1,6 +1,7 @@
using AutoFixture; using AutoFixture;
using Govor.Core.Infrastructure.Validators; using Govor.Core.Infrastructure.Validators;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Users;
namespace Govor.API.Tests.UnitTests.Infrastructure.Validators; namespace Govor.API.Tests.UnitTests.Infrastructure.Validators;
@@ -1,5 +1,6 @@
using Govor.Contracts.DTOs; using Govor.Contracts.DTOs;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Users;
using Govor.Core.Repositories.Friendships; using Govor.Core.Repositories.Friendships;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
@@ -1,6 +1,7 @@
using Govor.API.Services.AdminsStuff.Interfaces; using Govor.API.Services.AdminsStuff.Interfaces;
using Govor.Application.Interfaces;
using Govor.Contracts.Responses.Admins; using Govor.Contracts.Responses.Admins;
using Govor.Core.Models; using Govor.Core.Models.Users;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@@ -2,7 +2,7 @@ using Govor.Application.Exceptions.FriendsService;
using Govor.Application.Interfaces.Friends; using Govor.Application.Interfaces.Friends;
using Govor.Application.Interfaces.Infrastructure.Extensions; using Govor.Application.Interfaces.Infrastructure.Extensions;
using Govor.Contracts.DTOs; using Govor.Contracts.DTOs;
using Govor.Core.Models; using Govor.Core.Models.Users;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace Govor.API.Controllers.Friends; namespace Govor.API.Controllers.Friends;
@@ -4,7 +4,6 @@ using Govor.Application.Infrastructure.AdminsStuff;
using Govor.Application.Infrastructure.Extensions; using Govor.Application.Infrastructure.Extensions;
using Govor.Application.Infrastructure.Validators; using Govor.Application.Infrastructure.Validators;
using Govor.Application.Interfaces; using Govor.Application.Interfaces;
using Govor.Application.Interfaces.AdminsStuff;
using Govor.Application.Interfaces.Authentication; using Govor.Application.Interfaces.Authentication;
using Govor.Application.Interfaces.Friends; using Govor.Application.Interfaces.Friends;
using Govor.Application.Interfaces.Infrastructure.Extensions; using Govor.Application.Interfaces.Infrastructure.Extensions;
@@ -16,6 +15,8 @@ using Govor.Application.Services.Messages;
using Govor.Core.Infrastructure.Extensions; using Govor.Core.Infrastructure.Extensions;
using Govor.Core.Infrastructure.Validators; using Govor.Core.Infrastructure.Validators;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Messages;
using Govor.Core.Models.Users;
using Govor.Core.Repositories.Admins; using Govor.Core.Repositories.Admins;
using Govor.Core.Repositories.Friendships; using Govor.Core.Repositories.Friendships;
using Govor.Core.Repositories.Groups; using Govor.Core.Repositories.Groups;
+158 -110
View File
@@ -1,10 +1,10 @@
using Govor.API.Services; using Govor.Application.Exceptions.VerifyFriendship;
using Govor.Application.Interfaces; using Govor.Application.Interfaces;
using Govor.Application.Interfaces.Messages; using Govor.Application.Interfaces.Messages;
using Govor.Application.Interfaces.Messages.Parameters; using Govor.Application.Interfaces.Messages.Parameters;
using Govor.Contracts.Requests.SignalR; using Govor.Contracts.Requests.SignalR;
using Govor.Contracts.Responses.SignalR; using Govor.Contracts.Responses.SignalR;
using Govor.Core.Models; using Govor.Core.Models.Messages;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
@@ -85,143 +85,191 @@ public class ChatsHub : Hub
await base.OnDisconnectedAsync(exception); await base.OnDisconnectedAsync(exception);
} }
public async Task Send(MessageRequest request) public async Task<HubResult<UserMessageResponse>> Send(MessageRequest request)
{ {
var senderId = GetUserId(); var senderId = GetUserId();
if (string.IsNullOrWhiteSpace(request.EncryptedContent) && if (string.IsNullOrWhiteSpace(request.EncryptedContent) &&
(request.MediaAttachments == null || !request.MediaAttachments.Any())) (request.MediaAttachments == null || !request.MediaAttachments.Any()))
{ {
_logger.LogWarning("Empty message (no content and no attachments) received from user {UserId}", senderId); _logger.LogWarning("Empty message received from user {UserId}", senderId);
throw new ArgumentException("Message cannot be empty (must have content or attachments)."); return HubResult<UserMessageResponse>.BadRequest("Message must contain content or media.");
} }
_logger.LogInformation("Message send initiated by {SenderId} to {RecipientId} of type {RecipientType}", _logger.LogInformation("Sending message from {SenderId} to {RecipientId} ({RecipientType})",
senderId, request.RecipientId, request.RecipientType); senderId, request.RecipientId, request.RecipientType);
var sendMessageParams = new SendMessage(EncryptContent: request.EncryptedContent, var sendMessageParams = new SendMessage(
ReplyToMessageId: request.ReplyToMessageId, FromUserId: senderId, RecipientId: request.RecipientId, EncryptContent: request.EncryptedContent,
RecipientType: request.RecipientType, SendAt: DateTime.UtcNow, ReplyToMessageId: request.ReplyToMessageId,
FromUserId: senderId,
RecipientId: request.RecipientId,
RecipientType: request.RecipientType,
SendAt: DateTime.UtcNow,
Media: request.MediaAttachments?.Select(f => new SendMedia(f.MediaId, f.EncryptedKey)) ?? Media: request.MediaAttachments?.Select(f => new SendMedia(f.MediaId, f.EncryptedKey)) ??
Array.Empty<SendMedia>()); Array.Empty<SendMedia>()
var result = await _messageCommandService.SendMessageAsync(sendMessageParams);
if (!result.IsSuccess || result.Message.Id == Guid.Empty)
{
_logger.LogError(result.Exception,
"Failed to send message from {SenderId} to {RecipientId}. Error: {ErrorMessage}", senderId,
request.RecipientId, result.Exception?.Message ?? "Unknown error");
if (result.Exception != null)
throw result.Exception;
throw new HubException("Failed to send message due to an internal error.");
}
var messageResponse = new UserMessageResponse // Assuming a response DTO
{
MessageId = result.Message.Id,
SenderId = result.Message.SenderId,
RecipientId = result.Message.RecipientId,
RecipientType = result.Message.RecipientType,
EncryptedContent = result.Message.EncryptedContent,
SentAt = result.Message.SentAt,
IsEdited = false,
MediaAttachments = result.Message.MediaAttachments.Select(m => m.MediaFile).ToList(),
ReplyToMessageId = request.ReplyToMessageId
};
// Notify recipient (user or group)
if (request.RecipientType == RecipientType.User)
{
// Send to the recipient's personal group
await Clients.Group(request.RecipientId.ToString()).SendAsync("ReceiveMessage", messageResponse);
}
else if (request.RecipientType == RecipientType.Group)
{
// Send to all members of the group, including the sender if they are part of the group via a different connection
await Clients.Group($"group_{request.RecipientId}").SendAsync("ReceiveMessage", messageResponse);
}
// Notify sender (confirmation) on their connection
await Clients.Caller.SendAsync("MessageSent",
messageResponse); // Or use "ReceiveMessage" if the sender should also just get it like anyone else
_logger.LogInformation(
"Message {MessageId} sent successfully from {SenderId} to {RecipientId} ({RecipientType})",
result.Message.Id, senderId, request.RecipientId, request.RecipientType);
}
public async Task Remove(RemoveMessageRequest request)
{
var removerId = GetUserId();
_logger.LogInformation("Message removal initiated by {RemoverId} for message {MessageId}", removerId, request.MessageId);
var removeParams = new DeleteMessage(
DeleterId: removerId,
MessageId: request.MessageId
); );
try try
{ {
var result = await _messageCommandService.DeleteMessageAsync(removeParams); var result = await _messageCommandService.SendMessageAsync(sendMessageParams);
if (!result.IsSuccess)
{
_logger.LogError(result.Exception, "Failed to remove message {MessageId} by {RemoverId}. Error: {ErrorMessage}", request.MessageId, removerId, result.Exception?.Message ?? "Unknown error");
if (result.Exception != null) throw result.Exception;
throw new HubException("Failed to remove message.");
}
var originalMessage = result.OriginalMessage; // Assuming service returns this
if (originalMessage == null)
{
_logger.LogError("DeleteMessageAsync succeeded but did not return the original message details for message {MessageId}", request.MessageId);
throw new HubException("Failed to process message deletion due to missing message details.");
}
var removalNotification = new MessageRemovedResponse if (!result.IsSuccess || result.Message.Id == Guid.Empty)
{ return LogAndError<UserMessageResponse>(senderId, request.RecipientId, "Failed to send message", result.Exception);
MessageId = request.MessageId,
SenderId = originalMessage.SenderId,
RecipientId = originalMessage.RecipientId,
RecipientType = originalMessage.RecipientType
};
// Notify relevant clients var response = BuildUserMessageResponse(result.Message, request.ReplyToMessageId);
if (originalMessage.RecipientType == RecipientType.User)
{ await NotifyClientsAboutMessage(response);
await Clients.Group(originalMessage.SenderId.ToString()).SendAsync("MessageRemoved", removalNotification);
if (originalMessage.SenderId != originalMessage.RecipientId) return HubResult<UserMessageResponse>.Ok(response);
{
await Clients.Group(originalMessage.RecipientId.ToString()).SendAsync("MessageRemoved", removalNotification);
}
}
else if (originalMessage.RecipientType == RecipientType.Group)
{
await Clients.Group($"group_{originalMessage.RecipientId}").SendAsync("MessageRemoved", removalNotification);
}
_logger.LogInformation("Message {MessageId} removed successfully by {RemoverId}", request.MessageId, removerId);
} }
catch (UnauthorizedAccessException ex) catch (UnauthorizedAccessException ex)
{ {
_logger.LogWarning(ex, "Unauthorized attempt to remove message {MessageId} by user {RemoverId}", request.MessageId, removerId); return LogAndUnauthorized<UserMessageResponse>(ex, "Unauthorized sending attempt", senderId,
throw new HubException("You are not authorized to remove this message.", ex); request.RecipientId);
} }
catch (KeyNotFoundException ex) // Or a custom NotFoundException catch (FriendshipException)
{ {
_logger.LogWarning(ex, "Attempt to remove non-existent message {MessageId} by user {RemoverId}", request.MessageId, removerId); return HubResult<UserMessageResponse>.Unauthorized(
throw new HubException("Message not found.", ex); "You cannot send this message because you are not friends.");
}
catch (ArgumentException)
{
return HubResult<UserMessageResponse>.BadRequest("Invalid message content.");
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Error removing message {MessageId} by {RemoverId}", request.MessageId, removerId); return LogAndError<UserMessageResponse>(senderId, request.RecipientId, "Unhandled exception", ex);
throw new HubException("An error occurred while removing the message.", ex);
} }
} }
public async Task Edit(EditMessageRequest request)
public async Task<HubResult<MessageRemovedResponse>> Remove(RemoveMessageRequest request)
{ {
var removerId = GetUserId();
_logger.LogInformation("Removing message {MessageId} by user {RemoverId}", request.MessageId, removerId);
try
{
var result = await _messageCommandService.DeleteMessageAsync(new DeleteMessage(removerId, request.MessageId));
if (!result.IsSuccess || result.OriginalMessage == null)
return LogAndError<MessageRemovedResponse>(removerId, request.MessageId, "Message deletion failed", result.Exception);
var notification = new MessageRemovedResponse
{
MessageId = request.MessageId,
SenderId = result.OriginalMessage.SenderId,
RecipientId = result.OriginalMessage.RecipientId,
RecipientType = result.OriginalMessage.RecipientType
};
await NotifyClientsAboutRemoval(notification);
_logger.LogInformation("Message {MessageId} removed successfully by {RemoverId}", request.MessageId, removerId);
return HubResult<MessageRemovedResponse>.Ok(notification);
}
catch (UnauthorizedAccessException ex)
{
return LogAndUnauthorized<MessageRemovedResponse>(ex, "Unauthorized removal", removerId, request.MessageId);
}
catch (KeyNotFoundException ex)
{
return LogAndNotFound<MessageRemovedResponse>(ex, "Message not found", removerId, request.MessageId);
}
catch (Exception ex)
{
return LogAndError<MessageRemovedResponse>(removerId, request.MessageId, "Unhandled deletion error", ex);
}
} }
public async Task<HubResult<MessageEditResponse>> Edit(EditMessageRequest request)
{
var editor = GetUserId();
_logger.LogInformation("Editing message {MessageId} by user {EditorId}", request.MessageId, editor);
var editMessageParam = new EditMessage(editor,
request.MessageId,
request.NewEncryptedContent,
DateTime.UtcNow);
try
{
var result = await _messageCommandService.EditMessageAsync(editMessageParam);
if(!result.IsSuccess)
return LogAndError<MessageEditResponse>(editor, request.MessageId, "Edit message error", result.Exception);
return HubResult<MessageEditResponse>.Ok();
}
catch (Exception ex)
{
return LogAndError<MessageEditResponse>(editor, request.MessageId, "Unhandled exception error", ex);
}
}
private UserMessageResponse BuildUserMessageResponse(Message message, Guid? replyToId)
{
return new UserMessageResponse
{
MessageId = message.Id,
SenderId = message.SenderId,
RecipientId = message.RecipientId,
RecipientType = message.RecipientType,
EncryptedContent = message.EncryptedContent,
SentAt = message.SentAt,
IsEdited = false,
MediaAttachments = message.MediaAttachments.Select(m => m.MediaFile).ToList(),
ReplyToMessageId = replyToId
};
}
private async Task NotifyClientsAboutMessage(UserMessageResponse response)
{
string group = response.RecipientType == RecipientType.User
? response.RecipientId.ToString()
: $"group_{response.RecipientId}";
await Clients.Group(group).SendAsync("ReceiveMessage", response);
await Clients.Caller.SendAsync("MessageSent", response);
}
private async Task NotifyClientsAboutRemoval(MessageRemovedResponse response)
{
if (response.RecipientType == RecipientType.User)
{
await Clients.Group(response.SenderId.ToString()).SendAsync("MessageRemoved", response);
if (response.SenderId != response.RecipientId)
await Clients.Group(response.RecipientId.ToString()).SendAsync("MessageRemoved", response);
}
else
{
await Clients.Group($"group_{response.RecipientId}").SendAsync("MessageRemoved", response);
}
}
// Logging helpers
private HubResult<T> LogAndError<T>(Guid userId, Guid targetId, string message, Exception ex)
{
_logger.LogError(ex, "{Message} from {UserId} to {TargetId}", message, userId, targetId);
return HubResult<T>.Error(ex?.Message ?? "Internal server error");
}
private HubResult<T> LogAndUnauthorized<T>(Exception ex, string msg, Guid userId, Guid targetId)
{
_logger.LogWarning(ex, "{Msg}: {UserId} -> {TargetId}", msg, userId, targetId);
return HubResult<T>.Unauthorized("You are not authorized to perform this action.");
}
private HubResult<T> LogAndNotFound<T>(Exception ex, string msg, Guid userId, Guid targetId)
{
_logger.LogWarning(ex, "{Msg}: {UserId} -> {TargetId}", msg, userId, targetId);
return HubResult<T>.NotFound("Message not found.");
}
private Guid GetUserId(bool suppressException = false) private Guid GetUserId(bool suppressException = false)
{ {
var userIdClaim = Context.User?.FindFirst("userId")?.Value; var userIdClaim = Context.User?.FindFirst("userId")?.Value;
+1 -1
View File
@@ -121,7 +121,7 @@ app.UseAuthorization();
app.MapControllers(); app.MapControllers();
app.MapHub<ChatsHub>("/hubs/friends"); app.MapHub<ChatsHub>("/hubs/chats");
app.MapHub<FriendsHub>("/hubs/friends"); app.MapHub<FriendsHub>("/hubs/friends");
app.MapSwagger().RequireAuthorization(); app.MapSwagger().RequireAuthorization();
@@ -7,6 +7,7 @@ using Govor.Application.Exceptions.AuthService;
using Govor.Application.Interfaces.Authentication; using Govor.Application.Interfaces.Authentication;
using Govor.Application.Services; using Govor.Application.Services;
using Govor.Application.Services.Authentication; using Govor.Application.Services.Authentication;
using Govor.Core.Models.Users;
using Govor.Core.Repositories.Admins; using Govor.Core.Repositories.Admins;
using Moq; using Moq;
@@ -1,9 +1,9 @@
using System.IdentityModel.Tokens.Jwt; using System.IdentityModel.Tokens.Jwt;
using AutoFixture; using AutoFixture;
using Govor.API.Services.Authentication.Interfaces; using Govor.API.Services.Authentication.Interfaces;
using Govor.Application.Services; using Govor.Application.Interfaces.Authentication;
using Govor.Application.Services.Authentication; using Govor.Application.Services.Authentication;
using Govor.Core.Models; using Govor.Core.Models.Users;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Moq; using Moq;
@@ -2,6 +2,7 @@ using AutoFixture;
using Govor.Application.Interfaces.Friends; using Govor.Application.Interfaces.Friends;
using Govor.Application.Services.Friends; using Govor.Application.Services.Friends;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Users;
using Govor.Core.Repositories.Friendships; using Govor.Core.Repositories.Friendships;
using Govor.Core.Repositories.Users; using Govor.Core.Repositories.Users;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
@@ -3,6 +3,7 @@ using Govor.Application.Exceptions.FriendsService;
using Govor.Application.Interfaces.Friends; using Govor.Application.Interfaces.Friends;
using Govor.Application.Services.Friends; using Govor.Application.Services.Friends;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Users;
using Govor.Core.Repositories.Friendships; using Govor.Core.Repositories.Friendships;
using Govor.Core.Repositories.Users; using Govor.Core.Repositories.Users;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
@@ -3,6 +3,7 @@ using Govor.Application.Interfaces;
using Govor.Application.Interfaces.Messages.Parameters; using Govor.Application.Interfaces.Messages.Parameters;
using Govor.Application.Services.Messages; using Govor.Application.Services.Messages;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Messages;
using Govor.Core.Repositories.Groups; using Govor.Core.Repositories.Groups;
using Govor.Core.Repositories.Messages; using Govor.Core.Repositories.Messages;
using Govor.Core.Repositories.PrivateChats; using Govor.Core.Repositories.PrivateChats;
@@ -296,7 +297,7 @@ public class MessageCommandServiceTests
Assert.That(result, Is.Not.Null); Assert.That(result, Is.Not.Null);
Assert.That(result.IsSuccess, Is.False); Assert.That(result.IsSuccess, Is.False);
Assert.That(result.Exception, Is.Not.Null); Assert.That(result.Exception, Is.Not.Null);
Assert.That(result.Exception,Is.TypeOf<NotFoundByKeyException<Guid>>()); Assert.That(result.Exception,Is.TypeOf<KeyNotFoundException>());
Assert.That(result.OriginalMessage, Is.Null); Assert.That(result.OriginalMessage, Is.Null);
} }
@@ -1,5 +1,5 @@
using Govor.Application.Services; using Govor.Application.Services;
using Govor.Core.Models; using Govor.Core.Models.Users;
using Govor.Data; using Govor.Data;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
@@ -1,9 +1,9 @@
using Govor.API.Services.AdminsStuff.Interfaces; using Govor.Application.Interfaces;
using Govor.Core.Models; using Govor.Core.Models.Users;
using Govor.Core.Repositories.Users; using Govor.Core.Repositories.Users;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
namespace Govor.Application.Interfaces.AdminsStuff; namespace Govor.Application.Infrastructure.AdminsStuff;
public class UsersService : IUsersAdministration public class UsersService : IUsersAdministration
{ {
@@ -1,4 +1,5 @@
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Users;
namespace Govor.API.Services.Authentication.Interfaces; namespace Govor.API.Services.Authentication.Interfaces;
@@ -1,6 +1,6 @@
using Govor.Core.Models; using Govor.Core.Models.Users;
namespace Govor.API.Services.Authentication.Interfaces; namespace Govor.Application.Interfaces.Authentication;
public interface IJwtService public interface IJwtService
{ {
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Users;
namespace Govor.Application.Interfaces.Friends; namespace Govor.Application.Interfaces.Friends;
@@ -1,4 +1,5 @@
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Users;
namespace Govor.Application.Interfaces; namespace Govor.Application.Interfaces;
@@ -1,5 +1,6 @@
using Govor.Application.Interfaces.Messages.Parameters; using Govor.Application.Interfaces.Messages.Parameters;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Users;
namespace Govor.Application.Interfaces; namespace Govor.Application.Interfaces;
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Messages;
namespace Govor.Application.Interfaces; namespace Govor.Application.Interfaces;
@@ -1,6 +1,6 @@
using Govor.Core.Models; using Govor.Core.Models.Users;
namespace Govor.API.Services.AdminsStuff.Interfaces; namespace Govor.Application.Interfaces;
public interface IUsersAdministration public interface IUsersAdministration
{ {
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Messages;
namespace Govor.Application.Interfaces.Medias; namespace Govor.Application.Interfaces.Medias;
@@ -1,5 +1,5 @@
using Govor.Application.Interfaces.Messages.Parameters; using Govor.Application.Interfaces.Messages.Parameters;
using Govor.Core.Models; using Govor.Core.Models.Messages;
namespace Govor.Application.Interfaces.Messages; namespace Govor.Application.Interfaces.Messages;
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Messages;
namespace Govor.Application.Interfaces.Messages.Parameters; namespace Govor.Application.Interfaces.Messages.Parameters;
@@ -1,11 +1,9 @@
using System.Text.RegularExpressions;
using Govor.API.Services.Authentication.Interfaces;
using Govor.Application.Exceptions.AuthService; using Govor.Application.Exceptions.AuthService;
using Govor.Core.Infrastructure.Extensions; using Govor.Core.Infrastructure.Extensions;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Repositories.Users; using Govor.Core.Repositories.Users;
using Govor.Application.Interfaces.Authentication; using Govor.Application.Interfaces.Authentication;
using Govor.Core.Infrastructure.Validators; using Govor.Core.Models.Users;
using Govor.Core.Repositories.Admins; using Govor.Core.Repositories.Admins;
namespace Govor.Application.Services.Authentication; namespace Govor.Application.Services.Authentication;
@@ -2,7 +2,8 @@ using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims; using System.Security.Claims;
using System.Text; using System.Text;
using Govor.API.Services.Authentication.Interfaces; using Govor.API.Services.Authentication.Interfaces;
using Govor.Core.Models; using Govor.Application.Interfaces.Authentication;
using Govor.Core.Models.Users;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
@@ -1,6 +1,7 @@
using Govor.Application.Exceptions.FriendsService; using Govor.Application.Exceptions.FriendsService;
using Govor.Application.Interfaces.Friends; using Govor.Application.Interfaces.Friends;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Users;
using Govor.Core.Repositories.Friendships; using Govor.Core.Repositories.Friendships;
using Govor.Core.Repositories.Users; using Govor.Core.Repositories.Users;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
@@ -1,6 +1,7 @@
using Govor.API.Services.Authentication.Interfaces; using Govor.API.Services.Authentication.Interfaces;
using Govor.Application.Exceptions.InvitesService; using Govor.Application.Exceptions.InvitesService;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Users;
using Govor.Core.Repositories.Invaites; using Govor.Core.Repositories.Invaites;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
@@ -2,10 +2,12 @@ using Govor.Application.Interfaces;
using Govor.Application.Interfaces.Messages; using Govor.Application.Interfaces.Messages;
using Govor.Application.Interfaces.Messages.Parameters; using Govor.Application.Interfaces.Messages.Parameters;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Messages;
using Govor.Core.Repositories.Groups; using Govor.Core.Repositories.Groups;
using Govor.Core.Repositories.Messages; using Govor.Core.Repositories.Messages;
using Govor.Core.Repositories.PrivateChats; using Govor.Core.Repositories.PrivateChats;
using Govor.Core.Repositories.Users; using Govor.Core.Repositories.Users;
using Govor.Data.Repositories.Exceptions;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace Govor.Application.Services.Messages; namespace Govor.Application.Services.Messages;
@@ -183,11 +185,14 @@ public class MessageCommandService : IMessageCommandService
// return new DeleteMessageResult(false, new UnauthorizedAccessException("User is not authorized to delete this message."), null); // return new DeleteMessageResult(false, new UnauthorizedAccessException("User is not authorized to delete this message."), null);
// } // }
// } else { // } else {
_logger.LogWarning("User {DeleterId} attempted to delete message {MessageId} not sent by them (sender was {SenderId})", deleteParams.DeleterId, deleteParams.MessageId, message.SenderId); _logger.LogWarning(
return new DeleteMessageResult(false, new UnauthorizedAccessException("User is not authorized to delete this message."), default); "User {DeleterId} attempted to delete message {MessageId} not sent by them (sender was {SenderId})",
deleteParams.DeleterId, deleteParams.MessageId, message.SenderId);
return new DeleteMessageResult(false,
new UnauthorizedAccessException("User is not authorized to delete this message."), default);
// } // }
} }
var originalMessageForNotification = new Message var originalMessageForNotification = new Message
{ {
Id = message.Id, Id = message.Id,
@@ -195,12 +200,17 @@ public class MessageCommandService : IMessageCommandService
RecipientId = message.RecipientId, RecipientId = message.RecipientId,
RecipientType = message.RecipientType, RecipientType = message.RecipientType,
}; };
await _messagesRepository.RemoveAsync(deleteParams.MessageId); await _messagesRepository.RemoveAsync(deleteParams.MessageId);
_logger.LogInformation("Message {MessageId} deleted successfully by user {DeleterId}", deleteParams.MessageId, deleteParams.DeleterId); _logger.LogInformation("Message {MessageId} deleted successfully by user {DeleterId}",
deleteParams.MessageId, deleteParams.DeleterId);
return new DeleteMessageResult(true, default, originalMessageForNotification); return new DeleteMessageResult(true, default, originalMessageForNotification);
} }
catch (NotFoundByKeyException<Guid> ex)
{
return new DeleteMessageResult(false, new KeyNotFoundException("Message not found", ex), default);
}
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Error deleting message {MessageId} by user {DeleterId}", deleteParams.MessageId, deleteParams.DeleterId); _logger.LogError(ex, "Error deleting message {MessageId} by user {DeleterId}", deleteParams.MessageId, deleteParams.DeleterId);
@@ -1,5 +1,5 @@
using Govor.Application.Interfaces; using Govor.Application.Interfaces;
using Govor.Core.Models; using Govor.Core.Models.Messages;
using Govor.Core.Repositories.Groups; using Govor.Core.Repositories.Groups;
using Govor.Core.Repositories.Messages; using Govor.Core.Repositories.Messages;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
+2 -1
View File
@@ -10,6 +10,7 @@ using Govor.ConsoleClient.Commands;
using Govor.Contracts.Requests.SignalR; using Govor.Contracts.Requests.SignalR;
using Govor.Contracts.Responses.SignalR; using Govor.Contracts.Responses.SignalR;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Messages;
/*==================================== /*====================================
*Личные сообщения| Егор *Личные сообщения| Егор
@@ -200,7 +201,7 @@ namespace Govor.ConsoleClient
options.AccessTokenProvider = () => Task.FromResult(AuthToken); options.AccessTokenProvider = () => Task.FromResult(AuthToken);
}) })
.Build(); .Build();
_hubConnection.On<UserMessageResponse>("ReceiveMessage", (message) => _hubConnection.On<UserMessageResponse>("ReceiveMessage", (message) =>
{ {
var myId = GetMyUserId(); var myId = GetMyUserId();
@@ -1,5 +1,4 @@
using System.ComponentModel.DataAnnotations; using Govor.Core.Models.Messages;
using Govor.Core.Models;
namespace Govor.Contracts.Requests; namespace Govor.Contracts.Requests;
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Messages;
namespace Govor.Contracts.Requests.SignalR; namespace Govor.Contracts.Requests.SignalR;
@@ -0,0 +1,6 @@
namespace Govor.Contracts.Responses.SignalR;
public class MessageEditResponse
{
}
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Messages;
namespace Govor.Contracts.Responses.SignalR; namespace Govor.Contracts.Responses.SignalR;
@@ -1,5 +1,5 @@
using Govor.Contracts.Requests.SignalR;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Messages;
namespace Govor.Contracts.Responses.SignalR; namespace Govor.Contracts.Responses.SignalR;
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Users;
namespace Govor.Core.Infrastructure.Validators; namespace Govor.Core.Infrastructure.Validators;
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Messages;
using Exception = System.Exception; using Exception = System.Exception;
namespace Govor.Core.Infrastructure.Validators; namespace Govor.Core.Infrastructure.Validators;
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Messages;
namespace Govor.Core.Infrastructure.Validators; namespace Govor.Core.Infrastructure.Validators;
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Users;
using ArgumentNullException = System.ArgumentNullException; using ArgumentNullException = System.ArgumentNullException;
namespace Govor.Core.Infrastructure.Validators; namespace Govor.Core.Infrastructure.Validators;
+2
View File
@@ -1,3 +1,5 @@
using Govor.Core.Models.Users;
namespace Govor.Core.Models; namespace Govor.Core.Models;
public class Friendship public class Friendship
+2
View File
@@ -1,3 +1,5 @@
using Govor.Core.Models.Users;
namespace Govor.Core.Models; namespace Govor.Core.Models;
public class GroupInvitation public class GroupInvitation
+2
View File
@@ -1,3 +1,5 @@
using Govor.Core.Models.Users;
namespace Govor.Core.Models; namespace Govor.Core.Models;
public class Invitation public class Invitation
+2
View File
@@ -1,3 +1,5 @@
using Govor.Core.Models.Messages;
namespace Govor.Core.Models; namespace Govor.Core.Models;
public class MediaFile public class MediaFile
@@ -1,4 +1,4 @@
namespace Govor.Core.Models; namespace Govor.Core.Models.Messages;
public class MediaAttachments public class MediaAttachments
{ {
@@ -1,4 +1,4 @@
namespace Govor.Core.Models; namespace Govor.Core.Models.Messages;
public class Message public class Message
{ {
@@ -1,4 +1,6 @@
namespace Govor.Core.Models; using Govor.Core.Models.Users;
namespace Govor.Core.Models.Messages;
public class MessageReaction public class MessageReaction
{ {
@@ -1,4 +1,4 @@
namespace Govor.Core.Models; namespace Govor.Core.Models.Messages;
public class MessageView public class MessageView
{ {
+2
View File
@@ -1,3 +1,5 @@
using Govor.Core.Models.Messages;
namespace Govor.Core.Models; namespace Govor.Core.Models;
public class PrivateChat public class PrivateChat
@@ -1,6 +1,6 @@
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
namespace Govor.Core.Models; namespace Govor.Core.Models.Users;
public class Admin public class Admin
{ {
@@ -0,0 +1,39 @@
namespace Govor.Core.Models.Users;
public class PrivacyUserSettings
{
public Guid UserId { get; set; }
public bool IsGlobalAccount { get; set; }
public WhoCan CanSend { get; set; }
public List<Guid>? WhitelistSent { get; set; }
public List<Guid>? BlacklistSent { get; set; }
public WhoCan CanSeeTimeWas { get; set; }
public List<Guid>? WhitelistTimeWas { get; set; }
public List<Guid>? BlacklistTimeWas { get; set; }
public WhoCan CanSeeImage { get; set; }
public List<Guid>? WhitelistSeeImage { get; set; }
public List<Guid>? BlacklistSeeImage{ get; set; }
public DeletingMessagesVia Via { get; set; } // if min value = none
public int DeletingIn { get; set; }
}
public enum WhoCan
{
None = 0,
OnlyFriends = 1,
EveryoneCanSend = 2,
}
public enum DeletingMessagesVia
{
None = 0,
Hours = 1,
Days = 2,
Months = 3,
Years = 4
}
@@ -1,6 +1,6 @@
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
namespace Govor.Core.Models; namespace Govor.Core.Models.Users;
public class User public class User
{ {
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Users;
namespace Govor.Core.Repositories.Admins; namespace Govor.Core.Repositories.Admins;
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Users;
namespace Govor.Core.Repositories.Admins; namespace Govor.Core.Repositories.Admins;
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Users;
namespace Govor.Core.Repositories.Admins; namespace Govor.Core.Repositories.Admins;
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Messages;
namespace Govor.Core.Repositories.Groups; namespace Govor.Core.Repositories.Groups;
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Messages;
namespace Govor.Core.Repositories.MediasAttachments; namespace Govor.Core.Repositories.MediasAttachments;
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Messages;
namespace Govor.Core.Repositories.MediasAttachments; namespace Govor.Core.Repositories.MediasAttachments;
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Messages;
namespace Govor.Core.Repositories.MediasAttachments; namespace Govor.Core.Repositories.MediasAttachments;
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Messages;
namespace Govor.Core.Repositories.Messages; namespace Govor.Core.Repositories.Messages;
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Messages;
namespace Govor.Core.Repositories.Messages; namespace Govor.Core.Repositories.Messages;
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Messages;
namespace Govor.Core.Repositories.Messages; namespace Govor.Core.Repositories.Messages;
+1 -1
View File
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Users;
namespace Govor.Core.Repositories.Users; namespace Govor.Core.Repositories.Users;
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Users;
namespace Govor.Core.Repositories.Users; namespace Govor.Core.Repositories.Users;
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Users;
namespace Govor.Core.Repositories.Users; namespace Govor.Core.Repositories.Users;
@@ -1,6 +1,7 @@
using AutoFixture; using AutoFixture;
using Govor.Core.Infrastructure.Validators; using Govor.Core.Infrastructure.Validators;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Users;
using Govor.Data; using Govor.Data;
using Govor.Data.Repositories; using Govor.Data.Repositories;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
@@ -1,6 +1,7 @@
using AutoFixture; using AutoFixture;
using Govor.Core.Infrastructure.Validators; using Govor.Core.Infrastructure.Validators;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Messages;
using Govor.Data; using Govor.Data;
using Govor.Data.Repositories; using Govor.Data.Repositories;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
@@ -1,6 +1,7 @@
using AutoFixture; using AutoFixture;
using Govor.Core.Infrastructure.Validators; using Govor.Core.Infrastructure.Validators;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Messages;
using Govor.Data; using Govor.Data;
using Govor.Data.Repositories; using Govor.Data.Repositories;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
@@ -1,6 +1,7 @@
using AutoFixture; using AutoFixture;
using Govor.Core.Infrastructure.Validators; using Govor.Core.Infrastructure.Validators;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Users;
using Govor.Data; using Govor.Data;
using Govor.Data.Repositories; using Govor.Data.Repositories;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Users;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Metadata.Builders;
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Messages;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Metadata.Builders;
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Messages;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Metadata.Builders;
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Messages;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Metadata.Builders;
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Messages;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Metadata.Builders;
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Users;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Metadata.Builders;
+2
View File
@@ -1,5 +1,7 @@
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Messages;
using Govor.Core.Models.Users;
using Govor.Data.Configurations; using Govor.Data.Configurations;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@@ -1,6 +1,7 @@
using Govor.Core.Infrastructure.Extensions; using Govor.Core.Infrastructure.Extensions;
using Govor.Core.Infrastructure.Validators; using Govor.Core.Infrastructure.Validators;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Users;
using Govor.Core.Repositories.Admins; using Govor.Core.Repositories.Admins;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@@ -1,6 +1,7 @@
using Govor.Core.Infrastructure.Extensions; using Govor.Core.Infrastructure.Extensions;
using Govor.Core.Infrastructure.Validators; using Govor.Core.Infrastructure.Validators;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Users;
using Govor.Core.Repositories.Friendships; using Govor.Core.Repositories.Friendships;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
+1 -1
View File
@@ -1,7 +1,7 @@
using System.Text.RegularExpressions;
using Govor.Core.Infrastructure.Extensions; using Govor.Core.Infrastructure.Extensions;
using Govor.Core.Infrastructure.Validators; using Govor.Core.Infrastructure.Validators;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Users;
using Govor.Core.Repositories.Groups; using Govor.Core.Repositories.Groups;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@@ -1,6 +1,7 @@
using Govor.Core.Infrastructure.Extensions; using Govor.Core.Infrastructure.Extensions;
using Govor.Core.Infrastructure.Validators; using Govor.Core.Infrastructure.Validators;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Users;
using Govor.Core.Repositories.Invaites; using Govor.Core.Repositories.Invaites;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@@ -1,6 +1,6 @@
using Govor.Core.Infrastructure.Extensions; using Govor.Core.Infrastructure.Extensions;
using Govor.Core.Infrastructure.Validators; using Govor.Core.Infrastructure.Validators;
using Govor.Core.Models; using Govor.Core.Models.Messages;
using Govor.Core.Repositories.MediasAttachments; using Govor.Core.Repositories.MediasAttachments;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@@ -1,6 +1,7 @@
using Govor.Core.Infrastructure.Extensions; using Govor.Core.Infrastructure.Extensions;
using Govor.Core.Infrastructure.Validators; using Govor.Core.Infrastructure.Validators;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Messages;
using Govor.Core.Repositories.Messages; using Govor.Core.Repositories.Messages;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
+1 -2
View File
@@ -1,7 +1,6 @@
using Govor.Core.Infrastructure.Extensions; using Govor.Core.Infrastructure.Extensions;
using Govor.Core.Infrastructure.Validators; using Govor.Core.Infrastructure.Validators;
using Govor.Core.Models; using Govor.Core.Models.Users;
using Govor.Core.Repositories;
using Govor.Core.Repositories.Users; using Govor.Core.Repositories.Users;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;