144 lines
4.3 KiB
C#
144 lines
4.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using xAiModels.Interfaces.Enrichers;
|
|
using xAiModels.Models.Dtos;
|
|
using xCommons.Extensions;
|
|
using xIdentityService.Interfaces;
|
|
using xIdentityService.Extensions;
|
|
using xModels.Dtos;
|
|
using System.Linq;
|
|
using xAiModels.Interfaces.Dtos;
|
|
|
|
namespace xAiModels.Providers.Enrichers
|
|
{
|
|
public class XAiMessageEnricher : IXAiMessageEnricher
|
|
{
|
|
private readonly IXIdentityProvider identityProvider;
|
|
private readonly IXAiConversationRepositoryService conversationRepositoryService;
|
|
|
|
public XAiMessageEnricher(
|
|
IXIdentityProvider identityProvider,
|
|
IXAiConversationRepositoryService conversationRepositoryService
|
|
)
|
|
{
|
|
this.identityProvider = identityProvider;
|
|
this.conversationRepositoryService = conversationRepositoryService;
|
|
}
|
|
|
|
public async Task EnrichAsync(
|
|
XAiMessageDto source,
|
|
XAiMessageDto destination,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Run independent async operations concurrently
|
|
var ownerTask = ResolveOwnerAsync(source.OwnerId, cancellationToken);
|
|
var conversationTask = ResolveConversationAsync(source.ConversationId, cancellationToken);
|
|
|
|
//
|
|
// wait until All Tasks Resolved ...
|
|
await Task.WhenAll(ownerTask, conversationTask);
|
|
|
|
//
|
|
// Attached Resolved Info to Destination ...
|
|
destination.Owner = ownerTask.Result;
|
|
if (!conversationTask.Result.IsNullOrDefault())
|
|
{
|
|
//
|
|
destination.ConversationTitle = conversationTask.Result.Title;
|
|
destination.ConversationTitleResouce = conversationTask.Result.TitleResource;
|
|
}
|
|
}
|
|
|
|
public async Task EnrichRangeAsync(
|
|
IEnumerable<XAiMessageDto> sources,
|
|
IEnumerable<XAiMessageDto> destinations,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Make Tasks Ready ...
|
|
var tasks = sources.Zip(destinations, (src, dst) =>
|
|
EnrichAsync(src, dst, cancellationToken)
|
|
);
|
|
|
|
//
|
|
await Task.WhenAll(tasks);
|
|
}
|
|
|
|
//
|
|
#region Private ...
|
|
/// <summary>
|
|
/// Reading Owner User Info ...
|
|
/// </summary>
|
|
/// <param name="ownerId"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
private async Task<XPersonDto> ResolveOwnerAsync(
|
|
string ownerId,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
XPersonDto result = null;
|
|
|
|
//
|
|
// Validate ...
|
|
if (ownerId.IsNullOrEmpty())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Try to Retrieve User Info ...
|
|
var device = identityProvider.GetDevice();
|
|
var userInfo = await identityProvider.GetUserInfo(
|
|
device: device,
|
|
userSelectByParam: ownerId
|
|
);
|
|
result = userInfo.ToXPersonDto();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reading Conversation ...
|
|
/// </summary>
|
|
/// <param name="conversationId"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
private async Task<XAiConversationDto> ResolveConversationAsync(
|
|
Guid conversationId,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
XAiConversationDto result = null;
|
|
|
|
//
|
|
// Validate ...
|
|
if (conversationId.IsNull() ||
|
|
conversationId.IsDefaultGuid()
|
|
)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
result = await conversationRepositoryService.GetAsync(
|
|
id: conversationId,
|
|
includeBuilder: null,
|
|
ignoreSoftDeleteds: true,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
#endregion
|
|
}
|
|
} |