Files
Wino-Mail/Wino.Services/AccountService.cs
T

846 lines
32 KiB
C#
Raw Normal View History

2024-04-18 01:44:37 +02:00
using System;
using System.Collections.Generic;
using System.Globalization;
2024-04-18 01:44:37 +02:00
using System.Linq;
using System.Threading.Tasks;
using CommunityToolkit.Diagnostics;
using CommunityToolkit.Mvvm.Messaging;
using Serilog;
using Wino.Core.Domain;
using Wino.Core.Domain.Entities.Calendar;
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.Interfaces;
using Wino.Core.Domain.Models.Accounts;
2026-03-09 00:28:10 +01:00
using Wino.Core.Domain.Misc;
using Wino.Messaging.Client.Calendar;
using Wino.Messaging.Client.Accounts;
using Wino.Messaging.UI;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
namespace Wino.Services;
public class AccountService : BaseDatabaseService, IAccountService
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
public IAuthenticator ExternalAuthenticationAuthenticator { get; set; }
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
private readonly ISignatureService _signatureService;
private readonly IAuthenticationProvider _authenticationProvider;
private readonly IMimeFileService _mimeFileService;
2025-02-16 11:54:23 +01:00
private readonly IPreferencesService _preferencesService;
private readonly IContactPictureFileService _contactPictureFileService;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
private readonly ILogger _logger = Log.ForContext<AccountService>();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public AccountService(IDatabaseService databaseService,
ISignatureService signatureService,
IAuthenticationProvider authenticationProvider,
IMimeFileService mimeFileService,
IPreferencesService preferencesService,
IContactPictureFileService contactPictureFileService) : base(databaseService)
2025-02-16 11:54:23 +01:00
{
_signatureService = signatureService;
_authenticationProvider = authenticationProvider;
_mimeFileService = mimeFileService;
2025-02-16 11:54:23 +01:00
_preferencesService = preferencesService;
_contactPictureFileService = contactPictureFileService;
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 async Task ClearAccountAttentionAsync(Guid accountId)
{
var account = await GetAccountAsync(accountId);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
Guard.IsNotNull(account);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
account.AttentionReason = AccountAttentionReason.None;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
await UpdateAccountAsync(account);
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public async Task UpdateMergedInboxAsync(Guid mergedInboxId, IEnumerable<Guid> linkedAccountIds)
{
// First, remove all accounts from merged inbox.
await Connection.ExecuteAsync("UPDATE MailAccount SET MergedInboxId = NULL WHERE MergedInboxId = ?", mergedInboxId);
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
// Then, add new accounts to merged inbox.
2025-11-15 13:29:02 +01:00
var accountIdList = linkedAccountIds.ToList();
var placeholders = string.Join(",", accountIdList.Select(_ => "?"));
var sql = $"UPDATE MailAccount SET MergedInboxId = ? WHERE Id IN ({placeholders})";
var parameters = new List<object> { mergedInboxId };
parameters.AddRange(accountIdList.Cast<object>());
2025-11-15 13:29:02 +01:00
await Connection.ExecuteAsync(sql, parameters.ToArray());
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
WeakReferenceMessenger.Default.Send(new AccountsMenuRefreshRequested());
}
public async Task<string> UpdateSyncIdentifierRawAsync(Guid accountId, string syncIdentifier)
{
await Connection.ExecuteAsync("UPDATE MailAccount SET SynchronizationDeltaIdentifier = ? WHERE Id = ?", syncIdentifier, accountId);
return syncIdentifier;
}
2025-02-16 11:54:23 +01:00
public async Task UnlinkMergedInboxAsync(Guid mergedInboxId)
{
var mergedInbox = await Connection.Table<MergedInbox>().FirstOrDefaultAsync(a => a.Id == mergedInboxId).ConfigureAwait(false);
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
if (mergedInbox == null)
2025-02-16 11:43:30 +01:00
{
2025-02-16 11:54:23 +01:00
_logger.Warning("Could not find merged inbox with id {MergedInboxId}", mergedInboxId);
2025-02-16 11:43:30 +01:00
2025-02-16 11:54:23 +01:00
return;
}
2025-11-15 13:29:02 +01:00
await Connection.ExecuteAsync("UPDATE MailAccount SET MergedInboxId = NULL WHERE MergedInboxId = ?", mergedInboxId).ConfigureAwait(false);
await Connection.DeleteAsync<MergedInbox>(mergedInbox.Id).ConfigureAwait(false);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Change the startup entity id if it was the merged inbox.
// Take the first account as startup account.
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (_preferencesService.StartupEntityId == mergedInboxId)
{
var firstAccount = await Connection.Table<MailAccount>().FirstOrDefaultAsync();
2025-02-16 11:43:30 +01:00
2025-02-16 11:54:23 +01:00
if (firstAccount != null)
2025-02-16 11:35:43 +01:00
{
2025-02-16 11:54:23 +01:00
_preferencesService.StartupEntityId = firstAccount.Id;
}
else
{
_preferencesService.StartupEntityId = null;
2024-04-18 01:44:37 +02:00
}
}
2025-02-16 11:54:23 +01:00
WeakReferenceMessenger.Default.Send(new AccountsMenuRefreshRequested());
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public async Task CreateMergeAccountsAsync(MergedInbox mergedInbox, IEnumerable<MailAccount> accountsToMerge)
{
if (mergedInbox == null) return;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// 0. Give the merged inbox a new Guid.
mergedInbox.Id = Guid.NewGuid();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var accountFolderDictionary = new Dictionary<MailAccount, List<MailItemFolder>>();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// 1. Make all folders in the accounts unsticky. We will stick them based on common special folder types.
foreach (var account in accountsToMerge)
{
var accountFolderList = new List<MailItemFolder>();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var folders = await Connection.Table<MailItemFolder>().Where(a => a.MailAccountId == account.Id).ToListAsync();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
foreach (var folder in folders)
{
accountFolderList.Add(folder);
folder.IsSticky = false;
2024-04-18 01:44:37 +02:00
2025-11-14 14:28:10 +01:00
await Connection.UpdateAsync(folder, typeof(MailItemFolder));
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
accountFolderDictionary.Add(account, accountFolderList);
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// 2. Find the common special folders and stick them.
// Only following types will be considered as common special folder.
SpecialFolderType[] commonSpecialTypes =
[
SpecialFolderType.Inbox,
SpecialFolderType.Sent,
SpecialFolderType.Draft,
SpecialFolderType.Archive,
SpecialFolderType.Junk,
SpecialFolderType.Deleted
];
foreach (var type in commonSpecialTypes)
{
var isCommonType = accountFolderDictionary
.Select(a => a.Value)
.Where(a => a.Any(a => a.SpecialFolderType == type))
.Count() == accountsToMerge.Count();
if (isCommonType)
{
foreach (var account in accountsToMerge)
2025-02-16 11:43:30 +01:00
{
2025-02-16 11:54:23 +01:00
var folder = accountFolderDictionary[account].FirstOrDefault(a => a.SpecialFolderType == type);
2025-02-16 11:43:30 +01:00
2025-02-16 11:54:23 +01:00
if (folder != null)
{
folder.IsSticky = true;
2024-04-18 01:44:37 +02:00
2025-11-14 14:28:10 +01:00
await Connection.UpdateAsync(folder, typeof(MailItemFolder));
2024-04-18 01:44:37 +02:00
}
}
}
2025-02-16 11:43:30 +01:00
}
2025-02-16 11:54:23 +01:00
// 3. Insert merged inbox and assign accounts.
2025-11-14 14:28:10 +01:00
await Connection.InsertAsync(mergedInbox, typeof(MergedInbox));
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
foreach (var account in accountsToMerge)
{
account.MergedInboxId = mergedInbox.Id;
2025-02-16 11:43:30 +01:00
2025-11-14 14:28:10 +01:00
await Connection.UpdateAsync(account, typeof(MailAccount));
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
WeakReferenceMessenger.Default.Send(new AccountsMenuRefreshRequested());
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public async Task RenameMergedAccountAsync(Guid mergedInboxId, string newName)
{
2025-11-15 13:29:02 +01:00
await Connection.ExecuteAsync("UPDATE MergedInbox SET Name = ? WHERE Id = ?", newName, mergedInboxId);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
ReportUIChange(new MergedInboxRenamed(mergedInboxId, newName));
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public async Task FixTokenIssuesAsync(Guid accountId)
{
var account = await Connection.Table<MailAccount>().FirstOrDefaultAsync(a => a.Id == accountId);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (account == null) return;
2024-04-18 01:44:37 +02:00
var authenticator = _authenticationProvider.GetAuthenticator(account.ProviderType);
// This will re-generate token with interactive authentication
// New authentication will include calendar scopes
var token = await authenticator.GenerateTokenInformationAsync(account);
2024-04-18 01:44:37 +02:00
Guard.IsNotNull(token);
2024-04-18 01:44:37 +02:00
await UpdateAccountAsync(account);
2025-02-16 11:54:23 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
private Task<MailAccountPreferences> GetAccountPreferencesAsync(Guid accountId)
=> Connection.Table<MailAccountPreferences>().FirstOrDefaultAsync(a => a.AccountId == accountId);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public async Task<List<MailAccount>> GetAccountsAsync()
{
var accounts = await Connection.Table<MailAccount>().OrderBy(a => a.Order).ToListAsync();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
foreach (var account in accounts)
2024-08-15 16:02:02 +02:00
{
2025-02-16 11:54:23 +01:00
// Load IMAP server configuration.
if (account.ProviderType == MailProviderType.IMAP4)
account.ServerInformation = await GetAccountCustomServerInformationAsync(account.Id);
2024-08-15 16:02:02 +02:00
2025-02-16 11:54:23 +01:00
// Load MergedInbox information.
if (account.MergedInboxId != null)
account.MergedInbox = await GetMergedInboxInformationAsync(account.MergedInboxId.Value);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
account.Preferences = await GetAccountPreferencesAsync(account.Id);
2025-02-16 11:43:30 +01:00
}
2025-02-16 11:54:23 +01:00
return accounts;
}
2025-02-16 11:43:30 +01:00
2026-04-16 13:45:11 +02:00
public async Task<bool> AccountNameExistsAsync(string name, Guid? excludedAccountId = null)
{
var normalizedName = name?.Trim();
if (string.IsNullOrWhiteSpace(normalizedName))
return false;
var accounts = await Connection.Table<MailAccount>().ToListAsync().ConfigureAwait(false);
return accounts.Any(account =>
account.Id != excludedAccountId &&
string.Equals(account.Name?.Trim(), normalizedName, StringComparison.OrdinalIgnoreCase));
}
public async Task<bool> AccountAddressExistsAsync(string address, Guid? excludedAccountId = null)
{
var normalizedAddress = address?.Trim();
if (string.IsNullOrWhiteSpace(normalizedAddress))
return false;
var accounts = await Connection.Table<MailAccount>().ToListAsync().ConfigureAwait(false);
return accounts.Any(account =>
account.Id != excludedAccountId &&
string.Equals(account.Address?.Trim(), normalizedAddress, StringComparison.OrdinalIgnoreCase));
}
2025-02-16 11:54:23 +01:00
public async Task CreateRootAliasAsync(Guid accountId, string address)
{
if (string.IsNullOrWhiteSpace(address))
return;
2025-02-16 11:54:23 +01:00
var rootAlias = new MailAccountAlias()
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
AccountId = accountId,
AliasAddress = address,
IsPrimary = true,
IsRootAlias = true,
IsVerified = true,
ReplyToAddress = address,
Id = Guid.NewGuid(),
Source = AliasSource.Manual,
SendCapability = AliasSendCapability.Confirmed
2025-02-16 11:54:23 +01:00
};
2025-11-14 14:28:10 +01:00
await Connection.InsertAsync(rootAlias, typeof(MailAccountAlias)).ConfigureAwait(false);
2025-02-16 11:54:23 +01:00
Log.Information("Created root alias for the account {AccountId}", accountId);
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public async Task<List<MailAccountAlias>> GetAccountAliasesAsync(Guid accountId)
{
2025-11-15 13:29:02 +01:00
return await Connection.QueryAsync<MailAccountAlias>(
"SELECT * FROM MailAccountAlias WHERE AccountId = ? ORDER BY IsRootAlias DESC, IsPrimary DESC, AliasAddress ASC",
2025-11-15 13:29:02 +01:00
accountId).ConfigureAwait(false);
2025-02-16 11:54:23 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
private Task<MergedInbox> GetMergedInboxInformationAsync(Guid mergedInboxId)
=> Connection.Table<MergedInbox>().FirstOrDefaultAsync(a => a.Id == mergedInboxId);
2024-04-18 01:44:37 +02:00
public async Task DeleteAccountMailCacheAsync(Guid accountId, AccountCacheResetReason accountCacheResetReason)
2025-02-16 11:54:23 +01:00
{
2025-11-15 13:29:02 +01:00
await Connection.ExecuteAsync(
"DELETE FROM MailCopy WHERE Id IN (SELECT Id FROM MailCopy WHERE FolderId IN (SELECT Id FROM MailItemFolder WHERE MailAccountId = ?))",
accountId);
WeakReferenceMessenger.Default.Send(new AccountCacheResetMessage(accountId, accountCacheResetReason));
}
2024-04-18 01:44:37 +02:00
public async Task DeleteAccountAsync(MailAccount account)
{
// Collect calendar entities before deletion so we can notify UI subscribers.
var accountCalendars = await Connection.Table<AccountCalendar>()
.Where(a => a.AccountId == account.Id)
.ToListAsync()
.ConfigureAwait(false);
var deletedCalendarItems = new List<CalendarItem>();
foreach (var accountCalendar in accountCalendars)
{
var calendarItems = await Connection.Table<CalendarItem>()
.Where(a => a.CalendarId == accountCalendar.Id)
.ToListAsync()
.ConfigureAwait(false);
deletedCalendarItems.AddRange(calendarItems);
}
await DeleteAccountMailCacheAsync(account.Id, AccountCacheResetReason.AccountRemoval);
2024-04-18 01:44:37 +02:00
// Delete calendar metadata and related records for this account.
foreach (var calendarItem in deletedCalendarItems)
{
await Connection.Table<CalendarEventAttendee>().DeleteAsync(a => a.CalendarItemId == calendarItem.Id).ConfigureAwait(false);
await Connection.Table<Reminder>().DeleteAsync(a => a.CalendarItemId == calendarItem.Id).ConfigureAwait(false);
await Connection.Table<CalendarAttachment>().DeleteAsync(a => a.CalendarItemId == calendarItem.Id).ConfigureAwait(false);
}
foreach (var accountCalendar in accountCalendars)
{
await Connection.Table<CalendarItem>().DeleteAsync(a => a.CalendarId == accountCalendar.Id).ConfigureAwait(false);
}
await Connection.Table<AccountCalendar>().DeleteAsync(a => a.AccountId == account.Id).ConfigureAwait(false);
2025-02-16 11:54:23 +01:00
await Connection.Table<MailItemFolder>().DeleteAsync(a => a.MailAccountId == account.Id);
await Connection.Table<AccountSignature>().DeleteAsync(a => a.MailAccountId == account.Id);
await Connection.Table<MailAccountAlias>().DeleteAsync(a => a.AccountId == account.Id);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Account belongs to a merged inbox.
// In case of there'll be a single account in the merged inbox, remove the merged inbox as well.
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (account.MergedInboxId != null)
{
var mergedInboxAccountCount = await Connection.Table<MailAccount>().Where(a => a.MergedInboxId == account.MergedInboxId.Value).CountAsync();
2025-02-16 11:43:30 +01:00
2025-02-16 11:54:23 +01:00
// There will be only one account in the merged inbox. Remove the link for the other account as well.
if (mergedInboxAccountCount == 2)
2024-04-18 01:44:37 +02:00
{
2025-11-15 13:29:02 +01:00
await Connection.ExecuteAsync(
"UPDATE MailAccount SET MergedInboxId = NULL WHERE MergedInboxId = ?",
account.MergedInboxId.Value).ConfigureAwait(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
if (account.ProviderType == MailProviderType.IMAP4)
await Connection.Table<CustomServerInformation>().DeleteAsync(a => a.AccountId == account.Id);
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
if (account.Preferences != null)
await Connection.DeleteAsync<MailAccountPreferences>(account.Preferences.Id);
2025-02-15 12:53:32 +01:00
await Connection.DeleteAsync<MailAccount>(account.Id);
2025-02-16 11:54:23 +01:00
await _mimeFileService.DeleteUserMimeCacheAsync(account.Id).ConfigureAwait(false);
2025-02-16 11:54:23 +01:00
// Clear out or set up a new startup entity id.
// Next account after the deleted one will be the startup account.
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (_preferencesService.StartupEntityId == account.Id || _preferencesService.StartupEntityId == account.MergedInboxId)
{
2025-02-16 11:54:23 +01:00
var firstNonStartupAccount = await Connection.Table<MailAccount>().FirstOrDefaultAsync(a => a.Id != account.Id);
2025-02-16 11:54:23 +01:00
if (firstNonStartupAccount != null)
{
_preferencesService.StartupEntityId = firstNonStartupAccount.Id;
}
else
{
2025-02-16 11:54:23 +01:00
_preferencesService.StartupEntityId = null;
}
}
foreach (var calendarItem in deletedCalendarItems)
{
WeakReferenceMessenger.Default.Send(new CalendarItemDeleted(calendarItem, EntityUpdateSource.Server));
}
foreach (var accountCalendar in accountCalendars)
{
WeakReferenceMessenger.Default.Send(new CalendarListDeleted(accountCalendar));
}
2025-02-16 11:54:23 +01:00
ReportUIChange(new AccountRemovedMessage(account));
}
2024-08-23 02:07:25 +02:00
2025-02-16 11:54:23 +01:00
public async Task UpdateProfileInformationAsync(Guid accountId, ProfileInformation profileInformation)
{
var account = await GetAccountAsync(accountId).ConfigureAwait(false);
2025-02-16 11:54:23 +01:00
if (account != null)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
account.SenderName = profileInformation.SenderName;
account.Base64ProfilePictureData = profileInformation.Base64ProfilePictureData;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (string.IsNullOrEmpty(account.Address))
2024-05-30 02:34:54 +02:00
{
2025-02-16 11:54:23 +01:00
account.Address = profileInformation.AccountAddress;
2024-05-30 02:34:54 +02:00
}
2025-02-16 11:54:23 +01:00
// Forcefully add or update a contact data with the provided information.
2024-04-18 01:44:37 +02:00
var existingContact = await Connection.Table<AccountContact>()
.FirstOrDefaultAsync(a => a.Address == account.Address)
.ConfigureAwait(false);
var contactPictureFileId = await SaveProfilePictureAsync(
account.Base64ProfilePictureData,
existingContact?.ContactPictureFileId).ConfigureAwait(false);
2025-02-16 11:54:23 +01:00
var accountContact = new AccountContact()
{
Address = account.Address,
Name = account.SenderName,
ContactPictureFileId = contactPictureFileId,
2025-02-16 11:54:23 +01:00
IsRootContact = true
};
2024-04-18 01:44:37 +02:00
2025-11-14 14:28:10 +01:00
await Connection.InsertOrReplaceAsync(accountContact, typeof(AccountContact)).ConfigureAwait(false);
2025-02-16 11:43:30 +01:00
2025-02-16 11:54:23 +01:00
await UpdateAccountAsync(account).ConfigureAwait(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
private async Task<Guid?> SaveProfilePictureAsync(string base64ProfilePictureData, Guid? existingFileId)
{
if (string.IsNullOrWhiteSpace(base64ProfilePictureData))
{
if (existingFileId.HasValue)
await _contactPictureFileService.DeleteContactPictureAsync(existingFileId.Value).ConfigureAwait(false);
return null;
}
byte[] bytes;
try
{
bytes = Convert.FromBase64String(base64ProfilePictureData);
}
catch (FormatException ex)
{
_logger.Warning(ex, "Failed to decode account profile picture for contact migration.");
return existingFileId;
}
var newFileId = await _contactPictureFileService.SaveContactPictureAsync(bytes).ConfigureAwait(false);
if (existingFileId.HasValue)
await _contactPictureFileService.DeleteContactPictureAsync(existingFileId.Value).ConfigureAwait(false);
return newFileId;
}
2025-02-16 11:54:23 +01:00
public async Task<MailAccount> GetAccountAsync(Guid accountId)
{
var account = await Connection.Table<MailAccount>().FirstOrDefaultAsync(a => a.Id == accountId);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (account == null)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
_logger.Error("Could not find account with id {AccountId}", accountId);
2025-02-16 11:35:43 +01:00
}
2025-02-16 11:54:23 +01:00
else
2025-02-16 11:43:30 +01:00
{
2025-02-16 11:54:23 +01:00
if (account.ProviderType == MailProviderType.IMAP4)
account.ServerInformation = await GetAccountCustomServerInformationAsync(account.Id);
2024-08-17 19:54:52 +02:00
2025-02-16 11:54:23 +01:00
account.Preferences = await GetAccountPreferencesAsync(account.Id);
2024-08-17 19:54:52 +02:00
2025-02-16 11:54:23 +01:00
return account;
}
2024-08-17 19:54:52 +02:00
2025-02-16 11:54:23 +01:00
return null;
}
2024-08-17 19:54:52 +02:00
2025-02-16 11:54:23 +01:00
public Task<CustomServerInformation> GetAccountCustomServerInformationAsync(Guid accountId)
=> Connection.Table<CustomServerInformation>().FirstOrDefaultAsync(a => a.AccountId == accountId);
2024-08-17 19:54:52 +02:00
2025-02-16 11:54:23 +01:00
public async Task UpdateAccountAsync(MailAccount account)
{
2025-11-14 14:28:10 +01:00
await Connection.UpdateAsync(account.Preferences, typeof(MailAccountPreferences)).ConfigureAwait(false);
await Connection.UpdateAsync(account, typeof(MailAccount)).ConfigureAwait(false);
2024-08-17 19:54:52 +02:00
2025-02-16 11:54:23 +01:00
ReportUIChange(new AccountUpdatedMessage(account));
}
2024-08-17 19:54:52 +02:00
public async Task UpdateAccountCustomServerInformationAsync(CustomServerInformation customServerInformation)
{
await Connection.InsertOrReplaceAsync(customServerInformation, typeof(CustomServerInformation)).ConfigureAwait(false);
}
2025-02-16 11:54:23 +01:00
public async Task UpdateAccountAliasesAsync(Guid accountId, List<MailAccountAlias> aliases)
{
// Delete existing ones.
await Connection.Table<MailAccountAlias>().DeleteAsync(a => a.AccountId == accountId).ConfigureAwait(false);
2024-08-17 19:54:52 +02:00
2025-02-16 11:54:23 +01:00
// Insert new ones.
foreach (var alias in aliases)
{
2025-11-14 14:28:10 +01:00
await Connection.InsertAsync(alias, typeof(MailAccountAlias)).ConfigureAwait(false);
2025-02-16 11:54:23 +01:00
}
}
2024-08-17 19:54:52 +02:00
2025-02-16 11:54:23 +01:00
public async Task UpdateRemoteAliasInformationAsync(MailAccount account, List<RemoteAccountAlias> remoteAccountAliases)
{
var localAliases = await GetAccountAliasesAsync(account.Id).ConfigureAwait(false);
var normalizedRemoteAliases = remoteAccountAliases ?? [];
2025-02-16 11:43:30 +01:00
foreach (var remoteAlias in normalizedRemoteAliases)
2025-02-16 11:54:23 +01:00
{
if (string.IsNullOrWhiteSpace(remoteAlias?.AliasAddress))
continue;
var existingAlias = localAliases.Find(a =>
a.AccountId == account.Id &&
a.AliasAddress.Equals(remoteAlias.AliasAddress, StringComparison.OrdinalIgnoreCase));
2025-02-16 11:43:30 +01:00
2025-02-16 11:54:23 +01:00
if (existingAlias == null)
2025-02-16 11:35:43 +01:00
{
2025-02-16 11:54:23 +01:00
// Create new alias.
var newAlias = new MailAccountAlias()
{
AccountId = account.Id,
AliasAddress = remoteAlias.AliasAddress,
IsPrimary = remoteAlias.IsPrimary,
IsVerified = remoteAlias.IsVerified,
ReplyToAddress = remoteAlias.ReplyToAddress,
Id = Guid.NewGuid(),
IsRootAlias = remoteAlias.IsRootAlias,
AliasSenderName = remoteAlias.AliasSenderName,
Source = remoteAlias.Source,
SendCapability = remoteAlias.SendCapability
2025-02-16 11:54:23 +01:00
};
2024-08-17 19:54:52 +02:00
2025-11-14 14:28:10 +01:00
await Connection.InsertAsync(newAlias, typeof(MailAccountAlias));
2025-02-16 11:54:23 +01:00
localAliases.Add(newAlias);
}
else
{
// Update existing alias.
existingAlias.IsPrimary = remoteAlias.IsPrimary;
existingAlias.IsVerified = remoteAlias.IsVerified;
existingAlias.ReplyToAddress = remoteAlias.ReplyToAddress;
existingAlias.AliasSenderName = remoteAlias.AliasSenderName;
existingAlias.Source = remoteAlias.Source;
existingAlias.SendCapability = remoteAlias.SendCapability;
2025-02-16 11:43:30 +01:00
2025-11-14 14:28:10 +01:00
await Connection.UpdateAsync(existingAlias, typeof(MailAccountAlias));
2024-08-17 19:54:52 +02:00
}
}
2026-04-15 13:07:58 +02:00
if (localAliases.Count == 0 && !string.IsNullOrWhiteSpace(account.Address))
{
var fallbackAddress = account.Address.Trim();
var fallbackAlias = new MailAccountAlias()
{
AccountId = account.Id,
AliasAddress = fallbackAddress,
IsPrimary = true,
IsRootAlias = true,
IsVerified = true,
ReplyToAddress = fallbackAddress,
Id = Guid.NewGuid(),
Source = AliasSource.ProviderDiscovered,
SendCapability = AliasSendCapability.Confirmed
};
await Connection.InsertAsync(fallbackAlias, typeof(MailAccountAlias)).ConfigureAwait(false);
localAliases.Add(fallbackAlias);
}
2025-02-16 11:54:23 +01:00
// Make sure there is only 1 root alias and 1 primary alias selected.
bool shouldUpdatePrimary = localAliases.Count(a => a.IsPrimary) != 1;
bool shouldUpdateRoot = localAliases.Count(a => a.IsRootAlias) != 1;
if (shouldUpdatePrimary)
{
2025-02-16 11:54:23 +01:00
localAliases.ForEach(a => a.IsPrimary = false);
2025-02-16 11:54:23 +01:00
var idealPrimaryAlias = localAliases.Find(a => a.AliasAddress == account.Address) ?? localAliases.First();
2025-02-16 11:54:23 +01:00
idealPrimaryAlias.IsPrimary = true;
2025-11-14 14:28:10 +01:00
await Connection.UpdateAsync(idealPrimaryAlias, typeof(MailAccountAlias)).ConfigureAwait(false);
}
2025-02-16 11:54:23 +01:00
if (shouldUpdateRoot)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
localAliases.ForEach(a => a.IsRootAlias = false);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var idealRootAlias = localAliases.Find(a => a.AliasAddress == account.Address) ?? localAliases.First();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
idealRootAlias.IsRootAlias = true;
2025-11-14 14:28:10 +01:00
await Connection.UpdateAsync(idealRootAlias, typeof(MailAccountAlias)).ConfigureAwait(false);
2025-02-16 11:54:23 +01:00
}
}
2024-04-18 01:44:37 +02:00
public async Task UpdateAliasSendCapabilityAsync(Guid accountId, string aliasAddress, AliasSendCapability capability)
{
if (string.IsNullOrWhiteSpace(aliasAddress))
return;
var aliases = await GetAccountAliasesAsync(accountId).ConfigureAwait(false);
var alias = aliases.FirstOrDefault(a => a.AliasAddress.Equals(aliasAddress, StringComparison.OrdinalIgnoreCase));
if (alias == null)
return;
alias.SendCapability = capability;
await Connection.UpdateAsync(alias, typeof(MailAccountAlias)).ConfigureAwait(false);
}
2025-02-16 11:54:23 +01:00
public async Task DeleteAccountAliasAsync(Guid aliasId)
{
// Create query to delete alias.
2024-04-18 01:44:37 +02:00
2025-11-15 13:29:02 +01:00
await Connection.ExecuteAsync("DELETE FROM MailAccountAlias WHERE Id = ?", aliasId).ConfigureAwait(false);
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 async Task CreateAccountAsync(MailAccount account, CustomServerInformation customServerInformation)
{
Guard.IsNotNull(account);
2024-04-18 01:44:37 +02:00
2026-04-16 13:45:11 +02:00
if (await AccountNameExistsAsync(account.Name).ConfigureAwait(false))
throw new InvalidOperationException(Translator.DialogMessage_AccountNameExistsMessage);
if (await AccountAddressExistsAsync(account.Address).ConfigureAwait(false))
throw new InvalidOperationException(Translator.DialogMessage_AccountAddressExistsMessage);
2026-04-14 00:03:48 +02:00
if (!account.CreatedAt.HasValue)
{
account.CreatedAt = DateTime.UtcNow;
}
2025-02-16 11:54:23 +01:00
var accountCount = await Connection.Table<MailAccount>().CountAsync();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// If there are no accounts before this one, set it as startup account.
if (accountCount == 0)
{
_preferencesService.StartupEntityId = account.Id;
}
else
{
// Set the order of the account.
// This can be changed by the user later in manage accounts page.
account.Order = accountCount;
}
2025-11-14 14:28:10 +01:00
await Connection.InsertAsync(account, typeof(MailAccount));
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var preferences = new MailAccountPreferences()
{
Id = Guid.NewGuid(),
AccountId = account.Id,
IsNotificationsEnabled = true,
ShouldAppendMessagesToSentFolder = false
};
2024-04-18 01:44:37 +02:00
// iCloud does not appends sent messages to sent folder automatically.
if (account.SpecialImapProvider == SpecialImapProvider.iCloud || account.SpecialImapProvider == SpecialImapProvider.Yahoo)
{
preferences.ShouldAppendMessagesToSentFolder = true;
}
2025-02-16 11:54:23 +01:00
account.Preferences = preferences;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Outlook & Office 365 supports Focused inbox. Enabled by default.
bool isMicrosoftProvider = account.ProviderType == MailProviderType.Outlook;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// TODO: This should come from account settings API.
// Wino doesn't have MailboxSettings yet.
if (isMicrosoftProvider)
account.Preferences.IsFocusedInboxEnabled = true;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Setup default signature.
var defaultSignature = await _signatureService.CreateDefaultSignatureAsync(account.Id);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
account.Preferences.SignatureIdForNewMessages = defaultSignature.Id;
account.Preferences.SignatureIdForFollowingMessages = defaultSignature.Id;
account.Preferences.IsSignatureEnabled = true;
2024-04-18 01:44:37 +02:00
2025-11-14 14:28:10 +01:00
await Connection.InsertAsync(preferences, typeof(MailAccountPreferences));
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (customServerInformation != null)
2025-11-14 14:28:10 +01:00
await Connection.InsertAsync(customServerInformation, typeof(CustomServerInformation));
2024-04-18 01:44:37 +02:00
if (account.ProviderType == MailProviderType.IMAP4 &&
customServerInformation?.CalendarSupportMode == ImapCalendarSupportMode.LocalOnly)
{
await EnsureDefaultLocalCalendarForImapAsync(account.Id).ConfigureAwait(false);
}
}
2024-05-30 02:34:54 +02:00
private async Task EnsureDefaultLocalCalendarForImapAsync(Guid accountId)
{
var existingCalendarCount = await Connection.Table<AccountCalendar>()
.Where(a => a.AccountId == accountId)
.CountAsync()
.ConfigureAwait(false);
if (existingCalendarCount > 0)
return;
2024-08-17 22:55:58 +02:00
var localCalendar = new AccountCalendar
{
Id = Guid.NewGuid(),
AccountId = accountId,
Name = Translator.AccountDetailsPage_TabCalendar,
IsPrimary = true,
IsSynchronizationEnabled = true,
IsExtended = true,
RemoteCalendarId = string.Empty,
TimeZone = string.Empty,
BackgroundColorHex = await GetNextDistinctCalendarColorAsync().ConfigureAwait(false)
};
2025-02-16 11:35:43 +01:00
localCalendar.TextColorHex = GetReadableTextColorHex(localCalendar.BackgroundColorHex);
await Connection.InsertAsync(localCalendar, typeof(AccountCalendar)).ConfigureAwait(false);
}
2024-08-17 22:55:58 +02:00
2026-03-09 00:28:10 +01:00
private async Task<string> GetNextDistinctCalendarColorAsync()
{
2026-03-09 00:28:10 +01:00
var usedColors = await Connection.Table<AccountCalendar>()
.ToListAsync()
.ConfigureAwait(false);
2025-02-16 11:35:43 +01:00
2026-03-09 00:28:10 +01:00
return CalendarColorPalette.GetDistinctColor(usedColors.Select(a => a.BackgroundColorHex));
}
2025-02-16 11:54:23 +01:00
private static string GetReadableTextColorHex(string backgroundColorHex)
{
if (!TryParseHexColor(backgroundColorHex, out var red, out var green, out var blue))
return "#FFFFFF";
var luminance = ((0.299 * red) + (0.587 * green) + (0.114 * blue)) / 255d;
return luminance > 0.6 ? "#111111" : "#FFFFFF";
}
private static bool TryParseHexColor(string value, out int red, out int green, out int blue)
{
red = 255;
green = 255;
blue = 255;
if (string.IsNullOrWhiteSpace(value))
return false;
var color = value.Trim();
if (color.StartsWith('#'))
{
color = color[1..];
}
if (color.Length != 6 ||
!int.TryParse(color, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out _))
{
return false;
}
red = Convert.ToInt32(color.Substring(0, 2), 16);
green = Convert.ToInt32(color.Substring(2, 2), 16);
blue = Convert.ToInt32(color.Substring(4, 2), 16);
return true;
}
2025-02-16 11:54:23 +01:00
public async Task UpdateAccountOrdersAsync(Dictionary<Guid, int> accountIdOrderPair)
{
foreach (var pair in accountIdOrderPair)
2025-02-16 11:43:30 +01:00
{
2025-02-16 11:54:23 +01:00
var account = await GetAccountAsync(pair.Key);
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
if (account == null)
{
_logger.Information("Could not find account with id {Key} for reordering. It may be a linked account.", pair.Key);
continue;
}
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
account.Order = pair.Value;
2025-02-16 11:35:43 +01:00
2025-11-14 14:28:10 +01:00
await Connection.UpdateAsync(account, typeof(MailAccount));
2025-02-16 11:43:30 +01:00
}
2025-02-16 11:54:23 +01:00
Messenger.Send(new AccountMenuItemsReordered(accountIdOrderPair));
}
public async Task<MailAccountAlias> GetPrimaryAccountAliasAsync(Guid accountId)
{
var aliases = await GetAccountAliasesAsync(accountId);
if (aliases == null || aliases.Count == 0) return null;
return aliases.FirstOrDefault(a => a.IsPrimary) ?? aliases.First();
}
public async Task<bool> IsAccountFocusedEnabledAsync(Guid accountId)
{
var account = await GetAccountAsync(accountId);
return account.Preferences.IsFocusedInboxEnabled.GetValueOrDefault();
2024-04-18 01:44:37 +02:00
}
public async Task<bool> IsNotificationsEnabled(Guid accountId)
{
var account = await GetAccountAsync(accountId);
return account?.Preferences?.IsNotificationsEnabled ?? false;
}
public async Task UpdateLastFolderStructureSyncDateAsync(Guid accountId)
{
var account = await GetAccountAsync(accountId);
if (account == null) return;
account.LastFolderStructureSyncDate = DateTime.UtcNow;
await Connection.UpdateAsync(account, typeof(MailAccount)).ConfigureAwait(false);
}
public async Task<bool> ShouldSyncFolderStructureAsync(Guid accountId, TimeSpan syncInterval)
{
var account = await GetAccountAsync(accountId);
if (account == null) return true;
if (!account.LastFolderStructureSyncDate.HasValue)
return true;
return DateTime.UtcNow - account.LastFolderStructureSyncDate.Value > syncInterval;
}
2024-04-18 01:44:37 +02:00
}