From e81b7e2e61c3a8dd4aeb151528d06d28810eef58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Kaan=20K=C3=B6se?= Date: Mon, 25 Nov 2024 17:42:18 +0100 Subject: [PATCH] Remove base mail integrator. --- Wino.Core/Integration/BaseMailIntegrator.cs | 137 ------------------ .../Synchronizers/BaseMailSynchronizer.cs | 133 ++++++++++++++++- 2 files changed, 131 insertions(+), 139 deletions(-) delete mode 100644 Wino.Core/Integration/BaseMailIntegrator.cs diff --git a/Wino.Core/Integration/BaseMailIntegrator.cs b/Wino.Core/Integration/BaseMailIntegrator.cs deleted file mode 100644 index decab393..00000000 --- a/Wino.Core/Integration/BaseMailIntegrator.cs +++ /dev/null @@ -1,137 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using MailKit.Net.Imap; -using MoreLinq; -using Wino.Core.Domain.Interfaces; -using Wino.Core.Domain.Models.Requests; -using Wino.Core.Requests.Bundles; - -namespace Wino.Core.Integration -{ - public abstract class BaseMailIntegrator - { - /// - /// How many items per single HTTP call can be modified. - /// - public abstract uint BatchModificationSize { get; } - - /// - /// How many items must be downloaded per folder when the folder is first synchronized. - /// - public abstract uint InitialMessageDownloadCountPerFolder { get; } - - /// - /// Creates a batched HttpBundle without a response for a collection of MailItem. - /// - /// Generated batch request. - /// An action to get the native request from the MailItem. - /// Collection of http bundle that contains batch and native request. - public IEnumerable> CreateBatchedHttpBundleFromGroup( - IBatchChangeRequest batchChangeRequest, - Func, TNativeRequestType> action) - { - if (batchChangeRequest.Items == null) yield break; - - var groupedItems = batchChangeRequest.Items.Batch((int)BatchModificationSize); - - foreach (var group in groupedItems) - yield return new HttpRequestBundle(action(group), batchChangeRequest); - } - - public IEnumerable> CreateBatchedHttpBundle( - IBatchChangeRequest batchChangeRequest, - Func action) - { - if (batchChangeRequest.Items == null) yield break; - - var groupedItems = batchChangeRequest.Items.Batch((int)BatchModificationSize); - - foreach (var group in groupedItems) - foreach (var item in group) - yield return new HttpRequestBundle(action(item), item); - - yield break; - } - - /// - /// Creates a single HttpBundle without a response for a collection of MailItem. - /// - /// Batch request - /// An action to get the native request from the MailItem - /// Collection of http bundle that contains batch and native request. - public IEnumerable> CreateHttpBundle( - IBatchChangeRequest batchChangeRequest, - Func action) - { - if (batchChangeRequest.Items == null) yield break; - - foreach (var item in batchChangeRequest.Items) - yield return new HttpRequestBundle(action(item), batchChangeRequest); - } - - public IEnumerable> CreateHttpBundle( - IBatchChangeRequest batchChangeRequest, - Func action) - { - if (batchChangeRequest.Items == null) yield break; - - foreach (var item in batchChangeRequest.Items) - yield return new HttpRequestBundle(action(item), item); - } - - /// - /// Creates HttpBundle with TResponse of expected response type from the http call for each of the items in the batch. - /// - /// Expected http response type after the call. - /// Generated batch request. - /// An action to get the native request from the MailItem. - /// Collection of http bundle that contains batch and native request. - public IEnumerable> CreateHttpBundleWithResponse( - IBatchChangeRequest batchChangeRequest, - Func action) - { - if (batchChangeRequest.Items == null) yield break; - - foreach (var item in batchChangeRequest.Items) - yield return new HttpRequestBundle(action(item), batchChangeRequest); - } - - public IEnumerable> CreateHttpBundleWithResponse( - IRequestBase item, - Func action) - { - yield return new HttpRequestBundle(action(item), item); - } - - /// - /// Creates a batched HttpBundle with TResponse of expected response type from the http call for each of the items in the batch. - /// Func will be executed for each item separately in the batch request. - /// - /// Expected http response type after the call. - /// Generated batch request. - /// An action to get the native request from the MailItem. - /// Collection of http bundle that contains batch and native request. - public IEnumerable> CreateBatchedHttpBundle( - IBatchChangeRequest batchChangeRequest, - Func action) - { - if (batchChangeRequest.Items == null) yield break; - - var groupedItems = batchChangeRequest.Items.Batch((int)BatchModificationSize); - - foreach (var group in groupedItems) - foreach (var item in group) - yield return new HttpRequestBundle(action(item), item); - - yield break; - } - - public IEnumerable> CreateTaskBundle(Func value, IRequestBase request) - { - var imapreq = new ImapRequest(value, request); - - return [new ImapRequestBundle(imapreq, request)]; - } - } -} diff --git a/Wino.Core/Synchronizers/BaseMailSynchronizer.cs b/Wino.Core/Synchronizers/BaseMailSynchronizer.cs index 0f876dfc..8e6436c7 100644 --- a/Wino.Core/Synchronizers/BaseMailSynchronizer.cs +++ b/Wino.Core/Synchronizers/BaseMailSynchronizer.cs @@ -7,6 +7,8 @@ using System.Threading; using System.Threading.Tasks; using CommunityToolkit.Mvvm.Messaging; using MailKit; +using MailKit.Net.Imap; +using MoreLinq; using Serilog; using Wino.Core.Domain; using Wino.Core.Domain.Entities.Mail; @@ -16,14 +18,14 @@ using Wino.Core.Domain.Interfaces; using Wino.Core.Domain.Models.Accounts; using Wino.Core.Domain.Models.MailItem; using Wino.Core.Domain.Models.Synchronization; -using Wino.Core.Integration; using Wino.Core.Misc; using Wino.Core.Requests; +using Wino.Core.Requests.Bundles; using Wino.Messaging.UI; namespace Wino.Core.Synchronizers { - public abstract class BaseMailSynchronizer : BaseMailIntegrator, IBaseMailSynchronizer + public abstract class BaseMailSynchronizer : IBaseMailSynchronizer { private SemaphoreSlim synchronizationSemaphore = new(1); private CancellationToken activeSynchronizationCancellationToken; @@ -31,6 +33,16 @@ namespace Wino.Core.Synchronizers protected ConcurrentBag changeRequestQueue = []; protected ILogger Logger = Log.ForContext>(); + /// + /// How many items per single HTTP call can be modified. + /// + public abstract uint BatchModificationSize { get; } + + /// + /// How many items must be downloaded per folder when the folder is first synchronized. + /// + public abstract uint InitialMessageDownloadCountPerFolder { get; } + protected BaseMailSynchronizer(MailAccount account) { Account = account; @@ -441,5 +453,122 @@ namespace Wino.Core.Synchronizers // TODO: What if account is deleted during synchronization? return true; } + + #region Bundle Helpers + + /// + /// Creates a batched HttpBundle without a response for a collection of MailItem. + /// + /// Generated batch request. + /// An action to get the native request from the MailItem. + /// Collection of http bundle that contains batch and native request. + public IEnumerable> CreateBatchedHttpBundleFromGroup( + IBatchChangeRequest batchChangeRequest, + Func, TBaseRequest> action) + { + if (batchChangeRequest.Items == null) yield break; + + var groupedItems = batchChangeRequest.Items.Batch((int)BatchModificationSize); + + foreach (var group in groupedItems) + yield return new HttpRequestBundle(action(group), batchChangeRequest); + } + + public IEnumerable> CreateBatchedHttpBundle( + IBatchChangeRequest batchChangeRequest, + Func action) + { + if (batchChangeRequest.Items == null) yield break; + + var groupedItems = batchChangeRequest.Items.Batch((int)BatchModificationSize); + + foreach (var group in groupedItems) + foreach (var item in group) + yield return new HttpRequestBundle(action(item), item); + + yield break; + } + + /// + /// Creates a single HttpBundle without a response for a collection of MailItem. + /// + /// Batch request + /// An action to get the native request from the MailItem + /// Collection of http bundle that contains batch and native request. + public IEnumerable> CreateHttpBundle( + IBatchChangeRequest batchChangeRequest, + Func action) + { + if (batchChangeRequest.Items == null) yield break; + + foreach (var item in batchChangeRequest.Items) + yield return new HttpRequestBundle(action(item), batchChangeRequest); + } + + public IEnumerable> CreateHttpBundle( + IBatchChangeRequest batchChangeRequest, + Func action) + { + if (batchChangeRequest.Items == null) yield break; + + foreach (var item in batchChangeRequest.Items) + yield return new HttpRequestBundle(action(item), item); + } + + /// + /// Creates HttpBundle with TResponse of expected response type from the http call for each of the items in the batch. + /// + /// Expected http response type after the call. + /// Generated batch request. + /// An action to get the native request from the MailItem. + /// Collection of http bundle that contains batch and native request. + public IEnumerable> CreateHttpBundleWithResponse( + IBatchChangeRequest batchChangeRequest, + Func action) + { + if (batchChangeRequest.Items == null) yield break; + + foreach (var item in batchChangeRequest.Items) + yield return new HttpRequestBundle(action(item), batchChangeRequest); + } + + public IEnumerable> CreateHttpBundleWithResponse( + IRequestBase item, + Func action) + { + yield return new HttpRequestBundle(action(item), item); + } + + /// + /// Creates a batched HttpBundle with TResponse of expected response type from the http call for each of the items in the batch. + /// Func will be executed for each item separately in the batch request. + /// + /// Expected http response type after the call. + /// Generated batch request. + /// An action to get the native request from the MailItem. + /// Collection of http bundle that contains batch and native request. + public IEnumerable> CreateBatchedHttpBundle( + IBatchChangeRequest batchChangeRequest, + Func action) + { + if (batchChangeRequest.Items == null) yield break; + + var groupedItems = batchChangeRequest.Items.Batch((int)BatchModificationSize); + + foreach (var group in groupedItems) + foreach (var item in group) + yield return new HttpRequestBundle(action(item), item); + + yield break; + } + + public IEnumerable> CreateTaskBundle(Func value, IRequestBase request) + { + var imapreq = new ImapRequest(value, request); + + return [new ImapRequestBundle(imapreq, request)]; + } + + #endregion } }