Files
Wino-Mail/Wino.Core/Services/SynchronizerFactory.cs
T

135 lines
5.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
2024-11-10 23:28:25 +01:00
using Wino.Core.Domain.Entities.Shared;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Integration.Processors;
2024-11-10 23:28:25 +01:00
using Wino.Core.Synchronizers.Mail;
2025-02-16 11:54:23 +01:00
namespace Wino.Core.Services;
public class SynchronizerFactory : ISynchronizerFactory
{
2025-02-16 11:54:23 +01:00
private bool isInitialized = false;
private readonly IAccountService _accountService;
private readonly IImapSynchronizationStrategyProvider _imapSynchronizationStrategyProvider;
private readonly IApplicationConfiguration _applicationConfiguration;
private readonly IOutlookSynchronizerErrorHandlerFactory _outlookSynchronizerErrorHandlerFactory;
private readonly IGmailSynchronizerErrorHandlerFactory _gmailSynchronizerErrorHandlerFactory;
2025-02-16 11:54:23 +01:00
private readonly IOutlookChangeProcessor _outlookChangeProcessor;
private readonly IGmailChangeProcessor _gmailChangeProcessor;
private readonly IImapChangeProcessor _imapChangeProcessor;
private readonly IOutlookAuthenticator _outlookAuthenticator;
private readonly IGmailAuthenticator _gmailAuthenticator;
private readonly List<IWinoSynchronizerBase> synchronizerCache = new();
public SynchronizerFactory(IOutlookChangeProcessor outlookChangeProcessor,
IGmailChangeProcessor gmailChangeProcessor,
IImapChangeProcessor imapChangeProcessor,
IOutlookAuthenticator outlookAuthenticator,
IGmailAuthenticator gmailAuthenticator,
IAccountService accountService,
IImapSynchronizationStrategyProvider imapSynchronizationStrategyProvider,
IApplicationConfiguration applicationConfiguration,
IOutlookSynchronizerErrorHandlerFactory outlookSynchronizerErrorHandlerFactory,
IGmailSynchronizerErrorHandlerFactory gmailSynchronizerErrorHandlerFactory)
{
2025-02-16 11:54:23 +01:00
_outlookChangeProcessor = outlookChangeProcessor;
_gmailChangeProcessor = gmailChangeProcessor;
_imapChangeProcessor = imapChangeProcessor;
_outlookAuthenticator = outlookAuthenticator;
_gmailAuthenticator = gmailAuthenticator;
_accountService = accountService;
_imapSynchronizationStrategyProvider = imapSynchronizationStrategyProvider;
_applicationConfiguration = applicationConfiguration;
_outlookSynchronizerErrorHandlerFactory = outlookSynchronizerErrorHandlerFactory;
_gmailSynchronizerErrorHandlerFactory = gmailSynchronizerErrorHandlerFactory;
2025-02-16 11:54:23 +01:00
}
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
public async Task<IWinoSynchronizerBase> GetAccountSynchronizerAsync(Guid accountId)
{
var synchronizer = synchronizerCache.Find(a => a.Account.Id == accountId);
if (synchronizer == null)
{
2025-02-16 11:54:23 +01:00
var account = await _accountService.GetAccountAsync(accountId);
2025-02-16 11:54:23 +01:00
if (account != null)
{
2025-02-16 11:54:23 +01:00
synchronizer = CreateNewSynchronizer(account);
2025-02-16 11:54:23 +01:00
return await GetAccountSynchronizerAsync(accountId);
2025-02-16 11:43:30 +01:00
}
2025-02-16 11:35:43 +01:00
}
2025-02-16 11:54:23 +01:00
return synchronizer;
}
2025-02-16 11:54:23 +01:00
private IWinoSynchronizerBase CreateIntegratorWithDefaultProcessor(MailAccount mailAccount)
{
var providerType = mailAccount.ProviderType;
2025-02-16 11:54:23 +01:00
switch (providerType)
2025-02-16 11:43:30 +01:00
{
2025-02-16 11:54:23 +01:00
case Domain.Enums.MailProviderType.Outlook:
return new OutlookSynchronizer(mailAccount, _outlookAuthenticator, _outlookChangeProcessor, _outlookSynchronizerErrorHandlerFactory);
2025-02-16 11:54:23 +01:00
case Domain.Enums.MailProviderType.Gmail:
return new GmailSynchronizer(mailAccount, _gmailAuthenticator, _gmailChangeProcessor, _gmailSynchronizerErrorHandlerFactory);
2025-02-16 11:54:23 +01:00
case Domain.Enums.MailProviderType.IMAP4:
return new ImapSynchronizer(mailAccount, _imapChangeProcessor, _imapSynchronizationStrategyProvider, _applicationConfiguration);
default:
break;
}
2025-02-16 11:54:23 +01:00
return null;
}
2025-02-16 11:54:23 +01:00
public IWinoSynchronizerBase CreateNewSynchronizer(MailAccount account)
{
var synchronizer = CreateIntegratorWithDefaultProcessor(account);
2025-02-16 11:54:23 +01:00
if (synchronizer is IImapSynchronizer imapSynchronizer)
{
// Start the idle client for IMAP synchronizer.
_ = imapSynchronizer.StartIdleClientAsync();
// Pre-warm the client pool for IMAP synchronizer.
_ = imapSynchronizer.PreWarmClientPoolAsync();
2025-02-16 11:43:30 +01:00
}
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
synchronizerCache.Add(synchronizer);
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
return synchronizer;
}
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
public async Task InitializeAsync()
{
if (isInitialized) return;
2025-02-16 11:43:30 +01:00
2025-02-16 11:54:23 +01:00
var accounts = await _accountService.GetAccountsAsync();
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
foreach (var account in accounts)
2025-02-16 11:35:43 +01:00
{
2025-02-16 11:54:23 +01:00
CreateNewSynchronizer(account);
}
2025-02-16 11:43:30 +01:00
2025-02-16 11:54:23 +01:00
isInitialized = true;
}
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
public async Task DeleteSynchronizerAsync(Guid accountId)
{
var synchronizer = synchronizerCache.Find(a => a.Account.Id == accountId);
if (synchronizer != null)
{
// Stop the current synchronization.
await synchronizer.KillSynchronizerAsync();
synchronizerCache.Remove(synchronizer);
2025-02-15 12:53:32 +01:00
}
}
}