Event creation.
This commit is contained in:
@@ -16,6 +16,7 @@ using Wino.Core.Domain.Entities.Mail;
|
||||
using Wino.Core.Domain.Entities.Shared;
|
||||
using Wino.Core.Domain.Enums;
|
||||
using Wino.Core.Domain.Exceptions;
|
||||
using Wino.Core.Domain.Extensions;
|
||||
using Wino.Core.Domain.Interfaces;
|
||||
using Wino.Core.Domain.Models.Calendar;
|
||||
using Wino.Core.Domain.Models.Connectivity;
|
||||
@@ -95,7 +96,7 @@ public class ImapSynchronizer : WinoSynchronizer<ImapRequest, ImapMessageCreatio
|
||||
var poolOptions = ImapClientPoolOptions.CreateDefault(Account.ServerInformation, protocolLogStream);
|
||||
|
||||
_clientPool = new ImapClientPool(poolOptions);
|
||||
_localCalendarOperationHandler = new LocalCalendarOperationHandler(Account, _imapChangeProcessor, _calendarService, "local");
|
||||
_localCalendarOperationHandler = new LocalCalendarOperationHandler(Account, _imapChangeProcessor, _calendarService, _applicationConfiguration.ApplicationDataFolderPath, "local");
|
||||
_calDavCalendarOperationHandler = new CalDavCalendarOperationHandler(this, Account, _calendarService, _calDavClient);
|
||||
}
|
||||
|
||||
@@ -1531,32 +1532,38 @@ public class ImapSynchronizer : WinoSynchronizer<ImapRequest, ImapMessageCreatio
|
||||
private readonly MailAccount _account;
|
||||
private readonly IImapChangeProcessor _changeProcessor;
|
||||
private readonly ICalendarService _calendarService;
|
||||
private readonly string _applicationDataFolderPath;
|
||||
private readonly string _resourceScheme;
|
||||
|
||||
public bool RequiresConnectedClient => false;
|
||||
|
||||
public LocalCalendarOperationHandler(MailAccount account, IImapChangeProcessor changeProcessor, ICalendarService calendarService, string resourceScheme)
|
||||
public LocalCalendarOperationHandler(MailAccount account, IImapChangeProcessor changeProcessor, ICalendarService calendarService, string applicationDataFolderPath, string resourceScheme)
|
||||
{
|
||||
_account = account;
|
||||
_changeProcessor = changeProcessor;
|
||||
_calendarService = calendarService;
|
||||
_applicationDataFolderPath = applicationDataFolderPath;
|
||||
_resourceScheme = resourceScheme;
|
||||
}
|
||||
|
||||
public async Task CreateCalendarEventAsync(CreateCalendarEventRequest request)
|
||||
{
|
||||
var item = request.Item;
|
||||
var item = request.PreparedItem;
|
||||
var attendees = request.PreparedEvent.Attendees;
|
||||
var reminders = request.PreparedEvent.Reminders;
|
||||
EnsureCalendarItemDefaults(item, _account, "local");
|
||||
item.AssignedCalendar ??= await _calendarService.GetAccountCalendarAsync(item.CalendarId).ConfigureAwait(false);
|
||||
|
||||
var existing = await _calendarService.GetCalendarItemAsync(item.Id).ConfigureAwait(false);
|
||||
|
||||
if (existing == null)
|
||||
await _calendarService.CreateNewCalendarItemAsync(item, request.Attendees).ConfigureAwait(false);
|
||||
await _calendarService.CreateNewCalendarItemAsync(item, attendees).ConfigureAwait(false);
|
||||
else
|
||||
await _calendarService.UpdateCalendarItemAsync(item, request.Attendees).ConfigureAwait(false);
|
||||
await _calendarService.UpdateCalendarItemAsync(item, attendees).ConfigureAwait(false);
|
||||
|
||||
await PersistIcsAsync(item, request.Attendees).ConfigureAwait(false);
|
||||
await _calendarService.SaveRemindersAsync(item.Id, reminders).ConfigureAwait(false);
|
||||
await SaveAttachmentsAsync(request.ComposeResult, item.Id).ConfigureAwait(false);
|
||||
await PersistIcsAsync(item, attendees).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task UpdateCalendarEventAsync(UpdateCalendarEventRequest request)
|
||||
@@ -1616,6 +1623,45 @@ public class ImapSynchronizer : WinoSynchronizer<ImapRequest, ImapMessageCreatio
|
||||
DateTimeOffset.UtcNow.ToString("O"),
|
||||
icsContent);
|
||||
}
|
||||
|
||||
private async Task SaveAttachmentsAsync(CalendarEventComposeResult composeResult, Guid calendarItemId)
|
||||
{
|
||||
await _calendarService.DeleteAttachmentsAsync(calendarItemId).ConfigureAwait(false);
|
||||
|
||||
var attachments = composeResult?.Attachments;
|
||||
if (attachments == null || attachments.Count == 0)
|
||||
return;
|
||||
|
||||
var attachmentsRoot = Path.Combine(_applicationDataFolderPath, "CalendarAttachments", calendarItemId.ToString("N"));
|
||||
Directory.CreateDirectory(attachmentsRoot);
|
||||
|
||||
var storedAttachments = new List<CalendarAttachment>();
|
||||
|
||||
foreach (var attachment in attachments.Where(a => !string.IsNullOrWhiteSpace(a.FilePath) && File.Exists(a.FilePath)))
|
||||
{
|
||||
var fileName = string.IsNullOrWhiteSpace(attachment.FileName) ? Path.GetFileName(attachment.FilePath) : attachment.FileName;
|
||||
var destinationPath = Path.Combine(attachmentsRoot, fileName);
|
||||
File.Copy(attachment.FilePath, destinationPath, overwrite: true);
|
||||
|
||||
storedAttachments.Add(new CalendarAttachment
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
CalendarItemId = calendarItemId,
|
||||
RemoteAttachmentId = attachment.Id.ToString("N"),
|
||||
FileName = fileName,
|
||||
Size = attachment.Size,
|
||||
ContentType = MimeTypes.GetMimeType(fileName),
|
||||
IsDownloaded = true,
|
||||
LocalFilePath = destinationPath,
|
||||
LastModified = DateTimeOffset.UtcNow
|
||||
});
|
||||
}
|
||||
|
||||
if (storedAttachments.Count > 0)
|
||||
{
|
||||
await _calendarService.InsertOrReplaceAttachmentsAsync(storedAttachments).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class CalDavCalendarOperationHandler : IImapCalendarOperationHandler
|
||||
@@ -1640,7 +1686,7 @@ public class ImapSynchronizer : WinoSynchronizer<ImapRequest, ImapMessageCreatio
|
||||
}
|
||||
|
||||
public Task CreateCalendarEventAsync(CreateCalendarEventRequest request)
|
||||
=> UpsertCalendarEventAsync(request.Item, request.Attendees);
|
||||
=> UpsertCalendarEventAsync(request.PreparedItem, request.PreparedEvent.Attendees);
|
||||
|
||||
public Task UpdateCalendarEventAsync(UpdateCalendarEventRequest request)
|
||||
=> UpsertCalendarEventAsync(request.Item, request.Attendees);
|
||||
@@ -1654,7 +1700,7 @@ public class ImapSynchronizer : WinoSynchronizer<ImapRequest, ImapMessageCreatio
|
||||
}
|
||||
|
||||
await _calDavClient
|
||||
.DeleteCalendarEventAsync(connection, calendar, request.Item.RemoteEventId)
|
||||
.DeleteCalendarEventAsync(connection, calendar, request.Item.RemoteEventId.GetProviderRemoteEventId())
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
@@ -1689,7 +1735,7 @@ public class ImapSynchronizer : WinoSynchronizer<ImapRequest, ImapMessageCreatio
|
||||
var icsContent = BuildIcsContent(item, attendees);
|
||||
|
||||
await _calDavClient
|
||||
.UpsertCalendarEventAsync(connection, calendar, item.RemoteEventId, icsContent)
|
||||
.UpsertCalendarEventAsync(connection, calendar, item.RemoteEventId.GetProviderRemoteEventId(), icsContent)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user