widget email 3
This commit is contained in:
parent
5ae23eedd4
commit
2512f2db47
@ -19,39 +19,24 @@ export async function GET(req: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
// First, let's check if the Mail app is available
|
||||
const checkResponse = await fetch(`${nextcloudUrl}/ocs/v2.php/cloud/apps/mail`, {
|
||||
// Try to access the Mail app through WebDAV
|
||||
const response = await fetch(`${nextcloudUrl}/remote.php/dav/`, {
|
||||
method: 'PROPFIND',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${session.accessToken}`,
|
||||
'Accept': 'application/json',
|
||||
'OCS-APIRequest': 'true',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
if (!checkResponse.ok) {
|
||||
console.error('Mail app check failed:', {
|
||||
status: checkResponse.status,
|
||||
statusText: checkResponse.statusText,
|
||||
url: checkResponse.url
|
||||
});
|
||||
return NextResponse.json(
|
||||
{ error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
// If Mail app is available, fetch messages
|
||||
const response = await fetch(`${nextcloudUrl}/index.php/apps/mail/api/messages?unread=true`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${session.accessToken}`,
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
'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('Nextcloud API error:', {
|
||||
console.error('WebDAV access failed:', {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
url: response.url
|
||||
@ -61,41 +46,71 @@ export async function GET(req: NextRequest) {
|
||||
return NextResponse.json({ error: 'Nextcloud authentication failed' }, { status: 401 });
|
||||
}
|
||||
|
||||
if (response.status === 404) {
|
||||
return NextResponse.json(
|
||||
{ error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
throw new Error(`Failed to fetch emails: ${response.status} ${response.statusText}`);
|
||||
return NextResponse.json(
|
||||
{ error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
// Transform the data to match our Email interface
|
||||
const emails = (data || []).map((email: any) => ({
|
||||
id: email.id,
|
||||
subject: email.subject || '(No subject)',
|
||||
sender: {
|
||||
name: email.from?.[0]?.label || email.from?.[0]?.email || 'Unknown',
|
||||
email: email.from?.[0]?.email || 'unknown@example.com'
|
||||
// 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',
|
||||
},
|
||||
date: email.date,
|
||||
isUnread: true // Since we're only fetching unread messages
|
||||
}));
|
||||
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>`
|
||||
});
|
||||
|
||||
// Sort by date (newest first) and limit to 5 messages
|
||||
const sortedEmails = emails
|
||||
.sort((a: any, b: any) => new Date(b.date).getTime() - new Date(a.date).getTime())
|
||||
.slice(0, 5);
|
||||
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 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(sortedEmails);
|
||||
// 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 fetching emails:', error);
|
||||
console.error('Error checking Mail app:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch emails from Nextcloud' },
|
||||
{ status: 500 }
|
||||
{ error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user