fix: align backend APIs and upload flow
This commit is contained in:
@@ -8,7 +8,7 @@ namespace MessageService.WebApi.Application.Conversation
|
||||
public ConversationMapperConfig()
|
||||
{
|
||||
CreateMap<Domain.Entities.Conversation, ConversationResponse>()
|
||||
.ForMember(dest => dest.DateTime, opt => opt.MapFrom(src => src.ModificationTime))
|
||||
.ForMember(dest => dest.DateTime, opt => opt.MapFrom(src => src.LastMessageTime ?? src.CreationTime))
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using AutoMapper;
|
||||
using IM.Commons;
|
||||
using System.Diagnostics;
|
||||
using MessageService.Domain.IReposities;
|
||||
using MessageService.WebApi.Application.Dtos;
|
||||
|
||||
@@ -9,22 +10,53 @@ namespace MessageService.WebApi.Application.Conversation
|
||||
{
|
||||
private readonly IConversationReposity reposity;
|
||||
private readonly IMapper mapper;
|
||||
private readonly ILogger<ConversationService> logger;
|
||||
|
||||
public ConversationService(IConversationReposity reposity, IMapper mapper)
|
||||
public ConversationService(IConversationReposity reposity, IMapper mapper, ILogger<ConversationService> logger)
|
||||
{
|
||||
this.reposity = reposity;
|
||||
this.mapper = mapper;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public async Task<Result<List<ConversationResponse>>> GetByOwnerIdAsync(Guid userId)
|
||||
public async Task<Result<List<ConversationResponse>>> GetByOwnerIdAsync(Guid userId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var list = await reposity.FindByUserIdAsync(userId);
|
||||
return Result.Success(mapper.Map<List<ConversationResponse>>(list.ToList()));
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
try
|
||||
{
|
||||
var list = await reposity.ListByUserIdAsync(userId, cancellationToken);
|
||||
return Result.Success(list.Select(item => new ConversationResponse
|
||||
{
|
||||
Id = item.Id,
|
||||
UserId = item.UserId,
|
||||
TargetId = item.TargetId,
|
||||
TargetAvatar = item.TargetAvatar,
|
||||
TargetName = item.TargetName,
|
||||
LastReadSequenceId = item.LastReadSequenceId,
|
||||
UnreadCount = item.UnreadCount,
|
||||
ChatType = item.ChatType,
|
||||
LastMessage = item.LastMessage,
|
||||
DateTime = item.DateTime
|
||||
}).ToList());
|
||||
}
|
||||
finally
|
||||
{
|
||||
stopwatch.Stop();
|
||||
var traceId = Activity.Current?.TraceId.ToString() ?? string.Empty;
|
||||
if (stopwatch.ElapsedMilliseconds > 1000)
|
||||
{
|
||||
logger.LogWarning("Conversation list query was slow. TraceId={TraceId} UserId={UserId} ElapsedMs={ElapsedMs}", traceId, userId, stopwatch.ElapsedMilliseconds);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.LogInformation("Conversation list query completed. TraceId={TraceId} UserId={UserId} ElapsedMs={ElapsedMs}", traceId, userId, stopwatch.ElapsedMilliseconds);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Result<ConversationResponse>> GetByIdAsync(Guid id, Guid userId)
|
||||
public async Task<Result<ConversationResponse>> GetByIdAsync(Guid id, Guid userId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var conversation = await reposity.FindByIdAsync(id);
|
||||
var conversation = await reposity.FindByIdAsync(id, cancellationToken);
|
||||
|
||||
if (conversation is null || conversation.UserId != userId)
|
||||
{
|
||||
@@ -34,21 +66,21 @@ namespace MessageService.WebApi.Application.Conversation
|
||||
return Result.Success(mapper.Map<ConversationResponse>(conversation));
|
||||
}
|
||||
|
||||
public async Task<Result<List<string>>> GetStreamkeysAsync(Guid userId)
|
||||
public async Task<Result<List<string>>> GetStreamkeysAsync(Guid userId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var list = await reposity.FindAllStreamKeyAsync(userId);
|
||||
var list = await reposity.FindAllStreamKeyAsync(userId, cancellationToken);
|
||||
return Result.Success(list.ToList());
|
||||
}
|
||||
|
||||
public async Task<Result<object>> MarkAsReadAsync(Guid conversationId, Guid userId)
|
||||
public async Task<Result<object>> MarkAsReadAsync(Guid conversationId, Guid userId, long? lastReadSequenceId = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var conversation = await reposity.FindByIdAsync(conversationId);
|
||||
var conversation = await reposity.FindByIdAsync(conversationId, cancellationToken);
|
||||
if (conversation is null || conversation.UserId != userId)
|
||||
{
|
||||
return Result.Fail<object>(ResultCode.CONVERSATION_NOT_FOUND);
|
||||
}
|
||||
|
||||
conversation.MarkAsRead(lastReadSequenceId: null);
|
||||
conversation.MarkAsRead(lastReadSequenceId);
|
||||
return Result.Success();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user