From 336426efae962b7c26405fef996fc9e4681447bc Mon Sep 17 00:00:00 2001 From: Alma Date: Sun, 13 Apr 2025 21:27:36 +0200 Subject: [PATCH] widget email 13 --- app/api/emails/route.ts | 54 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/app/api/emails/route.ts b/app/api/emails/route.ts index b4ccdf89..25113ab3 100644 --- a/app/api/emails/route.ts +++ b/app/api/emails/route.ts @@ -20,7 +20,7 @@ export async function GET(req: NextRequest) { } // Try to access the Mail app using the Keycloak token - const response = await fetch(`${nextcloudUrl}/ocs/v2.php/apps/mail/api/v1/accounts`, { + const response = await fetch(`${nextcloudUrl}/ocs/v2.php/cloud/user`, { headers: { 'Accept': 'application/json', 'OCS-APIRequest': 'true', @@ -41,7 +41,7 @@ export async function GET(req: NextRequest) { } if (!response.ok) { - console.error('Mail app check failed:', { + console.error('User info check failed:', { status: response.status, statusText: response.statusText, error: responseData, @@ -58,7 +58,55 @@ export async function GET(req: NextRequest) { ); } - const accounts = responseData?.ocs?.data || []; + const userId = responseData?.ocs?.data?.id; + if (!userId) { + console.error('Failed to get user ID from Nextcloud'); + return NextResponse.json( + { error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." }, + { status: 404 } + ); + } + + // Now try to access the Mail app + const mailResponse = await fetch(`${nextcloudUrl}/ocs/v2.php/apps/mail/api/v1/accounts`, { + headers: { + 'Accept': 'application/json', + 'OCS-APIRequest': 'true', + 'Content-Type': 'application/json', + 'X-Requested-With': 'XMLHttpRequest', + 'Authorization': `Bearer ${session.accessToken}`, + }, + }); + + const mailResponseText = await mailResponse.text(); + let mailResponseData; + + try { + mailResponseData = JSON.parse(mailResponseText); + } catch (error) { + console.error('Failed to parse mail response:', mailResponseText); + return NextResponse.json({ error: 'Invalid response format' }, { status: 500 }); + } + + if (!mailResponse.ok) { + console.error('Mail app check failed:', { + status: mailResponse.status, + statusText: mailResponse.statusText, + error: mailResponseData, + url: mailResponse.url, + }); + + if (mailResponse.status === 401) { + return NextResponse.json({ error: 'Nextcloud authentication failed' }, { status: 401 }); + } + + return NextResponse.json( + { error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." }, + { status: 404 } + ); + } + + const accounts = mailResponseData?.ocs?.data || []; // For now, return a success response with an empty array return NextResponse.json([]);