Files
Wino-Mail/Wino.Core/Synchronizers/ImapSync/ImapSynchronizationStrategyBase.cs

187 lines
7.6 KiB
C#
Raw Normal View History

2025-02-15 12:53:32 +01:00
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MailKit;
using MailKit.Net.Imap;
using MailKit.Search;
using MoreLinq;
using Serilog;
using Wino.Core.Domain.Entities.Mail;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.MailItem;
using Wino.Services.Extensions;
using IMailService = Wino.Core.Domain.Interfaces.IMailService;
2025-02-16 11:54:23 +01:00
namespace Wino.Core.Synchronizers.ImapSync;
public abstract class ImapSynchronizationStrategyBase : IImapSynchronizerStrategy
2025-02-15 12:53:32 +01:00
{
2025-02-16 11:54:23 +01:00
// Minimum summary items to Fetch for mail synchronization from IMAP.
protected readonly MessageSummaryItems MailSynchronizationFlags =
MessageSummaryItems.Flags |
MessageSummaryItems.UniqueId |
MessageSummaryItems.ThreadId |
MessageSummaryItems.EmailId |
MessageSummaryItems.Headers |
MessageSummaryItems.PreviewText |
MessageSummaryItems.GMailThreadId |
MessageSummaryItems.References |
MessageSummaryItems.ModSeq;
protected IFolderService FolderService { get; }
protected IMailService MailService { get; }
protected MailItemFolder Folder { get; set; }
protected ImapSynchronizationStrategyBase(IFolderService folderService, IMailService mailService)
2025-02-15 12:53:32 +01:00
{
2025-02-16 11:54:23 +01:00
FolderService = folderService;
MailService = mailService;
}
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
public abstract Task<List<string>> HandleSynchronizationAsync(IImapClient client, MailItemFolder folder, IImapSynchronizer synchronizer, CancellationToken cancellationToken = default);
internal abstract Task<IList<UniqueId>> GetChangedUidsAsync(IImapClient client, IMailFolder remoteFolder, IImapSynchronizer synchronizer, CancellationToken cancellationToken = default);
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
protected async Task<List<string>> HandleChangedUIdsAsync(IImapSynchronizer synchronizer, IMailFolder remoteFolder, IList<UniqueId> changedUids, CancellationToken cancellationToken)
{
List<string> downloadedMessageIds = new();
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
var existingMails = await MailService.GetExistingMailsAsync(Folder.Id, changedUids).ConfigureAwait(false);
var existingMailUids = existingMails.Select(m => MailkitClientExtensions.ResolveUidStruct(m.Id)).ToArray();
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
// These are the non-existing mails. They will be downloaded + processed.
var newMessageIds = changedUids.Except(existingMailUids).ToList();
var deletedMessageIds = existingMailUids.Except(changedUids).ToList();
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
// Fetch minimum data for the existing mails in one query.
var existingFlagData = await remoteFolder.FetchAsync(existingMailUids, MessageSummaryItems.Flags | MessageSummaryItems.UniqueId).ConfigureAwait(false);
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
foreach (var update in existingFlagData)
{
if (update.UniqueId == UniqueId.Invalid)
2025-02-16 11:35:43 +01:00
{
2025-02-16 11:54:23 +01:00
Log.Warning($"Couldn't fetch UniqueId for the mail. FetchAsync failed.");
continue;
}
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
if (update.Flags == null)
{
Log.Warning($"Couldn't fetch flags for the mail with UID {update.UniqueId.Id}. FetchAsync failed.");
continue;
}
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
var existingMail = existingMails.FirstOrDefault(m => MailkitClientExtensions.ResolveUidStruct(m.Id).Id == update.UniqueId.Id);
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
if (existingMail == null)
{
Log.Warning($"Couldn't find the mail with UID {update.UniqueId.Id} in the local database. Flag update is ignored.");
continue;
}
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
await HandleMessageFlagsChangeAsync(existingMail, update.Flags.Value).ConfigureAwait(false);
}
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
// Fetch the new mails in batch.
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
var batchedMessageIds = newMessageIds.Batch(50);
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
foreach (var group in batchedMessageIds)
{
var uniqueIdSet = new UniqueIdSet(group, SortOrder.Ascending);
var summaries = await remoteFolder.FetchAsync(uniqueIdSet, MailSynchronizationFlags, cancellationToken).ConfigureAwait(false);
2025-02-16 11:54:23 +01:00
foreach (var summary in summaries)
{
var mimeMessage = await remoteFolder.GetMessageAsync(summary.UniqueId, cancellationToken).ConfigureAwait(false);
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
var creationPackage = new ImapMessageCreationPackage(summary, mimeMessage);
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
var mailPackages = await synchronizer.CreateNewMailPackagesAsync(creationPackage, Folder, cancellationToken).ConfigureAwait(false);
2025-02-16 11:54:23 +01:00
if (mailPackages != null)
{
foreach (var package in mailPackages)
2025-02-15 12:53:32 +01:00
{
2025-02-16 11:54:23 +01:00
// Local draft is mapped. We don't need to create a new mail copy.
if (package == null) continue;
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
bool isCreatedNew = await MailService.CreateMailAsync(Folder.MailAccountId, package).ConfigureAwait(false);
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
// This is upsert. We are not interested in updated mails.
if (isCreatedNew) downloadedMessageIds.Add(package.Copy.Id);
2025-02-15 12:53:32 +01:00
}
}
}
}
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
return downloadedMessageIds;
}
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
protected async Task HandleMessageFlagsChangeAsync(UniqueId? uniqueId, MessageFlags flags)
{
if (Folder == null) return;
if (uniqueId == null) return;
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
var localMailCopyId = MailkitClientExtensions.CreateUid(Folder.Id, uniqueId.Value.Id);
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
var isFlagged = MailkitClientExtensions.GetIsFlagged(flags);
var isRead = MailkitClientExtensions.GetIsRead(flags);
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
await MailService.ChangeReadStatusAsync(localMailCopyId, isRead).ConfigureAwait(false);
await MailService.ChangeFlagStatusAsync(localMailCopyId, isFlagged).ConfigureAwait(false);
}
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
protected async Task HandleMessageFlagsChangeAsync(MailCopy mailCopy, MessageFlags flags)
{
if (mailCopy == null) return;
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
var isFlagged = MailkitClientExtensions.GetIsFlagged(flags);
var isRead = MailkitClientExtensions.GetIsRead(flags);
if (isFlagged != mailCopy.IsFlagged)
{
await MailService.ChangeFlagStatusAsync(mailCopy.Id, isFlagged).ConfigureAwait(false);
2025-02-16 11:35:43 +01:00
}
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
if (isRead != mailCopy.IsRead)
2025-02-16 11:35:43 +01:00
{
2025-02-16 11:54:23 +01:00
await MailService.ChangeReadStatusAsync(mailCopy.Id, isRead).ConfigureAwait(false);
}
}
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
protected async Task HandleMessageDeletedAsync(IList<UniqueId> uniqueIds)
{
if (Folder == null) return;
if (uniqueIds == null || uniqueIds.Count == 0) return;
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
foreach (var uniqueId in uniqueIds)
{
if (uniqueId == null) continue;
var localMailCopyId = MailkitClientExtensions.CreateUid(Folder.Id, uniqueId.Id);
await MailService.DeleteMailAsync(Folder.MailAccountId, localMailCopyId).ConfigureAwait(false);
}
2025-02-16 11:54:23 +01:00
}
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
protected void OnMessagesVanished(object sender, MessagesVanishedEventArgs args)
=> HandleMessageDeletedAsync(args.UniqueIds).ConfigureAwait(false);
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
protected void OnMessageFlagsChanged(object sender, MessageFlagsChangedEventArgs args)
=> HandleMessageFlagsChangeAsync(args.UniqueId, args.Flags).ConfigureAwait(false);
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
protected async Task ManageUUIdBasedDeletedMessagesAsync(MailItemFolder localFolder, IMailFolder remoteFolder, CancellationToken cancellationToken = default)
{
var allUids = (await FolderService.GetKnownUidsForFolderAsync(localFolder.Id)).Select(a => new UniqueId(a)).ToList();
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
if (allUids.Count > 0)
{
var remoteAllUids = await remoteFolder.SearchAsync(SearchQuery.All, cancellationToken);
var deletedUids = allUids.Except(remoteAllUids).ToList();
2025-02-16 11:54:23 +01:00
await HandleMessageDeletedAsync(deletedUids).ConfigureAwait(false);
2025-02-15 12:53:32 +01:00
}
}
}