Refactored the calendar synchronization code using AI.

This commit is contained in:
Burak Kaan Köse
2025-07-06 17:25:38 +02:00
parent 3e889d8c08
commit 1ee0063b62
30 changed files with 3125 additions and 1086 deletions

View File

@@ -1,16 +1,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.Messaging;
using Ical.Net.CalendarComponents;
using Ical.Net.DataTypes;
using SqlKata;
using Wino.Core.Domain;
using Wino.Core.Domain.Entities.Calendar;
using Wino.Core.Domain.Enums;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.Calendar;
using Wino.Messaging.Client.Calendar;
@@ -58,27 +51,7 @@ public class CalendarService : BaseDatabaseService, ICalendarService
public async Task DeleteCalendarItemAsync(Guid calendarItemId)
{
var calendarItem = await Connection.GetAsync<CalendarItem>(calendarItemId);
if (calendarItem == null) return;
List<CalendarItem> eventsToRemove = new() { calendarItem };
// In case of parent event, delete all child events as well.
if (!string.IsNullOrEmpty(calendarItem.Recurrence))
{
var recurringEvents = await Connection.Table<CalendarItem>().Where(a => a.RecurringCalendarItemId == calendarItemId).ToListAsync().ConfigureAwait(false);
eventsToRemove.AddRange(recurringEvents);
}
foreach (var @event in eventsToRemove)
{
await Connection.Table<CalendarItem>().DeleteAsync(x => x.Id == @event.Id).ConfigureAwait(false);
await Connection.Table<CalendarEventAttendee>().DeleteAsync(a => a.CalendarItemId == @event.Id).ConfigureAwait(false);
WeakReferenceMessenger.Default.Send(new CalendarItemDeleted(@event));
}
}
public async Task CreateNewCalendarItemAsync(CalendarItem calendarItem, List<CalendarEventAttendee> attendees)
@@ -98,83 +71,8 @@ public class CalendarService : BaseDatabaseService, ICalendarService
public async Task<List<CalendarItem>> GetCalendarEventsAsync(IAccountCalendar calendar, DayRangeRenderModel dayRangeRenderModel)
{
// TODO: We might need to implement caching here.
// I don't know how much of the events we'll have in total, but this logic scans all events every time for given calendar.
var accountEvents = await Connection.Table<CalendarItem>()
.Where(x => x.CalendarId == calendar.Id && !x.IsHidden).ToListAsync();
var result = new List<CalendarItem>();
foreach (var ev in accountEvents)
{
ev.AssignedCalendar = calendar;
// Parse recurrence rules
var calendarEvent = new CalendarEvent
{
Start = new CalDateTime(ev.StartDate),
End = new CalDateTime(ev.EndDate),
};
if (string.IsNullOrEmpty(ev.Recurrence))
{
// No recurrence, only check if we fall into the given period.
if (ev.Period.OverlapsWith(dayRangeRenderModel.Period))
{
result.Add(ev);
}
}
else
{
// This event has recurrences.
// Wino stores exceptional recurrent events as a separate calendar item, without the recurrence rule.
// Because each instance of recurrent event can have different attendees, properties etc.
// Even though the event is recurrent, each updated instance is a separate calendar item.
// Calculate the all recurrences, and remove the exceptional instances like hidden ones.
var recurrenceLines = Regex.Split(ev.Recurrence, Constants.CalendarEventRecurrenceRuleSeperator);
foreach (var line in recurrenceLines)
{
calendarEvent.RecurrenceRules.Add(new RecurrencePattern(line));
}
// Calculate occurrences in the range.
var occurrences = calendarEvent.GetOccurrences(dayRangeRenderModel.Period.Start, dayRangeRenderModel.Period.End);
// Get all recurrent exceptional calendar events.
var exceptionalRecurrences = await Connection.Table<CalendarItem>()
.Where(a => a.RecurringCalendarItemId == ev.Id)
.ToListAsync()
.ConfigureAwait(false);
foreach (var occurrence in occurrences)
{
var exactInstanceCheck = exceptionalRecurrences.FirstOrDefault(a =>
a.Period.OverlapsWith(dayRangeRenderModel.Period));
if (exactInstanceCheck == null)
{
// There is no exception for the period.
// Change the instance StartDate and Duration.
var recurrence = ev.CreateRecurrence(occurrence.Period.StartTime.Value, occurrence.Period.Duration.TotalSeconds);
result.Add(recurrence);
}
else
{
// There is a single instance of this recurrent event.
// It will be added as single item if it's not hidden.
// We don't need to do anything here.
}
}
}
}
return result;
// TODO
return new List<CalendarItem>();
}
public Task<AccountCalendar> GetAccountCalendarAsync(Guid accountCalendarId)
@@ -221,7 +119,7 @@ public class CalendarService : BaseDatabaseService, ICalendarService
}
public Task<List<CalendarEventAttendee>> GetAttendeesAsync(Guid calendarEventTrackingId)
=> Connection.Table<CalendarEventAttendee>().Where(x => x.CalendarItemId == calendarEventTrackingId).ToListAsync();
=> Connection.Table<CalendarEventAttendee>().Where(x => x.EventId == calendarEventTrackingId).ToListAsync();
public async Task<List<CalendarEventAttendee>> ManageEventAttendeesAsync(Guid calendarItemId, List<CalendarEventAttendee> allAttendees)
{
@@ -230,7 +128,7 @@ public class CalendarService : BaseDatabaseService, ICalendarService
// Clear all attendees.
var query = new Query()
.From(nameof(CalendarEventAttendee))
.Where(nameof(CalendarEventAttendee.CalendarItemId), calendarItemId)
.Where(nameof(CalendarEventAttendee.EventId), calendarItemId)
.AsDelete();
connection.Execute(query.GetRawQuery());
@@ -239,67 +137,12 @@ public class CalendarService : BaseDatabaseService, ICalendarService
connection.InsertAll(allAttendees);
});
return await Connection.Table<CalendarEventAttendee>().Where(a => a.CalendarItemId == calendarItemId).ToListAsync();
return await Connection.Table<CalendarEventAttendee>().Where(a => a.EventId == calendarItemId).ToListAsync();
}
public async Task<CalendarItem> GetCalendarItemTargetAsync(CalendarItemTarget targetDetails)
{
var eventId = targetDetails.Item.Id;
// Get the event by Id first.
var item = await GetCalendarItemAsync(eventId).ConfigureAwait(false);
bool isRecurringChild = targetDetails.Item.IsRecurringChild;
bool isRecurringParent = targetDetails.Item.IsRecurringParent;
if (targetDetails.TargetType == CalendarEventTargetType.Single)
{
if (isRecurringChild)
{
if (item == null)
{
// This is an occurrence of a recurring event.
// They don't exist in db.
return targetDetails.Item;
}
else
{
// Single exception occurrence of recurring event.
// Return the item.
return item;
}
}
else if (isRecurringParent)
{
// Parent recurring events are never listed.
Debugger.Break();
return null;
}
else
{
// Single event.
return item;
}
}
else
{
// Series.
if (isRecurringChild)
{
// Return the parent.
return await GetCalendarItemAsync(targetDetails.Item.RecurringCalendarItemId.Value).ConfigureAwait(false);
}
else if (isRecurringParent)
return item;
else
{
// NA. Single events don't have series.
Debugger.Break();
return null;
}
}
// TODO
return null;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -17,6 +17,7 @@ public static class ServicesContainerSetup
services.AddSingleton<IMimeFileService, MimeFileService>();
services.AddTransient<ICalendarService, CalendarService>();
services.AddTransient<ICalendarServiceEx, CalendarServiceEx>();
services.AddTransient<IMailService, MailService>();
services.AddTransient<IFolderService, FolderService>();
services.AddTransient<IAccountService, AccountService>();
@@ -29,7 +30,5 @@ public static class ServicesContainerSetup
services.AddTransient<IOutlookThreadingStrategy, OutlookThreadingStrategy>();
services.AddTransient<IGmailThreadingStrategy, GmailThreadingStrategy>();
services.AddTransient<IImapThreadingStrategy, ImapThreadingStrategy>();
}
}