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); } /// /// 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)]; } } }