Prevent downloading existing messages for Outlook.

This commit is contained in:
Burak Kaan Köse
2024-05-25 17:00:52 +02:00
parent b66557f3be
commit 23d0eeab16
7 changed files with 64 additions and 18 deletions

View File

@@ -77,5 +77,13 @@ namespace Wino.Core.Domain.Interfaces
/// <param name="uniqueMailId">Unique id of the mail item.</param>
/// <returns>Account that mail belongs to.</returns>
Task<MailAccount> GetMailAccountByUniqueIdAsync(Guid uniqueMailId);
/// <summary>
/// Checks whether the given mail copy id exists in the database.
/// Safely used for Outlook to prevent downloading the same mail twice.
/// For Gmail, it should be avoided since one mail may belong to multiple folders.
/// </summary>
/// <param name="mailCopyId">Native mail id of the message.</param>
Task<bool> IsMailExistsAsync(string mailCopyId);
}
}

View File

@@ -23,6 +23,7 @@ namespace Wino.Core
services.AddSingleton<IMimeFileService, MimeFileService>();
services.AddTransient<IDefaultChangeProcessor, DefaultChangeProcessor>();
services.AddTransient<IOutlookChangeProcessor, OutlookChangeProcessor>();
services.AddTransient<ITokenService, TokenService>();
services.AddTransient<IProviderService, ProviderService>();
services.AddTransient<IFolderService, FolderService>();

View File

@@ -55,7 +55,13 @@ namespace Wino.Core.Integration.Processors
public interface IOutlookChangeProcessor : IDefaultChangeProcessor
{
/// <summary>
/// Interrupted initial synchronization may cause downloaded mails to be saved in the database twice.
/// Since downloading mime is costly in Outlook, we need to check if the actual copy of the message has been saved before.
/// </summary>
/// <param name="messageId">MailCopyId of the message.</param>
/// <returns>Whether the mime has b</returns>
Task<bool> IsMailExistsAsync(string messageId);
}
public class DefaultChangeProcessor(IDatabaseService databaseService,
@@ -64,8 +70,9 @@ namespace Wino.Core.Integration.Processors
IAccountService accountService,
IMimeFileService mimeFileService) : BaseDatabaseService(databaseService), IDefaultChangeProcessor
{
protected IMailService MailService = mailService;
private readonly IFolderService _folderService = folderService;
private readonly IMailService _mailService = mailService;
private readonly IAccountService _accountService = accountService;
private readonly IMimeFileService _mimeFileService = mimeFileService;
@@ -73,32 +80,32 @@ namespace Wino.Core.Integration.Processors
=> _accountService.UpdateSynchronizationIdentifierAsync(accountId, synchronizationDeltaIdentifier);
public Task ChangeFlagStatusAsync(string mailCopyId, bool isFlagged)
=> _mailService.ChangeFlagStatusAsync(mailCopyId, isFlagged);
=> MailService.ChangeFlagStatusAsync(mailCopyId, isFlagged);
public Task ChangeMailReadStatusAsync(string mailCopyId, bool isRead)
=> _mailService.ChangeReadStatusAsync(mailCopyId, isRead);
=> MailService.ChangeReadStatusAsync(mailCopyId, isRead);
public Task DeleteAssignmentAsync(Guid accountId, string mailCopyId, string remoteFolderId)
=> _mailService.DeleteAssignmentAsync(accountId, mailCopyId, remoteFolderId);
=> MailService.DeleteAssignmentAsync(accountId, mailCopyId, remoteFolderId);
public Task CreateAssignmentAsync(Guid accountId, string mailCopyId, string remoteFolderId)
=> _mailService.CreateAssignmentAsync(accountId, mailCopyId, remoteFolderId);
=> MailService.CreateAssignmentAsync(accountId, mailCopyId, remoteFolderId);
public Task DeleteMailAsync(Guid accountId, string mailId)
=> _mailService.DeleteMailAsync(accountId, mailId);
=> MailService.DeleteMailAsync(accountId, mailId);
public Task<bool> CreateMailAsync(Guid accountId, NewMailItemPackage package)
=> _mailService.CreateMailAsync(accountId, package);
=> MailService.CreateMailAsync(accountId, package);
// Folder methods
public Task UpdateFolderStructureAsync(Guid accountId, List<MailItemFolder> allFolders)
=> _folderService.BulkUpdateFolderStructureAsync(accountId, allFolders);
public Task<bool> MapLocalDraftAsync(Guid accountId, Guid localDraftCopyUniqueId, string newMailCopyId, string newDraftId, string newThreadId)
=> _mailService.MapLocalDraftAsync(accountId, localDraftCopyUniqueId, newMailCopyId, newDraftId, newThreadId);
=> MailService.MapLocalDraftAsync(accountId, localDraftCopyUniqueId, newMailCopyId, newDraftId, newThreadId);
public Task MapLocalDraftAsync(string mailCopyId, string newDraftId, string newThreadId)
=> _mailService.MapLocalDraftAsync(mailCopyId, newDraftId, newThreadId);
=> MailService.MapLocalDraftAsync(mailCopyId, newDraftId, newThreadId);
public Task<List<MailItemFolder>> GetSynchronizationFoldersAsync(SynchronizationOptions options)
=> _folderService.GetSynchronizationFoldersAsync(options);
@@ -113,7 +120,7 @@ namespace Wino.Core.Integration.Processors
=> _folderService.InsertFolderAsync(folder);
public Task<List<MailCopy>> GetDownloadedUnreadMailsAsync(Guid accountId, IEnumerable<string> downloadedMailCopyIds)
=> _mailService.GetDownloadedUnreadMailsAsync(accountId, downloadedMailCopyIds);
=> MailService.GetDownloadedUnreadMailsAsync(accountId, downloadedMailCopyIds);
public Task<IList<uint>> GetKnownUidsForFolderAsync(Guid folderId)
=> _folderService.GetKnownUidsForFolderAsync(folderId);

View File

@@ -0,0 +1,16 @@
using System.Threading.Tasks;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Services;
namespace Wino.Core.Integration.Processors
{
public class OutlookChangeProcessor(IDatabaseService databaseService,
IFolderService folderService,
IMailService mailService,
IAccountService accountService,
IMimeFileService mimeFileService) : DefaultChangeProcessor(databaseService, folderService, mailService, accountService, mimeFileService), IOutlookChangeProcessor
{
public Task<bool> IsMailExistsAsync(string messageId)
=> MailService.IsMailExistsAsync(messageId);
}
}

View File

@@ -856,6 +856,7 @@ namespace Wino.Core.Services
return Connection.FindWithQueryAsync<MailAccount>(query);
}
public Task<bool> IsMailExistsAsync(string mailCopyId)
=> Connection.ExecuteScalarAsync<bool>("SELECT EXISTS(SELECT 1 FROM MailCopy WHERE Id = ?)", mailCopyId);
}
}

