151 lines
4.3 KiB
TypeScript
151 lines
4.3 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?.user?.email) {
|
|
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 }
|
|
);
|
|
}
|
|
|
|
// Get the access token from the session
|
|
const accessToken = session.accessToken;
|
|
if (!accessToken) {
|
|
console.error('Missing access token');
|
|
return NextResponse.json(
|
|
{ error: 'Authentication token is missing' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
// First, try the unified inbox endpoint for unread messages
|
|
const unifiedResponse = await fetch(
|
|
`${nextcloudUrl}/apps/mail/api/messages?filter=is:unread`,
|
|
{
|
|
headers: {
|
|
'Authorization': `Bearer ${accessToken}`,
|
|
'Accept': 'application/json'
|
|
}
|
|
}
|
|
);
|
|
|
|
if (unifiedResponse.ok) {
|
|
const unifiedData = await unifiedResponse.json();
|
|
const messages = unifiedData.data || [];
|
|
|
|
const unreadEmails = messages.map((msg: any) => ({
|
|
id: msg.id,
|
|
subject: msg.subject,
|
|
sender: {
|
|
name: msg.from[0].label || msg.from[0].email,
|
|
email: msg.from[0].email
|
|
},
|
|
date: msg.date,
|
|
isUnread: true
|
|
}));
|
|
|
|
return NextResponse.json({
|
|
emails: unreadEmails,
|
|
mailUrl: `${nextcloudUrl}/apps/mail/box/unified`
|
|
});
|
|
}
|
|
|
|
// If unified inbox fails, fall back to the account-based approach
|
|
const accountsResponse = await fetch(
|
|
`${nextcloudUrl}/apps/mail/api/accounts`,
|
|
{
|
|
headers: {
|
|
'Authorization': `Bearer ${accessToken}`,
|
|
'Accept': 'application/json'
|
|
}
|
|
}
|
|
);
|
|
|
|
if (!accountsResponse.ok) {
|
|
console.error('Failed to fetch mail accounts:', await accountsResponse.text());
|
|
return NextResponse.json(
|
|
{
|
|
error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur.",
|
|
emails: []
|
|
},
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
const accountsData = await accountsResponse.json();
|
|
const accounts = accountsData.data || [];
|
|
|
|
const unreadEmails = [];
|
|
for (const account of accounts) {
|
|
// Get mailboxes for the account
|
|
const mailboxesResponse = await fetch(
|
|
`${nextcloudUrl}/apps/mail/api/accounts/${account.id}/mailboxes`,
|
|
{
|
|
headers: {
|
|
'Authorization': `Bearer ${accessToken}`,
|
|
'Accept': 'application/json'
|
|
}
|
|
}
|
|
);
|
|
|
|
if (!mailboxesResponse.ok) continue;
|
|
|
|
const mailboxesData = await mailboxesResponse.json();
|
|
const mailboxes = mailboxesData.data || [];
|
|
|
|
// Get unread messages from each mailbox
|
|
for (const mailbox of mailboxes) {
|
|
const messagesResponse = await fetch(
|
|
`${nextcloudUrl}/apps/mail/api/accounts/${account.id}/mailboxes/${mailbox.id}/messages?filter=is:unread`,
|
|
{
|
|
headers: {
|
|
'Authorization': `Bearer ${accessToken}`,
|
|
'Accept': 'application/json'
|
|
}
|
|
}
|
|
);
|
|
|
|
if (!messagesResponse.ok) continue;
|
|
|
|
const messagesData = await messagesResponse.json();
|
|
const messages = messagesData.data || [];
|
|
|
|
unreadEmails.push(...messages.map((msg: any) => ({
|
|
id: msg.id,
|
|
subject: msg.subject,
|
|
sender: {
|
|
name: msg.from[0].label || msg.from[0].email,
|
|
email: msg.from[0].email
|
|
},
|
|
date: msg.date,
|
|
isUnread: true
|
|
})));
|
|
}
|
|
}
|
|
|
|
return NextResponse.json({
|
|
emails: unreadEmails,
|
|
mailUrl: `${nextcloudUrl}/apps/mail/box/unified`
|
|
});
|
|
} catch (error) {
|
|
console.error('Error getting emails:', error);
|
|
return NextResponse.json(
|
|
{
|
|
error: "Une erreur est survenue. Veuillez contacter votre administrateur.",
|
|
emails: []
|
|
},
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|