Refactor: migrate Core -> Domain and reorganize projects

Large refactor that renames/moves core types into a new Govor.Domain surface and reorganizes the Application layer. Models, configurations, migrations and many files moved from Govor.Core/Govor.Data to Govor.Domain; numerous Application services, interfaces and implementations were relocated or added (authentication, friends, messages, medias, push notifications, user sessions, storage, synching, private chats, etc.). Tests updated to use Govor.Domain namespaces and adjusted project references (removed Govor.Data reference from API tests). Also updated API, Hub and mapping code and project files to reflect the new structure and naming. This is primarily a codebase-wide namespace and module reorganization to establish a Domain project and restructure application services.
This commit is contained in:
Artemy
2026-07-16 19:27:45 +07:00
parent 1d35356c8c
commit 6d1c53beeb
371 changed files with 2729 additions and 6694 deletions
@@ -0,0 +1,8 @@
using Govor.Application.Messages.Parameters;
namespace Govor.Application.Messages;
public interface IMessageEditingService
{
Task<EditMessageResult> EditMessageAsync(EditMessage editParams);
}
@@ -0,0 +1,8 @@
using Govor.Application.Messages.Parameters;
namespace Govor.Application.Messages;
public interface IMessageRemovingService
{
Task<DeleteMessageResult> DeleteMessageAsync(DeleteMessage deleteParams);
}
@@ -0,0 +1,8 @@
using Govor.Application.Messages.Parameters;
namespace Govor.Application.Messages;
public interface IMessageSendingService
{
Task<SendMessageResult> SendMessageAsync(SendMessage sendParams);
}
@@ -0,0 +1,9 @@
using Govor.Domain.Models.Messages;
namespace Govor.Application.Messages;
public interface IMessagesLoader
{
Task<List<Message>> LoadMessagesInUserChat(Guid privateChatId,Guid currentId, Guid? startMessageId, int before = 20, int after = 2);
Task<List<Message>> LoadMessagesInChatGroup(Guid chatId,Guid currentId, Guid? startMessageId, int before = 20, int after = 2);
}
@@ -0,0 +1,57 @@
using Govor.Application.Messages.Parameters;
using Microsoft.EntityFrameworkCore;
using Govor.Domain;
using Govor.Domain.Models.Messages;
using Microsoft.Extensions.Logging;
namespace Govor.Application.Messages;
public class MessageEditingService : IMessageEditingService
{
private readonly GovorDbContext _dbContext;
private readonly ILogger<MessageEditingService> _logger;
public MessageEditingService(GovorDbContext dbContext, ILogger<MessageEditingService> logger)
{
_dbContext = dbContext;
_logger = logger;
}
public async Task<EditMessageResult> EditMessageAsync(EditMessage editParams)
{
var message = await _dbContext.Messages
.Include(m => m.MediaAttachments)
.FirstOrDefaultAsync(m => m.Id == editParams.MessageId);
if (message == null)
{
return new EditMessageResult(false, new KeyNotFoundException("Message not found."), null);
}
if (message.SenderId != editParams.EditorId)
{
_logger.LogWarning("User {EditorId} unauthorized to edit message {MessageId}", editParams.EditorId, editParams.MessageId);
return new EditMessageResult(false, new UnauthorizedAccessException("User is not authorized to edit this message."), null);
}
var originalMessageSnapshot = new Message
{
Id = message.Id,
SenderId = message.SenderId,
RecipientId = message.RecipientId,
RecipientType = message.RecipientType,
SentAt = message.SentAt,
ReplyToMessageId = message.ReplyToMessageId,
MediaAttachments = message.MediaAttachments?.ToList() ?? []
};
message.EncryptedContent = editParams.NewContent;
message.IsEdited = true;
message.EditedAt = editParams.EditedAt;
await _dbContext.SaveChangesAsync();
_logger.LogInformation("Message {MessageId} edited successfully.", editParams.MessageId);
return new EditMessageResult(true, null, originalMessageSnapshot);
}
}
@@ -0,0 +1,24 @@
using Govor.Application.Messages.Parameters;
using Govor.Domain;
using Microsoft.Extensions.Logging;
namespace Govor.Application.Messages;
public class MessageRemovingService : IMessageRemovingService
{
private readonly GovorDbContext _govorDbContext;
private readonly ILogger<MessageRemovingService> _logger;
public MessageRemovingService
(GovorDbContext govorDbContext,
ILogger<MessageRemovingService> logger)
{
_govorDbContext = govorDbContext;
_logger = logger;
}
public Task<DeleteMessageResult> DeleteMessageAsync(DeleteMessage deleteParams)
{
throw new NotImplementedException();
}
}
@@ -0,0 +1,77 @@
using Govor.Application.Messages.Parameters;
using Microsoft.EntityFrameworkCore;
using Govor.Domain;
using Govor.Domain.Models.Messages;
using Microsoft.Extensions.Logging;
namespace Govor.Application.Messages;
public class MessageSendingService : IMessageSendingService
{
private readonly GovorDbContext _dbContext;
private readonly ILogger<MessageSendingService> _logger;
public MessageSendingService(GovorDbContext dbContext, ILogger<MessageSendingService> logger)
{
_dbContext = dbContext;
_logger = logger;
}
public async Task<SendMessageResult> SendMessageAsync(SendMessage sendParams)
{
var validationResult = sendParams.RecipientType switch
{
RecipientType.User => await ValidateUserRecipientAsync(sendParams.RecipientId),
RecipientType.Group => await ValidateGroupRecipientAsync(sendParams.FromUserId, sendParams.RecipientId),
_ => (Success: false, Error: "Invalid recipient type.")
};
if (!validationResult.Success)
{
_logger.LogWarning("Message send failed: {Error}", validationResult.Error);
return new SendMessageResult(false, new InvalidOperationException(validationResult.Error), default);
}
var messageId = Guid.NewGuid();
var message = new Message
{
Id = messageId,
SenderId = sendParams.FromUserId,
RecipientId = sendParams.RecipientId,
RecipientType = sendParams.RecipientType,
EncryptedContent = sendParams.EncryptContent,
SentAt = sendParams.SendAt,
IsEdited = false,
ReplyToMessageId = sendParams.ReplyToMessageId,
MediaAttachments = sendParams.Media?.Select(m => new MediaAttachments
{
Id = Guid.NewGuid(),
MessageId = messageId,
MediaFileId = m.MediaId
}).ToList() ?? []
};
await _dbContext.Messages.AddAsync(message);
await _dbContext.SaveChangesAsync();
_logger.LogInformation("Message {MessageId} sent successfully.", messageId);
return new SendMessageResult(true, null, message);
}
private async Task<(bool Success, string Error)> ValidateUserRecipientAsync(Guid chatId)
{
var chatExists = await _dbContext.PrivateChats.AnyAsync(c => c.Id == chatId);
return chatExists ? (true, null) : (false, $"Private chat {chatId} not found.");
}
private async Task<(bool Success, string Error)> ValidateGroupRecipientAsync(Guid userId, Guid groupId)
{
var groupExists = await _dbContext.ChatGroups.AnyAsync(g => g.Id == groupId);
if (!groupExists) return (false, $"Group {groupId} not found.");
var isMember = await _dbContext.GroupMemberships.AnyAsync(gm => gm.UserId == userId && gm.GroupId == groupId);
if (!isMember) return (false, "Sender is not a member of the group.");
return (true, null);
}
}
@@ -0,0 +1,107 @@
using Govor.Application.Interfaces;
using Govor.Domain.Models.Messages;
using Govor.Domain;
using Microsoft.EntityFrameworkCore;
namespace Govor.Application.Messages;
public class MessagesLoader : IMessagesLoader
{
private readonly GovorDbContext _dbContext;
public MessagesLoader(GovorDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task<List<Message>> LoadMessagesInUserChat(
Guid privateChatId,
Guid currentUser,
Guid? startMessageId,
int before = 20,
int after = 2)
{
if (privateChatId == Guid.Empty)
throw new ArgumentException("PrivateChatId id cannot be empty", nameof(privateChatId));
var chatExists = await _dbContext.PrivateChats.AnyAsync(c => c.Id == privateChatId);
if (!chatExists)
return [];
var query = _dbContext.Messages
.AsNoTracking()
.Include(m => m.MediaAttachments)
.ThenInclude(m => m.MediaFile)
.Where(m => m.RecipientType == RecipientType.User && m.RecipientId == privateChatId);
return await FetchPaginatedMessagesAsync(query, startMessageId, before, after);
}
public async Task<List<Message>> LoadMessagesInChatGroup(
Guid chatId,
Guid currentUser,
Guid? startMessageId,
int before = 20,
int after = 2)
{
if (chatId == Guid.Empty)
throw new ArgumentException("Chat id cannot be empty", nameof(chatId));
var isMember = await _dbContext.GroupMemberships
.AnyAsync(gm => gm.UserId == currentUser && gm.GroupId == chatId);
if (!isMember)
return [];
var query = _dbContext.Messages
.AsNoTracking()
.Include(m => m.MediaAttachments)
.ThenInclude(m => m.MediaFile)
.AsSplitQuery()
.Where(m => m.RecipientType == RecipientType.Group && m.RecipientId == chatId);
return await FetchPaginatedMessagesAsync(query, startMessageId, before, after);
}
private static async Task<List<Message>> FetchPaginatedMessagesAsync(
IQueryable<Message> baseQuery,
Guid? startMessageId,
int before,
int after)
{
if (startMessageId is null)
{
return await baseQuery
.OrderByDescending(m => m.SentAt)
.Take(before)
.OrderBy(m => m.SentAt)
.ToListAsync();
}
var startMessage = await baseQuery.FirstOrDefaultAsync(m => m.Id == startMessageId.Value);
if (startMessage == null)
return [];
var beforeMessages = await baseQuery
.Where(m => m.SentAt < startMessage.SentAt)
.OrderByDescending(m => m.SentAt)
.Take(before)
.ToListAsync();
var afterMessages = await baseQuery
.Where(m => m.SentAt > startMessage.SentAt)
.OrderBy(m => m.SentAt)
.Take(after)
.ToListAsync();
beforeMessages.Reverse();
var result = new List<Message>(beforeMessages.Count + 1 + afterMessages.Count);
result.AddRange(beforeMessages);
result.Add(startMessage);
result.AddRange(afterMessages);
return result;
}
}
@@ -0,0 +1,5 @@
namespace Govor.Application.Messages.Parameters;
public record DeleteMessage(
Guid DeleterId,
Guid MessageId);
@@ -0,0 +1,6 @@
using Govor.Domain.Models.Messages;
namespace Govor.Application.Messages.Parameters;
public record DeleteMessageResult(bool IsSuccess, Exception? Exception, Message? OriginalMessage)
: Result(IsSuccess, Exception, OriginalMessage?.Id ?? Guid.Empty);
@@ -0,0 +1,7 @@
namespace Govor.Application.Messages.Parameters;
public record EditMessage(
Guid EditorId,
Guid MessageId,
string NewContent,
DateTime EditedAt);
@@ -0,0 +1,9 @@
using Govor.Domain.Models.Messages;
namespace Govor.Application.Messages.Parameters;
public record EditMessageResult(bool IsSuccess, Exception? Exception, Message? OriginalMessage)
: Result(IsSuccess, Exception, OriginalMessage?.Id ?? Guid.Empty)
{
}
@@ -0,0 +1,3 @@
namespace Govor.Application.Messages.Parameters;
public record Result(bool IsSuccess, Exception Exception, Guid messageId);
@@ -0,0 +1,3 @@
namespace Govor.Application.Messages.Parameters;
public record SendMedia(Guid MediaId, string EncryptedKey);
@@ -0,0 +1,12 @@
using Govor.Domain.Models.Messages;
namespace Govor.Application.Messages.Parameters;
public record SendMessage(
string EncryptContent,
Guid? ReplyToMessageId,
Guid RecipientId,
RecipientType RecipientType,
Guid FromUserId,
DateTime SendAt,
IEnumerable<SendMedia> Media);
@@ -0,0 +1,6 @@
using Govor.Domain.Models.Messages;
namespace Govor.Application.Messages.Parameters;
public record SendMessageResult(bool IsSuccess, Exception? Exception, Message Message)
: Result(IsSuccess, Exception, Message?.Id ?? Guid.Empty);