widget email 2
This commit is contained in:
parent
d65df5d0fa
commit
5ae23eedd4
@ -19,8 +19,8 @@ export async function GET(req: NextRequest) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch unread messages from Nextcloud Mail API using OIDC token
|
// First, let's check if the Mail app is available
|
||||||
const response = await fetch(`${nextcloudUrl}/ocs/v2.php/apps/mail/api/v1/folders/inbox/messages?filter=unseen`, {
|
const checkResponse = await fetch(`${nextcloudUrl}/ocs/v2.php/cloud/apps/mail`, {
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': `Bearer ${session.accessToken}`,
|
'Authorization': `Bearer ${session.accessToken}`,
|
||||||
'Accept': 'application/json',
|
'Accept': 'application/json',
|
||||||
@ -29,6 +29,27 @@ export async function GET(req: NextRequest) {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
console.error('Nextcloud API error:', {
|
console.error('Nextcloud API error:', {
|
||||||
status: response.status,
|
status: response.status,
|
||||||
@ -42,7 +63,7 @@ export async function GET(req: NextRequest) {
|
|||||||
|
|
||||||
if (response.status === 404) {
|
if (response.status === 404) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: 'Mail app not available on Nextcloud' },
|
{ error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." },
|
||||||
{ status: 404 }
|
{ status: 404 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -53,14 +74,14 @@ export async function GET(req: NextRequest) {
|
|||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
||||||
// Transform the data to match our Email interface
|
// Transform the data to match our Email interface
|
||||||
const emails = (data?.ocs?.data || []).map((email: any) => ({
|
const emails = (data || []).map((email: any) => ({
|
||||||
id: email.id,
|
id: email.id,
|
||||||
subject: email.subject || '(No subject)',
|
subject: email.subject || '(No subject)',
|
||||||
sender: {
|
sender: {
|
||||||
name: email.from?.name || email.from?.email || 'Unknown',
|
name: email.from?.[0]?.label || email.from?.[0]?.email || 'Unknown',
|
||||||
email: email.from?.email || 'unknown@example.com'
|
email: email.from?.[0]?.email || 'unknown@example.com'
|
||||||
},
|
},
|
||||||
date: email.sentDate,
|
date: email.date,
|
||||||
isUnread: true // Since we're only fetching unread messages
|
isUnread: true // Since we're only fetching unread messages
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user