File scoped namespaces
This commit is contained in:
@@ -5,32 +5,31 @@ using Wino.Core.Domain.Enums;
|
||||
using Wino.Core.Domain.Interfaces;
|
||||
using IAuthenticationProvider = Wino.Core.Domain.Interfaces.IAuthenticationProvider;
|
||||
|
||||
namespace Wino.Core.Services
|
||||
namespace Wino.Core.Services;
|
||||
|
||||
public class AuthenticationProvider : IAuthenticationProvider
|
||||
{
|
||||
public class AuthenticationProvider : IAuthenticationProvider
|
||||
private readonly INativeAppService _nativeAppService;
|
||||
private readonly IApplicationConfiguration _applicationConfiguration;
|
||||
private readonly IAuthenticatorConfig _authenticatorConfig;
|
||||
|
||||
public AuthenticationProvider(INativeAppService nativeAppService,
|
||||
IApplicationConfiguration applicationConfiguration,
|
||||
IAuthenticatorConfig authenticatorConfig)
|
||||
{
|
||||
private readonly INativeAppService _nativeAppService;
|
||||
private readonly IApplicationConfiguration _applicationConfiguration;
|
||||
private readonly IAuthenticatorConfig _authenticatorConfig;
|
||||
_nativeAppService = nativeAppService;
|
||||
_applicationConfiguration = applicationConfiguration;
|
||||
_authenticatorConfig = authenticatorConfig;
|
||||
}
|
||||
|
||||
public AuthenticationProvider(INativeAppService nativeAppService,
|
||||
IApplicationConfiguration applicationConfiguration,
|
||||
IAuthenticatorConfig authenticatorConfig)
|
||||
public IAuthenticator GetAuthenticator(MailProviderType providerType)
|
||||
{
|
||||
// TODO: Move DI
|
||||
return providerType switch
|
||||
{
|
||||
_nativeAppService = nativeAppService;
|
||||
_applicationConfiguration = applicationConfiguration;
|
||||
_authenticatorConfig = authenticatorConfig;
|
||||
}
|
||||
|
||||
public IAuthenticator GetAuthenticator(MailProviderType providerType)
|
||||
{
|
||||
// TODO: Move DI
|
||||
return providerType switch
|
||||
{
|
||||
MailProviderType.Outlook => new OutlookAuthenticator(_nativeAppService, _applicationConfiguration, _authenticatorConfig),
|
||||
MailProviderType.Gmail => new GmailAuthenticator(_authenticatorConfig),
|
||||
_ => throw new ArgumentException(Translator.Exception_UnsupportedProvider),
|
||||
};
|
||||
}
|
||||
MailProviderType.Outlook => new OutlookAuthenticator(_nativeAppService, _applicationConfiguration, _authenticatorConfig),
|
||||
MailProviderType.Gmail => new GmailAuthenticator(_authenticatorConfig),
|
||||
_ => throw new ArgumentException(Translator.Exception_UnsupportedProvider),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,51 +7,50 @@ using Wino.Core.Domain.Interfaces;
|
||||
using Wino.Core.Domain.Models;
|
||||
using Wino.Core.Domain.Models.AutoDiscovery;
|
||||
|
||||
namespace Wino.Core.Services
|
||||
namespace Wino.Core.Services;
|
||||
|
||||
/// <summary>
|
||||
/// We have 2 methods to do auto discovery.
|
||||
/// 1. Use https://emailsettings.firetrust.com/settings?q={address} API
|
||||
/// 2. TODO: Thunderbird auto discovery file.
|
||||
/// </summary>
|
||||
public class AutoDiscoveryService : IAutoDiscoveryService
|
||||
{
|
||||
/// <summary>
|
||||
/// We have 2 methods to do auto discovery.
|
||||
/// 1. Use https://emailsettings.firetrust.com/settings?q={address} API
|
||||
/// 2. TODO: Thunderbird auto discovery file.
|
||||
/// </summary>
|
||||
public class AutoDiscoveryService : IAutoDiscoveryService
|
||||
private const string FiretrustURL = " https://emailsettings.firetrust.com/settings?q=";
|
||||
|
||||
// TODO: Try Thunderbird Auto Discovery as second approach.
|
||||
|
||||
public Task<AutoDiscoverySettings> GetAutoDiscoverySettings(AutoDiscoveryMinimalSettings autoDiscoveryMinimalSettings)
|
||||
=> GetSettingsFromFiretrustAsync(autoDiscoveryMinimalSettings.Email);
|
||||
|
||||
private static async Task<AutoDiscoverySettings> GetSettingsFromFiretrustAsync(string mailAddress)
|
||||
{
|
||||
private const string FiretrustURL = " https://emailsettings.firetrust.com/settings?q=";
|
||||
using var client = new HttpClient();
|
||||
var response = await client.GetAsync($"{FiretrustURL}{mailAddress}");
|
||||
|
||||
// TODO: Try Thunderbird Auto Discovery as second approach.
|
||||
|
||||
public Task<AutoDiscoverySettings> GetAutoDiscoverySettings(AutoDiscoveryMinimalSettings autoDiscoveryMinimalSettings)
|
||||
=> GetSettingsFromFiretrustAsync(autoDiscoveryMinimalSettings.Email);
|
||||
|
||||
private static async Task<AutoDiscoverySettings> GetSettingsFromFiretrustAsync(string mailAddress)
|
||||
if (response.IsSuccessStatusCode)
|
||||
return await DeserializeFiretrustResponse(response);
|
||||
else
|
||||
{
|
||||
using var client = new HttpClient();
|
||||
var response = await client.GetAsync($"{FiretrustURL}{mailAddress}");
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
return await DeserializeFiretrustResponse(response);
|
||||
else
|
||||
{
|
||||
Log.Warning($"Firetrust AutoDiscovery failed. ({response.StatusCode})");
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<AutoDiscoverySettings> DeserializeFiretrustResponse(HttpResponseMessage response)
|
||||
{
|
||||
try
|
||||
{
|
||||
var content = await response.Content.ReadAsStringAsync();
|
||||
|
||||
return JsonSerializer.Deserialize(content, DomainModelsJsonContext.Default.AutoDiscoverySettings);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Failed to deserialize Firetrust response.");
|
||||
}
|
||||
Log.Warning($"Firetrust AutoDiscovery failed. ({response.StatusCode})");
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<AutoDiscoverySettings> DeserializeFiretrustResponse(HttpResponseMessage response)
|
||||
{
|
||||
try
|
||||
{
|
||||
var content = await response.Content.ReadAsStringAsync();
|
||||
|
||||
return JsonSerializer.Deserialize(content, DomainModelsJsonContext.Default.AutoDiscoverySettings);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Failed to deserialize Firetrust response.");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,54 +5,53 @@ using Wino.Core.Domain.Interfaces;
|
||||
using Wino.Core.Domain.Models.Connectivity;
|
||||
using Wino.Core.Integration;
|
||||
|
||||
namespace Wino.Core.Services
|
||||
namespace Wino.Core.Services;
|
||||
|
||||
public class ImapTestService : IImapTestService
|
||||
{
|
||||
public class ImapTestService : IImapTestService
|
||||
public const string ProtocolLogFileName = "ImapProtocolLog.log";
|
||||
|
||||
private readonly IPreferencesService _preferencesService;
|
||||
private readonly IApplicationConfiguration _appInitializerService;
|
||||
|
||||
private Stream _protocolLogStream;
|
||||
|
||||
public ImapTestService(IPreferencesService preferencesService, IApplicationConfiguration appInitializerService)
|
||||
{
|
||||
public const string ProtocolLogFileName = "ImapProtocolLog.log";
|
||||
_preferencesService = preferencesService;
|
||||
_appInitializerService = appInitializerService;
|
||||
}
|
||||
|
||||
private readonly IPreferencesService _preferencesService;
|
||||
private readonly IApplicationConfiguration _appInitializerService;
|
||||
private void EnsureProtocolLogFileExists()
|
||||
{
|
||||
// Create new file for protocol logger.
|
||||
var localAppFolderPath = _appInitializerService.ApplicationDataFolderPath;
|
||||
|
||||
private Stream _protocolLogStream;
|
||||
var logFile = Path.Combine(localAppFolderPath, ProtocolLogFileName);
|
||||
|
||||
public ImapTestService(IPreferencesService preferencesService, IApplicationConfiguration appInitializerService)
|
||||
if (File.Exists(logFile))
|
||||
File.Delete(logFile);
|
||||
|
||||
_protocolLogStream = File.Create(logFile);
|
||||
}
|
||||
|
||||
public async Task TestImapConnectionAsync(CustomServerInformation serverInformation, bool allowSSLHandShake)
|
||||
{
|
||||
EnsureProtocolLogFileExists();
|
||||
|
||||
var poolOptions = ImapClientPoolOptions.CreateTestPool(serverInformation, _protocolLogStream);
|
||||
|
||||
var clientPool = new ImapClientPool(poolOptions)
|
||||
{
|
||||
_preferencesService = preferencesService;
|
||||
_appInitializerService = appInitializerService;
|
||||
}
|
||||
ThrowOnSSLHandshakeCallback = !allowSSLHandShake
|
||||
};
|
||||
|
||||
private void EnsureProtocolLogFileExists()
|
||||
using (clientPool)
|
||||
{
|
||||
// Create new file for protocol logger.
|
||||
var localAppFolderPath = _appInitializerService.ApplicationDataFolderPath;
|
||||
// This call will make sure that everything is authenticated + connected successfully.
|
||||
var client = await clientPool.GetClientAsync();
|
||||
|
||||
var logFile = Path.Combine(localAppFolderPath, ProtocolLogFileName);
|
||||
|
||||
if (File.Exists(logFile))
|
||||
File.Delete(logFile);
|
||||
|
||||
_protocolLogStream = File.Create(logFile);
|
||||
}
|
||||
|
||||
public async Task TestImapConnectionAsync(CustomServerInformation serverInformation, bool allowSSLHandShake)
|
||||
{
|
||||
EnsureProtocolLogFileExists();
|
||||
|
||||
var poolOptions = ImapClientPoolOptions.CreateTestPool(serverInformation, _protocolLogStream);
|
||||
|
||||
var clientPool = new ImapClientPool(poolOptions)
|
||||
{
|
||||
ThrowOnSSLHandshakeCallback = !allowSSLHandShake
|
||||
};
|
||||
|
||||
using (clientPool)
|
||||
{
|
||||
// This call will make sure that everything is authenticated + connected successfully.
|
||||
var client = await clientPool.GetClientAsync();
|
||||
|
||||
clientPool.Release(client);
|
||||
}
|
||||
clientPool.Release(client);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,121 +6,120 @@ using Wino.Core.Domain.Interfaces;
|
||||
using Wino.Core.Integration.Processors;
|
||||
using Wino.Core.Synchronizers.Mail;
|
||||
|
||||
namespace Wino.Core.Services
|
||||
namespace Wino.Core.Services;
|
||||
|
||||
public class SynchronizerFactory : ISynchronizerFactory
|
||||
{
|
||||
public class SynchronizerFactory : ISynchronizerFactory
|
||||
private bool isInitialized = false;
|
||||
|
||||
private readonly IAccountService _accountService;
|
||||
private readonly IImapSynchronizationStrategyProvider _imapSynchronizationStrategyProvider;
|
||||
private readonly IApplicationConfiguration _applicationConfiguration;
|
||||
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)
|
||||
{
|
||||
private bool isInitialized = false;
|
||||
_outlookChangeProcessor = outlookChangeProcessor;
|
||||
_gmailChangeProcessor = gmailChangeProcessor;
|
||||
_imapChangeProcessor = imapChangeProcessor;
|
||||
_outlookAuthenticator = outlookAuthenticator;
|
||||
_gmailAuthenticator = gmailAuthenticator;
|
||||
_accountService = accountService;
|
||||
_imapSynchronizationStrategyProvider = imapSynchronizationStrategyProvider;
|
||||
_applicationConfiguration = applicationConfiguration;
|
||||
}
|
||||
|
||||
private readonly IAccountService _accountService;
|
||||
private readonly IImapSynchronizationStrategyProvider _imapSynchronizationStrategyProvider;
|
||||
private readonly IApplicationConfiguration _applicationConfiguration;
|
||||
private readonly IOutlookChangeProcessor _outlookChangeProcessor;
|
||||
private readonly IGmailChangeProcessor _gmailChangeProcessor;
|
||||
private readonly IImapChangeProcessor _imapChangeProcessor;
|
||||
private readonly IOutlookAuthenticator _outlookAuthenticator;
|
||||
private readonly IGmailAuthenticator _gmailAuthenticator;
|
||||
public async Task<IWinoSynchronizerBase> GetAccountSynchronizerAsync(Guid accountId)
|
||||
{
|
||||
var synchronizer = synchronizerCache.Find(a => a.Account.Id == accountId);
|
||||
|
||||
private readonly List<IWinoSynchronizerBase> synchronizerCache = new();
|
||||
|
||||
public SynchronizerFactory(IOutlookChangeProcessor outlookChangeProcessor,
|
||||
IGmailChangeProcessor gmailChangeProcessor,
|
||||
IImapChangeProcessor imapChangeProcessor,
|
||||
IOutlookAuthenticator outlookAuthenticator,
|
||||
IGmailAuthenticator gmailAuthenticator,
|
||||
IAccountService accountService,
|
||||
IImapSynchronizationStrategyProvider imapSynchronizationStrategyProvider,
|
||||
IApplicationConfiguration applicationConfiguration)
|
||||
if (synchronizer == null)
|
||||
{
|
||||
_outlookChangeProcessor = outlookChangeProcessor;
|
||||
_gmailChangeProcessor = gmailChangeProcessor;
|
||||
_imapChangeProcessor = imapChangeProcessor;
|
||||
_outlookAuthenticator = outlookAuthenticator;
|
||||
_gmailAuthenticator = gmailAuthenticator;
|
||||
_accountService = accountService;
|
||||
_imapSynchronizationStrategyProvider = imapSynchronizationStrategyProvider;
|
||||
_applicationConfiguration = applicationConfiguration;
|
||||
var account = await _accountService.GetAccountAsync(accountId);
|
||||
|
||||
if (account != null)
|
||||
{
|
||||
synchronizer = CreateNewSynchronizer(account);
|
||||
|
||||
|
||||
return await GetAccountSynchronizerAsync(accountId);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IWinoSynchronizerBase> GetAccountSynchronizerAsync(Guid accountId)
|
||||
return synchronizer;
|
||||
}
|
||||
|
||||
private IWinoSynchronizerBase CreateIntegratorWithDefaultProcessor(MailAccount mailAccount)
|
||||
{
|
||||
var providerType = mailAccount.ProviderType;
|
||||
|
||||
switch (providerType)
|
||||
{
|
||||
var synchronizer = synchronizerCache.Find(a => a.Account.Id == accountId);
|
||||
|
||||
if (synchronizer == null)
|
||||
{
|
||||
var account = await _accountService.GetAccountAsync(accountId);
|
||||
|
||||
if (account != null)
|
||||
{
|
||||
synchronizer = CreateNewSynchronizer(account);
|
||||
|
||||
|
||||
return await GetAccountSynchronizerAsync(accountId);
|
||||
}
|
||||
}
|
||||
|
||||
return synchronizer;
|
||||
case Domain.Enums.MailProviderType.Outlook:
|
||||
return new OutlookSynchronizer(mailAccount, _outlookAuthenticator, _outlookChangeProcessor);
|
||||
case Domain.Enums.MailProviderType.Gmail:
|
||||
return new GmailSynchronizer(mailAccount, _gmailAuthenticator, _gmailChangeProcessor);
|
||||
case Domain.Enums.MailProviderType.IMAP4:
|
||||
return new ImapSynchronizer(mailAccount, _imapChangeProcessor, _imapSynchronizationStrategyProvider, _applicationConfiguration);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
private IWinoSynchronizerBase CreateIntegratorWithDefaultProcessor(MailAccount mailAccount)
|
||||
return null;
|
||||
}
|
||||
|
||||
public IWinoSynchronizerBase CreateNewSynchronizer(MailAccount account)
|
||||
{
|
||||
var synchronizer = CreateIntegratorWithDefaultProcessor(account);
|
||||
|
||||
if (synchronizer is IImapSynchronizer imapSynchronizer)
|
||||
{
|
||||
var providerType = mailAccount.ProviderType;
|
||||
|
||||
switch (providerType)
|
||||
{
|
||||
case Domain.Enums.MailProviderType.Outlook:
|
||||
return new OutlookSynchronizer(mailAccount, _outlookAuthenticator, _outlookChangeProcessor);
|
||||
case Domain.Enums.MailProviderType.Gmail:
|
||||
return new GmailSynchronizer(mailAccount, _gmailAuthenticator, _gmailChangeProcessor);
|
||||
case Domain.Enums.MailProviderType.IMAP4:
|
||||
return new ImapSynchronizer(mailAccount, _imapChangeProcessor, _imapSynchronizationStrategyProvider, _applicationConfiguration);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return null;
|
||||
// Start the idle client for IMAP synchronizer.
|
||||
_ = imapSynchronizer.StartIdleClientAsync();
|
||||
}
|
||||
|
||||
public IWinoSynchronizerBase CreateNewSynchronizer(MailAccount account)
|
||||
synchronizerCache.Add(synchronizer);
|
||||
|
||||
return synchronizer;
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
if (isInitialized) return;
|
||||
|
||||
var accounts = await _accountService.GetAccountsAsync();
|
||||
|
||||
foreach (var account in accounts)
|
||||
{
|
||||
var synchronizer = CreateIntegratorWithDefaultProcessor(account);
|
||||
|
||||
if (synchronizer is IImapSynchronizer imapSynchronizer)
|
||||
{
|
||||
// Start the idle client for IMAP synchronizer.
|
||||
_ = imapSynchronizer.StartIdleClientAsync();
|
||||
}
|
||||
|
||||
synchronizerCache.Add(synchronizer);
|
||||
|
||||
return synchronizer;
|
||||
CreateNewSynchronizer(account);
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
isInitialized = true;
|
||||
}
|
||||
|
||||
public async Task DeleteSynchronizerAsync(Guid accountId)
|
||||
{
|
||||
var synchronizer = synchronizerCache.Find(a => a.Account.Id == accountId);
|
||||
|
||||
if (synchronizer != null)
|
||||
{
|
||||
if (isInitialized) return;
|
||||
// Stop the current synchronization.
|
||||
await synchronizer.KillSynchronizerAsync();
|
||||
|
||||
var accounts = await _accountService.GetAccountsAsync();
|
||||
|
||||
foreach (var account in accounts)
|
||||
{
|
||||
CreateNewSynchronizer(account);
|
||||
}
|
||||
|
||||
isInitialized = true;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
synchronizerCache.Remove(synchronizer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,31 +2,30 @@
|
||||
using Wino.Core.Domain.Interfaces;
|
||||
using Wino.Services.Threading;
|
||||
|
||||
namespace Wino.Core.Services
|
||||
namespace Wino.Core.Services;
|
||||
|
||||
public class ThreadingStrategyProvider : IThreadingStrategyProvider
|
||||
{
|
||||
public class ThreadingStrategyProvider : IThreadingStrategyProvider
|
||||
private readonly OutlookThreadingStrategy _outlookThreadingStrategy;
|
||||
private readonly GmailThreadingStrategy _gmailThreadingStrategy;
|
||||
private readonly ImapThreadingStrategy _imapThreadStrategy;
|
||||
|
||||
public ThreadingStrategyProvider(OutlookThreadingStrategy outlookThreadingStrategy,
|
||||
GmailThreadingStrategy gmailThreadingStrategy,
|
||||
ImapThreadingStrategy imapThreadStrategy)
|
||||
{
|
||||
private readonly OutlookThreadingStrategy _outlookThreadingStrategy;
|
||||
private readonly GmailThreadingStrategy _gmailThreadingStrategy;
|
||||
private readonly ImapThreadingStrategy _imapThreadStrategy;
|
||||
_outlookThreadingStrategy = outlookThreadingStrategy;
|
||||
_gmailThreadingStrategy = gmailThreadingStrategy;
|
||||
_imapThreadStrategy = imapThreadStrategy;
|
||||
}
|
||||
|
||||
public ThreadingStrategyProvider(OutlookThreadingStrategy outlookThreadingStrategy,
|
||||
GmailThreadingStrategy gmailThreadingStrategy,
|
||||
ImapThreadingStrategy imapThreadStrategy)
|
||||
public IThreadingStrategy GetStrategy(MailProviderType mailProviderType)
|
||||
{
|
||||
return mailProviderType switch
|
||||
{
|
||||
_outlookThreadingStrategy = outlookThreadingStrategy;
|
||||
_gmailThreadingStrategy = gmailThreadingStrategy;
|
||||
_imapThreadStrategy = imapThreadStrategy;
|
||||
}
|
||||
|
||||
public IThreadingStrategy GetStrategy(MailProviderType mailProviderType)
|
||||
{
|
||||
return mailProviderType switch
|
||||
{
|
||||
MailProviderType.Outlook => _outlookThreadingStrategy,
|
||||
MailProviderType.Gmail => _gmailThreadingStrategy,
|
||||
_ => _imapThreadStrategy,
|
||||
};
|
||||
}
|
||||
MailProviderType.Outlook => _outlookThreadingStrategy,
|
||||
MailProviderType.Gmail => _gmailThreadingStrategy,
|
||||
_ => _imapThreadStrategy,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,31 +6,30 @@ using Serilog;
|
||||
using Wino.Core.Domain.Interfaces;
|
||||
using Wino.Core.Domain.Models.Reader;
|
||||
|
||||
namespace Wino.Core.Services
|
||||
namespace Wino.Core.Services;
|
||||
|
||||
public class UnsubscriptionService : IUnsubscriptionService
|
||||
{
|
||||
public class UnsubscriptionService : IUnsubscriptionService
|
||||
public async Task<bool> OneClickUnsubscribeAsync(UnsubscribeInfo info)
|
||||
{
|
||||
public async Task<bool> OneClickUnsubscribeAsync(UnsubscribeInfo info)
|
||||
try
|
||||
{
|
||||
try
|
||||
using var httpClient = new HttpClient();
|
||||
|
||||
var unsubscribeRequest = new HttpRequestMessage(HttpMethod.Post, info.HttpLink)
|
||||
{
|
||||
using var httpClient = new HttpClient();
|
||||
Content = new StringContent("List-Unsubscribe=One-Click", Encoding.UTF8, "application/x-www-form-urlencoded")
|
||||
};
|
||||
|
||||
var unsubscribeRequest = new HttpRequestMessage(HttpMethod.Post, info.HttpLink)
|
||||
{
|
||||
Content = new StringContent("List-Unsubscribe=One-Click", Encoding.UTF8, "application/x-www-form-urlencoded")
|
||||
};
|
||||
var result = await httpClient.SendAsync(unsubscribeRequest).ConfigureAwait(false);
|
||||
|
||||
var result = await httpClient.SendAsync(unsubscribeRequest).ConfigureAwait(false);
|
||||
|
||||
return result.IsSuccessStatusCode;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error("Failed to unsubscribe from {HttpLink} - {Message}", info.HttpLink, ex.Message);
|
||||
}
|
||||
|
||||
return false;
|
||||
return result.IsSuccessStatusCode;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error("Failed to unsubscribe from {HttpLink} - {Message}", info.HttpLink, ex.Message);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,149 +14,148 @@ using Wino.Core.Domain.Models.Synchronization;
|
||||
using Wino.Core.Requests.Mail;
|
||||
using Wino.Messaging.Server;
|
||||
|
||||
namespace Wino.Core.Services
|
||||
namespace Wino.Core.Services;
|
||||
|
||||
public class WinoRequestDelegator : IWinoRequestDelegator
|
||||
{
|
||||
public class WinoRequestDelegator : IWinoRequestDelegator
|
||||
private readonly IWinoRequestProcessor _winoRequestProcessor;
|
||||
private readonly IWinoServerConnectionManager _winoServerConnectionManager;
|
||||
private readonly IFolderService _folderService;
|
||||
private readonly IMailDialogService _dialogService;
|
||||
|
||||
public WinoRequestDelegator(IWinoRequestProcessor winoRequestProcessor,
|
||||
IWinoServerConnectionManager winoServerConnectionManager,
|
||||
IFolderService folderService,
|
||||
IMailDialogService dialogService)
|
||||
{
|
||||
private readonly IWinoRequestProcessor _winoRequestProcessor;
|
||||
private readonly IWinoServerConnectionManager _winoServerConnectionManager;
|
||||
private readonly IFolderService _folderService;
|
||||
private readonly IMailDialogService _dialogService;
|
||||
_winoRequestProcessor = winoRequestProcessor;
|
||||
_winoServerConnectionManager = winoServerConnectionManager;
|
||||
_folderService = folderService;
|
||||
_dialogService = dialogService;
|
||||
}
|
||||
|
||||
public WinoRequestDelegator(IWinoRequestProcessor winoRequestProcessor,
|
||||
IWinoServerConnectionManager winoServerConnectionManager,
|
||||
IFolderService folderService,
|
||||
IMailDialogService dialogService)
|
||||
public async Task ExecuteAsync(MailOperationPreperationRequest request)
|
||||
{
|
||||
var requests = new List<IMailActionRequest>();
|
||||
|
||||
try
|
||||
{
|
||||
_winoRequestProcessor = winoRequestProcessor;
|
||||
_winoServerConnectionManager = winoServerConnectionManager;
|
||||
_folderService = folderService;
|
||||
_dialogService = dialogService;
|
||||
requests = await _winoRequestProcessor.PrepareRequestsAsync(request);
|
||||
}
|
||||
catch (UnavailableSpecialFolderException unavailableSpecialFolderException)
|
||||
{
|
||||
_dialogService.InfoBarMessage(Translator.Info_MissingFolderTitle,
|
||||
string.Format(Translator.Info_MissingFolderMessage, unavailableSpecialFolderException.SpecialFolderType),
|
||||
InfoBarMessageType.Warning,
|
||||
Translator.SettingConfigureSpecialFolders_Button,
|
||||
() =>
|
||||
{
|
||||
_dialogService.HandleSystemFolderConfigurationDialogAsync(unavailableSpecialFolderException.AccountId, _folderService);
|
||||
});
|
||||
}
|
||||
catch (InvalidMoveTargetException)
|
||||
{
|
||||
_dialogService.InfoBarMessage(Translator.Info_InvalidMoveTargetTitle, Translator.Info_InvalidMoveTargetMessage, InfoBarMessageType.Warning);
|
||||
}
|
||||
catch (NotImplementedException)
|
||||
{
|
||||
_dialogService.ShowNotSupportedMessage();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Request creation failed.");
|
||||
_dialogService.InfoBarMessage(Translator.Info_RequestCreationFailedTitle, ex.Message, InfoBarMessageType.Error);
|
||||
}
|
||||
|
||||
public async Task ExecuteAsync(MailOperationPreperationRequest request)
|
||||
if (requests == null || !requests.Any()) return;
|
||||
|
||||
var accountIds = requests.GroupBy(a => a.Item.AssignedAccount.Id);
|
||||
|
||||
// Queue requests for each account and start synchronization.
|
||||
foreach (var accountId in accountIds)
|
||||
{
|
||||
var requests = new List<IMailActionRequest>();
|
||||
|
||||
try
|
||||
foreach (var accountRequest in accountId)
|
||||
{
|
||||
requests = await _winoRequestProcessor.PrepareRequestsAsync(request);
|
||||
}
|
||||
catch (UnavailableSpecialFolderException unavailableSpecialFolderException)
|
||||
{
|
||||
_dialogService.InfoBarMessage(Translator.Info_MissingFolderTitle,
|
||||
string.Format(Translator.Info_MissingFolderMessage, unavailableSpecialFolderException.SpecialFolderType),
|
||||
InfoBarMessageType.Warning,
|
||||
Translator.SettingConfigureSpecialFolders_Button,
|
||||
() =>
|
||||
{
|
||||
_dialogService.HandleSystemFolderConfigurationDialogAsync(unavailableSpecialFolderException.AccountId, _folderService);
|
||||
});
|
||||
}
|
||||
catch (InvalidMoveTargetException)
|
||||
{
|
||||
_dialogService.InfoBarMessage(Translator.Info_InvalidMoveTargetTitle, Translator.Info_InvalidMoveTargetMessage, InfoBarMessageType.Warning);
|
||||
}
|
||||
catch (NotImplementedException)
|
||||
{
|
||||
_dialogService.ShowNotSupportedMessage();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Request creation failed.");
|
||||
_dialogService.InfoBarMessage(Translator.Info_RequestCreationFailedTitle, ex.Message, InfoBarMessageType.Error);
|
||||
await QueueRequestAsync(accountRequest, accountId.Key);
|
||||
}
|
||||
|
||||
if (requests == null || !requests.Any()) return;
|
||||
|
||||
var accountIds = requests.GroupBy(a => a.Item.AssignedAccount.Id);
|
||||
|
||||
// Queue requests for each account and start synchronization.
|
||||
foreach (var accountId in accountIds)
|
||||
{
|
||||
foreach (var accountRequest in accountId)
|
||||
{
|
||||
await QueueRequestAsync(accountRequest, accountId.Key);
|
||||
}
|
||||
|
||||
await QueueSynchronizationAsync(accountId.Key);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ExecuteAsync(FolderOperationPreperationRequest folderRequest)
|
||||
{
|
||||
if (folderRequest == null || folderRequest.Folder == null) return;
|
||||
|
||||
IRequestBase request = null;
|
||||
|
||||
var accountId = folderRequest.Folder.MailAccountId;
|
||||
|
||||
try
|
||||
{
|
||||
request = await _winoRequestProcessor.PrepareFolderRequestAsync(folderRequest);
|
||||
}
|
||||
catch (NotImplementedException)
|
||||
{
|
||||
_dialogService.ShowNotSupportedMessage();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Folder operation execution failed.");
|
||||
}
|
||||
|
||||
if (request == null) return;
|
||||
|
||||
await QueueRequestAsync(request, accountId);
|
||||
await QueueSynchronizationAsync(accountId);
|
||||
}
|
||||
|
||||
public async Task ExecuteAsync(DraftPreparationRequest draftPreperationRequest)
|
||||
{
|
||||
var request = new CreateDraftRequest(draftPreperationRequest);
|
||||
|
||||
await QueueRequestAsync(request, draftPreperationRequest.Account.Id);
|
||||
await QueueSynchronizationAsync(draftPreperationRequest.Account.Id);
|
||||
}
|
||||
|
||||
public async Task ExecuteAsync(SendDraftPreparationRequest sendDraftPreperationRequest)
|
||||
{
|
||||
var request = new SendDraftRequest(sendDraftPreperationRequest);
|
||||
|
||||
await QueueRequestAsync(request, sendDraftPreperationRequest.MailItem.AssignedAccount.Id);
|
||||
await QueueSynchronizationAsync(sendDraftPreperationRequest.MailItem.AssignedAccount.Id);
|
||||
}
|
||||
|
||||
private async Task QueueRequestAsync(IRequestBase request, Guid accountId)
|
||||
{
|
||||
try
|
||||
{
|
||||
await EnsureServerConnectedAsync();
|
||||
await _winoServerConnectionManager.QueueRequestAsync(request, accountId);
|
||||
}
|
||||
catch (WinoServerException serverException)
|
||||
{
|
||||
_dialogService.InfoBarMessage("Wino Server Exception", serverException.Message, InfoBarMessageType.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task QueueSynchronizationAsync(Guid accountId)
|
||||
{
|
||||
await EnsureServerConnectedAsync();
|
||||
|
||||
var options = new MailSynchronizationOptions()
|
||||
{
|
||||
AccountId = accountId,
|
||||
Type = MailSynchronizationType.ExecuteRequests
|
||||
};
|
||||
|
||||
WeakReferenceMessenger.Default.Send(new NewMailSynchronizationRequested(options, SynchronizationSource.Client));
|
||||
}
|
||||
|
||||
private async Task EnsureServerConnectedAsync()
|
||||
{
|
||||
if (_winoServerConnectionManager.Status == WinoServerConnectionStatus.Connected) return;
|
||||
|
||||
await _winoServerConnectionManager.ConnectAsync();
|
||||
await QueueSynchronizationAsync(accountId.Key);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ExecuteAsync(FolderOperationPreperationRequest folderRequest)
|
||||
{
|
||||
if (folderRequest == null || folderRequest.Folder == null) return;
|
||||
|
||||
IRequestBase request = null;
|
||||
|
||||
var accountId = folderRequest.Folder.MailAccountId;
|
||||
|
||||
try
|
||||
{
|
||||
request = await _winoRequestProcessor.PrepareFolderRequestAsync(folderRequest);
|
||||
}
|
||||
catch (NotImplementedException)
|
||||
{
|
||||
_dialogService.ShowNotSupportedMessage();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Folder operation execution failed.");
|
||||
}
|
||||
|
||||
if (request == null) return;
|
||||
|
||||
await QueueRequestAsync(request, accountId);
|
||||
await QueueSynchronizationAsync(accountId);
|
||||
}
|
||||
|
||||
public async Task ExecuteAsync(DraftPreparationRequest draftPreperationRequest)
|
||||
{
|
||||
var request = new CreateDraftRequest(draftPreperationRequest);
|
||||
|
||||
await QueueRequestAsync(request, draftPreperationRequest.Account.Id);
|
||||
await QueueSynchronizationAsync(draftPreperationRequest.Account.Id);
|
||||
}
|
||||
|
||||
public async Task ExecuteAsync(SendDraftPreparationRequest sendDraftPreperationRequest)
|
||||
{
|
||||
var request = new SendDraftRequest(sendDraftPreperationRequest);
|
||||
|
||||
await QueueRequestAsync(request, sendDraftPreperationRequest.MailItem.AssignedAccount.Id);
|
||||
await QueueSynchronizationAsync(sendDraftPreperationRequest.MailItem.AssignedAccount.Id);
|
||||
}
|
||||
|
||||
private async Task QueueRequestAsync(IRequestBase request, Guid accountId)
|
||||
{
|
||||
try
|
||||
{
|
||||
await EnsureServerConnectedAsync();
|
||||
await _winoServerConnectionManager.QueueRequestAsync(request, accountId);
|
||||
}
|
||||
catch (WinoServerException serverException)
|
||||
{
|
||||
_dialogService.InfoBarMessage("Wino Server Exception", serverException.Message, InfoBarMessageType.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task QueueSynchronizationAsync(Guid accountId)
|
||||
{
|
||||
await EnsureServerConnectedAsync();
|
||||
|
||||
var options = new MailSynchronizationOptions()
|
||||
{
|
||||
AccountId = accountId,
|
||||
Type = MailSynchronizationType.ExecuteRequests
|
||||
};
|
||||
|
||||
WeakReferenceMessenger.Default.Send(new NewMailSynchronizationRequested(options, SynchronizationSource.Client));
|
||||
}
|
||||
|
||||
private async Task EnsureServerConnectedAsync()
|
||||
{
|
||||
if (_winoServerConnectionManager.Status == WinoServerConnectionStatus.Connected) return;
|
||||
|
||||
await _winoServerConnectionManager.ConnectAsync();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,256 +12,255 @@ using Wino.Core.Domain.Models.MailItem;
|
||||
using Wino.Core.Requests.Folder;
|
||||
using Wino.Core.Requests.Mail;
|
||||
|
||||
namespace Wino.Core.Services
|
||||
namespace Wino.Core.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Intermediary processor for converting a user action to executable Wino requests.
|
||||
/// Primarily responsible for batching requests by AccountId and FolderId.
|
||||
/// </summary>
|
||||
public class WinoRequestProcessor : IWinoRequestProcessor
|
||||
{
|
||||
private readonly IFolderService _folderService;
|
||||
private readonly IKeyPressService _keyPressService;
|
||||
private readonly IPreferencesService _preferencesService;
|
||||
private readonly IMailDialogService _dialogService;
|
||||
private readonly IMailService _mailService;
|
||||
|
||||
/// <summary>
|
||||
/// Intermediary processor for converting a user action to executable Wino requests.
|
||||
/// Primarily responsible for batching requests by AccountId and FolderId.
|
||||
/// Set of rules that defines which action should be executed if user wants to toggle an action.
|
||||
/// </summary>
|
||||
public class WinoRequestProcessor : IWinoRequestProcessor
|
||||
private readonly List<ToggleRequestRule> _toggleRequestRules =
|
||||
[
|
||||
new ToggleRequestRule(MailOperation.MarkAsRead, MailOperation.MarkAsUnread, new System.Func<IMailItem, bool>((item) => item.IsRead)),
|
||||
new ToggleRequestRule(MailOperation.MarkAsUnread, MailOperation.MarkAsRead, new System.Func<IMailItem, bool>((item) => !item.IsRead)),
|
||||
new ToggleRequestRule(MailOperation.SetFlag, MailOperation.ClearFlag, new System.Func<IMailItem, bool>((item) => item.IsFlagged)),
|
||||
new ToggleRequestRule(MailOperation.ClearFlag, MailOperation.SetFlag, new System.Func<IMailItem, bool>((item) => !item.IsFlagged)),
|
||||
];
|
||||
|
||||
public WinoRequestProcessor(IFolderService folderService,
|
||||
IKeyPressService keyPressService,
|
||||
IPreferencesService preferencesService,
|
||||
IMailDialogService dialogService,
|
||||
IMailService mailService)
|
||||
{
|
||||
private readonly IFolderService _folderService;
|
||||
private readonly IKeyPressService _keyPressService;
|
||||
private readonly IPreferencesService _preferencesService;
|
||||
private readonly IMailDialogService _dialogService;
|
||||
private readonly IMailService _mailService;
|
||||
_folderService = folderService;
|
||||
_keyPressService = keyPressService;
|
||||
_preferencesService = preferencesService;
|
||||
_dialogService = dialogService;
|
||||
_mailService = mailService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set of rules that defines which action should be executed if user wants to toggle an action.
|
||||
/// </summary>
|
||||
private readonly List<ToggleRequestRule> _toggleRequestRules =
|
||||
[
|
||||
new ToggleRequestRule(MailOperation.MarkAsRead, MailOperation.MarkAsUnread, new System.Func<IMailItem, bool>((item) => item.IsRead)),
|
||||
new ToggleRequestRule(MailOperation.MarkAsUnread, MailOperation.MarkAsRead, new System.Func<IMailItem, bool>((item) => !item.IsRead)),
|
||||
new ToggleRequestRule(MailOperation.SetFlag, MailOperation.ClearFlag, new System.Func<IMailItem, bool>((item) => item.IsFlagged)),
|
||||
new ToggleRequestRule(MailOperation.ClearFlag, MailOperation.SetFlag, new System.Func<IMailItem, bool>((item) => !item.IsFlagged)),
|
||||
];
|
||||
public async Task<List<IMailActionRequest>> PrepareRequestsAsync(MailOperationPreperationRequest preperationRequest)
|
||||
{
|
||||
var action = preperationRequest.Action;
|
||||
var moveTargetStructure = preperationRequest.MoveTargetFolder;
|
||||
|
||||
public WinoRequestProcessor(IFolderService folderService,
|
||||
IKeyPressService keyPressService,
|
||||
IPreferencesService preferencesService,
|
||||
IMailDialogService dialogService,
|
||||
IMailService mailService)
|
||||
// Ask confirmation for permanent delete operation.
|
||||
// Drafts are always hard deleted without any protection.
|
||||
|
||||
if (!preperationRequest.IgnoreHardDeleteProtection && ((action == MailOperation.SoftDelete && _keyPressService.IsShiftKeyPressed()) || action == MailOperation.HardDelete))
|
||||
{
|
||||
_folderService = folderService;
|
||||
_keyPressService = keyPressService;
|
||||
_preferencesService = preferencesService;
|
||||
_dialogService = dialogService;
|
||||
_mailService = mailService;
|
||||
if (_preferencesService.IsHardDeleteProtectionEnabled)
|
||||
{
|
||||
var shouldDelete = await _dialogService.ShowHardDeleteConfirmationAsync();
|
||||
|
||||
if (!shouldDelete) return default;
|
||||
}
|
||||
|
||||
action = MailOperation.HardDelete;
|
||||
}
|
||||
|
||||
public async Task<List<IMailActionRequest>> PrepareRequestsAsync(MailOperationPreperationRequest preperationRequest)
|
||||
// Make sure there is a move target folder if action is move.
|
||||
// Let user pick a folder to move from the dialog.
|
||||
|
||||
if (action == MailOperation.Move && moveTargetStructure == null)
|
||||
{
|
||||
var action = preperationRequest.Action;
|
||||
var moveTargetStructure = preperationRequest.MoveTargetFolder;
|
||||
// TODO: Handle multiple accounts for move operation.
|
||||
// What happens if we move 2 different mails from 2 different accounts?
|
||||
|
||||
// Ask confirmation for permanent delete operation.
|
||||
// Drafts are always hard deleted without any protection.
|
||||
var accountId = preperationRequest.MailItems.FirstOrDefault().AssignedAccount.Id;
|
||||
|
||||
if (!preperationRequest.IgnoreHardDeleteProtection && ((action == MailOperation.SoftDelete && _keyPressService.IsShiftKeyPressed()) || action == MailOperation.HardDelete))
|
||||
moveTargetStructure = await _dialogService.PickFolderAsync(accountId, PickFolderReason.Move, _folderService);
|
||||
|
||||
if (moveTargetStructure == null)
|
||||
return default;
|
||||
}
|
||||
|
||||
var requests = new List<IMailActionRequest>();
|
||||
|
||||
// TODO: Fix: Collection was modified; enumeration operation may not execute
|
||||
foreach (var item in preperationRequest.MailItems)
|
||||
{
|
||||
var singleRequest = await GetSingleRequestAsync(item, action, moveTargetStructure, preperationRequest.ToggleExecution);
|
||||
|
||||
if (singleRequest == null) continue;
|
||||
|
||||
requests.Add(singleRequest);
|
||||
}
|
||||
|
||||
return requests;
|
||||
}
|
||||
|
||||
private async Task<IMailActionRequest> GetSingleRequestAsync(MailCopy mailItem, MailOperation action, IMailItemFolder moveTargetStructure, bool shouldToggleActions)
|
||||
{
|
||||
if (mailItem.AssignedAccount == null) throw new ArgumentException(Translator.Exception_NullAssignedAccount);
|
||||
if (mailItem.AssignedFolder == null) throw new ArgumentException(Translator.Exception_NullAssignedFolder);
|
||||
|
||||
// Rule: Soft deletes from Trash folder must perform Hard Delete.
|
||||
if (action == MailOperation.SoftDelete && mailItem.AssignedFolder.SpecialFolderType == SpecialFolderType.Deleted)
|
||||
action = MailOperation.HardDelete;
|
||||
|
||||
// Rule: SoftDelete draft items must be performed as hard delete.
|
||||
if (action == MailOperation.SoftDelete && mailItem.IsDraft)
|
||||
action = MailOperation.HardDelete;
|
||||
|
||||
// Rule: Soft/Hard deletes on local drafts are always discard local draft.
|
||||
if ((action == MailOperation.SoftDelete || action == MailOperation.HardDelete) && mailItem.IsLocalDraft)
|
||||
action = MailOperation.DiscardLocalDraft;
|
||||
|
||||
// Rule: Toggle actions must be reverted if ToggleExecution is passed true.
|
||||
if (shouldToggleActions)
|
||||
{
|
||||
var toggleRule = _toggleRequestRules.Find(a => a.SourceAction == action);
|
||||
|
||||
if (toggleRule != null && toggleRule.Condition(mailItem))
|
||||
{
|
||||
if (_preferencesService.IsHardDeleteProtectionEnabled)
|
||||
{
|
||||
var shouldDelete = await _dialogService.ShowHardDeleteConfirmationAsync();
|
||||
action = toggleRule.TargetAction;
|
||||
}
|
||||
}
|
||||
|
||||
if (!shouldDelete) return default;
|
||||
if (action == MailOperation.MarkAsRead)
|
||||
return new MarkReadRequest(mailItem, true);
|
||||
else if (action == MailOperation.MarkAsUnread)
|
||||
return new MarkReadRequest(mailItem, false);
|
||||
else if (action == MailOperation.SetFlag)
|
||||
return new ChangeFlagRequest(mailItem, true);
|
||||
else if (action == MailOperation.ClearFlag)
|
||||
return new ChangeFlagRequest(mailItem, false);
|
||||
else if (action == MailOperation.HardDelete)
|
||||
return new DeleteRequest(mailItem);
|
||||
else if (action == MailOperation.Move)
|
||||
{
|
||||
if (moveTargetStructure == null)
|
||||
throw new InvalidMoveTargetException();
|
||||
|
||||
// TODO
|
||||
// Rule: You can't move items to non-move target folders;
|
||||
// Rule: You can't move items from a folder to itself.
|
||||
|
||||
//if (!moveTargetStructure.IsMoveTarget || moveTargetStructure.FolderId == mailItem.AssignedFolder.Id)
|
||||
// throw new InvalidMoveTargetException();
|
||||
|
||||
var pickedFolderItem = await _folderService.GetFolderAsync(moveTargetStructure.Id);
|
||||
|
||||
return new MoveRequest(mailItem, mailItem.AssignedFolder, pickedFolderItem);
|
||||
}
|
||||
else if (action == MailOperation.Archive)
|
||||
{
|
||||
// For IMAP and Outlook: Validate archive folder exists.
|
||||
// Gmail doesn't need archive folder existence.
|
||||
|
||||
MailItemFolder archiveFolder = null;
|
||||
|
||||
bool shouldRequireArchiveFolder = mailItem.AssignedAccount.ProviderType == MailProviderType.Outlook
|
||||
|| mailItem.AssignedAccount.ProviderType == MailProviderType.IMAP4;
|
||||
|
||||
if (shouldRequireArchiveFolder)
|
||||
{
|
||||
archiveFolder = await _folderService.GetSpecialFolderByAccountIdAsync(mailItem.AssignedAccount.Id, SpecialFolderType.Archive)
|
||||
?? throw new UnavailableSpecialFolderException(SpecialFolderType.Archive, mailItem.AssignedAccount.Id);
|
||||
}
|
||||
|
||||
return new ArchiveRequest(true, mailItem, mailItem.AssignedFolder, archiveFolder);
|
||||
}
|
||||
else if (action == MailOperation.MarkAsNotJunk)
|
||||
{
|
||||
var inboxFolder = await _folderService.GetSpecialFolderByAccountIdAsync(mailItem.AssignedAccount.Id, SpecialFolderType.Inbox)
|
||||
?? throw new UnavailableSpecialFolderException(SpecialFolderType.Inbox, mailItem.AssignedAccount.Id);
|
||||
|
||||
return new MoveRequest(mailItem, mailItem.AssignedFolder, inboxFolder);
|
||||
}
|
||||
else if (action == MailOperation.UnArchive)
|
||||
{
|
||||
var inboxFolder = await _folderService.GetSpecialFolderByAccountIdAsync(mailItem.AssignedAccount.Id, SpecialFolderType.Inbox)
|
||||
?? throw new UnavailableSpecialFolderException(SpecialFolderType.Inbox, mailItem.AssignedAccount.Id);
|
||||
|
||||
return new ArchiveRequest(false, mailItem, mailItem.AssignedFolder, inboxFolder);
|
||||
}
|
||||
else if (action == MailOperation.SoftDelete)
|
||||
{
|
||||
var trashFolder = await _folderService.GetSpecialFolderByAccountIdAsync(mailItem.AssignedAccount.Id, SpecialFolderType.Deleted)
|
||||
?? throw new UnavailableSpecialFolderException(SpecialFolderType.Deleted, mailItem.AssignedAccount.Id);
|
||||
|
||||
return new MoveRequest(mailItem, mailItem.AssignedFolder, trashFolder);
|
||||
}
|
||||
else if (action == MailOperation.MoveToJunk)
|
||||
{
|
||||
var junkFolder = await _folderService.GetSpecialFolderByAccountIdAsync(mailItem.AssignedAccount.Id, SpecialFolderType.Junk)
|
||||
?? throw new UnavailableSpecialFolderException(SpecialFolderType.Junk, mailItem.AssignedAccount.Id);
|
||||
|
||||
return new MoveRequest(mailItem, mailItem.AssignedFolder, junkFolder);
|
||||
}
|
||||
else if (action == MailOperation.AlwaysMoveToFocused || action == MailOperation.AlwaysMoveToOther)
|
||||
return new AlwaysMoveToRequest(mailItem, action == MailOperation.AlwaysMoveToFocused);
|
||||
else if (action == MailOperation.DiscardLocalDraft)
|
||||
await _mailService.DeleteMailAsync(mailItem.AssignedAccount.Id, mailItem.Id);
|
||||
else
|
||||
throw new NotSupportedException(string.Format(Translator.Exception_UnsupportedAction, action));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<IFolderActionRequest> PrepareFolderRequestAsync(FolderOperationPreperationRequest request)
|
||||
{
|
||||
if (request == null || request.Folder == null) return default;
|
||||
|
||||
IFolderActionRequest change = null;
|
||||
|
||||
var folder = request.Folder;
|
||||
var operation = request.Action;
|
||||
|
||||
switch (request.Action)
|
||||
{
|
||||
case FolderOperation.Pin:
|
||||
case FolderOperation.Unpin:
|
||||
await _folderService.ChangeStickyStatusAsync(folder.Id, operation == FolderOperation.Pin);
|
||||
break;
|
||||
|
||||
case FolderOperation.Rename:
|
||||
var newFolderName = await _dialogService.ShowTextInputDialogAsync(folder.FolderName, Translator.DialogMessage_RenameFolderTitle, Translator.DialogMessage_RenameFolderMessage, Translator.FolderOperation_Rename);
|
||||
|
||||
if (!string.IsNullOrEmpty(newFolderName))
|
||||
{
|
||||
change = new RenameFolderRequest(folder, folder.FolderName, newFolderName);
|
||||
}
|
||||
|
||||
action = MailOperation.HardDelete;
|
||||
}
|
||||
break;
|
||||
case FolderOperation.Empty:
|
||||
var mailsToDelete = await _mailService.GetMailsByFolderIdAsync(folder.Id).ConfigureAwait(false);
|
||||
|
||||
// Make sure there is a move target folder if action is move.
|
||||
// Let user pick a folder to move from the dialog.
|
||||
change = new EmptyFolderRequest(folder, mailsToDelete);
|
||||
|
||||
if (action == MailOperation.Move && moveTargetStructure == null)
|
||||
{
|
||||
// TODO: Handle multiple accounts for move operation.
|
||||
// What happens if we move 2 different mails from 2 different accounts?
|
||||
break;
|
||||
case FolderOperation.MarkAllAsRead:
|
||||
|
||||
var accountId = preperationRequest.MailItems.FirstOrDefault().AssignedAccount.Id;
|
||||
var unreadItems = await _mailService.GetUnreadMailsByFolderIdAsync(folder.Id).ConfigureAwait(false);
|
||||
|
||||
moveTargetStructure = await _dialogService.PickFolderAsync(accountId, PickFolderReason.Move, _folderService);
|
||||
if (unreadItems.Any())
|
||||
change = new MarkFolderAsReadRequest(folder, unreadItems);
|
||||
|
||||
if (moveTargetStructure == null)
|
||||
return default;
|
||||
}
|
||||
break;
|
||||
//case FolderOperation.Delete:
|
||||
// var isConfirmed = await _dialogService.ShowConfirmationDialogAsync($"'{folderStructure.FolderName}' is going to be deleted. Do you want to continue?", "Are you sure?", "Yes delete.");
|
||||
|
||||
var requests = new List<IMailActionRequest>();
|
||||
// if (isConfirmed)
|
||||
// change = new DeleteFolderRequest(accountId, folderStructure.RemoteFolderId, folderStructure.FolderId);
|
||||
|
||||
// TODO: Fix: Collection was modified; enumeration operation may not execute
|
||||
foreach (var item in preperationRequest.MailItems)
|
||||
{
|
||||
var singleRequest = await GetSingleRequestAsync(item, action, moveTargetStructure, preperationRequest.ToggleExecution);
|
||||
|
||||
if (singleRequest == null) continue;
|
||||
|
||||
requests.Add(singleRequest);
|
||||
}
|
||||
|
||||
return requests;
|
||||
// break;
|
||||
//default:
|
||||
// throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private async Task<IMailActionRequest> GetSingleRequestAsync(MailCopy mailItem, MailOperation action, IMailItemFolder moveTargetStructure, bool shouldToggleActions)
|
||||
{
|
||||
if (mailItem.AssignedAccount == null) throw new ArgumentException(Translator.Exception_NullAssignedAccount);
|
||||
if (mailItem.AssignedFolder == null) throw new ArgumentException(Translator.Exception_NullAssignedFolder);
|
||||
|
||||
// Rule: Soft deletes from Trash folder must perform Hard Delete.
|
||||
if (action == MailOperation.SoftDelete && mailItem.AssignedFolder.SpecialFolderType == SpecialFolderType.Deleted)
|
||||
action = MailOperation.HardDelete;
|
||||
|
||||
// Rule: SoftDelete draft items must be performed as hard delete.
|
||||
if (action == MailOperation.SoftDelete && mailItem.IsDraft)
|
||||
action = MailOperation.HardDelete;
|
||||
|
||||
// Rule: Soft/Hard deletes on local drafts are always discard local draft.
|
||||
if ((action == MailOperation.SoftDelete || action == MailOperation.HardDelete) && mailItem.IsLocalDraft)
|
||||
action = MailOperation.DiscardLocalDraft;
|
||||
|
||||
// Rule: Toggle actions must be reverted if ToggleExecution is passed true.
|
||||
if (shouldToggleActions)
|
||||
{
|
||||
var toggleRule = _toggleRequestRules.Find(a => a.SourceAction == action);
|
||||
|
||||
if (toggleRule != null && toggleRule.Condition(mailItem))
|
||||
{
|
||||
action = toggleRule.TargetAction;
|
||||
}
|
||||
}
|
||||
|
||||
if (action == MailOperation.MarkAsRead)
|
||||
return new MarkReadRequest(mailItem, true);
|
||||
else if (action == MailOperation.MarkAsUnread)
|
||||
return new MarkReadRequest(mailItem, false);
|
||||
else if (action == MailOperation.SetFlag)
|
||||
return new ChangeFlagRequest(mailItem, true);
|
||||
else if (action == MailOperation.ClearFlag)
|
||||
return new ChangeFlagRequest(mailItem, false);
|
||||
else if (action == MailOperation.HardDelete)
|
||||
return new DeleteRequest(mailItem);
|
||||
else if (action == MailOperation.Move)
|
||||
{
|
||||
if (moveTargetStructure == null)
|
||||
throw new InvalidMoveTargetException();
|
||||
|
||||
// TODO
|
||||
// Rule: You can't move items to non-move target folders;
|
||||
// Rule: You can't move items from a folder to itself.
|
||||
|
||||
//if (!moveTargetStructure.IsMoveTarget || moveTargetStructure.FolderId == mailItem.AssignedFolder.Id)
|
||||
// throw new InvalidMoveTargetException();
|
||||
|
||||
var pickedFolderItem = await _folderService.GetFolderAsync(moveTargetStructure.Id);
|
||||
|
||||
return new MoveRequest(mailItem, mailItem.AssignedFolder, pickedFolderItem);
|
||||
}
|
||||
else if (action == MailOperation.Archive)
|
||||
{
|
||||
// For IMAP and Outlook: Validate archive folder exists.
|
||||
// Gmail doesn't need archive folder existence.
|
||||
|
||||
MailItemFolder archiveFolder = null;
|
||||
|
||||
bool shouldRequireArchiveFolder = mailItem.AssignedAccount.ProviderType == MailProviderType.Outlook
|
||||
|| mailItem.AssignedAccount.ProviderType == MailProviderType.IMAP4;
|
||||
|
||||
if (shouldRequireArchiveFolder)
|
||||
{
|
||||
archiveFolder = await _folderService.GetSpecialFolderByAccountIdAsync(mailItem.AssignedAccount.Id, SpecialFolderType.Archive)
|
||||
?? throw new UnavailableSpecialFolderException(SpecialFolderType.Archive, mailItem.AssignedAccount.Id);
|
||||
}
|
||||
|
||||
return new ArchiveRequest(true, mailItem, mailItem.AssignedFolder, archiveFolder);
|
||||
}
|
||||
else if (action == MailOperation.MarkAsNotJunk)
|
||||
{
|
||||
var inboxFolder = await _folderService.GetSpecialFolderByAccountIdAsync(mailItem.AssignedAccount.Id, SpecialFolderType.Inbox)
|
||||
?? throw new UnavailableSpecialFolderException(SpecialFolderType.Inbox, mailItem.AssignedAccount.Id);
|
||||
|
||||
return new MoveRequest(mailItem, mailItem.AssignedFolder, inboxFolder);
|
||||
}
|
||||
else if (action == MailOperation.UnArchive)
|
||||
{
|
||||
var inboxFolder = await _folderService.GetSpecialFolderByAccountIdAsync(mailItem.AssignedAccount.Id, SpecialFolderType.Inbox)
|
||||
?? throw new UnavailableSpecialFolderException(SpecialFolderType.Inbox, mailItem.AssignedAccount.Id);
|
||||
|
||||
return new ArchiveRequest(false, mailItem, mailItem.AssignedFolder, inboxFolder);
|
||||
}
|
||||
else if (action == MailOperation.SoftDelete)
|
||||
{
|
||||
var trashFolder = await _folderService.GetSpecialFolderByAccountIdAsync(mailItem.AssignedAccount.Id, SpecialFolderType.Deleted)
|
||||
?? throw new UnavailableSpecialFolderException(SpecialFolderType.Deleted, mailItem.AssignedAccount.Id);
|
||||
|
||||
return new MoveRequest(mailItem, mailItem.AssignedFolder, trashFolder);
|
||||
}
|
||||
else if (action == MailOperation.MoveToJunk)
|
||||
{
|
||||
var junkFolder = await _folderService.GetSpecialFolderByAccountIdAsync(mailItem.AssignedAccount.Id, SpecialFolderType.Junk)
|
||||
?? throw new UnavailableSpecialFolderException(SpecialFolderType.Junk, mailItem.AssignedAccount.Id);
|
||||
|
||||
return new MoveRequest(mailItem, mailItem.AssignedFolder, junkFolder);
|
||||
}
|
||||
else if (action == MailOperation.AlwaysMoveToFocused || action == MailOperation.AlwaysMoveToOther)
|
||||
return new AlwaysMoveToRequest(mailItem, action == MailOperation.AlwaysMoveToFocused);
|
||||
else if (action == MailOperation.DiscardLocalDraft)
|
||||
await _mailService.DeleteMailAsync(mailItem.AssignedAccount.Id, mailItem.Id);
|
||||
else
|
||||
throw new NotSupportedException(string.Format(Translator.Exception_UnsupportedAction, action));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<IFolderActionRequest> PrepareFolderRequestAsync(FolderOperationPreperationRequest request)
|
||||
{
|
||||
if (request == null || request.Folder == null) return default;
|
||||
|
||||
IFolderActionRequest change = null;
|
||||
|
||||
var folder = request.Folder;
|
||||
var operation = request.Action;
|
||||
|
||||
switch (request.Action)
|
||||
{
|
||||
case FolderOperation.Pin:
|
||||
case FolderOperation.Unpin:
|
||||
await _folderService.ChangeStickyStatusAsync(folder.Id, operation == FolderOperation.Pin);
|
||||
break;
|
||||
|
||||
case FolderOperation.Rename:
|
||||
var newFolderName = await _dialogService.ShowTextInputDialogAsync(folder.FolderName, Translator.DialogMessage_RenameFolderTitle, Translator.DialogMessage_RenameFolderMessage, Translator.FolderOperation_Rename);
|
||||
|
||||
if (!string.IsNullOrEmpty(newFolderName))
|
||||
{
|
||||
change = new RenameFolderRequest(folder, folder.FolderName, newFolderName);
|
||||
}
|
||||
|
||||
break;
|
||||
case FolderOperation.Empty:
|
||||
var mailsToDelete = await _mailService.GetMailsByFolderIdAsync(folder.Id).ConfigureAwait(false);
|
||||
|
||||
change = new EmptyFolderRequest(folder, mailsToDelete);
|
||||
|
||||
break;
|
||||
case FolderOperation.MarkAllAsRead:
|
||||
|
||||
var unreadItems = await _mailService.GetUnreadMailsByFolderIdAsync(folder.Id).ConfigureAwait(false);
|
||||
|
||||
if (unreadItems.Any())
|
||||
change = new MarkFolderAsReadRequest(folder, unreadItems);
|
||||
|
||||
break;
|
||||
//case FolderOperation.Delete:
|
||||
// var isConfirmed = await _dialogService.ShowConfirmationDialogAsync($"'{folderStructure.FolderName}' is going to be deleted. Do you want to continue?", "Are you sure?", "Yes delete.");
|
||||
|
||||
// if (isConfirmed)
|
||||
// change = new DeleteFolderRequest(accountId, folderStructure.RemoteFolderId, folderStructure.FolderId);
|
||||
|
||||
// break;
|
||||
//default:
|
||||
// throw new NotImplementedException();
|
||||
}
|
||||
|
||||
return change;
|
||||
}
|
||||
return change;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user