widget email 4

This commit is contained in:
Alma 2025-04-13 20:53:45 +02:00
parent 2512f2db47
commit 30f30cb569

View File

@ -19,24 +19,54 @@ export async function GET(req: NextRequest) {
);
}
// Try to access the Mail app through WebDAV
const response = await fetch(`${nextcloudUrl}/remote.php/dav/`, {
method: 'PROPFIND',
// First, try to get the user's Nextcloud ID using the OIDC token
const userInfoResponse = await fetch(`${nextcloudUrl}/ocs/v2.php/cloud/user`, {
headers: {
'Authorization': `Bearer ${session.accessToken}`,
'Depth': '1',
'Content-Type': 'application/xml',
'Accept': 'application/json',
'OCS-APIRequest': 'true',
},
});
if (!userInfoResponse.ok) {
console.error('Failed to get user info:', {
status: userInfoResponse.status,
statusText: userInfoResponse.statusText,
url: userInfoResponse.url
});
if (userInfoResponse.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 userInfo = await userInfoResponse.json();
const userId = userInfo?.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 response = await fetch(`${nextcloudUrl}/ocs/v2.php/apps/mail/api/v1/accounts`, {
headers: {
'Authorization': `Bearer ${session.accessToken}`,
'Accept': 'application/json',
'OCS-APIRequest': 'true',
},
body: `<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:">
<d:prop>
<d:current-user-principal/>
</d:prop>
</d:propfind>`
});
if (!response.ok) {
console.error('WebDAV access failed:', {
console.error('Mail app check failed:', {
status: response.status,
statusText: response.statusText,
url: response.url
@ -52,57 +82,7 @@ export async function GET(req: NextRequest) {
);
}
// 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',
},
body: `<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:" xmlns:mail="http://nextcloud.com/ns/mail">
<d:prop>
<mail:inbox/>
</d:prop>
</d:propfind>`
});
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 }
);
}
// 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();
// If we get here, the Mail app is available
// 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([]);