Added Chat Page
This commit is contained in:
113
app/api/conversations/[id]/typing/route.ts
Normal file
113
app/api/conversations/[id]/typing/route.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
import { getCurrentUser } from '@/lib/auth';
|
||||
|
||||
// Store temporaire pour les indicateurs de frappe (en production, utiliser Redis ou une DB)
|
||||
const typingUsers = new Map<string, { userId: string; timestamp: number }>();
|
||||
|
||||
// Nettoyer les anciens indicateurs toutes les 5 secondes
|
||||
setInterval(() => {
|
||||
const now = Date.now();
|
||||
for (const [key, value] of typingUsers.entries()) {
|
||||
if (now - value.timestamp > 5000) {
|
||||
typingUsers.delete(key);
|
||||
}
|
||||
}
|
||||
}, 5000);
|
||||
|
||||
// POST - Signaler que l'utilisateur est en train d'écrire
|
||||
export async function POST(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
|
||||
}
|
||||
|
||||
const conversation = await prisma.conversation.findUnique({
|
||||
where: { id: params.id },
|
||||
include: {
|
||||
participants: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!conversation) {
|
||||
return NextResponse.json({ error: 'Conversation non trouvée' }, { status: 404 });
|
||||
}
|
||||
|
||||
// Vérifier que l'utilisateur est participant
|
||||
const isParticipant = conversation.participants.some((p) => p.userId === user.id);
|
||||
if (!isParticipant) {
|
||||
return NextResponse.json({ error: 'Accès non autorisé' }, { status: 403 });
|
||||
}
|
||||
|
||||
// Enregistrer que l'utilisateur est en train d'écrire
|
||||
const key = `${params.id}:${user.id}`;
|
||||
typingUsers.set(key, {
|
||||
userId: user.id,
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la signalisation du typing:', error);
|
||||
return NextResponse.json({ error: 'Erreur serveur' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// GET - Récupérer les utilisateurs en train d'écrire
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
|
||||
}
|
||||
|
||||
const conversation = await prisma.conversation.findUnique({
|
||||
where: { id: params.id },
|
||||
include: {
|
||||
participants: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!conversation) {
|
||||
return NextResponse.json({ error: 'Conversation non trouvée' }, { status: 404 });
|
||||
}
|
||||
|
||||
// Vérifier que l'utilisateur est participant
|
||||
const isParticipant = conversation.participants.some((p) => p.userId === user.id);
|
||||
if (!isParticipant) {
|
||||
return NextResponse.json({ error: 'Accès non autorisé' }, { status: 403 });
|
||||
}
|
||||
|
||||
// Récupérer les utilisateurs en train d'écrire (sauf l'utilisateur actuel)
|
||||
const typing = Array.from(typingUsers.entries())
|
||||
.filter(([key]) => key.startsWith(`${params.id}:`))
|
||||
.filter(([key]) => !key.endsWith(`:${user.id}`))
|
||||
.map(([, value]) => value.userId);
|
||||
|
||||
// Récupérer les informations des utilisateurs
|
||||
const typingUsersData = await prisma.user.findMany({
|
||||
where: {
|
||||
id: {
|
||||
in: typing,
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
name: true,
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({ typing: typingUsersData });
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la récupération du typing:', error);
|
||||
return NextResponse.json({ error: 'Erreur serveur' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user