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
|
// Try to access the Mail app through WebDAV
|
||||||
const checkResponse = await fetch(`${nextcloudUrl}/ocs/v2.php/cloud/apps/mail`, {
|
const response = await fetch(`${nextcloudUrl}/remote.php/dav/`, {
|
||||||
|
method: 'PROPFIND',
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': `Bearer ${session.accessToken}`,
|
'Authorization': `Bearer ${session.accessToken}`,
|
||||||
'Accept': 'application/json',
|
'Depth': '1',
|
||||||
'OCS-APIRequest': 'true',
|
'Content-Type': 'application/xml',
|
||||||
'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',
|
|
||||||
},
|
},
|
||||||
|
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('Nextcloud API error:', {
|
console.error('WebDAV access failed:', {
|
||||||
status: response.status,
|
status: response.status,
|
||||||
statusText: response.statusText,
|
statusText: response.statusText,
|
||||||
url: response.url
|
url: response.url
|
||||||
@ -61,41 +46,71 @@ export async function GET(req: NextRequest) {
|
|||||||
return NextResponse.json({ error: 'Nextcloud authentication failed' }, { status: 401 });
|
return NextResponse.json({ error: 'Nextcloud authentication failed' }, { status: 401 });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response.status === 404) {
|
return NextResponse.json(
|
||||||
return NextResponse.json(
|
{ error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." },
|
||||||
{ error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." },
|
{ status: 404 }
|
||||||
{ status: 404 }
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Error(`Failed to fetch emails: ${response.status} ${response.statusText}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
// If WebDAV access works, try to access mail
|
||||||
|
const mailResponse = await fetch(`${nextcloudUrl}/remote.php/dav/principals/users/${session.user.username}/mail/`, {
|
||||||
// Transform the data to match our Email interface
|
method: 'PROPFIND',
|
||||||
const emails = (data || []).map((email: any) => ({
|
headers: {
|
||||||
id: email.id,
|
'Authorization': `Bearer ${session.accessToken}`,
|
||||||
subject: email.subject || '(No subject)',
|
'Depth': '1',
|
||||||
sender: {
|
'Content-Type': 'application/xml',
|
||||||
name: email.from?.[0]?.label || email.from?.[0]?.email || 'Unknown',
|
|
||||||
email: email.from?.[0]?.email || 'unknown@example.com'
|
|
||||||
},
|
},
|
||||||
date: email.date,
|
body: `<?xml version="1.0"?>
|
||||||
isUnread: true // Since we're only fetching unread messages
|
<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
|
if (!mailResponse.ok) {
|
||||||
const sortedEmails = emails
|
console.error('Mail access failed:', {
|
||||||
.sort((a: any, b: any) => new Date(b.date).getTime() - new Date(a.date).getTime())
|
status: mailResponse.status,
|
||||||
.slice(0, 5);
|
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) {
|
} catch (error) {
|
||||||
console.error('Error fetching emails:', error);
|
console.error('Error checking Mail app:', error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: 'Failed to fetch emails from Nextcloud' },
|
{ error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." },
|
||||||
{ status: 500 }
|
{ status: 404 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user