View File

@@ -63,11 +63,11 @@ namespace Wino.Core.Synchronizers
];
private readonly ILogger _logger = Log.ForContext<OutlookSynchronizer>();
private readonly IDefaultChangeProcessor _outlookChangeProcessor;
private readonly IOutlookChangeProcessor _outlookChangeProcessor;
private readonly GraphServiceClient _graphClient;
public OutlookSynchronizer(MailAccount account,
IAuthenticator authenticator,
IDefaultChangeProcessor outlookChangeProcessor) : base(account)
IOutlookChangeProcessor outlookChangeProcessor) : base(account)
{
var tokenProvider = new MicrosoftTokenProvider(Account, authenticator);
@@ -191,7 +191,7 @@ namespace Wino.Core.Synchronizers
{
// Parse Delta Token from Delta Link since v5 of Graph SDK works based on the token, not the link.
var deltaToken = Regex.Split(messageIteratorAsync.Deltalink, "deltatoken=")[1];
var deltaToken = GetDeltaTokenFromDeltaLink(latestDeltaLink);
await _outlookChangeProcessor.UpdateFolderDeltaSynchronizationIdentifierAsync(folder.Id, deltaToken).ConfigureAwait(false);
}
@@ -199,6 +199,9 @@ namespace Wino.Core.Synchronizers
return downloadedMessageIds;
}
private string GetDeltaTokenFromDeltaLink(string deltaLink)
=> Regex.Split(deltaLink, "deltatoken=")[1];
private bool IsResourceDeleted(IDictionary<string, object> additionalData)
=> additionalData != null && additionalData.ContainsKey("@removed");
@@ -606,7 +609,14 @@ namespace Wino.Core.Synchronizers
public override async Task<List<NewMailItemPackage>> CreateNewMailPackagesAsync(Message message, MailItemFolder assignedFolder, CancellationToken cancellationToken = default)
{
var mimeMessage = await DownloadMimeMessageAsync(message.Id).ConfigureAwait(false);
bool isMailExists = await _outlookChangeProcessor.IsMailExistsAsync(message.Id);
if (isMailExists)
{
return null;
}
var mimeMessage = await DownloadMimeMessageAsync(message.Id, cancellationToken).ConfigureAwait(false);
var mailCopy = message.AsMailCopy();
if (mimeMessage.Headers.Contains(Domain.Constants.WinoLocalDraftHeader)

View File

@@ -38,6 +38,7 @@ namespace Wino.Core
private readonly IDatabaseService _databaseService;
private readonly IMimeFileService _mimeFileService;
private readonly IDefaultChangeProcessor _defaultChangeProcessor;
private readonly IOutlookChangeProcessor _outlookChangeProcessor;
public WinoSynchronizerFactory(INativeAppService nativeAppService,
ITokenService tokenService,
@@ -48,7 +49,8 @@ namespace Wino.Core
ISignatureService signatureService,
IDatabaseService databaseService,
IMimeFileService mimeFileService,
IDefaultChangeProcessor defaultChangeProcessor)
IDefaultChangeProcessor defaultChangeProcessor,
IOutlookChangeProcessor outlookChangeProcessor)
{
_contactService = contactService;
_notificationBuilder = notificationBuilder;
@@ -60,6 +62,7 @@ namespace Wino.Core
_databaseService = databaseService;
_mimeFileService = mimeFileService;
_defaultChangeProcessor = defaultChangeProcessor;
_outlookChangeProcessor = outlookChangeProcessor;
}
public IBaseSynchronizer GetAccountSynchronizer(Guid accountId)
@@ -73,7 +76,7 @@ namespace Wino.Core
{
case Domain.Enums.MailProviderType.Outlook:
var outlookAuthenticator = new OutlookAuthenticator(_tokenService, _nativeAppService);
return new OutlookSynchronizer(mailAccount, outlookAuthenticator, _defaultChangeProcessor);
return new OutlookSynchronizer(mailAccount, outlookAuthenticator, _outlookChangeProcessor);
case Domain.Enums.MailProviderType.Gmail:
var gmailAuthenticator = new GmailAuthenticator(_tokenService, _nativeAppService);