Files
Wino-Mail/Wino.Mail.ViewModels/AccountManagementViewModel.cs
T

419 lines
19 KiB
C#
Raw Normal View History

2024-04-18 01:44:37 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
2024-04-18 01:44:37 +02:00
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Serilog;
using Wino.Core.Domain;
2024-11-10 23:28:25 +01:00
using Wino.Core.Domain.Entities.Mail;
using Wino.Core.Domain.Entities.Shared;
2024-04-18 01:44:37 +02:00
using Wino.Core.Domain.Enums;
using Wino.Core.Domain.Exceptions;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.Navigation;
using Wino.Core.Domain.Models.Synchronization;
2025-10-04 23:10:07 +02:00
using Wino.Core.Services;
2024-11-10 23:28:25 +01:00
using Wino.Core.ViewModels;
using Wino.Core.ViewModels.Data;
2024-04-18 01:44:37 +02:00
using Wino.Mail.ViewModels.Data;
using Wino.Messaging.Client.Navigation;
using Wino.Messaging.UI;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
namespace Wino.Mail.ViewModels;
public partial class AccountManagementViewModel : AccountManagementPageViewModelBase
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
private readonly ISpecialImapProviderConfigResolver _specialImapProviderConfigResolver;
private readonly IImapTestService _imapTestService;
private readonly IWinoLogger _winoLogger;
2025-02-16 11:54:23 +01:00
public IMailDialogService MailDialogService { get; }
public AccountManagementViewModel(IMailDialogService dialogService,
INavigationService navigationService,
IAccountService accountService,
ISpecialImapProviderConfigResolver specialImapProviderConfigResolver,
IProviderService providerService,
IImapTestService imapTestService,
IStoreManagementService storeManagementService,
IWinoLogger winoLogger,
2025-02-16 11:54:23 +01:00
IAuthenticationProvider authenticationProvider,
2025-10-04 23:10:07 +02:00
IPreferencesService preferencesService) : base(dialogService, navigationService, accountService, providerService, storeManagementService, authenticationProvider, preferencesService)
2025-02-16 11:35:43 +01:00
{
2025-02-16 11:54:23 +01:00
MailDialogService = dialogService;
_specialImapProviderConfigResolver = specialImapProviderConfigResolver;
_imapTestService = imapTestService;
_winoLogger = winoLogger;
2025-02-16 11:54:23 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
[RelayCommand]
private async Task CreateMergedAccountAsync()
{
var linkName = await DialogService.ShowTextInputDialogAsync(string.Empty, Translator.DialogMessage_CreateLinkedAccountTitle, Translator.DialogMessage_CreateLinkedAccountMessage, Translator.Buttons_Create);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (string.IsNullOrEmpty(linkName)) return;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Create arbitary empty merged inbox with an empty Guid and go to edit page.
var mergedInbox = new MergedInbox()
{
Id = Guid.Empty,
Name = linkName
};
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var mergedAccountProviderDetailViewModel = new MergedAccountProviderDetailViewModel(mergedInbox, new List<AccountProviderDetailViewModel>());
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
Messenger.Send(new BreadcrumbNavigationRequested(mergedAccountProviderDetailViewModel.MergedInbox.Name,
WinoPage.MergedAccountDetailsPage,
mergedAccountProviderDetailViewModel));
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
[RelayCommand]
private async Task AddNewAccountAsync()
{
if (IsAccountCreationBlocked)
2025-02-16 11:43:30 +01:00
{
2025-02-16 11:54:23 +01:00
var isPurchaseClicked = await DialogService.ShowConfirmationDialogAsync(Translator.DialogMessage_AccountLimitMessage, Translator.DialogMessage_AccountLimitTitle, Translator.Buttons_Purchase);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (!isPurchaseClicked) return;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
await PurchaseUnlimitedAccountAsync();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
MailAccount createdAccount = null;
IAccountCreationDialog creationDialog = null;
2025-02-16 11:54:23 +01:00
try
{
var providers = ProviderService.GetAvailableProviders();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Select provider.
var accountCreationDialogResult = await MailDialogService.ShowAccountProviderSelectionDialogAsync(providers);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var accountCreationCancellationTokenSource = new CancellationTokenSource();
2025-02-16 11:43:30 +01:00
2025-02-16 11:54:23 +01:00
if (accountCreationDialogResult != null)
{
creationDialog = MailDialogService.GetAccountCreationDialog(accountCreationDialogResult);
2025-02-16 11:43:30 +01:00
2025-02-16 11:54:23 +01:00
CustomServerInformation customServerInformation = null;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
createdAccount = new MailAccount()
{
ProviderType = accountCreationDialogResult.ProviderType,
Name = accountCreationDialogResult.AccountName,
SpecialImapProvider = accountCreationDialogResult.SpecialImapProviderDetails?.SpecialImapProvider ?? SpecialImapProvider.None,
Id = Guid.NewGuid(),
AccountColorHex = accountCreationDialogResult.AccountColorHex,
IsCalendarAccessGranted = true // New accounts have calendar scopes
2025-02-16 11:54:23 +01:00
};
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
await creationDialog.ShowDialogAsync(accountCreationCancellationTokenSource);
2025-02-26 19:04:38 +01:00
await Task.Delay(500);
2025-02-16 11:54:23 +01:00
creationDialog.State = AccountCreationDialogState.SigningIn;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
string tokenInformation = string.Empty;
2025-02-16 11:54:23 +01:00
// Custom server implementation requires more async waiting.
if (creationDialog is IImapAccountCreationDialog customServerDialog)
{
// Pass along the account properties and perform initial navigation on the imap frame.
customServerDialog.StartImapConnectionSetup(createdAccount);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
customServerInformation = await customServerDialog.GetCustomServerInformationAsync()
?? throw new AccountSetupCanceledException();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// At this point connection is successful.
// Save the server setup information and later on we'll fetch folders.
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
customServerInformation.AccountId = createdAccount.Id;
createdAccount.Address = customServerInformation.Address;
createdAccount.ServerInformation = customServerInformation;
createdAccount.SenderName = customServerInformation.DisplayName;
}
else
{
// Hanle special imap providers like iCloud and Yahoo.
if (accountCreationDialogResult.SpecialImapProviderDetails != null)
{
// Special imap provider testing dialog. This is only available for iCloud and Yahoo.
customServerInformation = _specialImapProviderConfigResolver.GetServerInformation(createdAccount, accountCreationDialogResult);
customServerInformation.Id = Guid.NewGuid();
2024-04-18 01:44:37 +02:00
customServerInformation.AccountId = createdAccount.Id;
2025-02-16 11:54:23 +01:00
createdAccount.SenderName = accountCreationDialogResult.SpecialImapProviderDetails.SenderName;
2024-04-18 01:44:37 +02:00
createdAccount.Address = customServerInformation.Address;
2025-02-16 11:54:23 +01:00
// Let server validate the imap/smtp connection.
2025-10-04 23:10:07 +02:00
// TODO: Protocol log with detailed failure.
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);
//}
2024-04-18 01:44:37 +02:00
}
else
{
2025-02-16 11:54:23 +01:00
// OAuth authentication is handled here.
2025-10-04 23:10:07 +02:00
// Use SynchronizationManager to handle OAuth authentication.
2025-10-04 23:10:07 +02:00
var authTokenInfo = await SynchronizationManager.Instance.HandleAuthorizationAsync(
accountCreationDialogResult.ProviderType,
createdAccount,
createdAccount.ProviderType == MailProviderType.Gmail);
2025-02-16 11:54:23 +01:00
if (creationDialog.State == AccountCreationDialogState.Canceled)
throw new AccountSetupCanceledException();
2024-08-17 19:54:52 +02:00
2025-10-04 23:10:07 +02:00
// Update account address with authenticated user information
createdAccount.Address = authTokenInfo.AccountAddress;
2025-02-16 11:54:23 +01:00
}
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Address is still doesn't have a value for API synchronizers.
// It'll be synchronized with profile information.
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
await AccountService.CreateAccountAsync(createdAccount, customServerInformation);
2025-02-16 11:54:23 +01:00
// Local account has been created.
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Sync profile information if supported.
if (createdAccount.IsProfileInfoSyncSupported)
{
// Start profile information synchronization.
// It's only available for Outlook and Gmail synchronizers.
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var profileSyncOptions = new MailSynchronizationOptions()
2024-08-17 19:54:52 +02:00
{
2025-02-16 11:43:30 +01:00
AccountId = createdAccount.Id,
2025-02-16 11:54:23 +01:00
Type = MailSynchronizationType.UpdateProfile
2025-02-16 11:43:30 +01:00
};
2025-10-04 23:10:07 +02:00
var profileSynchronizationResult = await SynchronizationManager.Instance.SynchronizeProfileAsync(createdAccount.Id);
2024-08-17 19:54:52 +02:00
2025-02-16 11:54:23 +01:00
if (profileSynchronizationResult.CompletedState != SynchronizationCompletedState.Success)
throw new Exception(Translator.Exception_FailedToSynchronizeProfileInformation);
2024-08-17 19:54:52 +02:00
2025-10-04 23:10:07 +02:00
if (profileSynchronizationResult.ProfileInformation != null)
2025-02-16 11:43:30 +01:00
{
2025-10-04 23:10:07 +02:00
createdAccount.SenderName = profileSynchronizationResult.ProfileInformation.SenderName;
createdAccount.Base64ProfilePictureData = profileSynchronizationResult.ProfileInformation.Base64ProfilePictureData;
2024-04-18 01:44:37 +02:00
2025-10-04 23:10:07 +02:00
if (!string.IsNullOrEmpty(profileSynchronizationResult.ProfileInformation.AccountAddress))
{
createdAccount.Address = profileSynchronizationResult.ProfileInformation.AccountAddress;
}
await AccountService.UpdateProfileInformationAsync(createdAccount.Id, profileSynchronizationResult.ProfileInformation);
}
2025-02-16 11:54:23 +01:00
}
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
if (creationDialog is IImapAccountCreationDialog customServerAccountCreationDialog)
customServerAccountCreationDialog.ShowPreparingFolders();
else
creationDialog.State = AccountCreationDialogState.PreparingFolders;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Start synchronizing folders.
var folderSyncOptions = new MailSynchronizationOptions()
{
AccountId = createdAccount.Id,
Type = MailSynchronizationType.FoldersOnly
};
2024-04-18 01:44:37 +02:00
2025-10-04 23:10:07 +02:00
var folderSynchronizationResult = await SynchronizationManager.Instance.SynchronizeFoldersAsync(createdAccount.Id);
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
if (folderSynchronizationResult == null || folderSynchronizationResult.CompletedState != SynchronizationCompletedState.Success)
2025-10-04 23:10:07 +02:00
throw new Exception(Translator.Exception_FailedToSynchronizeFolders);
2025-02-16 11:54:23 +01:00
// Sync aliases if supported.
if (createdAccount.IsAliasSyncSupported)
{
// Try to synchronize aliases for the account.
2025-10-04 23:10:07 +02:00
var aliasSynchronizationResult = await SynchronizationManager.Instance.SynchronizeAliasesAsync(createdAccount.Id);
2025-02-16 11:54:23 +01:00
if (aliasSynchronizationResult.CompletedState != SynchronizationCompletedState.Success)
throw new Exception(Translator.Exception_FailedToSynchronizeAliases);
}
else
2025-02-16 11:43:30 +01:00
{
2025-02-16 11:54:23 +01:00
// Create root primary alias for the account.
// This is only available for accounts that do not support alias synchronization.
await AccountService.CreateRootAliasAsync(createdAccount.Id, createdAccount.Address);
2025-02-16 11:43:30 +01:00
}
2025-02-16 11:54:23 +01:00
// Send changes to listeners.
ReportUIChange(new AccountCreatedMessage(createdAccount));
// Notify success.
DialogService.InfoBarMessage(Translator.Info_AccountCreatedTitle, string.Format(Translator.Info_AccountCreatedMessage, createdAccount.Address), InfoBarMessageType.Success);
2025-02-16 11:43:30 +01:00
}
2025-02-16 11:54:23 +01:00
}
2025-02-26 19:04:38 +01:00
catch (Exception ex) when (ex.Message.Contains(nameof(GmailServiceDisabledException)))
{
// For Google Workspace accounts, Gmail API might be disabled by the admin.
// Wino can't continue synchronization in this case.
// We must notify the user about this and prevent account creation.
DialogService.InfoBarMessage(Translator.GmailServiceDisabled_Title, Translator.GmailServiceDisabled_Message, InfoBarMessageType.Error);
if (createdAccount != null)
{
await AccountService.DeleteAccountAsync(createdAccount);
}
}
2025-02-16 11:54:23 +01:00
catch (AccountSetupCanceledException)
{
// Ignore
}
catch (Exception ex) when (ex.Message.Contains(nameof(AccountSetupCanceledException)))
{
// Ignore
}
catch (ImapClientPoolException testClientPoolException) when (testClientPoolException.CustomServerInformation != null)
{
var properties = testClientPoolException.CustomServerInformation.GetConnectionProperties();
properties.Add("ProtocolLog", testClientPoolException.ProtocolLog);
properties.Add("DiagnosticId", PreferencesService.DiagnosticId);
_winoLogger.TrackEvent("IMAP Test Failed", properties);
DialogService.InfoBarMessage(Translator.Info_AccountCreationFailedTitle, testClientPoolException.Message, InfoBarMessageType.Error);
}
catch (ImapClientPoolException clientPoolException) when (clientPoolException.InnerException != null)
2025-02-16 11:54:23 +01:00
{
DialogService.InfoBarMessage(Translator.Info_AccountCreationFailedTitle, clientPoolException.InnerException.Message, InfoBarMessageType.Error);
}
catch (Exception ex)
{
Log.Error(ex, "Failed to create account.");
DialogService.InfoBarMessage(Translator.Info_AccountCreationFailedTitle, ex.Message, InfoBarMessageType.Error);
// Delete account in case of failure.
if (createdAccount != null)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
await AccountService.DeleteAccountAsync(createdAccount);
2024-04-18 01:44:37 +02:00
}
}
2025-02-16 11:54:23 +01:00
finally
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
creationDialog?.Complete(false);
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
[RelayCommand]
private void EditMergedAccounts(MergedAccountProviderDetailViewModel mergedAccountProviderDetailViewModel)
{
Messenger.Send(new BreadcrumbNavigationRequested(mergedAccountProviderDetailViewModel.MergedInbox.Name,
WinoPage.MergedAccountDetailsPage,
mergedAccountProviderDetailViewModel));
}
2024-05-30 02:34:54 +02:00
2025-02-16 11:54:23 +01:00
[RelayCommand(CanExecute = nameof(CanReorderAccounts))]
private Task ReorderAccountsAsync() => MailDialogService.ShowAccountReorderDialogAsync(availableAccounts: Accounts);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public override void OnNavigatedFrom(NavigationMode mode, object parameters)
{
base.OnNavigatedFrom(mode, parameters);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
Accounts.CollectionChanged -= AccountCollectionChanged;
2024-05-30 02:34:54 +02:00
2025-02-16 11:54:23 +01:00
PropertyChanged -= PagePropertyChanged;
}
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
private void AccountCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
OnPropertyChanged(nameof(HasAccountsDefined));
OnPropertyChanged(nameof(UsedAccountsString));
OnPropertyChanged(nameof(IsAccountCreationAlmostOnLimit));
ReorderAccountsCommand.NotifyCanExecuteChanged();
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
private void PagePropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(StartupAccount) && StartupAccount != null)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
PreferencesService.StartupEntityId = StartupAccount.StartupEntityId;
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public override async void OnNavigatedTo(NavigationMode mode, object parameters)
{
base.OnNavigatedTo(mode, parameters);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
Accounts.CollectionChanged -= AccountCollectionChanged;
Accounts.CollectionChanged += AccountCollectionChanged;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
await InitializeAccountsAsync();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
PropertyChanged -= PagePropertyChanged;
PropertyChanged += PagePropertyChanged;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public override async Task InitializeAccountsAsync()
{
StartupAccount = null;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
Accounts.Clear();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var accounts = await AccountService.GetAccountsAsync().ConfigureAwait(false);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Group accounts and display merged ones at the top.
var groupedAccounts = accounts.GroupBy(a => a.MergedInboxId);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
await ExecuteUIThread(() =>
{
foreach (var accountGroup in groupedAccounts)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
var mergedInboxId = accountGroup.Key;
2025-02-16 11:43:30 +01:00
2025-02-16 11:54:23 +01:00
if (mergedInboxId == null)
{
foreach (var account in accountGroup)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
var accountDetails = GetAccountProviderDetails(account);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
Accounts.Add(accountDetails);
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
}
else
{
var mergedInbox = accountGroup.First(a => a.MergedInboxId == mergedInboxId).MergedInbox;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var holdingAccountProviderDetails = accountGroup.Select(a => GetAccountProviderDetails(a)).ToList();
var mergedAccountViewModel = new MergedAccountProviderDetailViewModel(mergedInbox, holdingAccountProviderDetails);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
Accounts.Add(mergedAccountViewModel);
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Handle startup entity.
if (PreferencesService.StartupEntityId != null)
{
StartupAccount = Accounts.FirstOrDefault(a => a.StartupEntityId == PreferencesService.StartupEntityId);
}
});
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
await ManageStorePurchasesAsync().ConfigureAwait(false);
2024-04-18 01:44:37 +02:00
}
}