Synchronization manager.

This commit is contained in:
Burak Kaan Köse
2025-10-04 23:10:07 +02:00
parent 3b1eff1702
commit 9623c2e6d2
17 changed files with 709 additions and 352 deletions
@@ -11,8 +11,8 @@ using Wino.Core.Domain.Enums;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.Folders;
using Wino.Core.Domain.Models.Navigation;
using Wino.Core.Services;
using Wino.Messaging.Client.Navigation;
using Wino.Messaging.Server;
namespace Wino.Mail.ViewModels;
@@ -20,7 +20,6 @@ public partial class AccountDetailsPageViewModel : MailBaseViewModel
{
private readonly IMailDialogService _dialogService;
private readonly IAccountService _accountService;
private readonly IWinoServerConnectionManager _serverConnectionManager;
private readonly IFolderService _folderService;
private bool isLoaded = false;
@@ -50,12 +49,10 @@ public partial class AccountDetailsPageViewModel : MailBaseViewModel
public AccountDetailsPageViewModel(IMailDialogService dialogService,
IAccountService accountService,
IWinoServerConnectionManager serverConnectionManager,
IFolderService folderService)
{
_dialogService = dialogService;
_accountService = accountService;
_serverConnectionManager = serverConnectionManager;
_folderService = folderService;
}
@@ -95,21 +92,16 @@ public partial class AccountDetailsPageViewModel : MailBaseViewModel
return;
var isSynchronizerKilledResponse = await _serverConnectionManager.GetResponseAsync<bool, KillAccountSynchronizerRequested>(new KillAccountSynchronizerRequested(Account.Id));
await SynchronizationManager.Instance.DestroySynchronizerAsync(Account.Id);
if (isSynchronizerKilledResponse.IsSuccess)
{
await _accountService.DeleteAccountAsync(Account);
await _accountService.DeleteAccountAsync(Account);
_dialogService.InfoBarMessage(Translator.Info_AccountDeletedTitle, string.Format(Translator.Info_AccountDeletedMessage, Account.Name), InfoBarMessageType.Success);
_dialogService.InfoBarMessage(Translator.Info_AccountDeletedTitle, string.Format(Translator.Info_AccountDeletedMessage, Account.Name), InfoBarMessageType.Success);
Messenger.Send(new BackBreadcrumNavigationRequested());
}
Messenger.Send(new BackBreadcrumNavigationRequested());
}
public override async void OnNavigatedTo(NavigationMode mode, object parameters)
{
base.OnNavigatedTo(mode, parameters);
@@ -12,15 +12,13 @@ using Wino.Core.Domain.Entities.Shared;
using Wino.Core.Domain.Enums;
using Wino.Core.Domain.Exceptions;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.Authentication;
using Wino.Core.Domain.Models.Connectivity;
using Wino.Core.Domain.Models.Navigation;
using Wino.Core.Domain.Models.Synchronization;
using Wino.Core.Services;
using Wino.Core.ViewModels;
using Wino.Core.ViewModels.Data;
using Wino.Mail.ViewModels.Data;
using Wino.Messaging.Client.Navigation;
using Wino.Messaging.Server;
using Wino.Messaging.UI;
namespace Wino.Mail.ViewModels;
@@ -34,7 +32,6 @@ public partial class AccountManagementViewModel : AccountManagementPageViewModel
public IMailDialogService MailDialogService { get; }
public AccountManagementViewModel(IMailDialogService dialogService,
IWinoServerConnectionManager winoServerConnectionManager,
INavigationService navigationService,
IAccountService accountService,
ISpecialImapProviderConfigResolver specialImapProviderConfigResolver,
@@ -43,7 +40,7 @@ public partial class AccountManagementViewModel : AccountManagementPageViewModel
IStoreManagementService storeManagementService,
IWinoLogger winoLogger,
IAuthenticationProvider authenticationProvider,
IPreferencesService preferencesService) : base(dialogService, winoServerConnectionManager, navigationService, accountService, providerService, storeManagementService, authenticationProvider, preferencesService)
IPreferencesService preferencesService) : base(dialogService, navigationService, accountService, providerService, storeManagementService, authenticationProvider, preferencesService)
{
MailDialogService = dialogService;
_specialImapProviderConfigResolver = specialImapProviderConfigResolver;
@@ -154,37 +151,36 @@ public partial class AccountManagementViewModel : AccountManagementPageViewModel
createdAccount.Address = customServerInformation.Address;
// Let server validate the imap/smtp connection.
var testResultResponse = await WinoServerConnectionManager.GetResponseAsync<ImapConnectivityTestResults, ImapConnectivityTestRequested>(new ImapConnectivityTestRequested(customServerInformation, true));
// TODO: Protocol log with detailed failure.
if (!testResultResponse.IsSuccess)
{
throw new Exception($"{Translator.IMAPSetupDialog_ConnectionFailedTitle}\n{testResultResponse.Message}");
}
else if (!testResultResponse.Data.IsSuccess)
{
// Server connectivity might succeed, but result might be failed.
throw new ImapClientPoolException(testResultResponse.Data.FailedReason, customServerInformation, testResultResponse.Data.FailureProtocolLog);
}
await _imapTestService.TestImapConnectionAsync(customServerInformation, true);
//var testResultResponse = await WinoServerConnectionManager.GetResponseAsync<ImapConnectivityTestResults, ImapConnectivityTestRequested>(new ImapConnectivityTestRequested(customServerInformation, true));
//if (!testResultResponse.IsSuccess)
//{
// throw new Exception($"{Translator.IMAPSetupDialog_ConnectionFailedTitle}\n{testResultResponse.Message}");
//}
//else if (!testResultResponse.Data.IsSuccess)
//{
// // Server connectivity might succeed, but result might be failed.
// throw new ImapClientPoolException(testResultResponse.Data.FailedReason, customServerInformation, testResultResponse.Data.FailureProtocolLog);
//}
}
else
{
// OAuth authentication is handled here.
// Server authenticates, returns the token info here.
// Use SynchronizationManager to handle OAuth authentication.
var tokenInformationResponse = await WinoServerConnectionManager
.GetResponseAsync<TokenInformationEx, AuthorizationRequested>(new AuthorizationRequested(accountCreationDialogResult.ProviderType,
createdAccount,
createdAccount.ProviderType == MailProviderType.Gmail), accountCreationCancellationTokenSource.Token);
var authTokenInfo = await SynchronizationManager.Instance.HandleAuthorizationAsync(
accountCreationDialogResult.ProviderType,
createdAccount,
createdAccount.ProviderType == MailProviderType.Gmail);
if (creationDialog.State == AccountCreationDialogState.Canceled)
throw new AccountSetupCanceledException();
if (!tokenInformationResponse.IsSuccess)
throw new Exception(tokenInformationResponse.Message);
createdAccount.Address = tokenInformationResponse.Data.AccountAddress;
tokenInformationResponse.ThrowIfFailed();
// Update account address with authenticated user information
createdAccount.Address = authTokenInfo.AccountAddress;
}
}
@@ -207,22 +203,23 @@ public partial class AccountManagementViewModel : AccountManagementPageViewModel
Type = MailSynchronizationType.UpdateProfile
};
var profileSynchronizationResponse = await WinoServerConnectionManager.GetResponseAsync<MailSynchronizationResult, NewMailSynchronizationRequested>(new NewMailSynchronizationRequested(profileSyncOptions, SynchronizationSource.Client));
var profileSynchronizationResult = profileSynchronizationResponse.Data;
var profileSynchronizationResult = await SynchronizationManager.Instance.SynchronizeProfileAsync(createdAccount.Id);
if (profileSynchronizationResult.CompletedState != SynchronizationCompletedState.Success)
throw new Exception(Translator.Exception_FailedToSynchronizeProfileInformation);
createdAccount.SenderName = profileSynchronizationResult.ProfileInformation.SenderName;
createdAccount.Base64ProfilePictureData = profileSynchronizationResult.ProfileInformation.Base64ProfilePictureData;
if (!string.IsNullOrEmpty(profileSynchronizationResult.ProfileInformation.AccountAddress))
if (profileSynchronizationResult.ProfileInformation != null)
{
createdAccount.Address = profileSynchronizationResult.ProfileInformation.AccountAddress;
}
createdAccount.SenderName = profileSynchronizationResult.ProfileInformation.SenderName;
createdAccount.Base64ProfilePictureData = profileSynchronizationResult.ProfileInformation.Base64ProfilePictureData;
await AccountService.UpdateProfileInformationAsync(createdAccount.Id, profileSynchronizationResult.ProfileInformation);
if (!string.IsNullOrEmpty(profileSynchronizationResult.ProfileInformation.AccountAddress))
{
createdAccount.Address = profileSynchronizationResult.ProfileInformation.AccountAddress;
}
await AccountService.UpdateProfileInformationAsync(createdAccount.Id, profileSynchronizationResult.ProfileInformation);
}
}
if (creationDialog is IImapAccountCreationDialog customServerAccountCreationDialog)
@@ -237,26 +234,16 @@ public partial class AccountManagementViewModel : AccountManagementPageViewModel
Type = MailSynchronizationType.FoldersOnly
};
var folderSynchronizationResponse = await WinoServerConnectionManager.GetResponseAsync<MailSynchronizationResult, NewMailSynchronizationRequested>(new NewMailSynchronizationRequested(folderSyncOptions, SynchronizationSource.Client));
var folderSynchronizationResult = folderSynchronizationResponse.Data;
var folderSynchronizationResult = await SynchronizationManager.Instance.SynchronizeFoldersAsync(createdAccount.Id);
if (folderSynchronizationResult == null || folderSynchronizationResult.CompletedState != SynchronizationCompletedState.Success)
throw new Exception($"{Translator.Exception_FailedToSynchronizeFolders}\n{folderSynchronizationResponse.Message}");
throw new Exception(Translator.Exception_FailedToSynchronizeFolders);
// Sync aliases if supported.
if (createdAccount.IsAliasSyncSupported)
{
// Try to synchronize aliases for the account.
var aliasSyncOptions = new MailSynchronizationOptions()
{
AccountId = createdAccount.Id,
Type = MailSynchronizationType.Alias
};
var aliasSyncResponse = await WinoServerConnectionManager.GetResponseAsync<MailSynchronizationResult, NewMailSynchronizationRequested>(new NewMailSynchronizationRequested(aliasSyncOptions, SynchronizationSource.Client));
var aliasSynchronizationResult = folderSynchronizationResponse.Data;
var aliasSynchronizationResult = await SynchronizationManager.Instance.SynchronizeAliasesAsync(createdAccount.Id);
if (aliasSynchronizationResult.CompletedState != SynchronizationCompletedState.Success)
throw new Exception(Translator.Exception_FailedToSynchronizeAliases);
@@ -12,7 +12,7 @@ using Wino.Core.Domain.Enums;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.Navigation;
using Wino.Core.Domain.Models.Synchronization;
using Wino.Messaging.Server;
using Wino.Core.Services;
namespace Wino.Mail.ViewModels;
@@ -20,7 +20,6 @@ public partial class AliasManagementPageViewModel : MailBaseViewModel
{
private readonly IMailDialogService _dialogService;
private readonly IAccountService _accountService;
private readonly IWinoServerConnectionManager _winoServerConnectionManager;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(CanSynchronizeAliases))]
@@ -32,12 +31,10 @@ public partial class AliasManagementPageViewModel : MailBaseViewModel
public bool CanSynchronizeAliases => Account?.IsAliasSyncSupported ?? false;
public AliasManagementPageViewModel(IMailDialogService dialogService,
IAccountService accountService,
IWinoServerConnectionManager winoServerConnectionManager)
IAccountService accountService)
{
_dialogService = dialogService;
_accountService = accountService;
_winoServerConnectionManager = winoServerConnectionManager;
}
public override async void OnNavigatedTo(NavigationMode mode, object parameters)
@@ -82,12 +79,12 @@ public partial class AliasManagementPageViewModel : MailBaseViewModel
Type = MailSynchronizationType.Alias
};
var aliasSyncResponse = await _winoServerConnectionManager.GetResponseAsync<MailSynchronizationResult, NewMailSynchronizationRequested>(new NewMailSynchronizationRequested(aliasSyncOptions, SynchronizationSource.Client));
var aliasSyncResult = await SynchronizationManager.Instance.SynchronizeAliasesAsync(Account.Id);
if (aliasSyncResponse.IsSuccess)
if (aliasSyncResult.CompletedState == SynchronizationCompletedState.Success)
await LoadAliasesAsync();
else
_dialogService.InfoBarMessage(Translator.GeneralTitle_Error, aliasSyncResponse.Message, InfoBarMessageType.Error);
_dialogService.InfoBarMessage(Translator.GeneralTitle_Error, "Failed to synchronize aliases", InfoBarMessageType.Error);
}
[RelayCommand]
+7 -11
View File
@@ -13,11 +13,11 @@ using Wino.Core.Domain.Entities.Mail;
using Wino.Core.Domain.Entities.Shared;
using Wino.Core.Domain.Enums;
using Wino.Core.Domain.Exceptions;
using Wino.Core.Services;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.MailItem;
using Wino.Core.Domain.Models.Navigation;
using Wino.Core.Extensions;
using Wino.Core.Services;
using Wino.Mail.ViewModels.Data;
using Wino.Messaging.Client.Mails;
using Wino.Messaging.Server;
@@ -102,7 +102,6 @@ public partial class ComposePageViewModel : MailBaseViewModel
private readonly IWinoRequestDelegator _worker;
public readonly IFontService FontService;
public readonly IPreferencesService PreferencesService;
private readonly IWinoServerConnectionManager _winoServerConnectionManager;
public readonly IContactService ContactService;
public ComposePageViewModel(IMailDialogService dialogService,
@@ -115,8 +114,7 @@ public partial class ComposePageViewModel : MailBaseViewModel
IWinoRequestDelegator worker,
IContactService contactService,
IFontService fontService,
IPreferencesService preferencesService,
IWinoServerConnectionManager winoServerConnectionManager)
IPreferencesService preferencesService)
{
NativeAppService = nativeAppService;
ContactService = contactService;
@@ -130,7 +128,6 @@ public partial class ComposePageViewModel : MailBaseViewModel
_fileService = fileService;
_accountService = accountService;
_worker = worker;
_winoServerConnectionManager = winoServerConnectionManager;
}
[RelayCommand]
@@ -412,13 +409,12 @@ public partial class ComposePageViewModel : MailBaseViewModel
{
downloadIfNeeded = false;
var package = new DownloadMissingMessageRequested(CurrentMailDraftItem.AssignedAccount.Id, CurrentMailDraftItem.MailCopy);
var downloadResponse = await _winoServerConnectionManager.GetResponseAsync<bool, DownloadMissingMessageRequested>(package);
// Download missing MIME message using SynchronizationManager
await SynchronizationManager.Instance.DownloadMimeMessageAsync(
CurrentMailDraftItem.MailCopy,
CurrentMailDraftItem.AssignedAccount.Id);
if (downloadResponse.IsSuccess)
{
goto retry;
}
goto retry;
}
else
_dialogService.InfoBarMessage(Translator.Info_ComposerMissingMIMETitle, Translator.Info_ComposerMissingMIMEMessage, InfoBarMessageType.Error);
+43 -46
View File
@@ -23,6 +23,7 @@ using Wino.Core.Domain.Models.MailItem;
using Wino.Core.Domain.Models.Menus;
using Wino.Core.Domain.Models.Reader;
using Wino.Core.Domain.Models.Synchronization;
using Wino.Core.Services;
using Wino.Mail.ViewModels.Collections;
using Wino.Mail.ViewModels.Data;
using Wino.Mail.ViewModels.Messages;
@@ -81,7 +82,6 @@ public partial class MailListPageViewModel : MailBaseViewModel,
private readonly IWinoRequestDelegator _winoRequestDelegator;
private readonly IKeyPressService _keyPressService;
private readonly IWinoLogger _winoLogger;
private readonly IWinoServerConnectionManager _winoServerConnectionManager;
private MailItemViewModel _activeMailItem;
public List<SortingOption> SortingOptions { get; } =
@@ -160,14 +160,12 @@ public partial class MailListPageViewModel : MailBaseViewModel,
IKeyPressService keyPressService,
IPreferencesService preferencesService,
INewThemeService themeService,
IWinoLogger winoLogger,
IWinoServerConnectionManager winoServerConnectionManager)
IWinoLogger winoLogger)
{
MailCollection = new WinoMailCollection(threadingStrategyProvider);
PreferencesService = preferencesService;
ThemeService = themeService;
_winoLogger = winoLogger;
_winoServerConnectionManager = winoServerConnectionManager;
StatePersistenceService = statePersistenceService;
NavigationService = navigationService;
_accountService = accountService;
@@ -841,51 +839,52 @@ public partial class MailListPageViewModel : MailBaseViewModel,
// Perform online search.
if (isDoingOnlineSearch)
{
WinoServerResponse<OnlineSearchResult> onlineSearchResult = null;
string onlineSearchFailedMessage = null;
// TODO: Burak: Handle online search.
//WinoServerResponse<OnlineSearchResult> onlineSearchResult = null;
//string onlineSearchFailedMessage = null;
try
{
var accountIds = ActiveFolder.HandlingFolders.Select(a => a.MailAccountId).ToList();
var folders = ActiveFolder.HandlingFolders.ToList();
var searchRequest = new OnlineSearchRequested(accountIds, SearchQuery, folders);
//try
//{
// var accountIds = ActiveFolder.HandlingFolders.Select(a => a.MailAccountId).ToList();
// var folders = ActiveFolder.HandlingFolders.ToList();
// var searchRequest = new OnlineSearchRequested(accountIds, SearchQuery, folders);
onlineSearchResult = await _winoServerConnectionManager.GetResponseAsync<OnlineSearchResult, OnlineSearchRequested>(searchRequest, cancellationToken);
// onlineSearchResult = await _winoServerConnectionManager.GetResponseAsync<OnlineSearchResult, OnlineSearchRequested>(searchRequest, cancellationToken);
if (onlineSearchResult.IsSuccess)
{
await ExecuteUIThread(() => { AreSearchResultsOnline = true; });
// if (onlineSearchResult.IsSuccess)
// {
// await ExecuteUIThread(() => { AreSearchResultsOnline = true; });
onlineSearchItems = onlineSearchResult.Data.SearchResult;
}
else
{
onlineSearchFailedMessage = onlineSearchResult.Message;
}
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception ex)
{
Log.Warning(ex, "Failed to perform online search.");
onlineSearchFailedMessage = ex.Message;
}
// onlineSearchItems = onlineSearchResult.Data.SearchResult;
// }
// else
// {
// onlineSearchFailedMessage = onlineSearchResult.Message;
// }
//}
//catch (OperationCanceledException)
//{
// throw;
//}
//catch (Exception ex)
//{
// Log.Warning(ex, "Failed to perform online search.");
// onlineSearchFailedMessage = ex.Message;
//}
if (onlineSearchResult != null && !onlineSearchResult.IsSuccess)
{
// Query or server error.
var serverErrorMessage = string.Format(Translator.OnlineSearchFailed_Message, onlineSearchResult.Message);
_mailDialogService.InfoBarMessage(Translator.GeneralTitle_Error, serverErrorMessage, InfoBarMessageType.Warning);
//if (onlineSearchResult != null && !onlineSearchResult.IsSuccess)
//{
// // Query or server error.
// var serverErrorMessage = string.Format(Translator.OnlineSearchFailed_Message, onlineSearchResult.Message);
// _mailDialogService.InfoBarMessage(Translator.GeneralTitle_Error, serverErrorMessage, InfoBarMessageType.Warning);
}
else if (!string.IsNullOrEmpty(onlineSearchFailedMessage))
{
// Fatal error.
var serverErrorMessage = string.Format(Translator.OnlineSearchFailed_Message, onlineSearchFailedMessage);
_mailDialogService.InfoBarMessage(Translator.GeneralTitle_Error, serverErrorMessage, InfoBarMessageType.Warning);
}
//}
//else if (!string.IsNullOrEmpty(onlineSearchFailedMessage))
//{
// // Fatal error.
// var serverErrorMessage = string.Format(Translator.OnlineSearchFailed_Message, onlineSearchFailedMessage);
// _mailDialogService.InfoBarMessage(Translator.GeneralTitle_Error, serverErrorMessage, InfoBarMessageType.Warning);
//}
}
}
@@ -1110,9 +1109,7 @@ public partial class MailListPageViewModel : MailBaseViewModel,
foreach (var accountId in accountIds)
{
var serverResponse = await _winoServerConnectionManager.GetResponseAsync<bool, SynchronizationExistenceCheckRequest>(new SynchronizationExistenceCheckRequest(accountId));
if (serverResponse.IsSuccess && serverResponse.Data == true)
if (SynchronizationManager.Instance.IsAccountSynchronizing(accountId))
{
isAnyAccountSynchronizing = true;
break;
@@ -21,6 +21,7 @@ using Wino.Core.Domain.Models.MailItem;
using Wino.Core.Domain.Models.Menus;
using Wino.Core.Domain.Models.Navigation;
using Wino.Core.Domain.Models.Reader;
using Wino.Core.Services;
using Wino.Mail.ViewModels.Data;
using Wino.Mail.ViewModels.Messages;
using Wino.Messaging.Client.Mails;
@@ -46,7 +47,6 @@ public partial class MailRenderingPageViewModel : MailBaseViewModel,
private readonly IClipboardService _clipboardService;
private readonly IUnsubscriptionService _unsubscriptionService;
private readonly IApplicationConfiguration _applicationConfiguration;
private readonly IWinoServerConnectionManager _winoServerConnectionManager;
private bool forceImageLoading = false;
private MailItemViewModel initializedMailItemViewModel = null;
@@ -142,8 +142,7 @@ public partial class MailRenderingPageViewModel : MailBaseViewModel,
IUnsubscriptionService unsubscriptionService,
IPreferencesService preferencesService,
IPrintService printService,
IApplicationConfiguration applicationConfiguration,
IWinoServerConnectionManager winoServerConnectionManager)
IApplicationConfiguration applicationConfiguration)
{
_dialogService = dialogService;
NativeAppService = nativeAppService;
@@ -152,7 +151,6 @@ public partial class MailRenderingPageViewModel : MailBaseViewModel,
PreferencesService = preferencesService;
PrintService = printService;
_applicationConfiguration = applicationConfiguration;
_winoServerConnectionManager = winoServerConnectionManager;
_clipboardService = clipboardService;
_unsubscriptionService = unsubscriptionService;
_underlyingThemeService = underlyingThemeService;
@@ -355,8 +353,10 @@ public partial class MailRenderingPageViewModel : MailBaseViewModel,
// To show the progress on the UI.
CurrentDownloadPercentage = 1;
var package = new DownloadMissingMessageRequested(mailItemViewModel.AssignedAccount.Id, mailItemViewModel.MailCopy);
await _winoServerConnectionManager.GetResponseAsync<bool, DownloadMissingMessageRequested>(package);
// Download missing MIME message using SynchronizationManager
await SynchronizationManager.Instance.DownloadMimeMessageAsync(
mailItemViewModel.MailCopy,
mailItemViewModel.AssignedAccount.Id);
}
catch (OperationCanceledException)
{