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

81 lines
2.8 KiB
C#
Raw Normal View History

2025-02-15 12:53:32 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MailKit;
using MailKit.Net.Imap;
using MailKit.Search;
using Wino.Core.Domain.Entities.Mail;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Integration;
2025-02-16 11:35:43 +01:00
namespace Wino.Core.Synchronizers.ImapSync;
/// <summary>
/// Uid based IMAP Synchronization strategy.
/// </summary>
internal class UidBasedSynchronizer : ImapSynchronizationStrategyBase
2025-02-15 12:53:32 +01:00
{
2025-02-16 11:35:43 +01:00
public UidBasedSynchronizer(IFolderService folderService, Domain.Interfaces.IMailService mailService) : base(folderService, mailService)
2025-02-15 12:53:32 +01:00
{
2025-02-16 11:35:43 +01:00
}
2025-02-15 12:53:32 +01:00
2025-02-16 11:35:43 +01:00
public override async Task<List<string>> HandleSynchronizationAsync(IImapClient client, MailItemFolder folder, IImapSynchronizer synchronizer, CancellationToken cancellationToken = default)
{
if (client is not WinoImapClient winoClient)
throw new ArgumentException("Client must be of type WinoImapClient.", nameof(client));
2025-02-15 12:53:32 +01:00
2025-02-16 11:35:43 +01:00
Folder = folder;
2025-02-15 12:53:32 +01:00
2025-02-16 11:35:43 +01:00
var downloadedMessageIds = new List<string>();
IMailFolder remoteFolder = null;
2025-02-15 12:53:32 +01:00
2025-02-16 11:35:43 +01:00
try
{
remoteFolder = await winoClient.GetFolderAsync(folder.RemoteFolderId, cancellationToken).ConfigureAwait(false);
2025-02-15 12:53:32 +01:00
2025-02-16 11:35:43 +01:00
await remoteFolder.OpenAsync(FolderAccess.ReadOnly, cancellationToken).ConfigureAwait(false);
2025-02-15 12:53:32 +01:00
2025-02-16 11:35:43 +01:00
// Fetch UIDs from the remote folder
var remoteUids = await remoteFolder.SearchAsync(SearchQuery.All, cancellationToken).ConfigureAwait(false);
2025-02-15 12:53:32 +01:00
2025-02-16 11:35:43 +01:00
remoteUids = remoteUids.OrderByDescending(a => a.Id).Take((int)synchronizer.InitialMessageDownloadCountPerFolder).ToList();
2025-02-15 12:53:32 +01:00
2025-02-16 11:35:43 +01:00
await HandleChangedUIdsAsync(synchronizer, remoteFolder, remoteUids, cancellationToken).ConfigureAwait(false);
await ManageUUIdBasedDeletedMessagesAsync(folder, remoteFolder, cancellationToken).ConfigureAwait(false);
}
catch (FolderNotFoundException)
{
await FolderService.DeleteFolderAsync(folder.MailAccountId, folder.RemoteFolderId).ConfigureAwait(false);
2025-02-15 12:53:32 +01:00
2025-02-16 11:35:43 +01:00
return default;
}
catch (Exception)
{
2025-02-15 12:53:32 +01:00
2025-02-16 11:35:43 +01:00
throw;
}
finally
{
if (!cancellationToken.IsCancellationRequested)
2025-02-15 12:53:32 +01:00
{
2025-02-16 11:35:43 +01:00
if (remoteFolder != null)
2025-02-15 12:53:32 +01:00
{
2025-02-16 11:35:43 +01:00
if (remoteFolder.IsOpen)
2025-02-15 12:53:32 +01:00
{
2025-02-16 11:35:43 +01:00
await remoteFolder.CloseAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
2025-02-15 12:53:32 +01:00
}
}
}
}
2025-02-16 11:35:43 +01:00
return downloadedMessageIds;
}
internal override Task<IList<UniqueId>> GetChangedUidsAsync(IImapClient client, IMailFolder remoteFolder, IImapSynchronizer synchronizer, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
2025-02-15 12:53:32 +01:00
}
}