Handling of multi-day events, new rendering etc.
This commit is contained in:
@@ -202,19 +202,7 @@ namespace Wino.Core.Extensions
|
||||
return calendar;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts the start DateTimeOffset of a Google Calendar Event.
|
||||
/// Handles different date/time representations (date-only, date-time, recurring events).
|
||||
/// Uses the DateTimeDateTimeOffset property for optimal performance and accuracy.
|
||||
/// </summary>
|
||||
/// <param name="calendarEvent">The Google Calendar Event object.</param>
|
||||
/// <returns>The start DateTimeOffset of the event, or null if it cannot be determined.</returns>
|
||||
//public static DateTimeOffset GetEventStartDateTimeOffset(this Event calendarEvent)
|
||||
//{
|
||||
// return GetEventDateTimeOffset(calendarEvent.Start);
|
||||
//}
|
||||
|
||||
public static DateTimeOffset GetEventDateTimeOffset(EventDateTime calendarEvent)
|
||||
public static DateTimeOffset? GetEventDateTimeOffset(EventDateTime calendarEvent)
|
||||
{
|
||||
if (calendarEvent != null)
|
||||
{
|
||||
@@ -236,45 +224,9 @@ namespace Wino.Core.Extensions
|
||||
}
|
||||
}
|
||||
|
||||
throw new Exception("Google Calendar event has no date.");
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the duration of a Google Calendar Event in seconds.
|
||||
/// Handles date-only and date-time events, but *does not* handle recurring events correctly.
|
||||
/// For recurring events, this method will return the duration of the *first* instance.
|
||||
/// </summary>
|
||||
/// <param name="calendarEvent">The Google Calendar Event object.</param>
|
||||
/// <returns>The duration of the event in minutes, or null if it cannot be determined.</returns>
|
||||
//public static int GetEventDurationInSeconds(this Event calendarEvent)
|
||||
//{
|
||||
// var start = calendarEvent.GetEventStartDateTimeOffset();
|
||||
|
||||
// DateTimeOffset? end = null;
|
||||
// if (calendarEvent.End != null)
|
||||
// {
|
||||
// if (calendarEvent.End.DateTimeDateTimeOffset != null)
|
||||
// {
|
||||
// end = calendarEvent.End.DateTimeDateTimeOffset;
|
||||
// }
|
||||
// else if (calendarEvent.End.Date != null)
|
||||
// {
|
||||
// if (DateTime.TryParse(calendarEvent.End.Date, out DateTime endDate))
|
||||
// {
|
||||
// end = new DateTimeOffset(endDate, TimeSpan.Zero);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// throw new Exception("Invalid date format in Google Calendar event end date.");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// if (end == null) throw new Exception("Google Calendar event has no end date.");
|
||||
|
||||
// return (int)(end.Value - start).TotalSeconds;
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// RRULE, EXRULE, RDATE and EXDATE lines for a recurring event, as specified in RFC5545.
|
||||
/// </summary>
|
||||
|
||||
@@ -55,7 +55,7 @@ namespace Wino.Core.Integration.Processors
|
||||
{
|
||||
Task MapLocalDraftAsync(string mailCopyId, string newDraftId, string newThreadId);
|
||||
Task CreateAssignmentAsync(Guid accountId, string mailCopyId, string remoteFolderId);
|
||||
Task<CalendarItem> CreateCalendarItemAsync(Event calendarEvent, AccountCalendar assignedCalendar, MailAccount organizerAccount);
|
||||
Task ManageCalendarEventAsync(Event calendarEvent, AccountCalendar assignedCalendar, MailAccount organizerAccount);
|
||||
}
|
||||
|
||||
public interface IOutlookChangeProcessor : IDefaultChangeProcessor
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using Google.Apis.Calendar.v3.Data;
|
||||
using Serilog;
|
||||
using Wino.Core.Domain.Entities.Calendar;
|
||||
using Wino.Core.Domain.Entities.Shared;
|
||||
using Wino.Core.Domain.Enums;
|
||||
@@ -32,125 +33,250 @@ namespace Wino.Core.Integration.Processors
|
||||
public Task CreateAssignmentAsync(Guid accountId, string mailCopyId, string remoteFolderId)
|
||||
=> MailService.CreateAssignmentAsync(accountId, mailCopyId, remoteFolderId);
|
||||
|
||||
public async Task<CalendarItem> CreateCalendarItemAsync(Event calendarEvent, AccountCalendar assignedCalendar, MailAccount organizerAccount)
|
||||
public async Task ManageCalendarEventAsync(Event calendarEvent, AccountCalendar assignedCalendar, MailAccount organizerAccount)
|
||||
{
|
||||
var eventStartDateTimeOffset = GoogleIntegratorExtensions.GetEventDateTimeOffset(calendarEvent.Start);
|
||||
var eventEndDateTimeOffset = GoogleIntegratorExtensions.GetEventDateTimeOffset(calendarEvent.End);
|
||||
var status = calendarEvent.Status;
|
||||
|
||||
var totalDurationInSeconds = (eventEndDateTimeOffset - eventStartDateTimeOffset).TotalSeconds;
|
||||
var recurringEventId = calendarEvent.RecurringEventId;
|
||||
|
||||
var calendarItem = new CalendarItem()
|
||||
// 1. Canceled exceptions of recurred events are only guaranteed to have recurringEventId, Id and start time.
|
||||
// 2. Updated exceptions of recurred events have different Id, but recurringEventId is the same as parent.
|
||||
|
||||
// Check if we have this event before.
|
||||
var existingCalendarItem = await CalendarService.GetCalendarItemAsync(assignedCalendar.Id, calendarEvent.Id);
|
||||
|
||||
if (existingCalendarItem == null)
|
||||
{
|
||||
CalendarId = assignedCalendar.Id,
|
||||
CreatedAt = DateTimeOffset.UtcNow,
|
||||
Description = calendarEvent.Description,
|
||||
Id = Guid.NewGuid(),
|
||||
StartDate = eventStartDateTimeOffset.DateTime,
|
||||
StartDateOffset = eventStartDateTimeOffset.Offset,
|
||||
EndDateOffset = eventEndDateTimeOffset.Offset,
|
||||
DurationInSeconds = totalDurationInSeconds,
|
||||
Location = calendarEvent.Location,
|
||||
Recurrence = GoogleIntegratorExtensions.GetRecurrenceString(calendarEvent),
|
||||
Status = GetStatus(calendarEvent.Status),
|
||||
Title = calendarEvent.Summary,
|
||||
UpdatedAt = DateTimeOffset.UtcNow,
|
||||
Visibility = GetVisibility(calendarEvent.Visibility),
|
||||
HtmlLink = calendarEvent.HtmlLink
|
||||
};
|
||||
CalendarItem parentRecurringEvent = null;
|
||||
|
||||
Debug.WriteLine($"({assignedCalendar.Name}) {calendarItem.Title}, Start: {calendarItem.StartDate.ToString("f")}, End: {calendarItem.EndDate.ToString("f")}");
|
||||
|
||||
// TODO: There are some edge cases with cancellation here.
|
||||
CalendarItemStatus GetStatus(string status)
|
||||
{
|
||||
return status switch
|
||||
// Manage the recurring event id.
|
||||
if (!string.IsNullOrEmpty(recurringEventId))
|
||||
{
|
||||
"confirmed" => CalendarItemStatus.Confirmed,
|
||||
"tentative" => CalendarItemStatus.Tentative,
|
||||
"cancelled" => CalendarItemStatus.Cancelled,
|
||||
_ => CalendarItemStatus.Confirmed
|
||||
};
|
||||
}
|
||||
parentRecurringEvent = await CalendarService.GetCalendarItemAsync(assignedCalendar.Id, recurringEventId).ConfigureAwait(false);
|
||||
|
||||
CalendarItemVisibility GetVisibility(string visibility)
|
||||
{
|
||||
/// Visibility of the event. Optional. Possible values are: - "default" - Uses the default visibility for
|
||||
/// events on the calendar. This is the default value. - "public" - The event is public and event details are
|
||||
/// visible to all readers of the calendar. - "private" - The event is private and only event attendees may
|
||||
/// view event details. - "confidential" - The event is private. This value is provided for compatibility
|
||||
/// reasons.
|
||||
if (parentRecurringEvent == null)
|
||||
{
|
||||
Log.Information($"Parent recurring event is missing for event. Skipping creation of {calendarEvent.Id}");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
return visibility switch
|
||||
// We don't have this event yet. Create a new one.
|
||||
var eventStartDateTimeOffset = GoogleIntegratorExtensions.GetEventDateTimeOffset(calendarEvent.Start);
|
||||
var eventEndDateTimeOffset = GoogleIntegratorExtensions.GetEventDateTimeOffset(calendarEvent.End);
|
||||
|
||||
double totalDurationInSeconds = 0;
|
||||
|
||||
if (eventStartDateTimeOffset != null && eventEndDateTimeOffset != null)
|
||||
{
|
||||
"default" => CalendarItemVisibility.Default,
|
||||
"public" => CalendarItemVisibility.Public,
|
||||
"private" => CalendarItemVisibility.Private,
|
||||
"confidential" => CalendarItemVisibility.Confidential,
|
||||
_ => CalendarItemVisibility.Default
|
||||
};
|
||||
}
|
||||
totalDurationInSeconds = (eventEndDateTimeOffset.Value - eventStartDateTimeOffset.Value).TotalSeconds;
|
||||
}
|
||||
|
||||
// Attendees
|
||||
var attendees = new List<CalendarEventAttendee>();
|
||||
CalendarItem calendarItem = null;
|
||||
|
||||
if (calendarEvent.Attendees == null)
|
||||
{
|
||||
// Self-only event.
|
||||
|
||||
attendees.Add(new CalendarEventAttendee()
|
||||
if (parentRecurringEvent != null)
|
||||
{
|
||||
CalendarItemId = calendarItem.Id,
|
||||
IsOrganizer = true,
|
||||
Email = organizerAccount.Address,
|
||||
Name = organizerAccount.SenderName,
|
||||
AttendenceStatus = AttendeeStatus.Accepted,
|
||||
Id = Guid.NewGuid(),
|
||||
IsOptionalAttendee = false,
|
||||
});
|
||||
// Exceptions of parent events might not have all the fields populated.
|
||||
// We must use the parent event's data for fields that don't exists.
|
||||
|
||||
// Update duration if it's not populated.
|
||||
if (totalDurationInSeconds == 0)
|
||||
{
|
||||
totalDurationInSeconds = parentRecurringEvent.DurationInSeconds;
|
||||
}
|
||||
|
||||
calendarItem = new CalendarItem()
|
||||
{
|
||||
CalendarId = assignedCalendar.Id,
|
||||
CreatedAt = DateTimeOffset.UtcNow,
|
||||
Description = calendarEvent.Description ?? parentRecurringEvent.Description,
|
||||
Id = Guid.NewGuid(),
|
||||
StartDate = eventStartDateTimeOffset.Value.DateTime,
|
||||
StartDateOffset = eventStartDateTimeOffset.Value.Offset,
|
||||
EndDateOffset = eventEndDateTimeOffset?.Offset ?? parentRecurringEvent.EndDateOffset,
|
||||
DurationInSeconds = totalDurationInSeconds,
|
||||
Location = string.IsNullOrEmpty(calendarEvent.Location) ? parentRecurringEvent.Location : calendarEvent.Location,
|
||||
|
||||
// Leave it empty if it's not populated.
|
||||
Recurrence = GoogleIntegratorExtensions.GetRecurrenceString(calendarEvent) == null ? string.Empty : GoogleIntegratorExtensions.GetRecurrenceString(calendarEvent),
|
||||
Status = GetStatus(calendarEvent.Status),
|
||||
Title = string.IsNullOrEmpty(calendarEvent.Summary) ? parentRecurringEvent.Title : calendarEvent.Summary,
|
||||
UpdatedAt = DateTimeOffset.UtcNow,
|
||||
Visibility = string.IsNullOrEmpty(calendarEvent.Visibility) ? parentRecurringEvent.Visibility : GetVisibility(calendarEvent.Visibility),
|
||||
HtmlLink = calendarEvent.HtmlLink,
|
||||
RemoteEventId = calendarEvent.Id,
|
||||
IsLocked = calendarEvent.Locked.GetValueOrDefault()
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
// This is a parent event creation.
|
||||
// Start-End dates are guaranteed to be populated.
|
||||
|
||||
if (eventStartDateTimeOffset == null || eventEndDateTimeOffset == null)
|
||||
{
|
||||
Log.Error("Failed to create parent event because either start or end date is not specified.");
|
||||
return;
|
||||
}
|
||||
|
||||
calendarItem = new CalendarItem()
|
||||
{
|
||||
CalendarId = assignedCalendar.Id,
|
||||
CreatedAt = DateTimeOffset.UtcNow,
|
||||
Description = calendarEvent.Description,
|
||||
Id = Guid.NewGuid(),
|
||||
StartDate = eventStartDateTimeOffset.Value.DateTime,
|
||||
StartDateOffset = eventStartDateTimeOffset.Value.Offset,
|
||||
EndDateOffset = eventEndDateTimeOffset.Value.Offset,
|
||||
DurationInSeconds = totalDurationInSeconds,
|
||||
Location = calendarEvent.Location,
|
||||
Recurrence = GoogleIntegratorExtensions.GetRecurrenceString(calendarEvent),
|
||||
Status = GetStatus(calendarEvent.Status),
|
||||
Title = calendarEvent.Summary,
|
||||
UpdatedAt = DateTimeOffset.UtcNow,
|
||||
Visibility = GetVisibility(calendarEvent.Visibility),
|
||||
HtmlLink = calendarEvent.HtmlLink,
|
||||
RemoteEventId = calendarEvent.Id,
|
||||
IsLocked = calendarEvent.Locked.GetValueOrDefault()
|
||||
};
|
||||
}
|
||||
|
||||
// Hide canceled events.
|
||||
calendarItem.IsHidden = calendarItem.Status == CalendarItemStatus.Cancelled;
|
||||
|
||||
// Manage the recurring event id.
|
||||
if (parentRecurringEvent != null)
|
||||
{
|
||||
calendarItem.RecurringCalendarItemId = parentRecurringEvent.Id;
|
||||
}
|
||||
|
||||
Debug.WriteLine($"({assignedCalendar.Name}) {calendarItem.Title}, Start: {calendarItem.StartDate.ToString("f")}, End: {calendarItem.EndDate.ToString("f")}");
|
||||
|
||||
// Attendees
|
||||
var attendees = new List<CalendarEventAttendee>();
|
||||
|
||||
//if (calendarEvent.Attendees == null)
|
||||
//{
|
||||
// // Self-only event.
|
||||
|
||||
// attendees.Add(new CalendarEventAttendee()
|
||||
// {
|
||||
// CalendarItemId = calendarItem.Id,
|
||||
// IsOrganizer = true,
|
||||
// Email = organizerAccount.Address,
|
||||
// Name = organizerAccount.SenderName,
|
||||
// AttendenceStatus = AttendeeStatus.Accepted,
|
||||
// Id = Guid.NewGuid(),
|
||||
// IsOptionalAttendee = false,
|
||||
// });
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// foreach (var attendee in calendarEvent.Attendees)
|
||||
// {
|
||||
// if (attendee.Self == true)
|
||||
// {
|
||||
// // TODO:
|
||||
// }
|
||||
// else if (!string.IsNullOrEmpty(attendee.Email))
|
||||
// {
|
||||
// AttendeeStatus GetAttendenceStatus(string responseStatus)
|
||||
// {
|
||||
// return responseStatus switch
|
||||
// {
|
||||
// "accepted" => AttendeeStatus.Accepted,
|
||||
// "declined" => AttendeeStatus.Declined,
|
||||
// "tentative" => AttendeeStatus.Tentative,
|
||||
// "needsAction" => AttendeeStatus.NeedsAction,
|
||||
// _ => AttendeeStatus.NeedsAction
|
||||
// };
|
||||
// }
|
||||
|
||||
// var eventAttendee = new CalendarEventAttendee()
|
||||
// {
|
||||
// CalendarItemId = calendarItem.Id,
|
||||
// IsOrganizer = attendee.Organizer ?? false,
|
||||
// Comment = attendee.Comment,
|
||||
// Email = attendee.Email,
|
||||
// Name = attendee.DisplayName,
|
||||
// AttendenceStatus = GetAttendenceStatus(attendee.ResponseStatus),
|
||||
// Id = Guid.NewGuid(),
|
||||
// IsOptionalAttendee = attendee.Optional ?? false,
|
||||
// };
|
||||
|
||||
// attendees.Add(eventAttendee);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
await CalendarService.CreateNewCalendarItemAsync(calendarItem, attendees);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var attendee in calendarEvent.Attendees)
|
||||
// We have this event already. Update it.
|
||||
if (calendarEvent.Status == "cancelled")
|
||||
{
|
||||
if (attendee.Self == true)
|
||||
{
|
||||
// TODO:
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(attendee.Email))
|
||||
{
|
||||
AttendeeStatus GetAttendenceStatus(string responseStatus)
|
||||
{
|
||||
return responseStatus switch
|
||||
{
|
||||
"accepted" => AttendeeStatus.Accepted,
|
||||
"declined" => AttendeeStatus.Declined,
|
||||
"tentative" => AttendeeStatus.Tentative,
|
||||
"needsAction" => AttendeeStatus.NeedsAction,
|
||||
_ => AttendeeStatus.NeedsAction
|
||||
};
|
||||
}
|
||||
// Event is canceled.
|
||||
|
||||
var eventAttendee = new CalendarEventAttendee()
|
||||
{
|
||||
CalendarItemId = calendarItem.Id,
|
||||
IsOrganizer = attendee.Organizer ?? false,
|
||||
Comment = attendee.Comment,
|
||||
Email = attendee.Email,
|
||||
Name = attendee.DisplayName,
|
||||
AttendenceStatus = GetAttendenceStatus(attendee.ResponseStatus),
|
||||
Id = Guid.NewGuid(),
|
||||
IsOptionalAttendee = attendee.Optional ?? false,
|
||||
};
|
||||
// Parent event is canceled. We must delete everything.
|
||||
if (string.IsNullOrEmpty(recurringEventId))
|
||||
{
|
||||
Log.Information("Parent event is canceled. Deleting all instances of {Id}", existingCalendarItem.Id);
|
||||
|
||||
attendees.Add(eventAttendee);
|
||||
await CalendarService.DeleteCalendarItemAsync(existingCalendarItem.Id).ConfigureAwait(false);
|
||||
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Child event is canceled.
|
||||
// Child should live as long as parent lives, but must not be displayed to the user.
|
||||
|
||||
existingCalendarItem.IsHidden = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Make sure to unhide the event.
|
||||
// It might be marked as hidden before.
|
||||
existingCalendarItem.IsHidden = false;
|
||||
|
||||
// Update the event properties.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
await CalendarService.CreateNewCalendarItemAsync(calendarItem, attendees);
|
||||
|
||||
return calendarItem;
|
||||
// Upsert the event.
|
||||
await Connection.InsertOrReplaceAsync(existingCalendarItem);
|
||||
}
|
||||
|
||||
private CalendarItemStatus GetStatus(string status)
|
||||
{
|
||||
return status switch
|
||||
{
|
||||
"confirmed" => CalendarItemStatus.Confirmed,
|
||||
"tentative" => CalendarItemStatus.Tentative,
|
||||
"cancelled" => CalendarItemStatus.Cancelled,
|
||||
_ => CalendarItemStatus.Confirmed
|
||||
};
|
||||
}
|
||||
|
||||
private CalendarItemVisibility GetVisibility(string visibility)
|
||||
{
|
||||
/// Visibility of the event. Optional. Possible values are: - "default" - Uses the default visibility for
|
||||
/// events on the calendar. This is the default value. - "public" - The event is public and event details are
|
||||
/// visible to all readers of the calendar. - "private" - The event is private and only event attendees may
|
||||
/// view event details. - "confidential" - The event is private. This value is provided for compatibility
|
||||
/// reasons.
|
||||
|
||||
return visibility switch
|
||||
{
|
||||
"default" => CalendarItemVisibility.Default,
|
||||
"public" => CalendarItemVisibility.Public,
|
||||
"private" => CalendarItemVisibility.Private,
|
||||
"confidential" => CalendarItemVisibility.Confidential,
|
||||
_ => CalendarItemVisibility.Default
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -350,13 +350,18 @@ namespace Wino.Core.Synchronizers.Mail
|
||||
|
||||
calendar.SynchronizationDeltaToken = syncToken;
|
||||
|
||||
await _gmailChangeProcessor.UpdateAccountCalendarAsync(calendar).ConfigureAwait(false);
|
||||
// allEvents contains new or updated events.
|
||||
// Process them and create/update local calendar items.
|
||||
|
||||
foreach (var @event in allEvents)
|
||||
{
|
||||
// TODO: Exception handling for event processing.
|
||||
await _gmailChangeProcessor.CreateCalendarItemAsync(@event, calendar, Account).ConfigureAwait(false);
|
||||
// TODO: Also update attendees and other properties.
|
||||
|
||||
await _gmailChangeProcessor.ManageCalendarEventAsync(@event, calendar, Account).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
await _gmailChangeProcessor.UpdateAccountCalendarAsync(calendar).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return default;
|
||||
|
||||
Reference in New Issue
Block a user