widget email 4
This commit is contained in:
parent
2512f2db47
commit
30f30cb569
@ -19,24 +19,54 @@ export async function GET(req: NextRequest) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to access the Mail app through WebDAV
|
// First, try to get the user's Nextcloud ID using the OIDC token
|
||||||
const response = await fetch(`${nextcloudUrl}/remote.php/dav/`, {
|
const userInfoResponse = await fetch(`${nextcloudUrl}/ocs/v2.php/cloud/user`, {
|
||||||
method: 'PROPFIND',
|
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': `Bearer ${session.accessToken}`,
|
'Authorization': `Bearer ${session.accessToken}`,
|
||||||
'Depth': '1',
|
'Accept': 'application/json',
|
||||||
'Content-Type': 'application/xml',
|
'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) {
|
if (!response.ok) {
|
||||||
console.error('WebDAV access failed:', {
|
console.error('Mail app check failed:', {
|
||||||
status: response.status,
|
status: response.status,
|
||||||
statusText: response.statusText,
|
statusText: response.statusText,
|
||||||
url: response.url
|
url: response.url
|
||||||
@ -52,57 +82,7 @@ export async function GET(req: NextRequest) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If WebDAV access works, try to access mail
|
// If we get here, the Mail app is available
|
||||||
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();
|
|
||||||
|
|
||||||
// For now, return a success response with an empty array
|
// For now, return a success response with an empty array
|
||||||
// We'll implement the actual message fetching once we confirm the correct endpoint
|
// We'll implement the actual message fetching once we confirm the correct endpoint
|
||||||
return NextResponse.json([]);
|
return NextResponse.json([]);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user