2024-04-18 01:44:37 +02:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using CommunityToolkit.Mvvm.Messaging;
|
|
|
|
|
using Serilog;
|
|
|
|
|
using Wino.Core.Domain;
|
2026-01-03 19:33:36 +01:00
|
|
|
using Wino.Core.Domain.Entities.Calendar;
|
2024-04-18 01:44:37 +02:00
|
|
|
using Wino.Core.Domain.Enums;
|
|
|
|
|
using Wino.Core.Domain.Exceptions;
|
|
|
|
|
using Wino.Core.Domain.Interfaces;
|
2025-12-30 11:59:54 +01:00
|
|
|
using Wino.Core.Domain.Models.Calendar;
|
2024-04-18 01:44:37 +02:00
|
|
|
using Wino.Core.Domain.Models.Folders;
|
|
|
|
|
using Wino.Core.Domain.Models.MailItem;
|
|
|
|
|
using Wino.Core.Domain.Models.Synchronization;
|
2026-02-07 14:03:41 +01:00
|
|
|
using Wino.Core.Helpers;
|
2025-12-30 11:59:54 +01:00
|
|
|
using Wino.Core.Requests.Calendar;
|
2024-11-26 20:03:10 +01:00
|
|
|
using Wino.Core.Requests.Mail;
|
2024-08-05 00:36:26 +02:00
|
|
|
using Wino.Messaging.Server;
|
2026-02-07 14:03:41 +01:00
|
|
|
using Wino.Messaging.UI;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
namespace Wino.Core.Services;
|
|
|
|
|
|
|
|
|
|
public class WinoRequestDelegator : IWinoRequestDelegator
|
2024-04-18 01:44:37 +02:00
|
|
|
{
|
2025-02-16 11:54:23 +01:00
|
|
|
private readonly IWinoRequestProcessor _winoRequestProcessor;
|
|
|
|
|
private readonly IFolderService _folderService;
|
|
|
|
|
private readonly IMailDialogService _dialogService;
|
2026-02-07 14:03:41 +01:00
|
|
|
private readonly IAccountService _accountService;
|
2025-02-16 11:54:23 +01:00
|
|
|
|
|
|
|
|
public WinoRequestDelegator(IWinoRequestProcessor winoRequestProcessor,
|
|
|
|
|
IFolderService folderService,
|
2026-02-07 14:03:41 +01:00
|
|
|
IMailDialogService dialogService,
|
|
|
|
|
IAccountService accountService)
|
2024-04-18 01:44:37 +02:00
|
|
|
{
|
2025-02-16 11:54:23 +01:00
|
|
|
_winoRequestProcessor = winoRequestProcessor;
|
|
|
|
|
_folderService = folderService;
|
|
|
|
|
_dialogService = dialogService;
|
2026-02-07 14:03:41 +01:00
|
|
|
_accountService = accountService;
|
2025-02-16 11:54:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task ExecuteAsync(MailOperationPreperationRequest request)
|
|
|
|
|
{
|
|
|
|
|
var requests = new List<IMailActionRequest>();
|
|
|
|
|
|
|
|
|
|
try
|
2025-02-16 11:35:43 +01:00
|
|
|
{
|
2025-02-16 11:54:23 +01:00
|
|
|
requests = await _winoRequestProcessor.PrepareRequestsAsync(request);
|
2025-02-16 11:35:43 +01:00
|
|
|
}
|
2025-02-16 11:54:23 +01:00
|
|
|
catch (UnavailableSpecialFolderException unavailableSpecialFolderException)
|
|
|
|
|
{
|
|
|
|
|
_dialogService.InfoBarMessage(Translator.Info_MissingFolderTitle,
|
|
|
|
|
string.Format(Translator.Info_MissingFolderMessage, unavailableSpecialFolderException.SpecialFolderType),
|
|
|
|
|
InfoBarMessageType.Warning,
|
|
|
|
|
Translator.SettingConfigureSpecialFolders_Button,
|
|
|
|
|
() =>
|
|
|
|
|
{
|
|
|
|
|
_dialogService.HandleSystemFolderConfigurationDialogAsync(unavailableSpecialFolderException.AccountId, _folderService);
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-02-23 17:05:46 +01:00
|
|
|
catch (InvalidMoveTargetException invalidMoveTargetException)
|
2025-02-16 11:54:23 +01:00
|
|
|
{
|
2025-02-23 17:05:46 +01:00
|
|
|
switch (invalidMoveTargetException.Reason)
|
|
|
|
|
{
|
|
|
|
|
case InvalidMoveTargetReason.NonMoveTarget:
|
|
|
|
|
_dialogService.InfoBarMessage(Translator.Info_InvalidMoveTargetTitle, Translator.Info_InvalidMoveTargetMessage, InfoBarMessageType.Warning);
|
|
|
|
|
break;
|
|
|
|
|
case InvalidMoveTargetReason.MultipleAccounts:
|
|
|
|
|
_dialogService.InfoBarMessage(Translator.Info_InvalidMoveTargetTitle, Translator.Exception_InvalidMultiAccountMoveTarget, InfoBarMessageType.Warning);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-02-16 11:54:23 +01:00
|
|
|
}
|
|
|
|
|
catch (NotImplementedException)
|
|
|
|
|
{
|
|
|
|
|
_dialogService.ShowNotSupportedMessage();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
2025-02-16 11:35:43 +01:00
|
|
|
{
|
2025-02-16 11:54:23 +01:00
|
|
|
Log.Error(ex, "Request creation failed.");
|
|
|
|
|
_dialogService.InfoBarMessage(Translator.Info_RequestCreationFailedTitle, ex.Message, InfoBarMessageType.Error);
|
|
|
|
|
}
|
2025-02-16 11:43:30 +01:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
if (requests == null || !requests.Any()) return;
|
|
|
|
|
|
|
|
|
|
var accountIds = requests.GroupBy(a => a.Item.AssignedAccount.Id);
|
|
|
|
|
|
|
|
|
|
// Queue requests for each account and start synchronization.
|
2026-02-07 14:03:41 +01:00
|
|
|
foreach (var accountGroup in accountIds)
|
2025-02-16 11:54:23 +01:00
|
|
|
{
|
2026-02-07 14:03:41 +01:00
|
|
|
foreach (var accountRequest in accountGroup)
|
2025-02-16 11:43:30 +01:00
|
|
|
{
|
2026-02-07 14:03:41 +01:00
|
|
|
await QueueRequestAsync(accountRequest, accountGroup.Key);
|
2024-04-18 01:44:37 +02:00
|
|
|
}
|
|
|
|
|
|
2026-02-07 14:03:41 +01:00
|
|
|
var account = accountGroup.First().Item.AssignedAccount;
|
|
|
|
|
var actionItems = SynchronizationActionHelper.CreateActionItems(accountGroup, accountGroup.Key, account.Name);
|
|
|
|
|
|
|
|
|
|
if (actionItems.Count > 0)
|
|
|
|
|
WeakReferenceMessenger.Default.Send(new SynchronizationActionsAdded(accountGroup.Key, account.Name, actionItems));
|
|
|
|
|
|
|
|
|
|
await QueueSynchronizationAsync(accountGroup.Key);
|
2025-02-16 11:54:23 +01:00
|
|
|
}
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
public async Task ExecuteAsync(FolderOperationPreperationRequest folderRequest)
|
|
|
|
|
{
|
|
|
|
|
if (folderRequest == null || folderRequest.Folder == null) return;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
IRequestBase request = null;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
var accountId = folderRequest.Folder.MailAccountId;
|
2024-07-09 01:05:16 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
try
|
2025-02-16 11:43:30 +01:00
|
|
|
{
|
2025-02-16 11:54:23 +01:00
|
|
|
request = await _winoRequestProcessor.PrepareFolderRequestAsync(folderRequest);
|
|
|
|
|
}
|
|
|
|
|
catch (NotImplementedException)
|
|
|
|
|
{
|
|
|
|
|
_dialogService.ShowNotSupportedMessage();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Error(ex, "Folder operation execution failed.");
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
if (request == null) return;
|
2024-07-09 01:05:16 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
await QueueRequestAsync(request, accountId);
|
2026-02-07 14:03:41 +01:00
|
|
|
await SendSyncActionsAddedAsync([request], accountId);
|
2025-02-16 11:54:23 +01:00
|
|
|
await QueueSynchronizationAsync(accountId);
|
2026-02-07 19:47:21 +01:00
|
|
|
|
|
|
|
|
if (folderRequest.Action is FolderOperation.Delete or FolderOperation.CreateSubFolder)
|
|
|
|
|
{
|
|
|
|
|
await QueueFoldersOnlySynchronizationAsync(accountId);
|
|
|
|
|
}
|
2025-02-16 11:54:23 +01:00
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
public async Task ExecuteAsync(DraftPreparationRequest draftPreperationRequest)
|
|
|
|
|
{
|
|
|
|
|
var request = new CreateDraftRequest(draftPreperationRequest);
|
2026-02-07 14:03:41 +01:00
|
|
|
var accountId = draftPreperationRequest.Account.Id;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2026-02-07 14:03:41 +01:00
|
|
|
await QueueRequestAsync(request, accountId);
|
|
|
|
|
await SendSyncActionsAddedAsync([request], accountId, draftPreperationRequest.Account.Name);
|
|
|
|
|
await QueueSynchronizationAsync(accountId);
|
2025-02-16 11:54:23 +01:00
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
public async Task ExecuteAsync(SendDraftPreparationRequest sendDraftPreperationRequest)
|
|
|
|
|
{
|
|
|
|
|
var request = new SendDraftRequest(sendDraftPreperationRequest);
|
2026-02-07 14:03:41 +01:00
|
|
|
var account = sendDraftPreperationRequest.MailItem.AssignedAccount;
|
2025-02-16 11:43:30 +01:00
|
|
|
|
2026-02-07 14:03:41 +01:00
|
|
|
await QueueRequestAsync(request, account.Id);
|
|
|
|
|
await SendSyncActionsAddedAsync([request], account.Id, account.Name);
|
|
|
|
|
await QueueSynchronizationAsync(account.Id);
|
2025-02-16 11:54:23 +01:00
|
|
|
}
|
2025-02-16 11:43:30 +01:00
|
|
|
|
2025-12-30 11:59:54 +01:00
|
|
|
public async Task ExecuteAsync(CalendarOperationPreparationRequest calendarPreparationRequest)
|
|
|
|
|
{
|
|
|
|
|
IRequestBase request = calendarPreparationRequest.Operation switch
|
|
|
|
|
{
|
|
|
|
|
CalendarSynchronizerOperation.CreateEvent => new CreateCalendarEventRequest(calendarPreparationRequest.CalendarItem, calendarPreparationRequest.Attendees),
|
2026-01-01 10:07:56 +01:00
|
|
|
CalendarSynchronizerOperation.DeleteEvent => new DeleteCalendarEventRequest(calendarPreparationRequest.CalendarItem),
|
2026-01-03 19:33:36 +01:00
|
|
|
CalendarSynchronizerOperation.AcceptEvent => new AcceptEventRequest(calendarPreparationRequest.CalendarItem, calendarPreparationRequest.ResponseMessage),
|
|
|
|
|
CalendarSynchronizerOperation.DeclineEvent => CreateDeclineRequest(calendarPreparationRequest.CalendarItem, calendarPreparationRequest.ResponseMessage),
|
|
|
|
|
CalendarSynchronizerOperation.TentativeEvent => new TentativeEventRequest(calendarPreparationRequest.CalendarItem, calendarPreparationRequest.ResponseMessage),
|
2026-01-05 00:21:07 +01:00
|
|
|
CalendarSynchronizerOperation.UpdateEvent => new UpdateCalendarEventRequest(calendarPreparationRequest.CalendarItem, calendarPreparationRequest.Attendees)
|
|
|
|
|
{
|
|
|
|
|
OriginalItem = calendarPreparationRequest.OriginalItem,
|
|
|
|
|
OriginalAttendees = calendarPreparationRequest.OriginalAttendees
|
|
|
|
|
},
|
2025-12-30 11:59:54 +01:00
|
|
|
_ => throw new NotImplementedException($"Calendar operation {calendarPreparationRequest.Operation} is not implemented yet.")
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await QueueRequestAsync(request, calendarPreparationRequest.CalendarItem.AssignedCalendar.AccountId);
|
|
|
|
|
await QueueCalendarSynchronizationAsync(calendarPreparationRequest.CalendarItem.AssignedCalendar.AccountId);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-03 19:33:36 +01:00
|
|
|
private IRequestBase CreateDeclineRequest(CalendarItem calendarItem, string responseMessage)
|
|
|
|
|
{
|
|
|
|
|
// For Outlook accounts, declined events are deleted by the server after synchronization.
|
|
|
|
|
// Use OutlookDeclineEventRequest to handle UI removal.
|
|
|
|
|
if (calendarItem.AssignedCalendar?.MailAccount?.ProviderType == MailProviderType.Outlook)
|
|
|
|
|
{
|
|
|
|
|
return new OutlookDeclineEventRequest(calendarItem, responseMessage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new DeclineEventRequest(calendarItem, responseMessage);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
private async Task QueueRequestAsync(IRequestBase request, Guid accountId)
|
|
|
|
|
{
|
2025-10-06 17:46:00 +02:00
|
|
|
// Don't trigger synchronization for individual requests - we'll trigger it once for all requests
|
|
|
|
|
await SynchronizationManager.Instance.QueueRequestAsync(request, accountId, triggerSynchronization: false);
|
2025-02-16 11:54:23 +01:00
|
|
|
}
|
2024-08-11 15:25:40 +02:00
|
|
|
|
2025-10-04 23:10:07 +02:00
|
|
|
private Task QueueSynchronizationAsync(Guid accountId)
|
2025-02-16 11:54:23 +01:00
|
|
|
{
|
|
|
|
|
var options = new MailSynchronizationOptions()
|
|
|
|
|
{
|
|
|
|
|
AccountId = accountId,
|
|
|
|
|
Type = MailSynchronizationType.ExecuteRequests
|
|
|
|
|
};
|
2024-08-11 15:25:40 +02:00
|
|
|
|
2025-10-12 16:23:33 +02:00
|
|
|
WeakReferenceMessenger.Default.Send(new NewMailSynchronizationRequested(options));
|
2026-02-07 19:47:21 +01:00
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Task QueueFoldersOnlySynchronizationAsync(Guid accountId)
|
|
|
|
|
{
|
|
|
|
|
var options = new MailSynchronizationOptions()
|
|
|
|
|
{
|
|
|
|
|
AccountId = accountId,
|
|
|
|
|
Type = MailSynchronizationType.FoldersOnly
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
WeakReferenceMessenger.Default.Send(new NewMailSynchronizationRequested(options));
|
2025-10-04 23:10:07 +02:00
|
|
|
return Task.CompletedTask;
|
2024-04-18 01:44:37 +02:00
|
|
|
}
|
2025-12-30 11:59:54 +01:00
|
|
|
|
2026-02-07 14:03:41 +01:00
|
|
|
private async Task SendSyncActionsAddedAsync(IEnumerable<IRequestBase> requests, Guid accountId, string accountName = null)
|
|
|
|
|
{
|
|
|
|
|
if (accountName == null)
|
|
|
|
|
{
|
|
|
|
|
var account = await _accountService.GetAccountAsync(accountId);
|
|
|
|
|
accountName = account?.Name ?? string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var actionItems = SynchronizationActionHelper.CreateActionItems(requests, accountId, accountName);
|
|
|
|
|
|
|
|
|
|
if (actionItems.Count > 0)
|
|
|
|
|
WeakReferenceMessenger.Default.Send(new SynchronizationActionsAdded(accountId, accountName, actionItems));
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-30 11:59:54 +01:00
|
|
|
private Task QueueCalendarSynchronizationAsync(Guid accountId)
|
|
|
|
|
{
|
|
|
|
|
var options = new CalendarSynchronizationOptions()
|
|
|
|
|
{
|
|
|
|
|
AccountId = accountId,
|
|
|
|
|
Type = CalendarSynchronizationType.ExecuteRequests
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
WeakReferenceMessenger.Default.Send(new NewCalendarSynchronizationRequested(options));
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
}
|