fix: align backend APIs and upload flow

This commit is contained in:
2026-09-15 14:10:55 +08:00
parent 32177a7293
commit 53e6195938
149 changed files with 4791 additions and 435 deletions
@@ -2,11 +2,13 @@
using MassTransit;
using MessageService.Domain.IReposities;
using MessageService.Infrastructure;
using Microsoft.EntityFrameworkCore;
using MySqlConnector;
namespace MessageService.WebApi.Application.EventHandlers
{
public class ConversationAddHandler : IConsumer<GroupMemberJoinedEvent>,
IConsumer<FriendAddedEvent>
IConsumer<FriendAddedEvent>, IConsumer<GroupMemberLeftEvent>
{
private readonly IConversationReposity reposity;
@@ -21,6 +23,11 @@ namespace MessageService.WebApi.Application.EventHandlers
public async Task Consume(ConsumeContext<GroupMemberJoinedEvent> context)
{
var @event = context.Message;
var existing = await reposity.FindActiveAsync(@event.UserId, @event.GroupId, Domain.Enums.ChatType.GROUP, context.CancellationToken);
if (existing is not null)
{
return;
}
reposity.Create(new Domain.Entities.Conversation(
userId: @event.UserId,
targetId: @event.GroupId,
@@ -32,12 +39,17 @@ namespace MessageService.WebApi.Application.EventHandlers
lastMessage: string.Empty
));
await messageDb.SaveChangesAsync();
await SaveIdempotentlyAsync(context.CancellationToken);
}
public async Task Consume(ConsumeContext<FriendAddedEvent> context)
{
var @event = context.Message;
var existing = await reposity.FindActiveAsync(@event.OwnerId, @event.TargetId, Domain.Enums.ChatType.PRIVATE, context.CancellationToken);
if (existing is not null)
{
return;
}
reposity.Create(new Domain.Entities.Conversation(
userId: @event.OwnerId,
targetId: @event.TargetId,
@@ -49,7 +61,33 @@ namespace MessageService.WebApi.Application.EventHandlers
lastMessage: string.Empty
));
await messageDb.SaveChangesAsync();
await SaveIdempotentlyAsync(context.CancellationToken);
}
public async Task Consume(ConsumeContext<GroupMemberLeftEvent> context)
{
var conversation = await reposity.FindActiveAsync(
context.Message.UserId,
context.Message.GroupId,
Domain.Enums.ChatType.GROUP,
context.CancellationToken);
if (conversation is not null)
{
conversation.SoftDelete();
}
await messageDb.SaveChangesAsync(context.CancellationToken);
}
private async Task SaveIdempotentlyAsync(CancellationToken cancellationToken)
{
try
{
await messageDb.SaveChangesAsync(cancellationToken);
}
catch (DbUpdateException exception) when (exception.InnerException is MySqlException { Number: 1062 })
{
messageDb.ChangeTracker.Clear();
}
}
}
}