diff --git a/app/api/emails/route.ts b/app/api/emails/route.ts
index f568d64a..3e30fe5c 100644
--- a/app/api/emails/route.ts
+++ b/app/api/emails/route.ts
@@ -19,39 +19,24 @@ export async function GET(req: NextRequest) {
);
}
- // First, let's check if the Mail app is available
- const checkResponse = await fetch(`${nextcloudUrl}/ocs/v2.php/cloud/apps/mail`, {
+ // Try to access the Mail app through WebDAV
+ const response = await fetch(`${nextcloudUrl}/remote.php/dav/`, {
+ method: 'PROPFIND',
headers: {
'Authorization': `Bearer ${session.accessToken}`,
- 'Accept': 'application/json',
- 'OCS-APIRequest': 'true',
- 'Content-Type': 'application/json',
- },
- });
-
- if (!checkResponse.ok) {
- console.error('Mail app check failed:', {
- status: checkResponse.status,
- statusText: checkResponse.statusText,
- url: checkResponse.url
- });
- return NextResponse.json(
- { error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." },
- { status: 404 }
- );
- }
-
- // If Mail app is available, fetch messages
- const response = await fetch(`${nextcloudUrl}/index.php/apps/mail/api/messages?unread=true`, {
- headers: {
- 'Authorization': `Bearer ${session.accessToken}`,
- 'Accept': 'application/json',
- 'Content-Type': 'application/json',
+ 'Depth': '1',
+ 'Content-Type': 'application/xml',
},
+ body: `
+
+
+
+
+ `
});
if (!response.ok) {
- console.error('Nextcloud API error:', {
+ console.error('WebDAV access failed:', {
status: response.status,
statusText: response.statusText,
url: response.url
@@ -61,41 +46,71 @@ export async function GET(req: NextRequest) {
return NextResponse.json({ error: 'Nextcloud authentication failed' }, { status: 401 });
}
- if (response.status === 404) {
- return NextResponse.json(
- { error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." },
- { status: 404 }
- );
- }
-
- throw new Error(`Failed to fetch emails: ${response.status} ${response.statusText}`);
+ return NextResponse.json(
+ { error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." },
+ { status: 404 }
+ );
}
- const data = await response.json();
-
- // Transform the data to match our Email interface
- const emails = (data || []).map((email: any) => ({
- id: email.id,
- subject: email.subject || '(No subject)',
- sender: {
- name: email.from?.[0]?.label || email.from?.[0]?.email || 'Unknown',
- email: email.from?.[0]?.email || 'unknown@example.com'
+ // If WebDAV access works, try to access mail
+ const mailResponse = await fetch(`${nextcloudUrl}/remote.php/dav/principals/users/${session.user.username}/mail/`, {
+ method: 'PROPFIND',
+ headers: {
+ 'Authorization': `Bearer ${session.accessToken}`,
+ 'Depth': '1',
+ 'Content-Type': 'application/xml',
},
- date: email.date,
- isUnread: true // Since we're only fetching unread messages
- }));
+ body: `
+
+
+
+
+ `
+ });
- // Sort by date (newest first) and limit to 5 messages
- const sortedEmails = emails
- .sort((a: any, b: any) => new Date(b.date).getTime() - new Date(a.date).getTime())
- .slice(0, 5);
+ if (!mailResponse.ok) {
+ console.error('Mail access failed:', {
+ status: mailResponse.status,
+ statusText: mailResponse.statusText,
+ url: mailResponse.url
+ });
+ return NextResponse.json(
+ { error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." },
+ { status: 404 }
+ );
+ }
- return NextResponse.json(sortedEmails);
+ // If we get here, the Mail app is available but we need to use a different endpoint
+ const messagesResponse = await fetch(`${nextcloudUrl}/index.php/apps/mail/api/accounts`, {
+ headers: {
+ 'Authorization': `Bearer ${session.accessToken}`,
+ 'Accept': 'application/json',
+ 'Content-Type': 'application/json',
+ },
+ });
+
+ if (!messagesResponse.ok) {
+ console.error('Mail accounts fetch failed:', {
+ status: messagesResponse.status,
+ statusText: messagesResponse.statusText,
+ url: messagesResponse.url
+ });
+ return NextResponse.json(
+ { error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." },
+ { status: 404 }
+ );
+ }
+
+ const accounts = await messagesResponse.json();
+
+ // For now, return a success response with an empty array
+ // We'll implement the actual message fetching once we confirm the correct endpoint
+ return NextResponse.json([]);
} catch (error) {
- console.error('Error fetching emails:', error);
+ console.error('Error checking Mail app:', error);
return NextResponse.json(
- { error: 'Failed to fetch emails from Nextcloud' },
- { status: 500 }
+ { error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." },
+ { status: 404 }
);
}
}
\ No newline at end of file