widget email 16
This commit is contained in:
parent
f55bba3c1b
commit
273c332f09
@ -19,19 +19,133 @@ export async function GET(req: NextRequest) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the Nextcloud Mail iframe URL with an empty emails array
|
// 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({
|
return NextResponse.json({
|
||||||
emails: [], // Initialize with empty array
|
emails: unreadEmails,
|
||||||
mailUrl: `${nextcloudUrl}/apps/mail/box/unified`
|
mailUrl: `${nextcloudUrl}/apps/mail/box/unified`
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error getting Mail URL:', error);
|
console.error('Error getting emails:', error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{
|
{
|
||||||
error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur.",
|
error: "Une erreur est survenue. Veuillez contacter votre administrateur.",
|
||||||
emails: [] // Initialize with empty array even in error case
|
emails: []
|
||||||
},
|
},
|
||||||
{ status: 404 }
|
{ status: 500 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user