widget email 14
This commit is contained in:
parent
336426efae
commit
ef5c38a42e
@ -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 }
|
||||
|
||||
@ -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<Email[]>([]);
|
||||
const [mailUrl, setMailUrl] = useState<string | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(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() {
|
||||
<div
|
||||
key={email.id}
|
||||
className="p-2 hover:bg-gray-50/50 rounded-lg transition-colors cursor-pointer"
|
||||
onClick={() => window.open(process.env.NEXT_PUBLIC_IFRAME_MAIL_URL, '_blank')}
|
||||
onClick={() => mailUrl && window.open(mailUrl, '_blank')}
|
||||
>
|
||||
<div className="flex items-center justify-between mb-1">
|
||||
<span className="text-sm text-gray-600 truncate max-w-[60%]" title={email.sender.name}>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user