196 lines
6.8 KiB
C#
196 lines
6.8 KiB
C#
using System.Linq;
|
|
using xModels.Dtos;
|
|
using System.Threading;
|
|
using xCommons.Extensions;
|
|
using xAiModels.Interfaces;
|
|
using xAiModels.Models.Dtos;
|
|
using System.Threading.Tasks;
|
|
using xAiModels.Models.Entities;
|
|
using System.Collections.Generic;
|
|
using xIdentityService.Interfaces;
|
|
using xIdentityService.Extensions;
|
|
using xAiModels.Interfaces.Entities;
|
|
using xAiModels.Interfaces.Enrichers;
|
|
|
|
namespace xAiModels.Providers.Enrichers
|
|
{
|
|
public class XAiConversationEnricher : IXAiConversationEnricher
|
|
{
|
|
private readonly IXIdentityProvider identityProvider;
|
|
private readonly IXAiConversationRepository repository;
|
|
private readonly IXAiProjectTitleResourceProvider projectTitleResourceProvider;
|
|
private readonly IXAiConversationTitleResourceProvider conversationTitleResourceProvider;
|
|
private readonly IXAiProjectDescriptionResourceProvider projectDescriptionResourceProvider;
|
|
|
|
public XAiConversationEnricher(
|
|
IXIdentityProvider identityProvider,
|
|
IXAiConversationRepository repository,
|
|
IXAiProjectTitleResourceProvider projectTitleResourceProvider,
|
|
IXAiConversationTitleResourceProvider conversationTitleResourceProvider,
|
|
IXAiProjectDescriptionResourceProvider projectDescriptionResourceProvider
|
|
)
|
|
{
|
|
//
|
|
this.repository = repository;
|
|
this.identityProvider = identityProvider;
|
|
this.projectTitleResourceProvider = projectTitleResourceProvider;
|
|
this.conversationTitleResourceProvider = conversationTitleResourceProvider;
|
|
this.projectDescriptionResourceProvider = projectDescriptionResourceProvider;
|
|
}
|
|
|
|
//
|
|
#region Enrichers ...
|
|
/// <summary>
|
|
/// Enrich Specified Item ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="fillMessages"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<XAiConversationDto> Enrich(
|
|
XAiConversation source,
|
|
bool fillMessages = false,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Mapping Default ...
|
|
var result = source.MapConvert<XAiConversationDto, XAiConversation>();
|
|
result = await Enrich(
|
|
source: result,
|
|
fillMessages: fillMessages,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Enrich Specified Item ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="fillMessages"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<XAiConversationDto> Enrich(
|
|
XAiConversationDto source,
|
|
bool fillMessages = false,
|
|
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();
|
|
}
|
|
|
|
//
|
|
// Handle Title Resource ...
|
|
XResourceDto titleResource = null;
|
|
if (!source.Title.IsNullOrEmpty())
|
|
{
|
|
//
|
|
titleResource = await conversationTitleResourceProvider.GetResource(
|
|
owner: source.OwnerId,
|
|
identifier: source.Id,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
}
|
|
|
|
//
|
|
// Handle Project Info ...
|
|
XResourceDto projectTitleResource = null;
|
|
XResourceDto projectDescriptionResource = null;
|
|
if (!source.ProjectId.IsDefaultGuid())
|
|
{
|
|
//
|
|
projectTitleResource = await projectTitleResourceProvider.GetResource(
|
|
owner: source.OwnerId,
|
|
identifier: source.ProjectId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
projectDescriptionResource = await projectDescriptionResourceProvider.GetResource(
|
|
owner: source.OwnerId,
|
|
identifier: source.ProjectId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
}
|
|
|
|
//
|
|
// Attach Extracted Items ...
|
|
var result = source.MapConvert<XAiConversationDto, XAiConversationDto>();
|
|
result.Owner = owner;
|
|
result.TitleResource = titleResource;
|
|
result.ProjectTitleResource = projectTitleResource;
|
|
result.ProjectDescriptionResource = projectDescriptionResource;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Enrich Specified Items ...
|
|
/// </summary>
|
|
/// <param name="sources"></param>
|
|
/// <param name="fillMessages"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<IEnumerable<XAiConversationDto>> EnrichRange(
|
|
IEnumerable<XAiConversation> sources,
|
|
bool fillMessages = false,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var tasks = sources
|
|
.Select(x => Enrich(
|
|
source: x,
|
|
fillMessages: fillMessages,
|
|
cancellationToken: cancellationToken
|
|
));
|
|
|
|
//
|
|
var result = await Task.WhenAll(tasks);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Enrich Specified Items ...
|
|
/// </summary>
|
|
/// <param name="sources"></param>
|
|
/// <param name="fillMessages"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<IEnumerable<XAiConversationDto>> EnrichRange(
|
|
IEnumerable<XAiConversationDto> sources,
|
|
bool fillMessages = false,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var tasks = sources
|
|
.Select(x => Enrich(
|
|
source: x,
|
|
fillMessages: fillMessages,
|
|
cancellationToken: cancellationToken
|
|
));
|
|
|
|
//
|
|
var result = await Task.WhenAll(tasks);
|
|
return result;
|
|
}
|
|
#endregion
|
|
}
|
|
} |