165 lines
5.3 KiB
C#
165 lines
5.3 KiB
C#
using System;
|
|
using System.Linq;
|
|
using xModels.Dtos;
|
|
using System.Threading;
|
|
using xCommons.Extensions;
|
|
using xAiModels.Interfaces;
|
|
using xAiModels.Models.Dtos;
|
|
using System.Threading.Tasks;
|
|
using xDataService.Providers;
|
|
using xAiModels.Configurations;
|
|
using xAiModels.Interfaces.Dtos;
|
|
using xAiModels.Models.Entities;
|
|
using System.Collections.Generic;
|
|
using xIdentityService.Interfaces;
|
|
using xIdentityService.Extensions;
|
|
using xAiModels.Interfaces.Entities;
|
|
|
|
namespace xAiModels.Providers.Dtos
|
|
{
|
|
public class XAiMessageRepositoryService : XBaseRepositoryService<XAiMessage, XAiMessageDto, Guid>, IXAiMessageRepositoryService
|
|
{
|
|
private readonly IXIdentityProvider identityProvider;
|
|
private readonly IXAiConversationTitleResourceProvider conversationTitleResourceProvider;
|
|
|
|
public XAiMessageRepositoryService(
|
|
IXAiMessageRepository repository,
|
|
IXIdentityProvider identityProvider,
|
|
IXAiConversationTitleResourceProvider conversationTitleResourceProvider
|
|
) : base(
|
|
repository,
|
|
mapperProfiles:
|
|
[
|
|
new XAiModelMappersProfile()
|
|
]
|
|
)
|
|
{
|
|
this.identityProvider = identityProvider;
|
|
this.conversationTitleResourceProvider = conversationTitleResourceProvider;
|
|
}
|
|
|
|
//
|
|
#region Enrichers ...
|
|
/// <summary>
|
|
/// Enrich Specified Item ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<XAiMessageDto> Enrich(
|
|
XAiMessage source,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Mapping Default ...
|
|
var result = Mapper.Map<XAiMessageDto>(source);
|
|
result = await Enrich(
|
|
source: result,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <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 = Mapper.Map<XAiMessageDto>(source, options =>
|
|
{
|
|
//
|
|
options.Items[nameof(source.Owner)] = owner;
|
|
options.Items[nameof(source.ConversationTitle)] = conversationTitle;
|
|
options.Items[nameof(source.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
|
|
}
|
|
} |