fix: align backend APIs and upload flow
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using AutoMapper;
|
||||
using AutoMapper;
|
||||
using IM.Commons;
|
||||
using MessageService.Domain.Enums;
|
||||
using MessageService.Domain.IReposities;
|
||||
@@ -16,20 +16,22 @@ namespace MessageService.WebApi.Application.Message
|
||||
private readonly IMapper mapper;
|
||||
private readonly IGroupMemberIntegrationService memberService;
|
||||
private readonly IContactIntegrationService contactService;
|
||||
private readonly SquenceService squenceService;
|
||||
private readonly SquenceService squenceService; private readonly IM.InitCommon.Management.RuntimePolicy runtime;
|
||||
|
||||
public MessageService(IMessageReposity reposity, IConversationReposity conversationReposity, IMapper mapper, IGroupMemberIntegrationService memberService, IContactIntegrationService contactService, SquenceService squenceService)
|
||||
public MessageService(IMessageReposity reposity, IConversationReposity conversationReposity, IMapper mapper, IGroupMemberIntegrationService memberService, IContactIntegrationService contactService, SquenceService squenceService, IM.InitCommon.Management.RuntimePolicy runtime)
|
||||
{
|
||||
this.reposity = reposity;
|
||||
this.conversationReposity = conversationReposity;
|
||||
this.mapper = mapper;
|
||||
this.memberService = memberService;
|
||||
this.contactService = contactService;
|
||||
this.squenceService = squenceService;
|
||||
this.squenceService = squenceService; this.runtime = runtime;
|
||||
}
|
||||
|
||||
public async Task<Result<MessageResponse>> SendMsgAsync(SendMsgCommand command)
|
||||
{
|
||||
if (runtime.Current.TextLimit > 0 && command.MsgType == MessageType.Text && command.Text?.Length > runtime.Current.TextLimit)
|
||||
return Result.Fail<MessageResponse>(ResultCode.PARAMETER_ERROR, "文本超过平台长度限制");
|
||||
if (command.ChatType == Domain.Enums.ChatType.PRIVATE)
|
||||
{
|
||||
bool passed = await contactService.CheckContactAsync(command.SenderId, command.TargetId);
|
||||
@@ -52,13 +54,16 @@ namespace MessageService.WebApi.Application.Message
|
||||
{
|
||||
MessageType.Text => Domain.Entities.Message.BuildTxt(ctx, command.Text!, sequenceId),
|
||||
|
||||
MessageType.Image => Domain.Entities.Message.BuildImg(ctx, command.Url!,
|
||||
command.Width ?? 0, command.Height ?? 0, command.Thumb!, sequenceId),
|
||||
MessageType.Image => Domain.Entities.Message.BuildImg(ctx, command.Url,
|
||||
command.Width ?? 0, command.Height ?? 0, command.Thumb!, sequenceId, command.FileId),
|
||||
|
||||
MessageType.Video => Domain.Entities.Message.BuildVideo(ctx, command.Url!,
|
||||
command.Width ?? 0, command.Height ?? 0, command.Thumb!, sequenceId),
|
||||
MessageType.Video => Domain.Entities.Message.BuildVideo(ctx, command.Url,
|
||||
command.Width ?? 0, command.Height ?? 0, command.Thumb!, sequenceId, command.FileId),
|
||||
|
||||
MessageType.Voice => Domain.Entities.Message.BuildVoice(ctx, command.Url!, command.Duration ?? 0, sequenceId),
|
||||
MessageType.Voice => Domain.Entities.Message.BuildVoice(ctx, command.Url, command.Duration ?? 0, sequenceId, command.FileId),
|
||||
|
||||
MessageType.File => Domain.Entities.Message.BuildFile(ctx, command.FileId!.Value, command.Url,
|
||||
command.FileName!, command.FileSize!.Value, command.FileFormat!, sequenceId),
|
||||
|
||||
_ => null
|
||||
};
|
||||
@@ -102,14 +107,21 @@ namespace MessageService.WebApi.Application.Message
|
||||
return Result.Fail<object>(ResultCode.MESSAGE_NOT_FOUND);
|
||||
}
|
||||
|
||||
if (runtime.Current.RecallMinutes > 0 && DateTimeOffset.UtcNow - msg.CreationTime > TimeSpan.FromMinutes(runtime.Current.RecallMinutes))
|
||||
return Result.Fail<object>(ResultCode.PERMISSION_DENIED, "已超过平台撤回时限");
|
||||
msg.Withdraw();
|
||||
|
||||
return Result.Success();
|
||||
}
|
||||
|
||||
public async Task<Result<GetMessagesResponse>> GetMessagesAsync(GetMessageCommand command)
|
||||
public async Task<Result<GetMessagesResponse>> GetMessagesAsync(GetMessageCommand command, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var conversation = await conversationReposity.FindByIdAsync(command.conversationId);
|
||||
if (command.direction is not 0 and not 1 || command.limit is < 1 or > 100)
|
||||
{
|
||||
return Result.Fail<GetMessagesResponse>(ResultCode.PARAMETER_ERROR);
|
||||
}
|
||||
|
||||
var conversation = await conversationReposity.FindByIdAsync(command.conversationId, cancellationToken);
|
||||
|
||||
if(conversation is null || conversation.UserId != command.userId)
|
||||
{
|
||||
@@ -117,9 +129,37 @@ namespace MessageService.WebApi.Application.Message
|
||||
}
|
||||
|
||||
|
||||
var messages = await reposity.GetAsync(conversation.StreamKey, command.cusor, command.direction, command.limit);
|
||||
var messages = await reposity.GetAsync(conversation.StreamKey, command.cusor, command.direction, command.limit, cancellationToken);
|
||||
|
||||
return Result.Success(new GetMessagesResponse(mapper.Map<List<MessageResponse>>(messages.messages.ToList()),messages.hasMore));
|
||||
}
|
||||
|
||||
public async Task<Result<GetMessagesResponse>> SearchMessagesAsync(
|
||||
SearchMessageCommand command,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var keyword = command.Keyword?.Trim() ?? string.Empty;
|
||||
if (keyword.Length is < 1 or > 50 || command.Limit is < 1 or > 50)
|
||||
{
|
||||
return Result.Fail<GetMessagesResponse>(ResultCode.PARAMETER_ERROR);
|
||||
}
|
||||
|
||||
var conversation = await conversationReposity.FindByIdAsync(command.ConversationId, cancellationToken);
|
||||
if (conversation is null || conversation.UserId != command.UserId)
|
||||
{
|
||||
return Result.Fail<GetMessagesResponse>(ResultCode.PERMISSION_DENIED);
|
||||
}
|
||||
|
||||
var messages = await reposity.SearchAsync(
|
||||
conversation.StreamKey,
|
||||
keyword,
|
||||
command.Cursor,
|
||||
command.Limit,
|
||||
cancellationToken);
|
||||
|
||||
return Result.Success(new GetMessagesResponse(
|
||||
mapper.Map<List<MessageResponse>>(messages.messages.ToList()),
|
||||
messages.hasMore));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace MessageService.WebApi.Application.Message
|
||||
{
|
||||
public record SearchMessageCommand(
|
||||
Guid ConversationId,
|
||||
Guid UserId,
|
||||
string Keyword,
|
||||
long? Cursor,
|
||||
int Limit);
|
||||
}
|
||||
@@ -22,8 +22,12 @@ namespace MessageService.WebApi.Application.Message
|
||||
public int? Height { get; init; }
|
||||
public string? Thumb { get; init; }
|
||||
public int? Duration { get; init; }
|
||||
public Guid? FileId { get; init; }
|
||||
public string? FileName { get; init; }
|
||||
public long? FileSize { get; init; }
|
||||
public string? FileFormat { get; init; }
|
||||
|
||||
public SendMsgCommand(Guid senderId, Guid targetId, ChatType chatType, MessageType msgType, Guid clientMsgId, Guid? quoteMessageId, Dictionary<string, string>? ext, string? text, string? url, int? width, int? height, string? thumb, int? duration)
|
||||
public SendMsgCommand(Guid senderId, Guid targetId, ChatType chatType, MessageType msgType, Guid clientMsgId, Guid? quoteMessageId, Dictionary<string, string>? ext, string? text, string? url, int? width, int? height, string? thumb, int? duration, Guid? fileId, string? fileName, long? fileSize, string? fileFormat)
|
||||
{
|
||||
SenderId = senderId;
|
||||
TargetId = targetId;
|
||||
@@ -38,6 +42,10 @@ namespace MessageService.WebApi.Application.Message
|
||||
Height = height;
|
||||
Thumb = thumb;
|
||||
Duration = duration;
|
||||
FileId = fileId;
|
||||
FileName = fileName;
|
||||
FileSize = fileSize;
|
||||
FileFormat = fileFormat;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user