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 sources, IEnumerable destinations, CancellationToken cancellationToken = default ) { // // Make Tasks Ready ... var tasks = sources.Zip(destinations, (src, dst) => EnrichAsync(src, dst, cancellationToken) ); // await Task.WhenAll(tasks); } // #region Private ... /// /// Reading Owner User Info ... /// /// /// /// private async Task 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; } /// /// Reading Conversation ... /// /// /// /// private async Task 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 } }