2024-04-18 01:44:37 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using MimeKit;
|
|
|
|
|
|
using Wino.Core.Domain.Entities;
|
|
|
|
|
|
using Wino.Core.Domain.Interfaces;
|
|
|
|
|
|
using Wino.Core.Domain.Models.MailItem;
|
|
|
|
|
|
using Wino.Core.Domain.Models.Synchronization;
|
|
|
|
|
|
using Wino.Core.Services;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Wino.Core.Integration.Processors
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Database change processor that handles common operations for all synchronizers.
|
|
|
|
|
|
/// When a synchronizer detects a change, it should call the appropriate method in this class to reflect the change in the database.
|
|
|
|
|
|
/// Different synchronizers might need additional implementations.
|
2024-06-02 21:35:03 +02:00
|
|
|
|
/// <see cref="IGmailChangeProcessor"/>, <see cref="IOutlookChangeProcessor"/> and <see cref="IImapChangeProcessor"/>
|
2024-04-18 01:44:37 +02:00
|
|
|
|
/// None of the synchronizers can directly change anything in the database.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public interface IDefaultChangeProcessor
|
|
|
|
|
|
{
|
|
|
|
|
|
Task<string> UpdateAccountDeltaSynchronizationIdentifierAsync(Guid accountId, string deltaSynchronizationIdentifier);
|
|
|
|
|
|
Task CreateAssignmentAsync(Guid accountId, string mailCopyId, string remoteFolderId);
|
|
|
|
|
|
Task DeleteAssignmentAsync(Guid accountId, string mailCopyId, string remoteFolderId);
|
|
|
|
|
|
Task ChangeMailReadStatusAsync(string mailCopyId, bool isRead);
|
|
|
|
|
|
Task ChangeFlagStatusAsync(string mailCopyId, bool isFlagged);
|
|
|
|
|
|
Task<bool> CreateMailAsync(Guid AccountId, NewMailItemPackage package);
|
|
|
|
|
|
Task DeleteMailAsync(Guid accountId, string mailId);
|
|
|
|
|
|
Task<List<MailCopy>> GetDownloadedUnreadMailsAsync(Guid accountId, IEnumerable<string> downloadedMailCopyIds);
|
|
|
|
|
|
Task SaveMimeFileAsync(Guid fileId, MimeMessage mimeMessage, Guid accountId);
|
|
|
|
|
|
Task UpdateFolderStructureAsync(Guid accountId, List<MailItemFolder> allFolders);
|
|
|
|
|
|
Task DeleteFolderAsync(Guid accountId, string remoteFolderId);
|
|
|
|
|
|
Task<List<MailItemFolder>> GetSynchronizationFoldersAsync(SynchronizationOptions options);
|
|
|
|
|
|
Task InsertFolderAsync(MailItemFolder folder);
|
2024-06-02 21:35:03 +02:00
|
|
|
|
Task<bool> MapLocalDraftAsync(Guid accountId, Guid localDraftCopyUniqueId, string newMailCopyId, string newDraftId, string newThreadId);
|
|
|
|
|
|
Task UpdateFolderLastSyncDateAsync(Guid folderId);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public interface IGmailChangeProcessor : IDefaultChangeProcessor
|
|
|
|
|
|
{
|
2024-06-02 21:35:03 +02:00
|
|
|
|
Task MapLocalDraftAsync(string mailCopyId, string newDraftId, string newThreadId);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public interface IOutlookChangeProcessor : IDefaultChangeProcessor
|
|
|
|
|
|
{
|
2024-05-25 17:00:52 +02:00
|
|
|
|
/// <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);
|
2024-06-02 21:35:03 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Updates Folder's delta synchronization identifier.
|
|
|
|
|
|
/// Only used in Outlook since it does per-folder sync.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="folderId">Folder id</param>
|
|
|
|
|
|
/// <param name="synchronizationIdentifier">New synchronization identifier.</param>
|
|
|
|
|
|
/// <returns>New identifier if success.</returns>
|
|
|
|
|
|
Task UpdateFolderDeltaSynchronizationIdentifierAsync(Guid folderId, string deltaSynchronizationIdentifier);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public interface IImapChangeProcessor : IDefaultChangeProcessor
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns all known uids for the given folder.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="folderId">Folder id to retrieve uIds for.</param>
|
|
|
|
|
|
Task<IList<uint>> GetKnownUidsForFolderAsync(Guid folderId);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class DefaultChangeProcessor(IDatabaseService databaseService,
|
|
|
|
|
|
IFolderService folderService,
|
|
|
|
|
|
IMailService mailService,
|
|
|
|
|
|
IAccountService accountService,
|
|
|
|
|
|
IMimeFileService mimeFileService) : BaseDatabaseService(databaseService), IDefaultChangeProcessor
|
|
|
|
|
|
{
|
2024-05-25 17:00:52 +02:00
|
|
|
|
protected IMailService MailService = mailService;
|
|
|
|
|
|
|
2024-06-02 21:35:03 +02:00
|
|
|
|
protected IFolderService FolderService = folderService;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
private readonly IAccountService _accountService = accountService;
|
|
|
|
|
|
private readonly IMimeFileService _mimeFileService = mimeFileService;
|
|
|
|
|
|
|
|
|
|
|
|
public Task<string> UpdateAccountDeltaSynchronizationIdentifierAsync(Guid accountId, string synchronizationDeltaIdentifier)
|
|
|
|
|
|
=> _accountService.UpdateSynchronizationIdentifierAsync(accountId, synchronizationDeltaIdentifier);
|
|
|
|
|
|
|
|
|
|
|
|
public Task ChangeFlagStatusAsync(string mailCopyId, bool isFlagged)
|
2024-05-25 17:00:52 +02:00
|
|
|
|
=> MailService.ChangeFlagStatusAsync(mailCopyId, isFlagged);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
|
|
|
|
|
public Task ChangeMailReadStatusAsync(string mailCopyId, bool isRead)
|
2024-05-25 17:00:52 +02:00
|
|
|
|
=> MailService.ChangeReadStatusAsync(mailCopyId, isRead);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
|
|
|
|
|
public Task DeleteAssignmentAsync(Guid accountId, string mailCopyId, string remoteFolderId)
|
2024-05-25 17:00:52 +02:00
|
|
|
|
=> MailService.DeleteAssignmentAsync(accountId, mailCopyId, remoteFolderId);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
|
|
|
|
|
public Task CreateAssignmentAsync(Guid accountId, string mailCopyId, string remoteFolderId)
|
2024-05-25 17:00:52 +02:00
|
|
|
|
=> MailService.CreateAssignmentAsync(accountId, mailCopyId, remoteFolderId);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
|
|
|
|
|
public Task DeleteMailAsync(Guid accountId, string mailId)
|
2024-05-25 17:00:52 +02:00
|
|
|
|
=> MailService.DeleteMailAsync(accountId, mailId);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
|
|
|
|
|
public Task<bool> CreateMailAsync(Guid accountId, NewMailItemPackage package)
|
2024-05-25 17:00:52 +02:00
|
|
|
|
=> MailService.CreateMailAsync(accountId, package);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
|
|
|
|
|
// Folder methods
|
|
|
|
|
|
public Task UpdateFolderStructureAsync(Guid accountId, List<MailItemFolder> allFolders)
|
2024-06-02 21:35:03 +02:00
|
|
|
|
=> FolderService.BulkUpdateFolderStructureAsync(accountId, allFolders);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
|
|
|
|
|
public Task<bool> MapLocalDraftAsync(Guid accountId, Guid localDraftCopyUniqueId, string newMailCopyId, string newDraftId, string newThreadId)
|
2024-05-25 17:00:52 +02:00
|
|
|
|
=> MailService.MapLocalDraftAsync(accountId, localDraftCopyUniqueId, newMailCopyId, newDraftId, newThreadId);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-06-02 21:35:03 +02:00
|
|
|
|
public Task<List<MailItemFolder>> GetSynchronizationFoldersAsync(SynchronizationOptions options)
|
|
|
|
|
|
=> FolderService.GetSynchronizationFoldersAsync(options);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
|
|
|
|
|
public Task DeleteFolderAsync(Guid accountId, string remoteFolderId)
|
2024-06-02 21:35:03 +02:00
|
|
|
|
=> FolderService.DeleteFolderAsync(accountId, remoteFolderId);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
|
|
|
|
|
public Task InsertFolderAsync(MailItemFolder folder)
|
2024-06-02 21:35:03 +02:00
|
|
|
|
=> FolderService.InsertFolderAsync(folder);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
|
|
|
|
|
public Task<List<MailCopy>> GetDownloadedUnreadMailsAsync(Guid accountId, IEnumerable<string> downloadedMailCopyIds)
|
2024-05-25 17:00:52 +02:00
|
|
|
|
=> MailService.GetDownloadedUnreadMailsAsync(accountId, downloadedMailCopyIds);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2024-06-02 21:35:03 +02:00
|
|
|
|
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
|
|
|
|
|
public Task SaveMimeFileAsync(Guid fileId, MimeMessage mimeMessage, Guid accountId)
|
|
|
|
|
|
=> _mimeFileService.SaveMimeMessageAsync(fileId, mimeMessage, accountId);
|
2024-06-02 21:35:03 +02:00
|
|
|
|
|
|
|
|
|
|
public Task UpdateFolderLastSyncDateAsync(Guid folderId)
|
|
|
|
|
|
=> FolderService.UpdateFolderLastSyncDateAsync(folderId);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|