276 lines
8.6 KiB
C#
276 lines
8.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
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 xAiModels.Interfaces;
|
|
using xAiModels.Interfaces.Dtos;
|
|
|
|
namespace xAiModels.Providers.Enrichers
|
|
{
|
|
public class XAiProjectEnricher : IXAiProjectEnricher
|
|
{
|
|
private readonly IXIdentityProvider identityProvider;
|
|
private readonly IXAiProjectTitleResourceProvider projectTitleResourceProvider;
|
|
// private readonly IXAiConversationRepositoryService conversationRepositoryService;
|
|
private readonly IXAiProjectDescriptionResourceProvider projectDescriptionResourceProvider;
|
|
|
|
public XAiProjectEnricher(
|
|
IXIdentityProvider identityProvider,
|
|
IXAiProjectTitleResourceProvider projectTitleResourceProvider,
|
|
// IXAiConversationRepositoryService conversationRepositoryService,
|
|
IXAiProjectDescriptionResourceProvider projectDescriptionResourceProvider
|
|
)
|
|
{
|
|
this.identityProvider = identityProvider;
|
|
this.projectTitleResourceProvider = projectTitleResourceProvider;
|
|
// this.conversationRepositoryService = conversationRepositoryService;
|
|
this.projectDescriptionResourceProvider = projectDescriptionResourceProvider;
|
|
}
|
|
|
|
public async Task EnrichAsync(
|
|
XAiProjectDto source,
|
|
XAiProjectDto destination,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
await EnrichAsync(
|
|
source: source,
|
|
destination: destination,
|
|
fillConversations: false,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
}
|
|
|
|
public async Task EnrichAsync(
|
|
XAiProjectDto source,
|
|
XAiProjectDto destination,
|
|
bool fillConversations = true,
|
|
bool fillConversationMessages = true,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Run independent async operations concurrently
|
|
var conversationsTask = ResolveConversationsAsync(
|
|
source.Id,
|
|
fillConversationMessages,
|
|
cancellationToken
|
|
);
|
|
var titleTask = ResolveTitleAsync(source.Title, cancellationToken);
|
|
var ownerTask = ResolveOwnerAsync(source.OwnerId, cancellationToken);
|
|
var descriptionTask = ResolveDescriptionAsync(source.Description, cancellationToken);
|
|
|
|
//
|
|
// wait until All Tasks Resolved ...
|
|
Task[] tasks =
|
|
{
|
|
titleTask,
|
|
ownerTask,
|
|
descriptionTask
|
|
};
|
|
if (fillConversations)
|
|
{
|
|
tasks = [.. tasks, conversationsTask];
|
|
}
|
|
await Task.WhenAll(tasks);
|
|
|
|
//
|
|
// Attached Resolved Info to Destination ...
|
|
destination.Owner = ownerTask.Result;
|
|
destination.TitleResource = titleTask.Result;
|
|
destination.DescriptionResource = descriptionTask.Result;
|
|
if (!conversationsTask.Result.IsNull())
|
|
{
|
|
destination.Conversations = [.. conversationsTask.Result];
|
|
}
|
|
}
|
|
|
|
public async Task EnrichRangeAsync(
|
|
IEnumerable<XAiProjectDto> sources,
|
|
IEnumerable<XAiProjectDto> destinations,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Make Tasks Ready ...
|
|
var tasks = sources.Zip(destinations, (src, dst) =>
|
|
EnrichAsync(src, dst, cancellationToken)
|
|
);
|
|
|
|
//
|
|
await Task.WhenAll(tasks);
|
|
}
|
|
|
|
public async Task EnrichRangeAsync(
|
|
IEnumerable<XAiProjectDto> sources,
|
|
IEnumerable<XAiProjectDto> destinations,
|
|
bool fillConversations = true,
|
|
bool fillConversationMessages = true,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Make Tasks Ready ...
|
|
var tasks = sources.Zip(destinations, (src, dst) =>
|
|
EnrichAsync(
|
|
source: src,
|
|
destination: dst,
|
|
fillConversations: fillConversations,
|
|
cancellationToken: cancellationToken,
|
|
fillConversationMessages: fillConversationMessages
|
|
)
|
|
);
|
|
|
|
//
|
|
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>
|
|
/// Retrieve Title Resource ...
|
|
/// </summary>
|
|
/// <param name="identifier"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
private async Task<XResourceDto> ResolveTitleAsync(
|
|
string identifier,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
XResourceDto result = null;
|
|
|
|
//
|
|
// Validate ...
|
|
if (identifier.IsNullOrEmpty())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
result = await projectTitleResourceProvider.GetResource(
|
|
owner: null,
|
|
identifier: identifier,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve Description Resource ...
|
|
/// </summary>
|
|
/// <param name="identifier"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
private async Task<XResourceDto> ResolveDescriptionAsync(
|
|
string identifier,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
XResourceDto result = null;
|
|
|
|
//
|
|
// Validate ...
|
|
if (identifier.IsNullOrEmpty())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
result = await projectDescriptionResourceProvider.GetResource(
|
|
owner: null,
|
|
identifier: identifier,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve Project Conversations ...
|
|
/// </summary>
|
|
/// <param name="projectId"></param>
|
|
/// <param name="fillMessages"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
private async Task<List<XAiConversationDto>> ResolveConversationsAsync(
|
|
Guid projectId,
|
|
bool fillMessages = false,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
List<XAiConversationDto> result = [];
|
|
|
|
//
|
|
// Validate ...
|
|
if (projectId.IsNull() ||
|
|
projectId.IsDefaultGuid()
|
|
)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// var items = await conversationRepositoryService.FindManyAsync(
|
|
// orderBuilder: null,
|
|
// includeBuilder: null,
|
|
// ignoreSoftDeleteds: true,
|
|
// fillMessages: fillMessages,
|
|
// predicate: x => x.Id == projectId,
|
|
// cancellationToken: cancellationToken
|
|
// );
|
|
// result = [.. items];
|
|
|
|
//
|
|
return result;
|
|
}
|
|
#endregion
|
|
}
|
|
} |