From ef5c38a42e6a8a668f9b75f0539471515cbba8c3 Mon Sep 17 00:00:00 2001 From: Alma Date: Sun, 13 Apr 2025 21:32:16 +0200 Subject: [PATCH] widget email 14 --- app/api/emails/route.ts | 97 +++-------------------------------------- components/email.tsx | 17 +++++--- 2 files changed, 17 insertions(+), 97 deletions(-) diff --git a/app/api/emails/route.ts b/app/api/emails/route.ts index 25113ab3..e2da7e6c 100644 --- a/app/api/emails/route.ts +++ b/app/api/emails/route.ts @@ -6,7 +6,7 @@ export async function GET(req: NextRequest) { try { const session = await getServerSession(authOptions); - if (!session?.user?.email || !session?.accessToken) { + if (!session?.user?.email) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } @@ -19,99 +19,12 @@ export async function GET(req: NextRequest) { ); } - // Try to access the Mail app using the Keycloak token - const response = await fetch(`${nextcloudUrl}/ocs/v2.php/cloud/user`, { - headers: { - 'Accept': 'application/json', - 'OCS-APIRequest': 'true', - 'Content-Type': 'application/json', - 'X-Requested-With': 'XMLHttpRequest', - 'Authorization': `Bearer ${session.accessToken}`, - }, + // Return the Nextcloud Mail iframe URL + return NextResponse.json({ + url: `${nextcloudUrl}/apps/mail/box/unified` }); - - const responseText = await response.text(); - let responseData; - - try { - responseData = JSON.parse(responseText); - } catch (error) { - console.error('Failed to parse response:', responseText); - return NextResponse.json({ error: 'Invalid response format' }, { status: 500 }); - } - - if (!response.ok) { - console.error('User info check failed:', { - status: response.status, - statusText: response.statusText, - error: responseData, - url: response.url, - }); - - if (response.status === 401) { - return NextResponse.json({ error: 'Nextcloud authentication failed' }, { status: 401 }); - } - - return NextResponse.json( - { error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." }, - { status: 404 } - ); - } - - const userId = responseData?.ocs?.data?.id; - if (!userId) { - console.error('Failed to get user ID from Nextcloud'); - return NextResponse.json( - { error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." }, - { status: 404 } - ); - } - - // Now try to access the Mail app - const mailResponse = await fetch(`${nextcloudUrl}/ocs/v2.php/apps/mail/api/v1/accounts`, { - headers: { - 'Accept': 'application/json', - 'OCS-APIRequest': 'true', - 'Content-Type': 'application/json', - 'X-Requested-With': 'XMLHttpRequest', - 'Authorization': `Bearer ${session.accessToken}`, - }, - }); - - const mailResponseText = await mailResponse.text(); - let mailResponseData; - - try { - mailResponseData = JSON.parse(mailResponseText); - } catch (error) { - console.error('Failed to parse mail response:', mailResponseText); - return NextResponse.json({ error: 'Invalid response format' }, { status: 500 }); - } - - if (!mailResponse.ok) { - console.error('Mail app check failed:', { - status: mailResponse.status, - statusText: mailResponse.statusText, - error: mailResponseData, - url: mailResponse.url, - }); - - if (mailResponse.status === 401) { - return NextResponse.json({ error: 'Nextcloud authentication failed' }, { status: 401 }); - } - - return NextResponse.json( - { error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." }, - { status: 404 } - ); - } - - const accounts = mailResponseData?.ocs?.data || []; - - // For now, return a success response with an empty array - return NextResponse.json([]); } catch (error) { - console.error('Error checking Mail app:', error); + console.error('Error getting Mail URL:', error); return NextResponse.json( { error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." }, { status: 404 } diff --git a/components/email.tsx b/components/email.tsx index 95301f26..4e87432b 100644 --- a/components/email.tsx +++ b/components/email.tsx @@ -5,8 +5,8 @@ import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { RefreshCw, Mail } from "lucide-react"; import { useSession, signIn } from "next-auth/react"; -import { formatDistance } from 'date-fns'; -import { fr } from 'date-fns/locale'; +import { formatDistance } from 'date-fns/formatDistance'; +import { fr } from 'date-fns/locale/fr'; interface Email { id: string; @@ -19,8 +19,14 @@ interface Email { isUnread: boolean; } +interface EmailResponse { + emails: Email[]; + mailUrl: string; +} + export function Email() { const [emails, setEmails] = useState([]); + const [mailUrl, setMailUrl] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [refreshing, setRefreshing] = useState(false); @@ -32,7 +38,7 @@ export function Email() { try { const response = await fetch('/api/emails'); - const data = await response.json(); + const data: EmailResponse = await response.json(); if (!response.ok) { // Handle session expiration @@ -50,7 +56,8 @@ export function Email() { throw new Error(data.error || 'Failed to fetch emails'); } - setEmails(data); + setEmails(data.emails); + setMailUrl(data.mailUrl); setError(null); } catch (err) { console.error('Error fetching emails:', err); @@ -143,7 +150,7 @@ export function Email() {
window.open(process.env.NEXT_PUBLIC_IFRAME_MAIL_URL, '_blank')} + onClick={() => mailUrl && window.open(mailUrl, '_blank')} >