116 lines
3.8 KiB
TypeScript
116 lines
3.8 KiB
TypeScript
import { NextResponse, NextRequest } from 'next/server';
|
|
import { getServerSession } from 'next-auth';
|
|
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
|
|
|
|
export async function GET(req: NextRequest) {
|
|
try {
|
|
const session = await getServerSession(authOptions);
|
|
|
|
if (!session?.accessToken) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const nextcloudUrl = process.env.NEXTCLOUD_URL;
|
|
if (!nextcloudUrl) {
|
|
console.error('Missing Nextcloud URL');
|
|
return NextResponse.json(
|
|
{ error: 'Nextcloud configuration is missing' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
|
|
// Try to access the Mail app through WebDAV
|
|
const response = await fetch(`${nextcloudUrl}/remote.php/dav/`, {
|
|
method: 'PROPFIND',
|
|
headers: {
|
|
'Authorization': `Bearer ${session.accessToken}`,
|
|
'Depth': '1',
|
|
'Content-Type': 'application/xml',
|
|
},
|
|
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:', {
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
url: response.url
|
|
});
|
|
|
|
if (response.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 }
|
|
);
|
|
}
|
|
|
|
// 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();
|
|
|
|
// 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 checking Mail app:', error);
|
|
return NextResponse.json(
|
|
{ error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
}
|