Prevent downloading existing messages for Outlook.
This commit is contained in:
@@ -77,5 +77,13 @@ namespace Wino.Core.Domain.Interfaces
|
|||||||
/// <param name="uniqueMailId">Unique id of the mail item.</param>
|
/// <param name="uniqueMailId">Unique id of the mail item.</param>
|
||||||
/// <returns>Account that mail belongs to.</returns>
|
/// <returns>Account that mail belongs to.</returns>
|
||||||
Task<MailAccount> GetMailAccountByUniqueIdAsync(Guid uniqueMailId);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ namespace Wino.Core
|
|||||||
services.AddSingleton<IMimeFileService, MimeFileService>();
|
services.AddSingleton<IMimeFileService, MimeFileService>();
|
||||||
|
|
||||||
services.AddTransient<IDefaultChangeProcessor, DefaultChangeProcessor>();
|
services.AddTransient<IDefaultChangeProcessor, DefaultChangeProcessor>();
|
||||||
|
services.AddTransient<IOutlookChangeProcessor, OutlookChangeProcessor>();
|
||||||
services.AddTransient<ITokenService, TokenService>();
|
services.AddTransient<ITokenService, TokenService>();
|
||||||
services.AddTransient<IProviderService, ProviderService>();
|
services.AddTransient<IProviderService, ProviderService>();
|
||||||
services.AddTransient<IFolderService, FolderService>();
|
services.AddTransient<IFolderService, FolderService>();
|
||||||
|
|||||||
@@ -55,7 +55,13 @@ namespace Wino.Core.Integration.Processors
|
|||||||
|
|
||||||
public interface IOutlookChangeProcessor : IDefaultChangeProcessor
|
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,
|
public class DefaultChangeProcessor(IDatabaseService databaseService,
|
||||||
@@ -64,8 +70,9 @@ namespace Wino.Core.Integration.Processors
|
|||||||
IAccountService accountService,
|
IAccountService accountService,
|
||||||
IMimeFileService mimeFileService) : BaseDatabaseService(databaseService), IDefaultChangeProcessor
|
IMimeFileService mimeFileService) : BaseDatabaseService(databaseService), IDefaultChangeProcessor
|
||||||
{
|
{
|
||||||
|
protected IMailService MailService = mailService;
|
||||||
|
|
||||||
private readonly IFolderService _folderService = folderService;
|
private readonly IFolderService _folderService = folderService;
|
||||||
private readonly IMailService _mailService = mailService;
|
|
||||||
private readonly IAccountService _accountService = accountService;
|
private readonly IAccountService _accountService = accountService;
|
||||||
private readonly IMimeFileService _mimeFileService = mimeFileService;
|
private readonly IMimeFileService _mimeFileService = mimeFileService;
|
||||||
|
|
||||||
@@ -73,32 +80,32 @@ namespace Wino.Core.Integration.Processors
|
|||||||
=> _accountService.UpdateSynchronizationIdentifierAsync(accountId, synchronizationDeltaIdentifier);
|
=> _accountService.UpdateSynchronizationIdentifierAsync(accountId, synchronizationDeltaIdentifier);
|
||||||
|
|
||||||
public Task ChangeFlagStatusAsync(string mailCopyId, bool isFlagged)
|
public Task ChangeFlagStatusAsync(string mailCopyId, bool isFlagged)
|
||||||
=> _mailService.ChangeFlagStatusAsync(mailCopyId, isFlagged);
|
=> MailService.ChangeFlagStatusAsync(mailCopyId, isFlagged);
|
||||||
|
|
||||||
public Task ChangeMailReadStatusAsync(string mailCopyId, bool isRead)
|
public Task ChangeMailReadStatusAsync(string mailCopyId, bool isRead)
|
||||||
=> _mailService.ChangeReadStatusAsync(mailCopyId, isRead);
|
=> MailService.ChangeReadStatusAsync(mailCopyId, isRead);
|
||||||
|
|
||||||
public Task DeleteAssignmentAsync(Guid accountId, string mailCopyId, string remoteFolderId)
|
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)
|
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)
|
public Task DeleteMailAsync(Guid accountId, string mailId)
|
||||||
=> _mailService.DeleteMailAsync(accountId, mailId);
|
=> MailService.DeleteMailAsync(accountId, mailId);
|
||||||
|
|
||||||
public Task<bool> CreateMailAsync(Guid accountId, NewMailItemPackage package)
|
public Task<bool> CreateMailAsync(Guid accountId, NewMailItemPackage package)
|
||||||
=> _mailService.CreateMailAsync(accountId, package);
|
=> MailService.CreateMailAsync(accountId, package);
|
||||||
|
|
||||||
// Folder methods
|
// Folder methods
|
||||||
public Task UpdateFolderStructureAsync(Guid accountId, List<MailItemFolder> allFolders)
|
public Task UpdateFolderStructureAsync(Guid accountId, List<MailItemFolder> allFolders)
|
||||||
=> _folderService.BulkUpdateFolderStructureAsync(accountId, allFolders);
|
=> _folderService.BulkUpdateFolderStructureAsync(accountId, allFolders);
|
||||||
|
|
||||||
public Task<bool> MapLocalDraftAsync(Guid accountId, Guid localDraftCopyUniqueId, string newMailCopyId, string newDraftId, string newThreadId)
|
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)
|
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)
|
public Task<List<MailItemFolder>> GetSynchronizationFoldersAsync(SynchronizationOptions options)
|
||||||
=> _folderService.GetSynchronizationFoldersAsync(options);
|
=> _folderService.GetSynchronizationFoldersAsync(options);
|
||||||
@@ -113,7 +120,7 @@ namespace Wino.Core.Integration.Processors
|
|||||||
=> _folderService.InsertFolderAsync(folder);
|
=> _folderService.InsertFolderAsync(folder);
|
||||||
|
|
||||||
public Task<List<MailCopy>> GetDownloadedUnreadMailsAsync(Guid accountId, IEnumerable<string> downloadedMailCopyIds)
|
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)
|
public Task<IList<uint>> GetKnownUidsForFolderAsync(Guid folderId)
|
||||||
=> _folderService.GetKnownUidsForFolderAsync(folderId);
|
=> _folderService.GetKnownUidsForFolderAsync(folderId);
|
||||||
|
|||||||
16
Wino.Core/Integration/Processors/OutlookChangeProcessor.cs
Normal file
16
Wino.Core/Integration/Processors/OutlookChangeProcessor.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -856,6 +856,7 @@ namespace Wino.Core.Services
|
|||||||
return Connection.FindWithQueryAsync<MailAccount>(query);
|
return Connection.FindWithQueryAsync<MailAccount>(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Task<bool> IsMailExistsAsync(string mailCopyId)
|
||||||
|
=> Connection.ExecuteScalarAsync<bool>("SELECT EXISTS(SELECT 1 FROM MailCopy WHERE Id = ?)", mailCopyId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,11 +63,11 @@ namespace Wino.Core.Synchronizers
|
|||||||
];
|
];
|
||||||
|
|
||||||
private readonly ILogger _logger = Log.ForContext<OutlookSynchronizer>();
|
private readonly ILogger _logger = Log.ForContext<OutlookSynchronizer>();
|
||||||
private readonly IDefaultChangeProcessor _outlookChangeProcessor;
|
private readonly IOutlookChangeProcessor _outlookChangeProcessor;
|
||||||
private readonly GraphServiceClient _graphClient;
|
private readonly GraphServiceClient _graphClient;
|
||||||
public OutlookSynchronizer(MailAccount account,
|
public OutlookSynchronizer(MailAccount account,
|
||||||
IAuthenticator authenticator,
|
IAuthenticator authenticator,
|
||||||
IDefaultChangeProcessor outlookChangeProcessor) : base(account)
|
IOutlookChangeProcessor outlookChangeProcessor) : base(account)
|
||||||
{
|
{
|
||||||
var tokenProvider = new MicrosoftTokenProvider(Account, authenticator);
|
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.
|
// 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);
|
await _outlookChangeProcessor.UpdateFolderDeltaSynchronizationIdentifierAsync(folder.Id, deltaToken).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
@@ -199,6 +199,9 @@ namespace Wino.Core.Synchronizers
|
|||||||
return downloadedMessageIds;
|
return downloadedMessageIds;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private string GetDeltaTokenFromDeltaLink(string deltaLink)
|
||||||
|
=> Regex.Split(deltaLink, "deltatoken=")[1];
|
||||||
|
|
||||||
private bool IsResourceDeleted(IDictionary<string, object> additionalData)
|
private bool IsResourceDeleted(IDictionary<string, object> additionalData)
|
||||||
=> additionalData != null && additionalData.ContainsKey("@removed");
|
=> 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)
|
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();
|
var mailCopy = message.AsMailCopy();
|
||||||
|
|
||||||
if (mimeMessage.Headers.Contains(Domain.Constants.WinoLocalDraftHeader)
|
if (mimeMessage.Headers.Contains(Domain.Constants.WinoLocalDraftHeader)
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ namespace Wino.Core
|
|||||||
private readonly IDatabaseService _databaseService;
|
private readonly IDatabaseService _databaseService;
|
||||||
private readonly IMimeFileService _mimeFileService;
|
private readonly IMimeFileService _mimeFileService;
|
||||||
private readonly IDefaultChangeProcessor _defaultChangeProcessor;
|
private readonly IDefaultChangeProcessor _defaultChangeProcessor;
|
||||||
|
private readonly IOutlookChangeProcessor _outlookChangeProcessor;
|
||||||
|
|
||||||
public WinoSynchronizerFactory(INativeAppService nativeAppService,
|
public WinoSynchronizerFactory(INativeAppService nativeAppService,
|
||||||
ITokenService tokenService,
|
ITokenService tokenService,
|
||||||
@@ -48,7 +49,8 @@ namespace Wino.Core
|
|||||||
ISignatureService signatureService,
|
ISignatureService signatureService,
|
||||||
IDatabaseService databaseService,
|
IDatabaseService databaseService,
|
||||||
IMimeFileService mimeFileService,
|
IMimeFileService mimeFileService,
|
||||||
IDefaultChangeProcessor defaultChangeProcessor)
|
IDefaultChangeProcessor defaultChangeProcessor,
|
||||||
|
IOutlookChangeProcessor outlookChangeProcessor)
|
||||||
{
|
{
|
||||||
_contactService = contactService;
|
_contactService = contactService;
|
||||||
_notificationBuilder = notificationBuilder;
|
_notificationBuilder = notificationBuilder;
|
||||||
@@ -60,6 +62,7 @@ namespace Wino.Core
|
|||||||
_databaseService = databaseService;
|
_databaseService = databaseService;
|
||||||
_mimeFileService = mimeFileService;
|
_mimeFileService = mimeFileService;
|
||||||
_defaultChangeProcessor = defaultChangeProcessor;
|
_defaultChangeProcessor = defaultChangeProcessor;
|
||||||
|
_outlookChangeProcessor = outlookChangeProcessor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IBaseSynchronizer GetAccountSynchronizer(Guid accountId)
|
public IBaseSynchronizer GetAccountSynchronizer(Guid accountId)
|
||||||
@@ -73,7 +76,7 @@ namespace Wino.Core
|
|||||||
{
|
{
|
||||||
case Domain.Enums.MailProviderType.Outlook:
|
case Domain.Enums.MailProviderType.Outlook:
|
||||||
var outlookAuthenticator = new OutlookAuthenticator(_tokenService, _nativeAppService);
|
var outlookAuthenticator = new OutlookAuthenticator(_tokenService, _nativeAppService);
|
||||||
return new OutlookSynchronizer(mailAccount, outlookAuthenticator, _defaultChangeProcessor);
|
return new OutlookSynchronizer(mailAccount, outlookAuthenticator, _outlookChangeProcessor);
|
||||||
case Domain.Enums.MailProviderType.Gmail:
|
case Domain.Enums.MailProviderType.Gmail:
|
||||||
var gmailAuthenticator = new GmailAuthenticator(_tokenService, _nativeAppService);
|
var gmailAuthenticator = new GmailAuthenticator(_tokenService, _nativeAppService);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user