apply fix on Models Enrichments ...
This commit is contained in:
@@ -10,135 +10,144 @@ using xIdentityService.Extensions;
|
||||
using xModels.Dtos;
|
||||
using System.Linq;
|
||||
using xAiModels.Interfaces.Dtos;
|
||||
using xAiModels.Models.Entities;
|
||||
using xAiModels.Interfaces;
|
||||
|
||||
namespace xAiModels.Providers.Enrichers
|
||||
{
|
||||
public class XAiMessageEnricher : IXAiMessageEnricher
|
||||
{
|
||||
private readonly IXIdentityProvider identityProvider;
|
||||
private readonly IXAiConversationRepositoryService conversationRepositoryService;
|
||||
private readonly IXAiConversationTitleResourceProvider conversationTitleResourceProvider;
|
||||
|
||||
public XAiMessageEnricher(
|
||||
IXIdentityProvider identityProvider,
|
||||
IXAiConversationRepositoryService conversationRepositoryService
|
||||
IXAiConversationTitleResourceProvider conversationTitleResourceProvider
|
||||
)
|
||||
{
|
||||
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);
|
||||
this.conversationTitleResourceProvider = conversationTitleResourceProvider;
|
||||
}
|
||||
|
||||
//
|
||||
#region Private ...
|
||||
#region Enrichers ...
|
||||
/// <summary>
|
||||
/// Reading Owner User Info ...
|
||||
/// Enrich Specified Item ...
|
||||
/// </summary>
|
||||
/// <param name="ownerId"></param>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<XPersonDto> ResolveOwnerAsync(
|
||||
string ownerId,
|
||||
public virtual async Task<XAiMessageDto> Enrich(
|
||||
XAiMessage source,
|
||||
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,
|
||||
// Mapping Default ...
|
||||
var result = source.MapConvert<XAiMessageDto, XAiMessage>();
|
||||
result = await Enrich(
|
||||
source: result,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Enrich Specified Item ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async Task<XAiMessageDto> Enrich(
|
||||
XAiMessageDto source,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Handdle Owner ...
|
||||
XPersonDto owner = null;
|
||||
if (!source.OwnerId.IsNullOrEmpty())
|
||||
{
|
||||
//
|
||||
// Try to Retrieve User Info ...
|
||||
var device = identityProvider.GetDevice();
|
||||
var userInfo = await identityProvider.GetUserInfo(
|
||||
device: device,
|
||||
userSelectByParam: source.OwnerId
|
||||
);
|
||||
owner = userInfo.ToXPersonDto();
|
||||
}
|
||||
|
||||
//
|
||||
// Hanlde Conversation Title ...
|
||||
string conversationTitle = string.Empty;
|
||||
XResourceDto conversationTitleResource = null;
|
||||
if (!source.ConversationId.IsDefaultGuid())
|
||||
{
|
||||
//
|
||||
conversationTitleResource = await conversationTitleResourceProvider.GetResource(
|
||||
owner: source.OwnerId,
|
||||
identifier: source.ConversationId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
if (!conversationTitleResource.IsNullOrDefault())
|
||||
{
|
||||
conversationTitle = conversationTitleResource.Resource;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Attach Extracted Items ...
|
||||
var result = source.MapConvert<XAiMessageDto, XAiMessageDto>();
|
||||
result.Owner = owner;
|
||||
result.ConversationTitle = conversationTitle;
|
||||
result.ConversationTitleResouce = conversationTitleResource;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enrich Specified Items ...
|
||||
/// </summary>
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async Task<IEnumerable<XAiMessageDto>> EnrichRange(
|
||||
IEnumerable<XAiMessage> sources,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var tasks = sources
|
||||
.Select(x => Enrich(x, cancellationToken));
|
||||
|
||||
//
|
||||
var result = await Task.WhenAll(tasks);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enrich Specified Items ...
|
||||
/// </summary>
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async Task<IEnumerable<XAiMessageDto>> EnrichRange(
|
||||
IEnumerable<XAiMessageDto> sources,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var tasks = sources
|
||||
.Select(x => Enrich(x, cancellationToken));
|
||||
|
||||
//
|
||||
var result = await Task.WhenAll(tasks);
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user