Agenda Sync refactor

This commit is contained in:
alma 2026-01-14 14:17:53 +01:00
parent 760f27cb99
commit de8cbc5824

View File

@ -26,8 +26,8 @@ export async function getInfomaniakCalDAVClient(
email: string, email: string,
password: string password: string
): Promise<WebDAVClient> { ): Promise<WebDAVClient> {
// Infomaniak CalDAV base URL // Infomaniak CalDAV base URL (from Infomaniak sync assistant)
const baseUrl = 'https://sync.infomaniak.com/caldav'; const baseUrl = 'https://sync.infomaniak.com';
const client = createClient(baseUrl, { const client = createClient(baseUrl, {
username: email, username: email,
@ -47,13 +47,31 @@ export async function discoverInfomaniakCalendars(
try { try {
const client = await getInfomaniakCalDAVClient(email, password); const client = await getInfomaniakCalDAVClient(email, password);
// List all calendars using PROPFIND // Try to discover calendar home set using CalDAV discovery
const items = await client.getDirectoryContents('/'); // First, try to find the calendar home set using current-user-principal
let calendarHomePath = '/calendars/';
const calendars: CalDAVCalendar[] = []; // Extract username from email (before @) or use email as username
// Infomaniak might use the email as username or require the Infomaniak username
const username = email.split('@')[0];
// Try different paths
const possiblePaths = [
`/calendars/${username}/`,
`/calendars/${email}/`,
'/calendars/',
'/',
];
let calendars: CalDAVCalendar[] = [];
for (const path of possiblePaths) {
try {
// List all calendars using PROPFIND with Depth: 1
const items = await client.getDirectoryContents(path);
for (const item of items) { for (const item of items) {
if (item.type === 'directory' && item.filename !== '/') { if (item.type === 'directory' && item.filename !== '/' && item.filename !== path) {
// Get calendar properties // Get calendar properties
try { try {
const props = await client.customRequest(item.filename, { const props = await client.customRequest(item.filename, {
@ -67,10 +85,16 @@ export async function discoverInfomaniakCalendars(
<d:prop> <d:prop>
<d:displayname /> <d:displayname />
<c:calendar-color /> <c:calendar-color />
<d:resourcetype />
</d:prop> </d:prop>
</d:propfind>`, </d:propfind>`,
}); });
// Check if it's a calendar (has calendar resource type)
const isCalendar = typeof props.data === 'string' &&
(props.data.includes('<c:calendar') || props.data.includes('calendar'));
if (isCalendar || item.type === 'directory') {
// Parse XML response to extract calendar name and color // Parse XML response to extract calendar name and color
const displayName = extractDisplayName(props.data); const displayName = extractDisplayName(props.data);
const color = extractCalendarColor(props.data); const color = extractCalendarColor(props.data);
@ -81,12 +105,14 @@ export async function discoverInfomaniakCalendars(
url: item.filename, url: item.filename,
color: color, color: color,
}); });
}
} catch (error) { } catch (error) {
logger.error('Error fetching calendar properties', { logger.debug('Error fetching calendar properties', {
calendar: item.filename, calendar: item.filename,
error: error instanceof Error ? error.message : String(error), error: error instanceof Error ? error.message : String(error),
}); });
// Still add the calendar with default name // Still add the calendar with default name if it's a directory
if (item.type === 'directory') {
calendars.push({ calendars.push({
id: item.filename.replace(/^\//, '').replace(/\/$/, ''), id: item.filename.replace(/^\//, '').replace(/\/$/, ''),
name: item.basename || 'Calendrier', name: item.basename || 'Calendrier',
@ -95,6 +121,20 @@ export async function discoverInfomaniakCalendars(
} }
} }
} }
}
// If we found calendars, break
if (calendars.length > 0) {
break;
}
} catch (pathError) {
// Try next path
logger.debug(`Path ${path} failed, trying next`, {
error: pathError instanceof Error ? pathError.message : String(pathError),
});
continue;
}
}
return calendars; return calendars;
} catch (error) { } catch (error) {
@ -102,7 +142,9 @@ export async function discoverInfomaniakCalendars(
email, email,
error: error instanceof Error ? error.message : String(error), error: error instanceof Error ? error.message : String(error),
}); });
throw error; // Ne pas faire échouer toute la page agenda si la découverte échoue
// On retourne simplement une liste vide -> pas de sync auto possible
return [];
} }
} }